Installation
composer require dontdrinkandroot/openid-bundle
Add to config/app.php under providers:
Dontdrinkandroot\OpenidBundle\OpenidBundle::class,
Publish Configuration
php artisan vendor:publish --provider="Dontdrinkandroot\OpenidBundle\OpenidBundle" --tag="config"
Edit config/openid.php to configure your OpenID provider (e.g., Google, GitHub, or custom).
First Use Case: Login via OpenID
Add a route in routes/web.php:
Route::get('/login/openid', [OpenidController::class, 'login'])->name('openid.login');
Route::get('/openid/callback', [OpenidController::class, 'callback'])->name('openid.callback');
Use the OpenidController to handle authentication:
use Dontdrinkandroot\OpenidBundle\Controller\OpenidController;
Authentication Flow
/login/openid to initiate OpenID login./openid/callback.Route::middleware(['auth:openid'])->group(function () {
// Protected routes
});
User Integration
OpenidAuthenticated event. Listen to it in an event service provider:
public function boot()
{
event(new OpenidAuthenticated($user));
}
User):
$user = User::firstOrCreate([
'openid_provider' => $provider,
'openid_id' => $openidId,
], [
'name' => $userData['name'],
'email' => $userData['email'],
]);
Custom Providers
OpenidProvider class to support non-standard providers:
namespace App\Openid;
use Dontdrinkandroot\OpenidBundle\OpenidProvider;
class CustomProvider extends OpenidProvider
{
public function getAuthUrl(): string
{
return 'https://custom-provider.com/oauth/authorize';
}
public function getTokenUrl(): string
{
return 'https://custom-provider.com/oauth/token';
}
}
config/openid.php:
'providers' => [
'custom' => [
'class' => App\Openid\CustomProvider::class,
'client_id' => env('CUSTOM_CLIENT_ID'),
'client_secret' => env('CUSTOM_CLIENT_SECRET'),
],
],
Session Management
OpenidGuard to manage sessions:
Auth::guard('openid')->attempt($credentials);
Missing Configuration
php artisan vendor:publish) will result in undefined provider settings. Always verify config/openid.php after installation.Callback URL Mismatch
redirect_uri in your OpenID provider settings matches the callback route (/openid/callback). Mismatches will cause authentication failures.State Parameter Handling
state parameter for CSRF protection. If you modify the flow (e.g., adding custom query params), ensure the state is preserved and validated.User Data Mapping
OpenidAuthenticated event listener or override the handleAuthentication method in a custom provider.Token Expiry
Enable Logging
Add to config/logging.php to debug OpenID requests:
'channels' => [
'openid' => [
'driver' => 'single',
'path' => storage_path('logs/openid.log'),
'level' => 'debug',
],
],
Then log requests in your custom provider:
\Log::channel('openid')->debug('Auth URL:', [$this->getAuthUrl()]);
Check Provider Responses
Use dd() or Log::debug() to inspect the raw response from the OpenID provider:
$response = $this->getAuthResponse();
\Log::debug('Provider Response:', [$response->getBody()]);
Environment Variables
Store sensitive data (e.g., client_id, client_secret) in .env:
OPENID_GOOGLE_CLIENT_ID=your_client_id
OPENID_GITHUB_CLIENT_SECRET=your_secret
Testing Use mock providers (e.g., Mockoon) to simulate OpenID responses during development. Override the provider class in tests:
$this->app->bind(
Dontdrinkandroot\OpenidBundle\OpenidProvider::class,
App\Openid\MockProvider::class
);
Extending Functionality
event(new OpenidAuthenticated($user, ['custom_claim' => $data]));
$this->app->bind(
Dontdrinkandroot\OpenidBundle\Services\UserResolver::class,
App\Openid\CustomUserResolver::class
);
Performance
$provider = Cache::remember('openid.provider.google', 86400, function () {
return new GoogleProvider(config('openid.providers.google'));
});
Security
state parameter on callback to prevent CSRF attacks:
if (!hash_equals($_SESSION['openid_state'], $_GET['state'])) {
abort(403, 'Invalid state parameter.');
}
How can I help you explore Laravel packages today?