alancting/php-microsoft-jwt
Laravel/PHP helper for validating Microsoft (Azure AD) JWTs. Fetches and caches JWKS signing keys, verifies token signatures and claims, and supports common AAD scenarios so APIs can authenticate Microsoft identity tokens with minimal setup.
alancting/php-microsoft-jwt package is specifically designed for validating and verifying Microsoft Identity Platform (Azure AD) JWT tokens, making it a highly specialized fit for applications requiring OAuth 2.0/OIDC integration with Microsoft services (e.g., Azure AD, Microsoft Graph API, or custom identity providers).aud, iss, nonce, and Azure AD-specific claims) aligns well with Laravel’s ecosystem for authentication (e.g., Sanctum, Passport, or custom guard implementations).issuer, audience, and key validation.openssl and json extensions.AzureGuard extending TokenGuard) or middleware (e.g., ValidateMicrosoftJwt).| Risk Area | Assessment | Mitigation Strategy |
|---|---|---|
| Package Obsolescence | Last release in 2021; no updates for PHP 8.2+ or Laravel 10+. Risk of compatibility issues with modern PHP versions or Azure AD token formats. | Fork the repo and maintain it internally, or evaluate alternatives like firebase/php-jwt with custom Microsoft key validation. |
| Key Management | Relies on fetching Microsoft’s public keys dynamically. If Microsoft changes its discovery endpoint or key format, the package may break. | Cache keys locally (e.g., using Laravel’s cache) with a fallback to the discovery endpoint. Monitor Microsoft’s documentation for changes. |
| Token Format Changes | Azure AD may evolve its JWT structure (e.g., new claims, signing algorithms). The package’s rigid validation could fail silently. | Add custom validation hooks in Laravel to extend the package’s logic (e.g., via a decorator pattern). |
| Performance | Dynamic key fetching adds latency. For high-throughput apps, this could become a bottleneck. | Pre-load keys during application boot or use a CDN for Microsoft’s discovery endpoint. |
| Testing Coverage | No visible test suite in the repo. Risk of edge cases (e.g., malformed tokens, expired keys) not being handled gracefully. | Write comprehensive tests for the integration layer, including mocking Microsoft’s discovery endpoint and edge cases. |
firebase/php-jwt)?league/oauth2-client, php-jwt) considered, and if so, why was this chosen?AzureGuard extending TokenGuard).ValidateMicrosoftJwtMiddleware).| Step | Action | Dependencies |
|---|---|---|
| 1. Package Installation | Composer install alancting/php-microsoft-jwt. If PHP 8.2+, fork and patch for compatibility. |
Composer, PHP 7.4–8.1 (or patched version). |
| 2. Service Provider | Create a Laravel service provider (e.g., MicrosoftJwtServiceProvider) to bind the package’s validator to the container. |
Laravel Service Container. |
| 3. Configuration | Add config for issuer, audience, and key validation settings (e.g., config/auth.php). |
Laravel Config System. |
| 4. Middleware | Create middleware to validate JWTs on protected routes (e.g., app/Http/Middleware/ValidateMicrosoftJwt.php). |
Laravel Middleware, HTTP Kernel. |
| 5. Guard Integration | Extend Laravel’s TokenGuard to use the Microsoft validator (optional, for auth system integration). |
Laravel Auth System. |
| 6. Key Caching | Implement caching for Microsoft’s public keys (e.g., using Laravel Cache or Redis) to reduce latency. | Redis/Memcached or Laravel Cache. |
| 7. Testing | Write tests for: |
openssl and json extensions (standard in Laravel environments).How can I help you explore Laravel packages today?