PHP code example of joandysson / hyperf-oidc
1. Go to this page and download the library: Download joandysson/hyperf-oidc 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/ */
joandysson / hyperf-oidc example snippets
use function Hyperf\Support\env;
return [
'default' => 'default',
'providers' => [
'default' => [
'issuer' => env('OIDC_ISSUER'),
'client_id' => env('OIDC_CLIENT_ID'),
'client_secret' => env('OIDC_CLIENT_SECRET'),
'redirect_uri' => env('OIDC_REDIRECT_URI'),
'timeout' => (float) env('OIDC_TIMEOUT', 0),
'scope' => env('OIDC_SCOPE', 'openid'),
'discovery' => [
'enabled' => (bool) env('OIDC_DISCOVERY_ENABLED', true),
'url' => env('OIDC_DISCOVERY_URL'),
],
'endpoints' => [
'authorization' => env('OIDC_AUTHORIZATION_ENDPOINT', '/authorize'),
'token' => env('OIDC_TOKEN_ENDPOINT', '/token'),
'introspection' => env('OIDC_INTROSPECTION_ENDPOINT', '/introspect'),
'end_session' => env('OIDC_END_SESSION_ENDPOINT', '/logout'),
],
],
],
];
use Joandysson\HyperfOidc\Oidc;
use function Hyperf\Support\make;
$oidc = make(Oidc::class);
$state = bin2hex(random_bytes(16));
// Store $state in the user's session before redirecting.
$loginUrl = $oidc->getLoginUrl(
state: $state,
scope: 'profile email',
);
$codeVerifier = $oidc->generateCodeVerifier();
// Store $codeVerifier in the user's session before redirecting.
$loginUrl = $oidc->getLoginUrl(
state: $state,
scope: 'profile email',
codeVerifier: $codeVerifier,
);
$code = $request->input('code');
$state = $request->input('state');
// Validate $state against the value stored in your session before using $code.
$response = $oidc->authorizationCode($code);
$tokens = $oidc->tokenPayload($response);
$accessToken = $tokens['access_token'] ?? null;
$refreshToken = $tokens['refresh_token'] ?? null;
$idToken = $tokens['id_token'] ?? null;
$response = $oidc->authorizationCode($code, $codeVerifierFromSession);
$tokens = $oidc->tokenPayload($response);
$response = $oidc->authorizationToken($refreshToken);
$tokens = $oidc->tokenPayload($response);
$response = $oidc->authorizationLogin('[email protected] ', 'password');
$tokens = $oidc->tokenPayload($response);
$response = $oidc->authorizationClientCredentials();
$tokens = $oidc->tokenPayload($response);
$response = $oidc->introspect($accessToken, 'access_token');
$introspection = $oidc->json($response);
if (($introspection['active'] ?? false) !== true) {
// Reject the token.
}
$response = $oidc->introspect($accessToken);
$response = $oidc->logout($refreshToken);
return [
'default' => 'internal',
'providers' => [
'internal' => [
'issuer' => env('OIDC_INTERNAL_ISSUER'),
'client_id' => env('OIDC_INTERNAL_CLIENT_ID'),
'client_secret' => env('OIDC_INTERNAL_CLIENT_SECRET'),
'redirect_uri' => env('OIDC_INTERNAL_REDIRECT_URI'),
'timeout' => 5,
'scope' => 'openid profile email',
'discovery' => [
'enabled' => true,
'url' => null,
],
'endpoints' => [
'authorization' => '/authorize',
'token' => '/token',
'introspection' => '/introspect',
'end_session' => '/logout',
],
],
'keycloak' => [
'issuer' => env('KEYCLOAK_ISSUER'),
'client_id' => env('KEYCLOAK_CLIENT_ID'),
'client_secret' => env('KEYCLOAK_CLIENT_SECRET'),
'redirect_uri' => env('KEYCLOAK_REDIRECT_URI'),
'timeout' => 5,
'scope' => 'openid profile email',
'discovery' => [
'enabled' => true,
'url' => null,
],
'endpoints' => [
'authorization' => '/protocol/openid-connect/auth',
'token' => '/protocol/openid-connect/token',
'introspection' => '/protocol/openid-connect/token/introspect',
'end_session' => '/protocol/openid-connect/logout',
],
],
],
];
use Joandysson\HyperfOidc\OidcFactory;
use function Hyperf\Support\make;
$oidc = make(OidcFactory::class)->forProvider('keycloak');
'client_secret' => null,
$response = $oidc->authorizationClientCredentials();
$status = $response->getStatusCode();
$payload = $oidc->json($response);
$tokens = $oidc->tokenPayload($response);
use GuzzleHttp\Exception\GuzzleException;
use Joandysson\HyperfOidc\Exceptions\InvalidConfigException;
use Joandysson\HyperfOidc\Exceptions\InvalidResponseException;
use Joandysson\HyperfOidc\Exceptions\MissingTokenException;
use Joandysson\HyperfOidc\Exceptions\OidcException;
try {
$response = $oidc->authorizationClientCredentials();
$tokens = $oidc->tokenPayload($response);
} catch (InvalidConfigException $exception) {
// Provider config is missing or invalid.
} catch (InvalidResponseException $exception) {
// Provider returned invalid JSON or an unexpected payload shape.
} catch (MissingTokenException $exception) {
// Token response did not bash
php bin/hyperf.php vendor:publish joandysson/hyperf-oidc
text
config/autoload/oidc.php
bash
composer analyse