Installation Add the bundle via Composer:
composer require dos/oauth-server-bundle
Register the bundle in config/bundles.php (Symfony) or config/app.php (Laravel via Symfony bridge):
return [
// ...
Dos\OAuthServerBundle\DosOAuthServerBundle::class => ['all' => true],
];
Configuration Publish the default config:
php artisan vendor:publish --provider="Dos\OAuthServerBundle\DosOAuthServerBundle" --tag=config
Update config/oauth_server.php with your OAuth server settings (e.g., grant_types, access_token_ttl).
First Use Case: Token Endpoint
Test the /oauth/token endpoint with a POST request:
curl -X POST http://your-app.test/oauth/token \
-d "grant_type=password&username=user&password=pass&client_id=client_id&client_secret=client_secret"
Verify the response includes an access_token.
Redirect to Authorization Endpoint
// In a Laravel controller
return redirect()->away(
route('oauth_server.authorize', [
'client_id' => 'your_client_id',
'redirect_uri' => url('oauth/callback'),
'response_type' => 'code',
'scope' => 'read write',
])
);
Use the route('oauth_server.authorize') helper (if registered).
Handle Callback
Configure a route for /oauth/callback:
Route::get('/oauth/callback', [OAuthController::class, 'handleCallback']);
Exchange the code for a token in the controller:
public function handleCallback(Request $request) {
$token = $request->input('code');
$response = Http::post('http://your-app.test/oauth/token', [
'grant_type' => 'authorization_code',
'code' => $token,
'redirect_uri' => url('oauth/callback'),
'client_id' => 'your_client_id',
'client_secret' => 'your_client_secret',
]);
// Store token and redirect.
}
Http facade or Guzzle for token requests.Route::middleware(['auth:api', 'oauth'])->group(function () {
// Protected routes
});
public function handle($request, Closure $next) {
if (!$request->user()->tokenCan('read')) {
abort(403);
}
return $next($request);
}
Deprecated Bundle
league/oauth2-server as a drop-in alternative.Config Overrides
security.yaml. For Laravel, manually map:
'users_provider' => function () {
return User::class; // Your Laravel User model
},
Token Storage
oauth_access_tokens table exists:
php artisan migrate
(If missing, create it manually or use a package like spatie/laravel-oauth-server.)Enable Logging
Add to config/oauth_server.php:
'logging' => true,
Check Laravel logs (storage/logs/laravel.log) for OAuth events.
Token Validation Errors Common issues:
invalid_grant: Expired code or mismatched redirect_uri.invalid_client: Incorrect client_id/secret.Custom Grant Types Extend the bundle by creating a custom grant handler:
// app/Providers/OAuthServiceProvider.php
public function register() {
$this->app->extend('oauth2.grant_type.custom', function () {
return new CustomGrant();
});
}
Register in config:
'grant_types' => ['password', 'custom'],
Laravel Events Listen for OAuth events (if supported):
Event::listen('oauth.server.token.created', function ($token) {
// Custom logic (e.g., log tokens)
});
API Resource Protection Use Laravel’s built-in auth + OAuth middleware:
Route::get('/api/data', function () {
return response()->json(['data' => 'protected']);
})->middleware(['auth:api', 'oauth']);
How can I help you explore Laravel packages today?