I’m try to do login script, where I want to login with the Microsoft OAuth2.0 and Azure Active Directory (or Mirosoft entra ID). The Authentification works and I get the Autorizecode, but the access token is not sent back to my OAuth2.0 provider from the success.php side. I’ve tried so many things like debugging, new code…, but nothing works. By the way, the authorization code is given on the success.php in the path above. Of course, all the data is changed for security reasons.
index.php
<?php
//TODO: Azure Synchronisation
session_start();
//* Überprüfen, ob der Benutzer authentifiziert ist
if (isset($_SESSION['token']))
{
header('Location: http://localhost/success.php'); //! Der Benutzer ist bereits authentifiziert
exit();
}
else
{
header('Location: https://login.microsoftonline.com/a502008d-5bd7-4ad1-b8e0-668da72219a8/oauth2/v2.0/authorize?client_id=XXXXXXXXX&response_type=code&redirect_uri=http%3A%2F%2Flocalhost%2Fsuccess.php&response_mode=query&scope=openid+email+offline_access+profile+User.Read&state=12345&code_challenge=XXXXXXXXXXXXXX&code_challenge_method=S256'); //! Der Benutzer ist bereits authentifiziert
exit();
}
//* Überprüfen, ob der Benutzer bereits mit einem Office-Dienst verbunden ist
if (isset($_SERVER['AUTH_USER']))
{
sleep(1);
header('Location: http://localhost/success.php'); //! Der Benutzer ist bereits angemeldet
exit();
}
require_once($_SERVER['DOCUMENT_ROOT'].'/Assets/Configs/server.php'); //*Einbinden von server.php
$windowsUser = isset($_SERVER['AUTH_USER']) ? $_SERVER['AUTH_USER'] : null; //*Variable für Authentifizierung
require_once __DIR__.'/vendor/autoload.php'; //* Datei autoload.php einbinden
use LeagueOAuth2ClientProviderGenericProvider; //* Composer (PHP-Bibliothek)
if($windowsUser)
{
$clientId = 'XXXXXXXXXXXXXXXXX'; //* Anwendungs-ID (Client)
$clientSecret = 'XXXXXXXXXXXXXXXXX'; //* Geheime Clientschlüssel
$tenantId = 'XXXXXXXXXXXXXXXXX'; //* Verzeichnis-ID (Mandant)
//* Konfigurationsinformationen für OAuth2-Provider
$provider = new GenericProvider([
'clientId' => $clientId,
'clientSecret' => $clientSecret,
'redirectUri' => 'http://localhost/success.php', //* UmleitungsURI
'urlAuthorize' => "https://login.microsoftonline.com/XXXXXXXXXXXXXXXXX/oauth2/v2.0/authorize?client_id=XXXXXXXXXXXXXXXXX&response_type=code&redirect_uri=http%3A%2F%2Flocalhost%2Fsuccess.php&response_mode=query&scope=openid+email+offline_access+profile+User.Read&state=12345&code_challenge=XXXXXXXXXXXXXXXXX&code_challenge_method=S256",
'urlAccessToken' => "https://login.microsoftonline.com/XXXXXXXXXXXXXXXXX/oauth2/v2.0/token",
'urlResourceOwnerDetails' => '',
'scopes' => ['openid', 'email', 'offline_access', 'profile', 'User.Read'], //* Berechtigungen
'codeChallengeMethod' => 'S256',
]);
if (isset($_GET['code'])) {
$authorization_code = $_GET['code']; // Autorisierungscode aus GET-Parametern auslesen
try {
// Autorisierungscode verwenden, um Zugriffstoken zu erhalten
$accessToken = $provider->getAccessToken('authorization_code', [
'code' => $authorization_code,
]);
// JSON-Antwort in ein assoziatives Array umwandeln
$response = json_decode($accessToken->getBody(), true);
// Zugriffstoken speichern
$_SESSION['token'] = $accessToken->getToken();
$_SESSION['id_token'] = $accessToken->getValues()['id_token'];
$accessTokenValue = $accessToken->getToken();
$_SESSION['accesstoken'] = $accessTokenValue;
// Weiterleitung zur Erfolgsseite
header('Location: http://localhost/success.php');
exit();
} catch (LeagueOAuth2ClientProviderExceptionIdentityProviderException $e) {
echo "Fehler beim Abrufen des Zugriffstokens: " . $e->getMessage();
}
} else {
// Benutzer muss sich einloggen und wird dann zu Azure AD weitergeleitet
$authUrl = $provider->getAuthorizationUrl(); // Autorisierungs-URL abrufen
header('Location: ' . $authUrl); // Weiterleitung zur Autorisierungs-URL
exit();
}
}
?>
Success.php
<!DOCTYPE html>
<html lang="DE">
<?php
session_start();
$username = $_SESSION['user'];
$Erfolg ='Sie haben sich erfolgreich angemeldet';
?>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="Css.css">
<title>
Erfolg
</title>
</head>
<body>
<header>
<div id="Überschrift">
<h1>ITH Bolting Technology - Login</h1>
<?php
echo "Benutzer: ";
echo($username);
?>
</div>
</header>
<main>
<h3>
<?php
echo($Erfolg);
// Überprüfen, ob die Anzahl der Benutzer in der Session vorhanden ist
if(isset($_GET['code'])) {
$authorization_code = $_GET['code'];
echo "Autorisierungscode: " . $authorization_code;
} else {
echo "Autorisierungscode nicht gefunden.";
}
if (isset($_SESSION['user_count'])) {
echo "<p>Anzahl der Benutzer aus Azure AD: " . $_SESSION['user_count'] . "</p>";
} else {
echo "<p>Es wurden keine Benutzer abgerufen.</p>";
}
if(isset($_SESSION['accessToken']))
{
echo "Access Token: " . $_SESSION['accesstoken'];
}
else
{
echo "Es wurde kein Accesstoken gefunden";
}
?>
</h3>
</main>
</body>
</html>