Anmeldung mit Microsoft Entra ID
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
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>
This commit is contained in:
parent
9ddc35a62c
commit
51c94cd0cf
111 changed files with 5721 additions and 244 deletions
131
app/Identity/Entra/EntraLogin.php
Normal file
131
app/Identity/Entra/EntraLogin.php
Normal file
|
|
@ -0,0 +1,131 @@
|
|||
<?php
|
||||
|
||||
namespace App\Identity\Entra;
|
||||
|
||||
use App\Identity\IdentityException;
|
||||
use App\Identity\IdentitySync;
|
||||
use App\Models\IdentityProvider;
|
||||
use App\Models\User;
|
||||
use App\Models\UserIdentity;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Str;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* Anmeldung über Entra ID (OpenID Connect, Autorisierungscode mit PKCE).
|
||||
*/
|
||||
class EntraLogin
|
||||
{
|
||||
public function __construct(private readonly IdentitySync $sync) {}
|
||||
|
||||
/**
|
||||
* @return array{url: string, session: array<string, string>}
|
||||
*/
|
||||
public function start(IdentityProvider $provider, string $redirectUri): array
|
||||
{
|
||||
$verifier = Str::random(64);
|
||||
$security = [
|
||||
'state' => Str::random(40),
|
||||
'nonce' => Str::random(40),
|
||||
'code_challenge' => rtrim(strtr(base64_encode(hash('sha256', $verifier, true)), '+/', '-_'), '='),
|
||||
];
|
||||
|
||||
return [
|
||||
'url' => (new EntraClient($provider))->authorizationUrl($redirectUri, $security),
|
||||
'session' => ['state' => $security['state'], 'nonce' => $security['nonce'], 'verifier' => $verifier, 'provider' => (string) $provider->id],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, string> $session aus start()
|
||||
*/
|
||||
public function complete(IdentityProvider $provider, string $code, string $redirectUri, array $session): User
|
||||
{
|
||||
$client = new EntraClient($provider);
|
||||
$tokens = $client->exchangeCode($code, $redirectUri, $session['verifier']);
|
||||
$claims = (new IdTokenValidator($client))->validate($tokens['id_token'], $session['nonce']);
|
||||
$subject = (string) $claims['oid'];
|
||||
|
||||
[$profile, $groupIds, $photo] = $this->fetchFromGraph($client, $provider, $tokens['access_token'], $claims);
|
||||
|
||||
$user = $this->sync->resolveUser($provider, $subject, $profile, (bool) $provider->setting('auto_provision', true));
|
||||
|
||||
if (! $user) {
|
||||
throw new IdentityException('Für Ihr Microsoft-Konto gibt es kein Konto in diesem Portal. Bitte wenden Sie sich an die Administration.');
|
||||
}
|
||||
|
||||
if (! $user->is_active) {
|
||||
throw new IdentityException('Ihr Konto ist deaktiviert. Bitte wenden Sie sich an die Administration.');
|
||||
}
|
||||
|
||||
$identity = UserIdentity::query()->where('identity_provider_id', $provider->id)->where('subject', $subject)->firstOrFail();
|
||||
$identity->update(['last_login_at' => now(), 'email' => $profile['email'] ?? $identity->email]);
|
||||
|
||||
$this->sync->syncProfile($provider, $user, $profile);
|
||||
$this->sync->syncRoles($provider, $user, $groupIds, array_values(array_filter((array) ($claims['roles'] ?? []), 'is_string')));
|
||||
|
||||
if ($provider->setting('sync_photo', true) && $photo !== false) {
|
||||
$this->sync->syncPhoto($identity->fresh('user'), $photo);
|
||||
}
|
||||
|
||||
return $user->fresh();
|
||||
}
|
||||
|
||||
/**
|
||||
* Profil, Gruppen und Foto über Graph (mit dem Token der angemeldeten Person).
|
||||
* Fällt Graph aus, reichen die Angaben aus dem ID-Token für die Anmeldung.
|
||||
*
|
||||
* @param array<string, mixed> $claims
|
||||
* @return array{0: array<string, mixed>, 1: list<string>, 2: string|null|false} false = Foto unbekannt
|
||||
*/
|
||||
private function fetchFromGraph(EntraClient $client, IdentityProvider $provider, string $token, array $claims): array
|
||||
{
|
||||
$groups = array_values(array_filter((array) ($claims['groups'] ?? []), 'is_string'));
|
||||
$overage = isset($claims['_claim_names']['groups']) || ($claims['hasgroups'] ?? false);
|
||||
|
||||
try {
|
||||
$me = $client->graph($token)->get('/me', ['$select' => EntraProfile::SELECT]);
|
||||
$client->assertGraph($me);
|
||||
|
||||
$withManager = in_array('manager', $provider->syncedFields(), true);
|
||||
$managerSubject = null;
|
||||
|
||||
if ($withManager) {
|
||||
$manager = $client->graph($token)->get('/me/manager', ['$select' => 'id']);
|
||||
$managerSubject = $manager->successful() ? $manager->json('id') : null;
|
||||
}
|
||||
|
||||
$profile = EntraProfile::fromGraph((array) $me->json(), $managerSubject, $withManager);
|
||||
} catch (Throwable $e) {
|
||||
Log::warning('Entra-Anmeldung: Profil aus Graph nicht abrufbar, nutze ID-Token. '.$e->getMessage());
|
||||
$profile = EntraProfile::fromClaims($claims);
|
||||
}
|
||||
|
||||
// Group-Overage: Bei vielen Gruppen stehen sie nicht im Token – dann über Graph
|
||||
// (transitiv, also inklusive verschachtelter Gruppen).
|
||||
if ($overage || $groups === []) {
|
||||
try {
|
||||
$groups = array_column($client->graphAll($token, '/me/transitiveMemberOf/microsoft.graph.group?$select=id&$top=999'), 'id');
|
||||
} catch (Throwable $e) {
|
||||
if ($overage) {
|
||||
Log::error('Entra-Anmeldung: Gruppen konnten nicht ermittelt werden. '.$e->getMessage());
|
||||
|
||||
throw new IdentityException('Ihre Gruppenmitgliedschaften konnten nicht ermittelt werden. Bitte später erneut versuchen.');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$photo = false;
|
||||
|
||||
if ($provider->setting('sync_photo', true)) {
|
||||
try {
|
||||
$response = $client->graph($token)->accept('image/*')->get('/me/photo/$value');
|
||||
$photo = $response->successful() ? $response->body() : ($response->status() === 404 ? null : false);
|
||||
} catch (Throwable) {
|
||||
$photo = false;
|
||||
}
|
||||
}
|
||||
|
||||
return [$profile, $groups, $photo];
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue