OAUTH_APP_ID, 'clientSecret' => OAUTH_APP_SECRET, 'redirectUri' => $host . OAUTH_REDIRECT_URI, 'urlAuthorize' => OAUTH_AUTHORITY . OAUTH_AUTHORIZE_ENDPOINT, 'urlAccessToken' => OAUTH_AUTHORITY . OAUTH_TOKEN_ENDPOINT, 'urlResourceOwnerDetails' => '', 'scopes' => OAUTH_SCOPES, ]); $authUrl = $oAuthClient->getAuthorizationUrl(); $_SESSION['oauthState'] = $oAuthClient->getState(); header('Location: ' . $authUrl); } if ('/callback' === $requestPath) { $expectedState = $_SESSION['oauthState']; unset($_SESSION['oauthState']); if (!isset($_GET['state']) || !isset($_GET['code'])) { header('Location: ' . $host . '/?type=error&message=No%20OAuth%20session'); } $providedState = $_GET['state']; if (!isset($expectedState)) { // If there is no expected state in the session, // do nothing and redirect to the home page. header('Location: ' . $host . '/?type=error&message=Expected%20state%20not%20available'); } if (!isset($providedState) || $expectedState != $providedState) { header('Location: ' . $host . '/?type=error&message=State%20does%20not%20match'); } // Authorization code should be in the "code" query param $authCode = $_GET['code']; if (isset($authCode)) { // Initialize the OAuth client $oAuthClient = new GenericProvider([ 'clientId' => OAUTH_APP_ID, 'clientSecret' => OAUTH_APP_SECRET, 'redirectUri' => $host . OAUTH_REDIRECT_URI, 'urlAuthorize' => OAUTH_AUTHORITY . OAUTH_AUTHORIZE_ENDPOINT, 'urlAccessToken' => OAUTH_AUTHORITY . OAUTH_TOKEN_ENDPOINT, 'urlResourceOwnerDetails' => '', 'scopes' => OAUTH_SCOPES, ]); $accessToken = null; try { // Make the token request $accessToken = $oAuthClient->getAccessToken('authorization_code', [ 'code' => $authCode ]); } catch (\League\OAuth2\Client\Provider\Exception\IdentityProviderException $e) { header('Location: ' . $host . '/?type=error&message=' . urlencode($e->getMessage())); } } $user = []; if (null !== $accessToken) { $graph = new Graph(); $graph->setAccessToken($accessToken->getToken()); try { $azureUser = $graph->createRequest('GET', '/me?$select=displayName,mail,userPrincipalName') ->setReturnType(Model\User::class) ->execute(); } catch (Exception $exception) { header('Location: ' . $host . '/?type=error&message=' . urlencode('Unable to get user details: ' . $exception->getMessage())); } $user = [ 'name' => $azureUser->getDisplayName(), 'email' => $azureUser->getMail(), ]; $_SESSION['user'] = serialize($user); } header('Location: ' . $host); } ?> <?php echo htmlentities($title, ENT_QUOTES, 'UTF-8') ?>

Welcome to PHP on Azure App Service .

Home Login Logout

User details