kelvinmo/simplejwt
SimpleJWT is a lightweight PHP library for creating, signing, verifying, and encrypting JSON Web Tokens (JWT/JWS/JWE). Supports JWK/COSE keys, HMAC/RSA/ECDSA/EdDSA algorithms, and common key management and AES encryption methods.
Pros:
KeySet (JWK, PEM, symmetric secrets) simplifies integration with existing infrastructure (e.g., HashiCorp Vault, AWS KMS).AlgorithmInterface, KeyInterface) allows custom algorithms or key types if needed.Cons:
gmp, openssl, sodium (for EdDSA/X25519), which may not be enabled in all PHP environments.ValidateJWT middleware).exp, nbf) for security.config or environment variables for secrets, and integrate with a key management service (KMS).alg/enc headers (e.g., RS256 without RSA keys) may cause runtime errors.gmp, openssl, or sodium.sodium may not be FIPS-certified in all versions.)SimpleJWT\Keys\KeySet can be bound as a singleton or resolved dynamically.app/Http/Middleware/ValidateJWT.php).php artisan jwt:generate-key).json or text columns (e.g., key_sets table with key_id, algorithm, public_key, private_key).encrypt for sensitive keys in the database.KeySet objects in Redis/Memcached to avoid repeated file I/O.kid headers to identify active keys).// app/Http/Middleware/ValidateJWT.php
public function handle(Request $request, Closure $next) {
$token = $request->bearerToken();
$keySet = app(KeySet::class);
try {
$jwt = JWT::decode($token, $keySet, 'HS256');
$request->merge(['user' => $jwt->getClaim('sub')]);
} catch (InvalidTokenException $e) {
return response('Unauthorized', 401);
}
return $next($request);
}
simplejwt for token generation/validation.axios interceptors for session cookies with JWT storage (e.g., localStorage or HTTP-only cookies).JWT::deserialise() to parse tokens from non-Laravel systems without validation.composer require kelvinmo/simplejwt.gmp, openssl, sodium).openssl genrsa) and store in KeySet.// config/jwt.php
'keys' => [
'default' => [
'algorithm' => 'RS256',
'public_key' => file_get_contents(storage_path('app/jwt/public.pem')),
'private_key' => file_get_contents(storage_path('app/jwt/private.pem')),
],
];
app/Services/JWTService.php).kid rotation).kid headers enable smooth rotation (e.g., deprecate old keys after new ones are validated).KeySet.kelvinmo/simplejwt for security patches (e.g., algorithm vulnerabilities).exp claims are set correctly (e.g., `now()->addHours(1How can I help you explore Laravel packages today?