AZURE_CLIENT_ID, 'redirectUri' => AZURE_REDIRECT_URI, 'urlAuthorize' => AZURE_AUTHORITY . '/oauth2/v2.0/authorize', 'urlAccessToken' => AZURE_AUTHORITY . '/oauth2/v2.0/token', 'urlResourceOwnerDetails' => 'https://graph.microsoft.com/v1.0/me', 'scopes' => 'openid profile email User.Read' ]); // Exchange authorization code for access token with PKCE $accessToken = $provider->getAccessToken('authorization_code', [ 'code' => $_GET['code'], 'code_verifier' => $codeVerifier ]); // Get user information from Microsoft Graph API $request = $provider->getAuthenticatedRequest( 'GET', 'https://graph.microsoft.com/v1.0/me', $accessToken->getToken() ); $client = new \GuzzleHttp\Client(); $response = $client->send($request); $userData = json_decode($response->getBody(), true); // Store user information in session $_SESSION['authenticated'] = true; $_SESSION['user_id'] = $userData['id']; $_SESSION['user_name'] = $userData['displayName'] ?? $userData['userPrincipalName']; $_SESSION['user_email'] = $userData['userPrincipalName'] ?? $userData['mail']; $_SESSION['access_token'] = $accessToken->getToken(); $_SESSION['last_activity'] = time(); // Initialize user files array for tracking uploads $_SESSION['user_files'] = []; // Clean up temporary session variables unset($_SESSION['oauth2state']); unset($_SESSION['oauth2_code_verifier']); // Regenerate session ID for security session_regenerate_id(true); // Redirect to main application (clean URL, no query parameters) header('Location: index.php'); exit; } catch (\League\OAuth2\Client\Provider\Exception\IdentityProviderException $e) { // Handle authentication errors die('Authentication failed: ' . htmlspecialchars($e->getMessage())); } catch (\Exception $e) { // Handle other errors die('An error occurred: ' . htmlspecialchars($e->getMessage())); } } // Normal flow - require authentication requireAuth(); // Get current user info $user = getCurrentUser(); ?>