firebase/php-jwt
Encode and decode JSON Web Tokens (JWT) in PHP per RFC 7519. Supports common signing algorithms, key handling, header access after verification, and clock-skew leeway. Install via Composer; optional sodium_compat for libsodium environments.
Illuminate\Contracts\Auth\Authenticatable) and guard systems, enabling JWT-based sessions or API tokens alongside traditional database auth.Key, CachedKeySet).VerifyJWTToken) to validate incoming requests, replacing or augmenting Laravel’s built-in auth middleware.CachedKeySet leverages PSR-6 caches (e.g., Redis, file-based), which Laravel already supports via Illuminate\Cache.| Risk Area | Mitigation Strategy |
|---|---|
| Algorithm Misuse | Enforce whitelisted algorithms in config (e.g., HS256 only for internal APIs). |
| Key Management | Use CachedKeySet with short TTLs (e.g., 5 mins) for key rotation. |
| Libsodium Dependency | Fallback to paragonie/sodium_compat if native libsodium is unavailable. |
| Performance | Benchmark HS256 vs. RS256 for latency-sensitive APIs (HS256 is faster but less secure). |
| Exception Handling | Centralize JWT error responses (e.g., 401 Unauthorized for SignatureInvalidException). |
CachedKeySet’s auto-refresh suffice, or is manual intervention needed?ExpiredException) trigger security alerts?sub, roles) map to Laravel’s user model or a custom structure?Illuminate\Auth\Guard to validate JWTs.JwtMiddleware to parse tokens from Authorization: Bearer headers.Firebase\JWT\JWT as a singleton for dependency injection.firebase/php-jwt (JWT logic).paragonie/sodium_compat (if libsodium is missing).guzzlehttp/guzzle + phpfastcache (for CachedKeySet).league/oauth2-server (if integrating with OAuth 2.0).revoked_tokens table.| Phase | Action | Tools/Libraries |
|---|---|---|
| Evaluation | Benchmark against Laravel Sanctum/Passport for performance/security tradeoffs. | phpbench, laravel-debugbar |
| Pilot | Replace a single API endpoint’s auth with JWT (e.g., /api/v1/protected). |
Laravel Middleware, Postman for testing |
| Core Integration | Add JWT middleware globally; integrate with Laravel’s Auth::attempt() for hybrid auth. |
Illuminate\Auth\Events, HasApiTokens |
| Key Management | Deploy CachedKeySet with Redis cache; set up key rotation cron job. |
spatie/laravel-cron-job |
| Monitoring | Log JWT decode failures; alert on SignatureInvalidException spikes. |
Laravel Horizon, Sentry |
paragonie/sodium_compat is installed.openssl (for RS/ES algorithms).libsodium (for EdDSA; fallback to sodium_compat).Phase 1: Core JWT Support
firebase/php-jwt to composer.json.JwtServiceProvider to bind the JWT class and configure defaults (e.g., JWT::$leeway = 300).JwtMiddleware to parse tokens from headers/cookies.Phase 2: Key Management
CachedKeySet with Redis for JWKS caching.Phase 3: Laravel Integration
Auth::user() to accept JWT payloads (e.g., sub claim maps to users(id)).HasApiTokens trait to models for hybrid auth.Phase 4: Security Hardening
HS256 or RS256 in config.throttle:60,1).firebase/php-jwt for CVE updates (e.g., algorithm vulnerabilities).paragonie/sodium_compat updates if using EdDSA.CachedKeySet (refreshes on invalid key access).SignatureInvalidException) to detect brute-force attacks.iat, exp claims) for compliance.JWT::$leeway = 60 to account for server time differences.{"keys": [...]}).kid claims match keys in CachedKeySet.jwt.io to decode tokens manually for troubleshooting.dd() or Xdebug for payload inspection.DELETE FROM revoked_tokens).How can I help you explore Laravel packages today?