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>} $jwks * @return array */ 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); } } }