1. Go to this page and download the library: Download cilogon/oauth2-cilogon 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/ */
cilogon / oauth2-cilogon example snippets
$provider = new League\OAuth2\Client\Provider\CILogon([
'clientId' => '{cilogon-client-id}',
'clientSecret' => '{cilogon-client-secret}',
'redirectUri' => 'https://example.com/callback-url',
]);
if (!empty($_GET['error'])) {
// Got an error, probably user denied access
exit('Got error: ' . $_GET['error'] .
'Description: ' . $GET['error_description']);
} elseif (empty($_GET['code'])) {
// If we don't have an authorization code then get one with all
// possible CILogon-specific scopes.
$authUrl = $provider->getAuthorizationUrl([
'scope' => ['openid','email','profile','org.cilogon.userinfo']
]);
$_SESSION['oauth2state'] = $provider->getState();
header('Location: '.$authUrl);
exit;
} elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) {
// Check given state against previously stored one to mitigate CSRF attack
unset($_SESSION['oauth2state']);
exit('Invalid state');
} else {
try {
// Try to get an access token using the authorization code grant
$token = $provider->getAccessToken('authorization_code', [
'code' => $_GET['code']
]);
// Print out the access token, which can be used in
// authenticated requests against the service provider's API.
echo '<xmp>' . "\n";
echo 'Token : ' . $token->getToken() . "\n";
$expires = $token->getExpires();
if (!is_null($expires)) {
echo 'Expires : ' . $token->getExpires();
echo ($token->hasExpired() ? ' (expired)' : ' (active)') . "\n";
}
echo '</xmp>' . "\n";
// Using the access token, get the user's details
$user = $provider->getResourceOwner($token);
echo '<xmp>' . "\n";
echo 'User ID : ' . $user->getId() . "\n";
echo 'First name : ' . $user->getGivenName() . "\n"; // or getFirstName()
echo 'Last name : ' . $user->getFamilyName() . "\n"; // or getLastName()
echo 'Full name : ' . $user->getName() . "\n";
echo 'Email : ' . $user->getEmail() . "\n";
echo 'eduPersonPrincipalName : ' . $user->getEPPN() . "\n";
echo 'eduPersonTargetedId : ' . $user->getEPTID() . "\n";
echo 'IdP entityId : ' . $user->getIdP() . "\n";
echo 'IdP Display Name : ' . $user->getIdPName() . "\n";
echo 'Org Unit : ' . $user->getOU() . "\n";
echo 'Affiliation : ' . $user->getAffiliation() . "\n";
echo '</xmp>';
} catch (Exception $e) {
// Failed to get access token or user details
exit('Something went wrong: ' . $e->getMessage());
}
}