1. Go to this page and download the library: Download itk-dev/openid-connect library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
itk-dev / openid-connect example snippets
use ItkDev\OpenIdConnect\Security\OpenIdConfigurationProvider;
$provider = new OpenIdConfigurationProvider([
'redirectUri' => 'https://some.url', // Absolute url to where the user is redirected after a successful login
'openIDConnectMetadataUrl' => 'https:/.../openid-configuration', // url to OpenId Discovery document
'cacheItemPool' => 'Psr6/CacheItemPoolInterface', // Implementation of CacheItemPoolInterface for caching above discovery document
'clientId' => 'client_id', // Client id assigned by authorizer
'clientSecret' => 'client_secret', // Client password assigned by authorizer
// optional values
'leeway' => 30, // Defaults to 10 (seconds)
'cacheDuration' => 3600, // Defaults to 86400 (seconds)
'allowHttp' => true, // Defaults to false. Allow OIDC urls with http scheme. Use only during development!
]);
// Get "state" and "nonce"
$state = $provider->generateState();
$nonce = $provider->generateNonce();
// Save to session
$session->set('oauth2state', $state);
$session->set('oauth2nonce', $nonce);
$authUrl = $provider->getAuthorizationUrl(['state' => $state, 'nonce' => $nonce]);
// redirect to $authUrl
// Validate that the request state and session state match
$sessionState = $this->session->get('oauth2state');
$this->session->remove('oauth2state');
if (!$sessionState || $request->query->get('state') !== $sessionState) {
throw new ValidationException('Invalid state');
}
// Validate the id token. This will validate the token against the keys published by the
// provider (Azure AD B2C). If the token is invalid or the nonce doesn't match an
// exception will thrown.
try {
$claims = $provider->validateIdToken($request->query->get('id_token'), $session->get('oauth2nonce'));
// Authentication successful
} catch (ItkOpenIdConnectException $exception) {
// Handle failed authentication
} finally {
$this->session->remove('oauth2nonce');
}
OpenIdConfigurationProvider.php
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.