steverhoades/oauth2-openid-connect-server
Laravel-friendly OAuth2 + OpenID Connect server built on League OAuth2. Provides OIDC discovery and JWT ID tokens, common grant types, and flexible storage/adapters so you can run your own authorization server for APIs and SSO.
Authenticatable, HasApiTokens) for user management, minimizing custom logic for token generation/validation.Illuminate\Http\Middleware).config/ structure. Example:
// config/oauth.php
'openid' => [
'issuer' => env('APP_URL'),
'claims' => ['sub', 'name', 'email'],
],
openssl extension must be enabled; fallback to libraries like firebase/php-jwt may be needed if native support is lacking.state parameter requires secure storage (e.g., Redis) to prevent CSRF. Laravel’s session or cache drivers can mitigate this, but performance implications exist at scale..well-known/openid-configuration endpoint must be publicly accessible. Laravel’s route caching and middleware groups (e.g., web) may need adjustment to expose it without auth.UserEntity interface. Laravel’s User model can implement this, but custom fields (e.g., avatar_url) may need mapping logic.users table, LDAP, or a custom provider?)userinfo endpoint scale with high read loads? (Caching strategies?)log channel or a dedicated service like Sentry?)auth:api).Route::get('/.well-known/openid-configuration', [OIDCConfigController::class]);
Illuminate\Auth\Events for OIDC-specific hooks (e.g., OIDCLogin).openssl is unavailable.symfony/http-client).Phase 1: Setup
composer require steverhoades/oauth2-openid-connect-server
config/oauth.php:
php artisan vendor:publish --provider="League\OAuth2\Server\Laravel\ServiceProvider"
AppServiceProvider:
$server = new \League\OAuth2\Server\Server(
new \League\OAuth2\Server\ResourceServer(),
new \League\OAuth2\Server\Grant\PasswordGrant(),
new \League\OAuth2\Server\Storage\Pdo(),
new \League\OAuth2\Server\CryptKey\PrivateKey(
file_get_contents(storage_path('app/private.key')),
null,
'RS256'
),
new \League\OAuth2\Server\CryptKey\PublicKey(
file_get_contents(storage_path('app/public.key')),
'RS256'
)
);
$server->enableGrantType(
new \Steverhoades\OIDC\Grant\PasswordGrant(),
new \Steverhoades\OIDC\Grant\AuthorizationCodeGrant()
);
Phase 2: User Integration
UserEntity interface for Laravel’s User model:
class User extends Authenticatable implements UserEntity {
public function getIdentifier() { return $this->id; }
public function getUsername() { return $this->email; }
public function getEmail() { return $this->email; }
// ... other required methods
}
Phase 3: Endpoint Exposure
Route::post('/oauth/token', [OAuthController::class, 'issueToken']);
Route::get('/userinfo', [OAuthController::class, 'userInfo']);
Route::get('/.well-known/openid-configuration', [OIDCConfigController::class]);
auth:api for /userinfo).Phase 4: Testing
cache() helper for userinfo responses to reduce database load.auth() helper; OIDC tokens can be mapped to Laravel sessions via middleware.league/oauth2-server).Log::channel('oidc')).league/oauth2-server and steverhoades/oauth2-openid-connect-server for breaking changes. Laravel’s composer update + testing pipeline mitigates risk.config/oauth.php to avoid hardcoded values.// app/Console/Commands/RotateOIDCKeys.php
public function handle() {
$this->generateNewKeys();
$this->updateOAuthConfig();
}
dd() or dump() for middleware inspection. Enable OAuth2 server debug mode:
$server->setDebug(true);
axios for SPAs, requests-oauthlib for Python clients).docs/oidc.md in the repo with:
How can I help you explore Laravel packages today?