Some checks failed
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 1m37s
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 1m41s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 1m40s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 1m42s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 2m10s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 2m6s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 1m55s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 2m2s
CI / Build-Artefakte aktuell (pull_request) Successful in 52s
- OpenID Connect mit PKCE, State und Nonce; ID-Token-Prüfung gegen die Microsoft-Schlüssel (Signatur, Mandant, App, Gültigkeit, Nonce) inkl. Neuladen bei Schlüsselwechsel - Konto bei erster Anmeldung anlegen oder per E-Mail verknüpfen, optionale Domain-Beschränkung, deaktivierte Konten bleiben gesperrt - Gruppen (inkl. Overage über Graph, transitiv) und App-Rollen → Rollen, additiv; nur Zuweisungen aus Entra werden abgeglichen, manuelle bleiben - Profilfelder, Vorgesetzte*r und Profilfoto aus Graph, je Feld wählbar und im Portal schreibgeschützt; Fallback auf Token-Angaben, wenn Graph ausfällt - Hintergrund-Abgleich mit Delta-Abfragen (Zeitbudget, Fortsetzung über nextLink/deltaLink), Offboarding deaktivierter/gelöschter Konten, optionale Vorab-Anlage, Fotos/Vorgesetzte rollierend, Gruppen und App-Rollen stündlich - Admin „Anmeldung“: Einrichtung mit Umleitungs-URI und Berechtigungen, verschlüsseltes Geheimnis (nie im Browser), Gruppensuche, Status, „Mapping testen“ - Fix: @odata.nextLink/@odata.deltaLink wurden als Pfad gelesen – Folgeseiten fehlten - Profilseite mit Vorgesetzter/m und Mitarbeitenden; Version 0.4.0 Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
74 lines
2.6 KiB
PHP
74 lines
2.6 KiB
PHP
<?php
|
||
|
||
namespace App\Identity\Entra;
|
||
|
||
use App\Identity\IdentityException;
|
||
use Firebase\JWT\JWK;
|
||
use Firebase\JWT\JWT;
|
||
use Throwable;
|
||
use UnexpectedValueException;
|
||
|
||
/**
|
||
* Prüft das ID-Token von Entra ID: Signatur (Schlüssel von Microsoft), Aussteller
|
||
* (genau dieser Mandant), Empfänger (diese App), Gültigkeit und Nonce.
|
||
*/
|
||
class IdTokenValidator
|
||
{
|
||
public function __construct(private readonly EntraClient $client) {}
|
||
|
||
/**
|
||
* @return array<string, mixed> Claims
|
||
*/
|
||
public function validate(string $idToken, string $expectedNonce): array
|
||
{
|
||
JWT::$leeway = 120;
|
||
|
||
try {
|
||
$claims = $this->decode($idToken, $this->client->jwks());
|
||
} catch (UnexpectedValueException $e) {
|
||
if (! str_contains($e->getMessage(), '"kid"')) {
|
||
throw new IdentityException('Die Anmeldung von Microsoft konnte nicht bestätigt werden.', previous: $e);
|
||
}
|
||
|
||
// Unbekannter Schlüssel: Microsoft hat die Schlüssel gewechselt – einmal neu laden.
|
||
try {
|
||
$claims = $this->decode($idToken, $this->client->jwks(refresh: true));
|
||
} catch (UnexpectedValueException $e) {
|
||
throw new IdentityException('Die Anmeldung von Microsoft konnte nicht bestätigt werden.', previous: $e);
|
||
}
|
||
}
|
||
|
||
if (($claims['iss'] ?? null) !== $this->client->issuer()) {
|
||
throw new IdentityException('Die Anmeldung stammt nicht aus dem erwarteten Microsoft-Mandanten.');
|
||
}
|
||
|
||
if (($claims['aud'] ?? null) !== $this->client->clientId()) {
|
||
throw new IdentityException('Die Anmeldung war für eine andere Anwendung bestimmt.');
|
||
}
|
||
|
||
if (! hash_equals($expectedNonce, (string) ($claims['nonce'] ?? ''))) {
|
||
throw new IdentityException('Die Anmeldung konnte nicht zugeordnet werden. Bitte erneut versuchen.');
|
||
}
|
||
|
||
if (! is_string($claims['oid'] ?? null) || $claims['oid'] === '') {
|
||
throw new IdentityException('Microsoft hat keine Benutzerkennung übermittelt.');
|
||
}
|
||
|
||
return $claims;
|
||
}
|
||
|
||
/**
|
||
* @param array{keys: list<array<string, mixed>>} $jwks
|
||
* @return array<string, mixed>
|
||
*/
|
||
private function decode(string $token, array $jwks): array
|
||
{
|
||
try {
|
||
return (array) json_decode(json_encode(JWT::decode($token, JWK::parseKeySet($jwks, 'RS256'))), true);
|
||
} catch (UnexpectedValueException $e) {
|
||
throw $e;
|
||
} catch (Throwable $e) {
|
||
throw new IdentityException('Die Anmeldung von Microsoft konnte nicht bestätigt werden.', previous: $e);
|
||
}
|
||
}
|
||
}
|