laravel/passport
Laravel Passport provides an OAuth2 server for Laravel, enabling API authentication with personal access tokens, password and authorization code grants, and client credentials. Integrates with Laravel’s auth system for secure, standards-based token issuing.
Laravel Passport is a first-class fit for Laravel-based applications requiring OAuth2 authentication. It integrates seamlessly with Laravel’s ecosystem, leveraging:
auth:api) for route protection.Key architectural strengths:
Client, Token, RefreshToken) and migrations, reducing boilerplate.Potential misfits:
| Aspect | Feasibility | Notes |
|---|---|---|
| Laravel Version | High | Supports Laravel 10+ (tested up to v13.x). Backward-compatible with minor adjustments. |
| Database Schema | Medium | Requires oauth_clients, oauth_access_tokens, oauth_refresh_tokens, and oauth_scopes tables. |
| Existing Auth System | High | Integrates with Laravel’s User model or custom auth providers via findForPassport(). |
| API Gateway | High | Works with API gateways (e.g., Kong, AWS ALB) via token validation middleware. |
| Third-Party Clients | High | Supports public/confidential clients (e.g., SPAs, mobile apps, server-to-server). |
| Customization | High | Extensible via events (Registered, Revoked), middleware, and token model overrides. |
Critical dependencies:
league/oauth2-server (v9.x): Core OAuth2 logic.firebase/php-jwt: JWT token handling (v6+).hash helper: For client secret hashing.| Risk Area | Severity | Mitigation |
|---|---|---|
| Migration Complexity | Medium | Schema changes in v13.x (e.g., UUID clients, table refactoring) require careful backward migration. |
| Token Revocation | Low | Built-in revocation logic, but custom scopes/claims may need manual cleanup. |
| Performance | Medium | Token validation adds overhead (~1–5ms per request). Benchmark under load; consider caching. |
| Security | High | Defaults are secure (e.g., hashed secrets, CSRF protection), but misconfigurations (e.g., public clients with secrets) risk exposure. |
| Grant Type Support | Low | All common grants are supported, but niche flows (e.g., SAML) require custom middleware. |
| Deprecations | Low | JSON API deprecated in v13.x; upgrade path is documented. |
| PHP 8.5+ Compatibility | Low | Tested and compatible, but some legacy code may need adjustments. |
Key questions for the TPM:
Client model events.)TokenGuard caching or Redis.)Client model attributes.)Laravel Passport is optimized for:
Less ideal for:
composer require laravel/passport
php artisan passport:install
php artisan vendor:publish --provider="Laravel\Passport\PassportServiceProvider".config/auth.php to use TokenGuard.App\User (or custom model) to implement Laravel\Passport\HasApiTokens.php artisan migrate to create OAuth tables.auth:api middleware.Route::middleware(['auth:api'])->get('/user', function (Request $request) {
return $request->user();
});
Client model or admin UI (e.g., Laravel Nova).$client = Client::create([
'name' => 'Mobile App',
'secret' => Str::random(40),
'redirect' => 'myapp://callback',
]);
Passport::enableRefreshTokens().oauth_scopes table or via migrations.$token->scopes()->attach(['read', 'write']);
Laravel\Passport\Token to add claims:
class CustomToken extends Token {
public function getCustomClaim() {
return $this->claims['custom'] ?? null;
}
}
Passport::tokensExpireIn(CarbonInterval::hours(1));
Passport::refreshTokensExpireIn(CarbonInterval::days(30));
Passport::personalAccessTokensExpireIn(never());
Registered, Revoked, etc.:
Passport::tokensRegistered(function ($user, $token) {
// Log or notify
});
Passport::actingAs() for API tests:
Passport::actingAs($user, ['read']);
oauth_access_tokens table is indexed for performance.How can I help you explore Laravel packages today?