laravel/passport
Laravel Passport provides a full OAuth2 server for Laravel, enabling API authentication with access tokens, personal access tokens, and client credentials. Includes token issuing, revocation, and scope support with first-party integration.
Laravel Passport is a first-class OAuth2 server implementation for Laravel, designed to integrate seamlessly with the framework’s ecosystem. It aligns well with:
Key strengths:
Anti-patterns:
| Factor | Feasibility | Notes |
|---|---|---|
| Laravel Version | High | Supports Laravel 10+ (v13.x); backward-compatible with 9.x via older versions. |
| Database | High | Uses standard oauth_* tables (MySQL, PostgreSQL, SQLite). Customizable via migrations. |
| Existing Auth | Medium-High | Requires OAuthenticatable interface on user models; may need guard/config adjustments. |
| Third-Party APIs | High | Acts as an OAuth2 provider for external clients (e.g., React Native, Postman, or other APIs). |
| Performance | High | Optimized for token generation/revocation; supports caching (e.g., Redis for token storage). |
| Security | High | Built-in CSRF, PKCE, and token revocation; MITM-resistant with HTTPS. |
Critical Dependencies:
league/oauth2-server (v9.x): Core OAuth2 logic.firebase/php-jwt: JWT token handling (v7.x+).auth and hash components.Potential Conflicts:
findForPassport() method (customizable but adds boilerplate).Passport::routes() must be called in AuthServiceProvider.| Risk Area | Severity | Mitigation |
|---|---|---|
| Migration Complexity | Medium | Schema changes (e.g., UUIDs for clients in v13) may require downtime. Use Passport::hash() and test thoroughly. |
| Token Leakage | High | Client secrets must be hashed; avoid logging tokens. Use Passport::tokensExpireIn() wisely. |
| Grant Misconfiguration | Medium | Incorrect grant types (e.g., enabling Password Grant for public APIs) can expose vulnerabilities. Audit with php artisan passport:keys. |
| Performance Bottlenecks | Low-Medium | Token revocation queries can be slow; consider softDeletes or Redis for large-scale systems. |
| Version Lock-in | Low | Laravel’s semantic versioning ensures backward compatibility; major upgrades (e.g., v12→v13) require testing. |
Key Questions for Stakeholders:
OAuthenticatable without breaking existing auth?sub, groups)?passport:keys --force) in production?Token model.)| Component | Compatibility | Notes |
|---|---|---|
| Laravel Core | Native | Designed for Laravel’s service container, Eloquent, and routing. |
| PHP 8.2+ | High | v13.x requires PHP 8.2+; v12.x supports 8.1. |
| Databases | High | MySQL, PostgreSQL, SQLite (via Eloquent). |
| Caching | Optional | Redis recommended for token storage (via Passport::tokensCanBeRevoked()). |
| Queues | Optional | Async token revocation possible with Laravel Queues. |
| Frontend | High | Works with React, Vue, mobile apps (via OAuth2 flows). |
| API Gateways | High | Acts as an OAuth2 provider for Kong, Apigee, or custom gateways. |
Recommended Stack for New Projects:
Passport::tokensCanBeRevoked()).oauth_clients table).composer require laravel/passport
php artisan passport:install
php artisan migrate
Passport::routes() to AuthServiceProvider.OAuthenticatable on user model.php artisan vendor:publish --provider="Laravel\Passport\PassportServiceProvider".Passport::actingAs() for testing./oauth/token, /oauth/authorize).php artisan passport:install (creates migrations).OAuthenticatable.Passport::tokensCanBeRevoked() to invalidate old tokens./api).Route::middleware(['auth:api', 'passport'])->group(function () {
// API routes using OAuth2
});
| Scenario | Compatibility | Workarounds |
|---|---|---|
| Laravel 9.x | Medium | Use Passport v12.x; may require manual migrations for v13.x features. |
| Custom User Models | High | Implement OAuthenticatable and findForPassport(). |
| Non-UUID Primary Keys | Medium | Configure Passport::useUUIDs(false) (but lose some features like client confidentiality). |
| Third-Party OAuth Clients | High | Clients can register via API (/oauth/clients) or manually. |
| Legacy Token Systems | Low-Medium | Use Passport::tokensCanBeRevoked() to invalidate old tokens during migration. |
| Multi-Tenancy | High | Scope tokens to tenants via custom claims or middleware. |
**Breaking Changes
How can I help you explore Laravel packages today?