PHP code example of dalpras / oauth2-openid-server
1. Go to this page and download the library: Download dalpras/oauth2-openid-server library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?phprequire_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
dalpras / oauth2-openid-server example snippets
// Init Repositories
$clientRepository = new ClientRepository();
$scopeRepository = new ScopeRepository();
$accessTokenRepository = new AccessTokenRepository();
$authCodeRepository = new AuthCodeRepository();
$refreshTokenRepository = new RefreshTokenRepository();
$privateKeyPath = 'file://' . __DIR__ . '/../private.key';
$publicKeyPath = 'file://' . __DIR__ . '/../public.key';
// OpenID Response Type
$oidcResponse = new OidcResponse();
$oidcResponse->setIdentityRepository(new IdentityRepository());
$oidcResponse->setClaimExtractor(new ClaimExtractor());
// Setup the authorization server
$server = new \League\OAuth2\Server\AuthorizationServer(
$clientRepository,
$accessTokenRepository,
$scopeRepository,
$privateKey,
$publicKey,
$oidcResponse
);
$grant = new \DalPraS\OpenId\Server\Grant\OidcAuthCodeGrant($authCodeRepository, $refreshTokenRepository,
new \DateInterval(self::TTL_AUTH_CODE));
$grant->setRefreshTokenTTL(new \DateInterval('P1M')); // refresh tokens will expire after 1 month// Enable the authentication code grant on the server
$server->enableGrantType(
$grant,
new \DateInterval('PT1H') // access tokens will expire after 1 hour
);
return $server;
try {
// Validate the HTTP request and return an AuthorizationRequest object.// The auth request object can be serialized into a user's session
$authRequest = $server->validateAuthorizationRequest($request);
// Once the user has logged in set the user on the AuthorizationRequest
$authRequest->setUser($user);
// Once the user has approved or denied the client update the status// (true = approved, false = denied)
$authRequest->setAuthorizationApproved(true);
// Return the HTTP redirect responsereturn $server->completeAuthorizationRequest($authRequest, $response);
} catch (OAuthServerException $e) {
return $e->generateHttpResponse($response);
} catch (\Exception $e) {
return (new OAuthServerException($e->getMessage(), 0, 'unknown_error', 500))->generateHttpResponse($response);
}
$claimExtractor = new \DalPraS\OpenId\Server\ClaimExtractor();
// OpenID Response
$oidcResponse = new OidcResponse($userRepo, $claimExtractor);
// Setup the authorization server
$authServer = new \League\OAuth2\Server\AuthorizationServer(
$clientRepo,
$accessTokenRepo,
$scopeRepo,
$privateKeyPath,
'XXXX_XXX_XXX_XXX_XX',
$oidcResponse
);
// OpenID Response Type instead of Bearer
$middleware = new AuthorizationServerMiddleware($this->getAuthServer());
return $middleware->__invoke($psrRequest, $psrResponse, function($request, $response){
return $response;
});
// Example of the profile ClaimSet
$claimSet = new ClaimSetEntity('profile', [
'name',
'family_name',
'given_name',
'middle_name',
'nickname',
'preferred_username',
'profile',
'picture',
'website',
'gender',
'birthdate',
'zoneinfo',
'locale',
'updated_at'
]);
$extractor = new ClaimExtractor();
// Create your custom scope
$claimSet = new ClaimSetEntity('company', [
'company_name',
'company_phone',
'company_address'
]);
// Add it to the ClaimExtract (this is what you pass to IdTokenResponse, see configuration above)
$extractor->addClaimSet($claimSet);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.