firebase/php-jwt
Encode and decode JSON Web Tokens (JWT) in PHP per RFC 7519. Supports common signing algorithms, key handling, and optional leeway for clock skew. Install via Composer; libsodium compatible via sodium_compat.
Illuminate\Contracts\Auth\Guard) and middleware (e.g., auth:api). Can be used alongside Laravel Sanctum or Laravel Passport for hybrid auth flows.kid, alg), enabling multi-tenant key validation or algorithm negotiation.Firebase\JWT\JWT and Firebase\JWT\Key, reducing manual instantiation.JwtAuthMiddleware) to validate tokens before route execution, replacing Laravel’s default auth:api.ExpiredException, SignatureInvalidException) can trigger Laravel events (e.g., jwt.expired) for logging or retries.CachedKeySet aligns with Laravel’s cache drivers (Redis, file, etc.), reducing JWK fetch latency in distributed systems.| Risk Area | Mitigation Strategy |
|---|---|
| Algorithm Mismatch | Enforce strict algorithm validation in middleware (e.g., reject none alg). |
| Key Rotation | Use CachedKeySet with short TTLs (e.g., 5 mins) and webhook triggers for cache invalidation. |
| Performance | Benchmark HS256 vs. RS256 (HS256 is faster but less secure for public keys). |
| Libsodium Dependency | Require paragonie/sodium_compat in composer.json with PHP 7.2+ fallback. |
| Token Size | Monitor JWT payload size (max ~4KB) to avoid base64 bloat in headers. |
| Deprecation | Monitor PHP 8.2+ compatibility (e.g., openssl API changes). |
SignatureInvalidException?iss, aud)?auth:api).axios interceptors).firebase/php-jwt.app/Http/Middleware/JwtAuth.php) to validate tokens globally.public function handle($request, Closure $next) {
try {
$token = $request->bearerToken();
$decoded = JWT::decode($token, new Key(config('jwt.secret'), 'HS256'));
auth()->setUser($decoded); // Integrate with Laravel auth
return $next($request);
} catch (Exception $e) {
return response()->json(['error' => 'Unauthorized'], 401);
}
}
CachedKeySet with Redis).iat, exp) to ELK/Datadog.| Component | Compatibility Notes |
|---|---|
| Laravel 10.x | Full support (PHP 8.1+). |
| Laravel Sanctum | Can coexist but requires custom middleware to avoid conflicts. |
| Laravel Passport | Use firebase/php-jwt for resource server validation (Passport handles OAuth). |
| PHP 7.2–8.2 | Works with libsodium or paragonie/sodium_compat. |
| Docker/Kubernetes | No issues; keys can be mounted as secrets. |
dd() or Xdebug to inspect stdClass payloads.JWT::$leeway = 300 (5 mins) for distributed systems.urlsafeB64Decode for EdDSA keys.throttle middleware to limit JWT decode attempts.| Failure Scenario | Mitigation |
|---|---|
| Expired Tokens | Return 401 Unauthorized with WWW-Authenticate: Bearer error="expired" header. |
| Invalid Signatures | Log IP/UA for brute-force detection. |
| Key Revocation | Use CachedKeySet with short TTLs to force cache refresh. |
| Payload Tampering | Validate critical claims (iss, aud) in middleware. |
| Libsodium Missing | Fallback to paragonie/sodium_compat in composer.json. |
openssl genrsa) and JWT structure.JWT::decode).SignatureInvalidException vs. ExpiredException).How can I help you explore Laravel packages today?