Installation Add the bundle to your Laravel project via Composer:
composer require dbp/relay-auth-bundle
Publish the configuration file:
php artisan vendor:publish --provider="DBP\RelayAuthBundle\RelayAuthServiceProvider" --tag="config"
Configuration
Update config/relay-auth.php with your OIDC server details (e.g., issuer URL, client ID, client secret, and redirect URI). Example:
'oidc' => [
'issuer' => env('OIDC_ISSUER_URL'),
'client_id' => env('OIDC_CLIENT_ID'),
'client_secret' => env('OIDC_CLIENT_SECRET'),
'redirect_uri' => env('OIDC_REDIRECT_URI'),
],
First Use Case: Authenticate a User
Use the RelayAuth facade to authenticate a user via OIDC:
use DBP\RelayAuthBundle\Facades\RelayAuth;
// Redirect user to OIDC provider for login
$authUrl = RelayAuth::login();
return redirect()->to($authUrl);
// Handle callback after login
$user = RelayAuth::handleCallback(request());
Middleware Integration
Add the auth.relay middleware to routes requiring authentication:
Route::middleware(['auth.relay'])->group(function () {
// Protected routes
});
OIDC Authentication Flow
RelayAuth::login().RelayAuth::handleCallback($request).User Management
RelayAuth::createOrUpdateUser($idToken) method to sync user data from the OIDC provider to your Laravel users table.UserMapper class to handle custom claims from the OIDC token:
// app/Providers/RelayAuthServiceProvider.php
public function boot()
{
RelayAuth::extendUserMapper(function ($mapper) {
$mapper->mapCustomClaim('custom_claim', 'user_custom_field');
});
}
API Gateway Integration
RelayAuth::validateToken($token) method in middleware or controllers.RelayAuth::getRelayUser() helper to access the authenticated user in a Relay context (e.g., for API gateways).// Sync OIDC user with Sanctum/Passport
$user = RelayAuth::createOrUpdateUser($idToken);
$token = $user->createToken('Relay Token')->accessToken;
League\OAuth2\Client\Provider\GenericProvider instance:
RelayAuth::setOidcProvider($customProvider);
config/relay-auth.php for troubleshooting:
'debug' => env('RELAY_AUTH_DEBUG', false),
Callback URL Mismatch
redirect_uri in config/relay-auth.php matches the URL configured in your OIDC provider. Mismatches will cause authentication failures.OIDC_REDIRECT_URI in your .env file and OIDC provider settings.Token Expiry Handling
use DBP\RelayAuthBundle\Facades\RelayAuth;
public function handle($request, Closure $next)
{
if (!RelayAuth::validateToken($request->bearerToken())) {
return response()->json(['error' => 'Unauthorized'], 401);
}
return $next($request);
}
User Sync Conflicts
sub claim that doesn’t map cleanly to your users table, the createOrUpdateUser method may fail.UserMapper to handle edge cases:
RelayAuth::extendUserMapper(function ($mapper) {
$mapper->mapIdClaim('custom_id_field'); // Override default 'sub' claim
});
Session Fixation
'session' => [
'driver' => 'cookie',
'cookie' => 'secure',
],
'debug' => true in config/relay-auth.php to log detailed OIDC interactions.storage/logs/laravel.log for OIDC errors (e.g., invalid tokens, network issues).Custom User Model Override the default user model by binding a custom implementation:
RelayAuth::setUserModel(App\Models\CustomUser::class);
Event Listeners
Listen to OIDC events (e.g., oidc.login.success) for custom logic:
// app/Providers/EventServiceProvider.php
protected $listen = [
'DBP\RelayAuthBundle\Events\OidcLoginSuccess' => [
'App\Listeners\LogOidcLogin',
],
];
Relay-Specific Extensions
Extend the RelayAuth facade to add Relay-specific methods:
// app/Providers/RelayAuthServiceProvider.php
public function boot()
{
RelayAuth::extend(function ($auth) {
$auth->getRelayContext = function () {
return [
'user_id' => auth()->id(),
'scopes' => auth()->user()->scopes,
];
};
});
}
Testing
Mock the OIDC provider in tests using a library like mockery or league/oauth2-server:
$provider = Mockery::mock('overload', 'League\OAuth2\Client\Provider\GenericProvider');
RelayAuth::setOidcProvider($provider);
```markdown
### Config Quirks
- **Environment Variables**: The bundle expects specific env vars (`OIDC_ISSUER_URL`, `OIDC_CLIENT_ID`, etc.). Use placeholders like `env('OIDC_ISSUER_URL')` in `config/relay-auth.php` to avoid hardcoding.
- **Caching**: The bundle caches OIDC configuration by default. Clear the cache (`php artisan cache:clear`) if you update the config without seeing changes.
- **HTTPS Requirement**: The OIDC provider may reject requests over HTTP. Ensure your Laravel app uses HTTPS in production:
```php
'trusted_proxies' => [
'*',
],
'secure' => env('APP_ENV') === 'production',
How can I help you explore Laravel packages today?