-
Notifications
You must be signed in to change notification settings - Fork 117
Open
Description
I've changed the sample code in this
<?php
session_start();
require_once __DIR__ . '/vendor/autoload.php';
define( 'SETTINGS', parse_ini_file( __DIR__ . '/settings.ini', true ) );
$provider = new TheNetworg\OAuth2\Client\Provider\Azure( [
'clientId' => SETTINGS[ 'aad' ][ 'client-id' ],
'clientSecret' => SETTINGS[ 'aad' ][ 'client-secret' ],
'redirectUri' => 'https://example.com/hello.php',
'scopes' => ['openid'],
] );
// Set to use v2 API, skip the line or set the value to Azure::ENDPOINT_VERSION_1_0 if willing to use v1 API
$provider->defaultEndPointVersion = TheNetworg\OAuth2\Client\Provider\Azure::ENDPOINT_VERSION_2_0;
$baseGraphUri = $provider->getRootMicrosoftGraphUri(null);
$provider->scope = 'openid profile email offline_access ' . $baseGraphUri . '/User.Read';
if (isset($_GET['code']) && isset($_SESSION['OAuth2.state']) && isset($_GET['state'])) {
if ($_GET['state'] == $_SESSION['OAuth2.state']) {
unset($_SESSION['OAuth2.state']);
// Try to get an access token (using the authorization code grant)
/** @var AccessToken $token */
$token = $provider->getAccessToken('authorization_code', [
'scope' => $provider->scope,
'code' => $_GET['code'],
]);
// Verify token
// Save it to local server session data
$_SESSION['token'] = $token->getToken();
} else {
echo 'Invalid state';
return null;
}
} else {
// Check local server's session data for a token
// and verify if still valid
/** @var ?AccessToken $token */
$token = $_SESSION['token']??null;
if (isset($token)) {
$me = $provider->get($provider->getRootMicrosoftGraphUri($token) . '/v1.0/me', $token);
$userEmail = $me['mail'];
if ($token->hasExpired()) {
if (!is_null($token->getRefreshToken())) {
$token = $provider->getAccessToken('refresh_token', [
'scope' => $provider->scope,
'refresh_token' => $token->getRefreshToken()
]);
} else {
$token = null;
}
}
}
// If the token is not found in
if (!isset($token)) {
$authorizationUrl = $provider->getAuthorizationUrl(['scope' => $provider->scope]);
$_SESSION['OAuth2.state'] = $provider->getState();
header('Location: ' . $authorizationUrl);
exit;
}
$_SESSION['token'] = $token->getToken();
}but this loops in the https://login.microsoftonline.com/ redirect
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels