Skip to content

Instantly share code, notes, and snippets.

@mgolebiowski
Created January 25, 2019 11:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mgolebiowski/2c4dacc758dff70026a2e33576427e94 to your computer and use it in GitHub Desktop.
Save mgolebiowski/2c4dacc758dff70026a2e33576427e94 to your computer and use it in GitHub Desktop.
{
"name": "",
"type": "project",
"require": {
"fproject/php-jwt": "^4.0",
"league/oauth2-client": "^2.4"
}
}
<?php
use Firebase\JWT\JWT;
use Firebase\JWT\JWK;
$config = getConfig();
$client_id = 'client_id';
$client_secret = 'client_secret';
$redirect_uri = 'redirect_uri';
$provider = new \League\OAuth2\Client\Provider\GenericProvider([
'clientId' => $client_id,
'clientSecret' => $client_secret,
'redirectUri' => $redirect_uri,
'urlAuthorize' => '_MY_URL_/v1/authorize',
'urlAccessToken' => '_MY_URL_/v1/token',
'urlResourceOwnerDetails' => '_MY_URL_/v1/resource',
'scopeSeparator' => ' ',
'scopes' => array('openid', 'profile', 'groups')
]);
if (!isset($_GET['code'])) {
$authorizationUrl = $provider->getAuthorizationUrl();
$_SESSION['oauth2state'] = $provider->getState();
header('Location: ' . $authorizationUrl);
exit;
} elseif (empty($_GET['state']) || (isset($_SESSION['oauth2state']) && $_GET['state'] !== $_SESSION['oauth2state'])) {
if (isset($_SESSION['oauth2state'])) {
unset($_SESSION['oauth2state']);
}
exit('Invalid state');
} else {
try {
$accessToken = $provider->getAccessToken('authorization_code', [
'code' => $_GET['code']
]);
$id_token = $accessToken->getValues()['id_token'];
$keysPayload = http('_MY_URL_/v1/keys?client_id='.$client_id);
$jwk = JWK::parseKeySet($keysPayload);
$jwt = JWT::decode($id_token, $jwk, array('RS256'));
print_r($jwt);
die();
} catch (\League\OAuth2\Client\Provider\Exception\IdentityProviderException $e) {
exit($e->getMessage());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment