- How do I integrate AES-KW encryption into Laravel for JWTs?
- Use the package with the web-token/jwt-framework to wrap/unwrap JWTs. Register a PSR-15 middleware in Laravel’s middleware stack to handle JWE encoding/decoding before/after processing requests. Example: `app/Http/Middleware/EncryptJWT.php` with `JWE::encrypt()` and `JWE::decrypt()`.
- Which Laravel versions support this package?
- This package works with Laravel 8+ and requires PHP 7.4+. Ensure OpenSSL is enabled in your PHP environment, as AES-KW operations depend on it. Test thoroughly in your Laravel version before production deployment.
- Can I use this for OAuth2 token encryption in Laravel?
- Yes, AES-KW is ideal for encrypting OAuth2 tokens in Laravel. Integrate it with libraries like `lucadegasperi/oauth2-server` by wrapping access tokens in JWE before issuance. Ensure your OAuth2 provider supports JWE payloads.
- How do I handle key management for AES-KW in production?
- Store encryption keys securely using Laravel’s `.env` or a secrets manager like AWS KMS or HashiCorp Vault. Avoid hardcoding keys. Implement key rotation by generating new keys and phasing out old ones gradually, updating all services accordingly.
- Is AES-KW slower than other JWT encryption methods like AES-GCM?
- Yes, AES-KW is more CPU-intensive than AES-GCM due to key wrapping overhead. Benchmark performance in your Laravel environment, especially for high-throughput APIs (e.g., >10K RPS). Consider AES-GCM via `defuse/php-encryption` if simplicity is a priority.
- Will existing JWT clients break if I switch to AES-KW?
- Yes, existing clients relying on plain JWTs will fail. Update all clients to decode JWE payloads before processing. Use feature flags or versioned endpoints to migrate gradually, providing clear deprecation timelines.
- Are there Laravel-specific helpers for this package?
- No, this package is framework-agnostic. You’ll need to manually integrate it into Laravel’s middleware, service container, or custom logic. For example, bind the `JWE` class to an interface in Laravel’s `AppServiceProvider` for dependency injection.
- How do I test AES-KW JWTs in Laravel?
- Use Pest or PHPUnit to test JWE encoding/decoding with sample payloads. Mock the middleware in tests to verify request/response transformations. Test edge cases like truncated tokens, wrong keys, and malformed JWE payloads to ensure graceful error handling.
- What alternatives exist for JWT encryption in Laravel?
- Consider `firebase/php-jwt` with custom encryption (e.g., AES-GCM) or `spomky-labs/uuid` for simpler use cases. For Laravel-specific solutions, explore `typhonlabs/laravel-jwt-auth` (though it may require forking for AES-KW support). Evaluate trade-offs like ease of use and performance.
- How do I handle malformed JWE tokens in Laravel?
- Implement error handling in your middleware to catch exceptions like `JWEException` or `InvalidArgumentException`. Return HTTP 400 or 401 responses with clear messages (e.g., ‘Invalid or corrupted token’). Log errors for debugging without exposing sensitive details.