web-token/jwt-encryption-algorithm-aeskw
AES Key Wrap (AES-KW) encryption algorithm support for the web-token JWT framework. Adds key wrapping/unwrapping utilities to enable JWE encryption workflows using AES-KW in PHP, integrating with existing web-token components and algorithm managers.
Installation Add the package via Composer:
composer require web-token/jwt-encryption-algorithm-aeskw
Basic Usage
Import the algorithm and use it with a JWT library (e.g., firebase/php-jwt or lucadegasperi/oauth2-server):
use WebToken\JWT\Signature\Algorithm\AESKW;
// Initialize the algorithm with a key (must be 256-bit for AES-256-KW)
$algorithm = new AESKW('your-256-bit-secret-key-here');
// Use with a JWT library (example with firebase/php-jwt)
$jwt = JWT::encode($payload, null, [$algorithm]);
First Use Case Encrypt a JWT payload for secure storage/transmission where the recipient has the same key:
$payload = ['user_id' => 123, 'exp' => time() + 3600];
$token = JWT::encode($payload, null, [$algorithm]);
config, environment variables, or a dedicated secrets manager like AWS KMS).AESKW instance dynamically.
// Example: Rotate keys periodically
$currentKey = config('jwt.aeskw_key');
$algorithm = new AESKW($currentKey);
Service Provider Bind the algorithm to Laravel’s container for reuse:
// app/Providers/JWTServiceProvider.php
public function register()
{
$this->app->singleton(AESKW::class, function ($app) {
return new AESKW(config('jwt.aeskw_key'));
});
}
Middleware for JWT Validation Use the algorithm in middleware to decrypt and validate tokens:
// app/Http/Middleware/ValidateJWT.php
public function handle($request, Closure $next)
{
$token = $request->bearerToken();
$algorithm = app(AESKW::class);
$decoded = JWT::decode($token, null, [$algorithm]);
// Proceed with authenticated request
}
Encrypt (Issue Token)
$algorithm = app(AESKW::class);
$token = JWT::encode($payload, null, [$algorithm]);
Decrypt (Validate Token)
try {
$decoded = JWT::decode($token, null, [$algorithm]);
} catch (Exception $e) {
// Handle invalid token
}
Key Length Requirement
openssl rand -hex 32
Algorithm Compatibility
firebase/php-jwt) require explicit registration:
JWT::setAlgorithmMethod('AESKW', [$algorithm]);
Performance Overhead
Invalid Token Errors
exp claims must be valid).Logging Enable JWT library logging to debug decryption failures:
JWT::setLogger(new Monolog\Logger('jwt'));
Custom Key Derivation Extend the algorithm to derive keys from passwords using PBKDF2:
use WebToken\JWT\Signature\Algorithm\AESKW;
use function Sodium\crypto_pwhash;
$key = crypto_pwhash('str', 'password', ['algo' => SODIUM_CRYPTO_PWHASH_ALG_DEFAULT]);
$algorithm = new AESKW($key);
Hybrid Encryption Combine with RSA for asymmetric key exchange (e.g., encrypt the AES key with RSA, then use AES-KW for the JWT):
// Pseudocode
$aesKey = openssl_random_pseudo_bytes(32);
$rsaEncryptedKey = openssl_public_encrypt($aesKey, $encrypted, $publicKey);
$algorithm = new AESKW($aesKey);
Laravel Configuration
Centralize key management in config/jwt.php:
'aeskw' => [
'key' => env('JWT_AESKW_KEY'),
'enabled' => env('JWT_AESKW_ENABLED', false),
],
How can I help you explore Laravel packages today?