errorMessage($_GET['error_description']); exit; } //retrieve session data from database $sessionData = $modDB->QuerySingle('SELECT * FROM tblAuthSessions WHERE txtSessionKey=\'' . $modDB->Escape($_SESSION['sessionkey']) . '\''); if ($sessionData) { // Request token from Azure AD $oauthRequest = $oAuth->generateRequest('grant_type=authorization_code&client_id=' . _OAUTH_CLIENTID . '&redirect_uri=' . urlencode(_URL . '/oauth.php') . '&code=' . $_GET['code'] . '&code_verifier=' . $sessionData['txtCodeVerifier']); $response = $oAuth->postRequest('token', $oauthRequest); // Decode response from Azure AD. Extract JWT data from supplied access_token and id_token and update database. if (!$response) { echo $oAuth->errorMessage('Unknown error acquiring token'); exit; } $reply = json_decode($response); if ($reply->error) { echo $oAuth->errorMessage($reply->error_description); exit; } $idToken = base64_decode(explode('.', $reply->id_token)[1]); $modDB->Update('tblAuthSessions', array('txtToken' => $reply->access_token, 'txtRefreshToken' => $reply->refresh_token, 'txtIDToken' => $idToken, 'txtRedir' => '', 'dtExpires' => date('Y-m-d H:i:s', strtotime('+' . $reply->expires_in . ' seconds'))), array('intAuthID' => $sessionData['intAuthID'])); // Redirect user back to where they came from. header('Location: ' . $sessionData['txtRedir']); } else { header('Location: /'); } ?>