league/oauth2-server
Standards-compliant OAuth 2.0 authorization server for PHP. Protect APIs with bearer access tokens, issue and refresh tokens, and support common grants (auth code, client credentials, device, implicit, password, refresh) with PSR-7 interoperability and RFC support.
Pros:
league/oauth2-server). The core OAuth2 logic is framework-agnostic but adheres to PSR-7 (HTTP messages) and PSR-15 (middleware), making it a natural fit for Laravel’s middleware stack.GrantTypeInterface, CryptKeyInterface, and AuthorizationRequestInterface allow customization (e.g., adding new grants, token storage backends).RequestAccessTokenEvent, RequestRefreshTokenEvent) for observability and auditing.Cons:
laminas/diactoros or nyholm/psr7), adding minor overhead for routing/response handling.league/oauth2-server middleware) or a service container. Example:
use League\OAuth2\Server\AuthorizationServer;
use League\OAuth2\Server\ResourceServer;
$authServer = new AuthorizationServer(
$grantFactory,
$tokenRepository,
$clientRepository,
$entityManager
);
$app->pipe(ResourceServer::class);
league/oauth2-server + lcobucci/jwt (for JWT support).league/oauth2-server + laravel/passport (recommended) or custom PSR-7/PSR-15 setup.league/uri for URI parsing, symfony/event-dispatcher for events).spatie/laravel-activitylog for auditing.GrantTypeInterface.revokeRefreshTokens() but requires custom logic for token blacklisting.league/oauth2-server's setLeeway) must align with your clock skew tolerance.auth:api).Passport::tokens().league/oauth2-server as PSR-15 middleware:
$app->pipe(function (ServerRequestInterface $request) {
$resourceServer = new ResourceServer(
$tokenRepository,
$entityManager
);
return $resourceServer->validateAuthenticatedRequest($request);
});
# config/services.yaml
League\OAuth2\Server\AuthorizationServer:
arguments:
$grantFactory: '@League\OAuth2\Server\GrantFactory'
$tokenRepository: '@App\Repository\TokenRepository'
# ...
oauth_clients, oauth_access_tokens, oauth_refresh_tokens).AccessTokenRepositoryInterface, ClientRepositoryInterface, etc., using Redis, DynamoDB, or a database.composer require laravel/passport.php artisan passport:install.AuthServiceProvider and add Passport::routes().league/oauth2-server directly.
composer require league/oauth2-server lcobucci/jwt.TokenRepository, ClientRepository).Route::middleware(['auth:api'])->group(function () {
// Protected routes
});
php:8.2 or php:8.3 in Docker.laminas/diactoros (default) or nyholm/psr7.lcobucci/jwt (v4.1+ for StrictValidAt support).symfony/event-dispatcher (optional, for custom events).AccessTokenEntityInterface.league/oauth2-server.oauth_clients table or custom storage).How can I help you explore Laravel packages today?