1. Go to this page and download the library: Download gorontalokota/sso-client 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/ */
gorontalokota / sso-client example snippets
use Gorontalokota\SSO\Client\Provider\Broker;
$provider = new Broker([
'realm' => '{Realms}',
'clientId' => '{clientID}',
'clientSecret' => '{clientSecret}',
'redirectUri' => '{http://example/oauth/authorized}',
]);
$authUrl = $provider->getAuthorizationUrl();
//Simpan status Auth ke Session untuk mencegah csrf
$_SESSION['oauth2state'] = $provider->getState();
//Redirect Url Auth
header('Location: '.$authUrl);
use Gorontalokota\SSO\Client\Provider\Broker;
$provider = new Broker([
'realm' => '{Realms}',
'clientId' => '{clientID}',
'clientSecret' => '{clientSecret}',
'redirectUri' => '{http://example/oauth/authorized}',
]);
//Periksa status yang diberikan terhadap status yang disimpan sebelumnya untuk mengurangi serangan CSRF
if (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) {
unset($_SESSION['oauth2state']);
exit('Invalid state, make sure HTTP sessions are enabled.');
} else {
try {
$token = $provider->getAccessToken('authorization_code', [
'code' => $_GET['code']
]);
$user = $provider->getResourceOwner($token);
$user->getUsername();
$user->getEmail();
$user->getName();
} catch (Exception $e) {
exit('Failed to get access token: '.$e->getMessage());
}
}