Installation
composer require 2lenet/oauth-client-bundle
Add the bundle to config/bundles.php:
return [
// ...
TwoLenet\OAuthClientBundle\TwoLenetOAuthClientBundle::class => ['all' => true],
];
Configuration
config/packages/two_lenet_oauth_client.yaml.dist to config/packages/two_lenet_oauth_client.yaml and update:
two_lenet_oauth_client:
client_id: '%env(OAUTH_CLIENT_ID)%'
client_secret: '%env(OAUTH_CLIENT_SECRET)%'
redirect_uri: '%env(OAUTH_REDIRECT_URI)%'
base_url: 'https://connect.2le.net' # Default; override if needed
First Use Case: Authentication Flow
use TwoLenet\OAuthClientBundle\Service\OAuthService;
public function authenticate(OAuthService $oauthService)
{
$authUrl = $oauthService->getAuthorizationUrl(['scope' => 'read write']);
return redirect($authUrl);
}
public function callback(OAuthService $oauthService, Request $request)
{
$token = $oauthService->handleAuthorizationCallback($request);
// Store $token->getAccessToken() securely (e.g., session/DB)
}
Token Management
OAuthService::refreshAccessToken($refreshToken).$user->accessToken = $token->getAccessToken();
$user->refreshToken = $token->getRefreshToken();
$user->tokenExpiresAt = $token->getExpiresAt();
$user->save();
API Integration
$client = new Client([
'headers' => [
'Authorization' => 'Bearer ' . $user->accessToken,
],
]);
Scopes and Permissions
$authUrl = $oauthService->getAuthorizationUrl(['scope' => 'scope1 scope2']);
Middleware for Protected Routes
use TwoLenet\OAuthClientBundle\Service\TokenValidator;
public function handle(Request $request, Closure $next, TokenValidator $validator)
{
if (!$validator->validate($request->bearerToken())) {
abort(401);
}
return $next($request);
}
.env for sensitive data:
OAUTH_CLIENT_ID=your_client_id
OAUTH_CLIENT_SECRET=your_secret
OAUTH_REDIRECT_URI=https://your-app.com/oauth/callback
$authUrl = $oauthService->getAuthorizationUrl([
'scope' => 'read',
'state' => bin2hex(random_bytes(32)),
]);
config/packages/two_lenet_oauth_client.yaml:
debug: true
Logs will appear in var/log/dev.log.Internal Documentation Dependency
Token Expiry Handling
if ($token->hasExpired()) {
$token = $oauthService->refreshAccessToken($user->refreshToken);
$user->update(['accessToken' => $token->getAccessToken()]);
}
Redirect URI Mismatch
redirect_uri in the bundle must match the one registered in 2le Connect.Scope Validation
debug: true in config to log OAuth responses.POST https://connect.2le.net/oauth/token
Headers: Content-Type: application/x-www-form-urlencoded
Body: grant_type=authorization_code&code={CODE}&redirect_uri={URI}
invalid_grant: Expired code or refresh token.redirect_uri_mismatch: Callback URL doesn’t match registration.Custom Token Storage
TwoLenet\OAuthClientBundle\Service\TokenStorageInterface to use a custom storage backend (e.g., Redis).Event Listeners
oauth.token.refresh):
// config/services.yaml
TwoLenet\OAuthClientBundle\EventListener\TokenRefreshListener:
tags:
- { name: kernel.event_listener, event: oauth.token.refresh, method: onTokenRefresh }
Custom Grant Types
authorization_code by default. For other grants (e.g., client_credentials), extend OAuthService:
$token = $oauthService->getToken('client_credentials', [
'client_id' => $clientId,
'client_secret' => $clientSecret,
]);
How can I help you explore Laravel packages today?