web-token/jwt-encryption-algorithm-aesgcm
JWT encryption algorithm implementation using AES-GCM for the web-token/jwt framework. Adds AESGCM-based JWE support with authenticated encryption, suitable for securing tokens with modern AEAD cryptography in PHP applications.
Installation
composer require web-token/jwt-encryption-algorithm-aesgcm
Add to composer.json under require if not using Composer globally.
Basic Usage Import the algorithm in your Laravel project:
use WebToken\JWTEncryptionAlgorithm\AESGCM;
First Use Case: Encrypting JWT Payload
use Firebase\JWT\JWT;
use WebToken\JWTEncryptionAlgorithm\AESGCM;
$key = 'your-32-byte-secret-key'; // Must be 32 bytes for AES-256-GCM
$algorithm = new AESGCM($key);
$payload = [
'user_id' => 123,
'exp' => time() + 3600,
'data' => ['secret' => 'sensitive_info']
];
$token = JWT::encode($payload, '', 'HS256', $algorithm);
Decoding the Token
$decoded = JWT::decode($token, '', ['HS256'], $algorithm);
Key Management
.env (e.g., JWT_AES_KEY=base64:32bytekey).config() helper to fetch keys securely:
$key = base64_decode(config('jwt.aes_key'));
Integration with Laravel Sanctum/Passport
CreateFreshApiToken or Passport’s TokenRepository to encrypt sensitive claims:
$userToken = $user->createToken('API Token');
$token = $userToken->accessToken;
$token->setPayloadAttribute('encrypted_data', $encryptedPayload);
Middleware for Decryption
public function handle($request, Closure $next) {
$token = $request->bearerToken();
$decoded = JWT::decode($token, '', ['HS256'], new AESGCM(config('jwt.aes_key')));
$request->merge(['decrypted_data' => $decoded->data]);
return $next($request);
}
Batch Processing
public function handle() {
$tokens = Token::all();
foreach ($tokens as $token) {
$decrypted = JWT::decode($token->encrypted_payload, '', ['HS256'], $algorithm);
// Process decrypted data...
}
}
Key Length Requirements
Invalid key length for AES-GCM (must be 32 bytes for AES-256).$key = random_bytes(32); // For new keys
$key = base64_decode('your_base64_key'); // For existing keys
Nonce Handling
tag mismatch.Compatibility with Other Libraries
firebase/php-jwt) may not natively support custom algorithms.AESGCM class directly with the library’s encode/decode methods, as shown in Getting Started.Performance Overhead
php artisan tinker
>>> \WebToken\JWTEncryptionAlgorithm\AESGCM::bench();
Decryption Failures
var_dump($key === config('jwt.aes_key'))).try {
$decoded = JWT::decode($token, '', ['HS256'], $algorithm);
} catch (\Exception $e) {
\Log::error('JWT Decode Error', ['token' => $token, 'error' => $e->getMessage()]);
}
Tag Mismatch Errors
$algorithm = new AESGCM($key, ['nonce' => random_bytes(12)]); // Custom nonce
Custom Nonce Generation
$algorithm = new AESGCM($key, [
'nonce' => 'custom-nonce-123', // Must be 12 bytes
]);
Integration with Laravel’s Encryption
Crypt facade for hybrid encryption:
$encryptedKey = Crypt::encrypt($key);
$algorithm = new AESGCM(Crypt::decrypt($encryptedKey));
Testing
PHPUnit to mock the algorithm for unit tests:
$mockAlgorithm = $this->createMock(AESGCM::class);
$mockAlgorithm->method('encrypt')->willReturn('mocked-ciphertext');
How can I help you explore Laravel packages today?