diff --git a/index.php b/index.php index ff68360..315dbec 100755 --- a/index.php +++ b/index.php @@ -1,139 +1,21 @@ 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 - DETAILED DEBUG OUTPUT - echo '
'; - echo 'Error Message: ' . htmlspecialchars($e->getMessage()) . '
'; - - echo '';
- $responseBody = $e->getResponseBody();
- if (is_array($responseBody)) {
- echo htmlspecialchars(print_r($responseBody, true));
- } else {
- echo htmlspecialchars($responseBody);
- }
- echo '';
-
- echo ''; - echo 'oauth2state: ' . (isset($_SESSION['oauth2state']) ? $_SESSION['oauth2state'] : 'NOT SET') . "\n"; - echo 'oauth2_code_verifier: ' . (isset($_SESSION['oauth2_code_verifier']) ? 'SET (length: ' . strlen($_SESSION['oauth2_code_verifier']) . ')' : 'NOT SET') . "\n"; - echo 'authenticated: ' . (isset($_SESSION['authenticated']) ? ($_SESSION['authenticated'] ? 'true' : 'false') : 'NOT SET') . "\n"; - echo ''; - - echo '
'; - echo 'GET code: ' . (isset($_GET['code']) ? 'present (length: ' . strlen($_GET['code']) . ')' : 'missing') . "\n"; - echo 'GET state: ' . (isset($_GET['state']) ? $_GET['state'] : 'missing') . "\n"; - echo ''; - - echo '
'; - echo 'AZURE_CLIENT_ID: ' . substr(AZURE_CLIENT_ID, 0, 8) . '...' . "\n"; - echo 'AZURE_AUTHORITY: ' . AZURE_AUTHORITY . "\n"; - echo 'AZURE_REDIRECT_URI: ' . AZURE_REDIRECT_URI . "\n"; - echo 'DEV_MODE: ' . (DEV_MODE ? 'true' : 'false') . "\n"; - echo ''; - - echo ''; - echo ''; - die(); - } catch (\Exception $e) { - // Handle other errors - echo ''; - echo '
Error: ' . htmlspecialchars($e->getMessage()) . '
'; - echo ''; - echo htmlspecialchars($e->getTraceAsString()); - echo ''; - echo ''; - echo ''; - die(); - } +// If not authenticated and not a potential MSAL callback, redirect to login +if (!$isPotentialMSALCallback && !isAuthenticated()) { + header('Location: login.php'); + exit; } -// Normal flow - require authentication -requireAuth(); - -// Get current user info +// Get current user info (may be null if MSAL callback is being processed) $user = getCurrentUser(); +$isAuthenticated = isAuthenticated(); ?> @@ -146,22 +28,23 @@ $user = getCurrentUser(); -