fp/lightopenid
Deprecated: composer-enabled mirror of the official LightOpenID library from gitorious. Originally used with FpOpenIdBundle for easier setup. Switch to the official Packagist package lightopenid/lightopenid instead.
Install the Official Package (as per README):
composer require lightopenid/lightopenid
(Note: Ignore the deprecated mirror; use the official lightopenid/lightopenid package.)
Basic Authentication Flow:
use Lightopenid\Lightopenid;
$openid = new Lightopenid('https://your-laravel-app.com/auth/callback');
if (!$openid->authenticate($_GET['openid'], $_POST['openid'])) {
die('Authentication failed.');
}
$identity = $openid->identity;
$verified = $openid->verified;
First Use Case: Login Route
Route::get('/login', function () {
return view('auth.openid_login', [
'openid' => new Lightopenid('https://your-app.com/auth/callback')
]);
});
Route::get('/auth/callback', function () {
$openid = new Lightopenid('https://your-app.com/auth/callback');
if ($openid->handle()) {
$user = User::firstOrCreate(['openid' => $openid->identity]);
auth()->login($user);
return redirect('/dashboard');
}
return redirect('/login?error=1');
});
View Template (resources/views/auth/openid_login.blade.php):
<form action="{{ $openid->authUrl($_GET['openid'] ?? '') }}" method="post">
<input type="text" name="openid" value="{{ old('openid') }}">
<button type="submit">Login with OpenID</button>
</form>
User Initiates Login:
/login with a form to input OpenID URL (e.g., https://example.com/user or [email protected]).OpenID Provider Redirect:
authUrl() (generated by Lightopenid), which redirects to the OpenID provider (Google, GitHub, etc.).Callback Handling:
/auth/callback with openid and mode params.$openid->handle() to validate the response and extract identity.User Association:
User model (e.g., via openid column).auth()->login() to authenticate the user.Middleware for Protected Routes:
Route::middleware(['auth:openid'])->group(function () {
// Routes requiring OpenID auth
});
(Create a custom middleware to validate OpenID sessions.)
Session Persistence:
Store $openid->identity in the user session or database to avoid re-authentication on subsequent requests.
Fallback for Non-OpenID Users: Combine with Laravel’s default auth (e.g., email/password) for hybrid systems:
if (auth()->check()) {
// User already logged in via another method
} else {
// Proceed with OpenID
}
Logging and Debugging:
Log $openid->getMessages() for troubleshooting failed authentications:
\Log::debug('OpenID Errors:', $openid->getMessages());
Deprecated Package Warning:
fp/lightopenid is deprecated. Always use lightopenid/lightopenid (official package).CSRF Vulnerabilities:
/auth/callback) is protected against CSRF if it accepts POST data.verifyCsrfToken middleware for POST requests to the callback.Identity Format Inconsistencies:
https://...) or emails ([email protected]). Normalize them before storing in the database:
$identity = str_starts_with($openid->identity, 'http') ?
$openid->identity :
'https://' . str_replace('@', '/~', $openid->identity);
Provider-Specific Quirks:
Lightopenid compatibility.return_to or realm parameters to the Lightopenid constructor:
$openid = new Lightopenid('https://your-app.com/auth/callback', [
'return_to' => 'https://your-app.com/dashboard',
'realm' => 'https://your-app.com'
]);
Session Fixation:
if ($openid->handle()) {
session()->regenerate();
// Proceed with login
}
Enable Debug Mode:
Pass true to the Lightopenid constructor to see verbose output:
$openid = new Lightopenid('...', [], true);
Check getMessages():
Always inspect $openid->getMessages() after failed authentications. Example errors:
openid.cancel: User canceled the login.openid.setup_needed: Provider requires additional setup.openid.failure: Generic failure (check provider logs).Provider-Specific Debugging:
Custom User Model Binding:
Extend the User model to handle OpenID-specific logic:
class User extends Authenticatable {
public static function findForOpenId($identity) {
return self::where('openid', $identity)->first();
}
}
OpenID Attribute Mapping: Fetch additional user attributes (e.g., name, email) from the provider:
$attributes = $openid->getAttributes();
$user->name = $attributes['name'] ?? $user->name;
$user->email = $attributes['email'] ?? $user->email;
$user->save();
Multi-Provider Support: Use a facade or service container to switch between OpenID providers dynamically:
class OpenIdService {
public function authenticate($provider, $identity) {
$openid = new Lightopenid('...');
// Provider-specific logic
return $openid->authenticate($identity);
}
}
Rate Limiting: Implement rate limiting for OpenID callbacks to prevent brute-force attacks:
Route::middleware(['throttle:60,1'])->get('/auth/callback', ...);
Webhook Integration:
For advanced use cases, integrate OpenID with Laravel webhooks (e.g., GitHub OAuth) by extending Lightopenid or using it as a base for custom providers.
How can I help you explore Laravel packages today?