Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 7m50s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 7m34s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 5m45s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 5m30s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 7m7s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 7m9s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 6m8s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 6m5s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 48s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m5s
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
191 lines
8.3 KiB
PHP
191 lines
8.3 KiB
PHP
<?php
|
|
|
|
namespace Inhaus\Plugins\Oidc;
|
|
|
|
use App\Http\Controllers\Auth\IdentityLoginController;
|
|
use App\Identity\AttributeProfile;
|
|
use App\Identity\ExternalUser;
|
|
use App\Identity\IdentityDriver;
|
|
use App\Identity\IdentityException;
|
|
use App\Models\IdentityProvider;
|
|
use Filament\Forms\Components\Select;
|
|
use Filament\Forms\Components\TextInput;
|
|
use Filament\Forms\Components\Toggle;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Str;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Throwable;
|
|
|
|
/**
|
|
* Anmeldung über einen beliebigen OpenID-Connect-Anbieter.
|
|
*/
|
|
class OidcDriver extends IdentityDriver
|
|
{
|
|
public const TYPE = 'oidc';
|
|
|
|
private const SESSION = 'identity_login';
|
|
|
|
public function type(): string
|
|
{
|
|
return self::TYPE;
|
|
}
|
|
|
|
public function label(): string
|
|
{
|
|
return 'OpenID Connect';
|
|
}
|
|
|
|
public function description(): string
|
|
{
|
|
return 'Keycloak, Authentik, Okta, Google Workspace, Univention, Nextcloud und alle anderen OIDC-Anbieter.';
|
|
}
|
|
|
|
public function connectionHelp(?IdentityProvider $provider): string
|
|
{
|
|
$callback = $provider ? IdentityLoginController::callbackUrlFor($provider) : url('/auth/<kürzel>/callback');
|
|
|
|
return 'Beim Anbieter einen vertraulichen Client (Autorisierungscode-Verfahren) anlegen. Weiterleitungs-URI: <code>'.e($callback).'</code>';
|
|
}
|
|
|
|
public function connectionSchema(): array
|
|
{
|
|
return [
|
|
TextInput::make('config.issuer')->label('Aussteller (Issuer-URL)')->required()->url()->maxLength(255)
|
|
->rule(fn () => fn (string $attribute, mixed $value, \Closure $fail) => OidcClient::secureUrl((string) $value) ? null : $fail('Bitte eine https://-Adresse angeben.'))
|
|
->placeholder('https://login.firma.de/realms/mitarbeitende')
|
|
->helperText('Die Adresse, unter der /.well-known/openid-configuration liegt.')
|
|
->columnSpanFull(),
|
|
TextInput::make('config.client_id')->label('Client-ID')->required()->maxLength(255),
|
|
TextInput::make('config.client_secret')->label('Client-Geheimnis')->password()->revealable()->maxLength(1000)
|
|
->required(fn (string $operation) => $operation === 'create')
|
|
->helperText(fn (string $operation) => $operation === 'edit' ? 'Leer lassen, um das gespeicherte Geheimnis zu behalten.' : null),
|
|
TextInput::make('config.scopes')->label('Scopes')->default('openid profile email')->maxLength(255)
|
|
->helperText('Für Gruppen je nach Anbieter z. B. „groups“ ergänzen.'),
|
|
TextInput::make('config.groups_claim')->label('Claim mit den Gruppen')->default('groups')->maxLength(191)
|
|
->helperText('z. B. groups, roles oder realm_access.roles (Keycloak).'),
|
|
Select::make('config.token_auth')->label('Client-Authentifizierung')->native(false)->default('client_secret_basic')->selectablePlaceholder(false)
|
|
->options(['client_secret_basic' => 'HTTP Basic (client_secret_basic)', 'client_secret_post' => 'Im Formular (client_secret_post)']),
|
|
Select::make('config.prompt')->label('Anmeldeaufforderung')->native(false)->placeholder('Anbieter entscheidet')
|
|
->options(['login' => 'Immer Passwort abfragen', 'select_account' => 'Konto auswählen lassen']),
|
|
Toggle::make('config.use_userinfo')->label('Zusätzliche Angaben vom UserInfo-Endpunkt laden')->default(true)->columnSpanFull(),
|
|
];
|
|
}
|
|
|
|
public function secretFields(): array
|
|
{
|
|
return ['client_secret'];
|
|
}
|
|
|
|
public function destinationFields(): array
|
|
{
|
|
return ['issuer'];
|
|
}
|
|
|
|
public function defaultAttributeMap(): array
|
|
{
|
|
return ['first_name' => 'given_name', 'last_name' => 'family_name', 'email' => 'email'];
|
|
}
|
|
|
|
public function attributeSuggestions(?IdentityProvider $provider): array
|
|
{
|
|
return [
|
|
'given_name' => 'Vorname', 'family_name' => 'Nachname', 'name' => 'Anzeigename', 'email' => 'E-Mail',
|
|
'preferred_username' => 'Benutzername', 'phone_number' => 'Telefon', 'locale' => 'Sprache',
|
|
'address.locality' => 'Ort (Adresse)', 'department' => 'Abteilung (eigener Claim)', 'employee_number' => 'Personalnummer (eigener Claim)',
|
|
];
|
|
}
|
|
|
|
public function attributeLabel(): string
|
|
{
|
|
return 'Claim';
|
|
}
|
|
|
|
public function groupValueLabel(): string
|
|
{
|
|
return 'Gruppe (Wert im Claim)';
|
|
}
|
|
|
|
public function start(IdentityProvider $provider, Request $request): Response
|
|
{
|
|
$verifier = Str::random(64);
|
|
$security = [
|
|
'state' => Str::random(40),
|
|
'nonce' => Str::random(40),
|
|
'code_challenge' => rtrim(strtr(base64_encode(hash('sha256', $verifier, true)), '+/', '-_'), '='),
|
|
];
|
|
|
|
try {
|
|
$url = (new OidcClient($provider))->authorizationUrl(IdentityLoginController::callbackUrlFor($provider), $security);
|
|
} catch (Throwable $e) {
|
|
report($e);
|
|
|
|
return redirect('/login')->with('identity_error', 'Der Anmeldedienst „'.$provider->name.'“ ist gerade nicht erreichbar. Bitte später erneut versuchen.');
|
|
}
|
|
|
|
$request->session()->put(self::SESSION, ['state' => $security['state'], 'nonce' => $security['nonce'], 'verifier' => $verifier, 'provider' => (string) $provider->id]);
|
|
|
|
return redirect()->away($url);
|
|
}
|
|
|
|
protected function authenticate(IdentityProvider $provider, Request $request): ExternalUser
|
|
{
|
|
$session = $request->session()->pull(self::SESSION);
|
|
|
|
if ($request->filled('error')) {
|
|
logger()->notice('OIDC-Anmeldung abgebrochen: '.$request->string('error').' '.$request->string('error_description'));
|
|
|
|
throw new IdentityException($request->string('error')->toString() === 'access_denied'
|
|
? 'Die Anmeldung wurde abgebrochen.'
|
|
: 'Der Anmeldedienst hat die Anmeldung abgelehnt. Bitte wenden Sie sich an die Administration.');
|
|
}
|
|
|
|
if (! is_array($session) || ! hash_equals($session['state'] ?? '', (string) $request->query('state')) || ($session['provider'] ?? null) !== (string) $provider->id) {
|
|
throw new IdentityException('Die Anmeldung ist abgelaufen. Bitte erneut versuchen.');
|
|
}
|
|
|
|
$client = new OidcClient($provider);
|
|
$tokens = $client->exchangeCode((string) $request->query('code'), IdentityLoginController::callbackUrlFor($provider), $session['verifier']);
|
|
$claims = $client->validateIdToken($tokens['id_token'], $session['nonce']);
|
|
$subject = (string) ($claims['sub'] ?? '');
|
|
|
|
if ($subject === '') {
|
|
throw new IdentityException('Der Anmeldedienst hat keine Benutzerkennung übermittelt.');
|
|
}
|
|
|
|
if ($tokens['access_token'] && $provider->setting('use_userinfo', true)) {
|
|
try {
|
|
$info = $client->userinfo($tokens['access_token']);
|
|
|
|
// UserInfo darf nur Angaben derselben Person ergänzen (OIDC Core 5.3.2).
|
|
if (($info['sub'] ?? null) === $subject) {
|
|
$claims = array_merge($info, $claims);
|
|
}
|
|
} catch (Throwable $e) {
|
|
logger()->warning('OIDC-UserInfo nicht abrufbar: '.$e->getMessage());
|
|
}
|
|
}
|
|
|
|
if (isset($claims['email_verified']) && $claims['email_verified'] === false && ! $provider->setting('trust_unverified_email', false)) {
|
|
// Unbestätigte Adressen nie zum Verknüpfen bestehender Konten nutzen.
|
|
unset($claims['email']);
|
|
}
|
|
|
|
$claims = array_diff_key($claims, array_flip(['nonce', 'at_hash', 'c_hash', 'sid', 'auth_time', 'iat', 'exp', 'nbf']));
|
|
$profile = AttributeProfile::build($provider, $claims, [
|
|
'display_name' => 'name',
|
|
'first_name' => 'given_name',
|
|
'last_name' => 'family_name',
|
|
'email' => 'email',
|
|
]);
|
|
|
|
// Hat der Anbieter die Adresse bestätigt? (für das Verknüpfen bestehender Konten)
|
|
$profile['email_verified'] = in_array($claims['email_verified'] ?? null, [true, 'true'], true);
|
|
|
|
return new ExternalUser(
|
|
subject: $subject,
|
|
profile: $profile,
|
|
groups: AttributeProfile::values($claims, (string) $provider->setting('groups_claim', 'groups')),
|
|
attributes: $claims,
|
|
);
|
|
}
|
|
}
|