Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 1m43s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 1m52s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 1m49s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 1m49s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 2m12s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 2m22s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 1m57s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 1m59s
CI / Build-Artefakte aktuell (pull_request) Successful in 57s
- Profilfelder frei definierbar (Typ, Sichtbarkeit, selbst pflegbar), Werte als JSON am Benutzer - Attribut-Zuordnung je Anbieter statt fester Feldliste: jedes Graph-Attribut inkl. verschachtelter Pfade, onPremisesExtensionAttributes und Verzeichniserweiterungen - Erweiterungsattribute per Knopf aus Entra laden - $select dynamisch aus der Zuordnung; Delta-Abgleich startet bei geänderter Zuordnung neu - Rückfall auf Grundattribute, wenn einzelne Attribute nicht lesbar sind - Ältere Konfiguration (synced_fields) wird weiter gelesen Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
139 lines
5.8 KiB
PHP
139 lines
5.8 KiB
PHP
<?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' => EntraAttributes::select($provider)]);
|
||
|
||
// Einzelne Attribute brauchen weitere Berechtigungen (z. B. employeeHireDate) –
|
||
// dann wenigstens die Grundangaben übernehmen.
|
||
if ($me->clientError() && $me->status() !== 401) {
|
||
Log::warning('Entra-Anmeldung: Zugeordnete Attribute nicht abrufbar ('.$me->status().'), nutze Grundangaben.');
|
||
$me = $client->graph($token)->get('/me', ['$select' => implode(',', EntraAttributes::BASE_SELECT)]);
|
||
}
|
||
|
||
$client->assertGraph($me);
|
||
|
||
$withManager = $provider->syncsManager();
|
||
$managerSubject = null;
|
||
|
||
if ($withManager) {
|
||
$manager = $client->graph($token)->get('/me/manager', ['$select' => 'id']);
|
||
$managerSubject = $manager->successful() ? $manager->json('id') : null;
|
||
}
|
||
|
||
$profile = EntraProfile::fromGraph($provider, (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];
|
||
}
|
||
}
|