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.
firebase/php-jwt or lucadegasperi/oauth2-server for secure token handling, but requires explicit integration since Laravel’s default JWT libraries (e.g., tymlor/laravel-jwt-auth) lack native AES-GCM support.openssl_encrypt/openssl_decrypt).sodium or openssl extensions; AES-GCM is native in PHP 7.4+ via openssl).web-token/jwt-framework or firebase/php-jwt).| Risk Area | Severity | Mitigation Strategy |
|---|---|---|
| Cryptographic Misuse | High | Validate key lengths (16/24/32 bytes), GCM tags, and IVs. Use web-token/jwt-framework for structured validation. |
| Key Management | Critical | Integrate with a KMS or HSM; avoid hardcoded keys. |
| Performance Overhead | Medium | Benchmark AES-GCM vs. RSA/OAEP for your workload. Consider hybrid encryption (e.g., RSA-KEM + AES-GCM). |
| Backward Compatibility | High | Encrypted JWTs break existing decoders. Requires dual-mode (encrypted + unencrypted) during migration. |
| IV/Nonce Handling | High | Ensure unique IVs per encryption (AES-GCM requirement). Use random_bytes() or openssl_random_pseudo_bytes(). |
paragonie/sodium_compat) or Tink (Google’s crypto library)?Laravel Ecosystem:
Auth::login() or custom JWTService to encrypt payloads before signing.JWTGuard or middleware to decrypt payloads pre-authentication.web-token/jwt-framework (modern, supports encryption) + this package.lucadegasperi/oauth2-server (if using OAuth2) with custom encryption logic.tymlor/laravel-jwt-auth (no native encryption support).Non-Laravel Dependencies:
extension=openssl is enabled in php.ini.sodium_crypto_secretbox (if using PHP 7.2+)./api/secure).web-token/jwt-framework:
use WebToken\JWT;
use WebToken\Encryption\AESGCM\AESGCM;
$jwt = new JWT();
$encrypter = new AESGCM(['key' => $yourKey, 'iv' => random_bytes(12)]);
$payload = ['user_id' => 123, 'data' => 'sensitive'];
$encryptedPayload = $encrypter->encrypt($payload);
$token = $jwt->encode($encryptedPayload, $signingKey);
JWTService to wrap encryption:
class EncryptedJWTService {
public function generate(array $payload): string {
$encrypted = $this->aesGcm->encrypt($payload);
return app(JWT::class)->encode($encrypted, config('jwt.secret'));
}
}
Auth::login() to use EncryptedJWTService.class DecryptJWTMiddleware {
public function handle($request, Closure $next) {
$token = $request->bearerToken();
$decrypted = $this->aesGcm->decrypt(
app(JWT::class)->decode($token, config('jwt.secret'))
);
$request->merge(['user' => $decrypted]);
return $next($request);
}
}
$client = new Aws\Kms\KmsClient([...]);
$key = $client->decrypt(['CiphertextBlob' => base64_decode($encryptedKey)]);
web-token/jwt-framework, firebase/php-jwt (with manual payload handling).tymlor/laravel-jwt-auth (no encryption hooks).openssl.paragonie/sodium_compat or rncryptor.config(['jwt.encrypt' => true])).web-token/jwt-encryption-algorithm-aesgcm to a specific version (low stars = higher risk of abandonment).try {
$decrypted = $this->aesGcm->
How can I help you explore Laravel packages today?