ekapusta/oauth2-esia
Laravel/PHP OAuth2 client for Russia’s ESIA (Gosuslugi) authentication. Provides ESIA OAuth flow integration, token handling, and user profile retrieval to add ESIA login to your application with minimal setup.
Installation
composer require ekapusta/oauth2-esia
Add the service provider to config/app.php:
Ekapusta\Oauth2Esia\Oauth2EsiaServiceProvider::class,
Configuration Publish the config file:
php artisan vendor:publish --provider="Ekapusta\Oauth2Esia\Oauth2EsiaServiceProvider" --tag="config"
Update .env with your ESIA credentials (Client ID, Secret, and Redirect URI).
First Use Case: Authentication Flow Redirect users to ESIA for authentication:
use Ekapusta\Oauth2Esia\Facades\Oauth2Esia;
$authUrl = Oauth2Esia::getAuthorizationUrl();
return redirect()->to($authUrl);
Handle the callback in your route:
Route::get('/esia/callback', function (Request $request) {
$token = Oauth2Esia::getAccessToken($request->query('code'));
$userData = Oauth2Esia::getUserData($token);
// Store $userData in session/database
});
Multi-Step Authentication
getAuthorizationUrl() to initiate OAuth flow.code in the callback, then exchange it for a token.getUserData($token).Token Management
$refreshedToken = Oauth2Esia::refreshAccessToken($refreshToken);
User Data Handling
personal_code, first_name) into your user model:
$user = User::updateOrCreate(
['esia_personal_code' => $userData['personal_code']],
[
'first_name' => $userData['first_name'],
'last_name' => $userData['last_name'],
]
);
Integration with Laravel Sessions
session(['esia_user' => $userData]);
Redirect URI Mismatch
redirect_uri in your config matches the callback URL in ESIA’s developer portal. Mismatches cause invalid_redirect_uri errors.Token Expiry
token_expired errors by refreshing the token or re-authenticating.Scope Restrictions
Rate Limiting
Enable Logging
Add to config/oauth2-esia.php:
'debug' => env('ESIA_DEBUG', false),
Check storage/logs/laravel.log for OAuth errors.
Validate Responses
Use dd($userData) to inspect raw ESIA responses. Fields may vary by endpoint.
Custom User Mapping Override the default data mapping in a service:
Oauth2Esia::setUserMapper(function ($data) {
return [
'tax_id' => $data['tax_id'] ?? null,
// Custom logic
];
});
Token Storage Extend the package to store tokens in a custom repository:
Oauth2Esia::setTokenRepository(new YourTokenRepository());
Webhook Integration Use ESIA’s webhooks (if supported) to receive real-time updates (e.g., user profile changes).
How can I help you explore Laravel packages today?