From b374335b3a7b444ad28dae021596d41849f37e25 Mon Sep 17 00:00:00 2001 From: Lars Date: Sun, 15 Nov 2026 10:15:33 +0100 Subject: [PATCH] =?UTF-8?q?Benenne=20den=20Authenticator=20und=20merke=20d?= =?UTF-8?q?enselben=20Browser=20f=C3=BCr=2030=20Tage.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Cursor --- app/Auth/StaffAuthenticatorName.php | 11 +++ app/Auth/TrustedStaffDevice.php | 73 ++++++++++++++++++ app/Filament/Auth/StaffLogin.php | 14 ++++ app/Filament/Auth/TrustedDeviceChallenge.php | 21 ++++++ app/Filament/Resources/Users/UserResource.php | 14 +++- app/Http/Responses/StaffLoginResponse.php | 24 ++++++ app/Providers/AppServiceProvider.php | 3 + app/Providers/Filament/AdminPanelProvider.php | 8 +- docs/betrieb/MVP-Stand.md | 2 +- tests/Feature/StaffAuthenticationTest.php | 75 +++++++++++++++++++ 10 files changed, 241 insertions(+), 4 deletions(-) create mode 100644 app/Auth/StaffAuthenticatorName.php create mode 100644 app/Auth/TrustedStaffDevice.php create mode 100644 app/Filament/Auth/StaffLogin.php create mode 100644 app/Filament/Auth/TrustedDeviceChallenge.php create mode 100644 app/Http/Responses/StaffLoginResponse.php create mode 100644 tests/Feature/StaffAuthenticationTest.php diff --git a/app/Auth/StaffAuthenticatorName.php b/app/Auth/StaffAuthenticatorName.php new file mode 100644 index 0000000..ada9849 --- /dev/null +++ b/app/Auth/StaffAuthenticatorName.php @@ -0,0 +1,11 @@ +isProduction() ? 'Lara&Felix' : 'Lara&Felix (dev)'; + } +} diff --git a/app/Auth/TrustedStaffDevice.php b/app/Auth/TrustedStaffDevice.php new file mode 100644 index 0000000..e37c60f --- /dev/null +++ b/app/Auth/TrustedStaffDevice.php @@ -0,0 +1,73 @@ +token($user), + self::DAYS * 24 * 60, + '/', + null, + request()->isSecure(), + true, + false, + 'lax', + )); + } + + public function token(User $user): string + { + return Crypt::encryptString(json_encode([ + 'user' => $user->getKey(), + 'until' => now()->addDays(self::DAYS)->getTimestamp(), + 'secret' => $this->fingerprint($user), + ], JSON_THROW_ON_ERROR)); + } + + public function isTrusted(User $user, ?Request $request = null): bool + { + $request ??= request(); + $token = $request->cookies->get(self::COOKIE); + + if (! is_string($token) || $token === '') { + return false; + } + + try { + $payload = json_decode(Crypt::decryptString($token), true, 8, JSON_THROW_ON_ERROR); + } catch (\Throwable) { + return false; + } + + if (! is_array($payload)) { + return false; + } + + $until = $payload['until'] ?? null; + $fingerprint = $payload['secret'] ?? null; + + return (int) ($payload['user'] ?? 0) === (int) $user->getKey() + && is_int($until) + && $until >= now()->getTimestamp() + && is_string($fingerprint) + && hash_equals($this->fingerprint($user), $fingerprint); + } + + private function fingerprint(User $user): string + { + return hash('sha256', (string) $user->getAppAuthenticationSecret()); + } +} diff --git a/app/Filament/Auth/StaffLogin.php b/app/Filament/Auth/StaffLogin.php new file mode 100644 index 0000000..48ce69c --- /dev/null +++ b/app/Filament/Auth/StaffLogin.php @@ -0,0 +1,14 @@ +isTrusted($user)) { + return null; + } + + return parent::getFirstEnabledProvider($user); + } +} diff --git a/app/Filament/Resources/Users/UserResource.php b/app/Filament/Resources/Users/UserResource.php index 731899f..02d72cf 100644 --- a/app/Filament/Resources/Users/UserResource.php +++ b/app/Filament/Resources/Users/UserResource.php @@ -10,6 +10,7 @@ use App\Filament\Resources\Users\Pages\EditUser; use App\Filament\Resources\Users\Pages\ListUsers; use App\Models\User; +use Filament\Facades\Filament; use Filament\Forms\Components\CheckboxList; use Filament\Forms\Components\Select; use Filament\Forms\Components\TextInput; @@ -106,9 +107,20 @@ public static function persist(User $user, array $data, bool $creating): User } $user->update($data); + + $actor = Filament::auth()->user(); + + if ($actor instanceof User && $actor->is($user) && array_key_exists('password', $data)) { + $actor->refresh(); + session()->put([ + 'password_hash_staff' => $actor->getAuthPassword(), + ]); + } } - app(AssignStaffRoles::class)->sync($user, $roles, auth()->user()); + $actor = Filament::auth()->user() ?? auth()->user(); + + app(AssignStaffRoles::class)->sync($user, $roles, $actor); return $user; } diff --git a/app/Http/Responses/StaffLoginResponse.php b/app/Http/Responses/StaffLoginResponse.php new file mode 100644 index 0000000..4b7d1b2 --- /dev/null +++ b/app/Http/Responses/StaffLoginResponse.php @@ -0,0 +1,24 @@ +user(); + + if ($user instanceof User && filled($user->getAppAuthenticationSecret())) { + app(TrustedStaffDevice::class)->remember($user); + } + + return redirect()->intended(Filament::getUrl()); + } +} diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 06b49c3..97599e1 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -6,8 +6,10 @@ use App\Domain\Cms\NavigationItemPolicy; use App\Domain\Cms\Page; use App\Domain\Cms\PagePolicy; +use App\Http\Responses\StaffLoginResponse; use App\Models\User; use App\Policies\UserPolicy; +use Filament\Auth\Http\Responses\Contracts\LoginResponse as LoginResponseContract; use Illuminate\Support\Facades\Gate; use Illuminate\Support\Facades\View; use Illuminate\Support\ServiceProvider; @@ -21,6 +23,7 @@ public function register(): void public function boot(): void { + $this->app->bind(LoginResponseContract::class, StaffLoginResponse::class); Gate::policy(Page::class, PagePolicy::class); Gate::policy(NavigationItem::class, NavigationItemPolicy::class); Gate::policy(User::class, UserPolicy::class); diff --git a/app/Providers/Filament/AdminPanelProvider.php b/app/Providers/Filament/AdminPanelProvider.php index db30764..a018d01 100644 --- a/app/Providers/Filament/AdminPanelProvider.php +++ b/app/Providers/Filament/AdminPanelProvider.php @@ -2,6 +2,8 @@ namespace App\Providers\Filament; +use App\Auth\StaffAuthenticatorName; +use App\Filament\Auth\StaffLogin; use App\Http\Middleware\SanitizeIntendedUrl; use Filament\Auth\MultiFactor\App\AppAuthentication; use Filament\Http\Middleware\Authenticate; @@ -28,14 +30,16 @@ public function panel(Panel $panel): Panel ->default() ->id('verwaltung') ->path('verwaltung') - ->login() + ->login(StaffLogin::class) ->brandName('Verwaltung') ->authGuard('staff') ->colors([ 'primary' => Color::hex('#0F6E56'), ]) ->multiFactorAuthentication([ - AppAuthentication::make()->recoverable(), + AppAuthentication::make() + ->recoverable() + ->brandName(StaffAuthenticatorName::current()), ], isRequired: true) ->discoverResources(in: app_path('Filament/Resources'), for: 'App\Filament\Resources') ->discoverPages(in: app_path('Filament/Pages'), for: 'App\Filament\Pages') diff --git a/docs/betrieb/MVP-Stand.md b/docs/betrieb/MVP-Stand.md index 0434703..b845bc2 100644 --- a/docs/betrieb/MVP-Stand.md +++ b/docs/betrieb/MVP-Stand.md @@ -8,7 +8,7 @@ ## Bedienbar - Inhaltselemente, Fassungen und Kompositionen als Redakteur. Eine Datei wird von der Queue nach `derived/` kopiert. - Lizenzartikel, verschachtelte Pakete, Rechteprofile und Angebote als Katalogverwalter. Die jeweilige Seite zeigt den Stand, die Bestandteile, die aufgelösten Artikel und die daraus entstehenden Berechtigungen. Ein neuer Stand zeigt Inhaber, Angebote und Inhaltszahl; ein Rückzug stellt den vorherigen Stand wieder her und nennt die Betroffenen. - Manuelle Vergabe als Kundenbetreuung, am Benutzer in der Verwaltung. -- Registrierung, Anmeldung, kostenloser Abruf, Testkauf, Ablehnung und Widerruf im Mitgliederbereich. +- Registrierung, Anmeldung, kostenloser Abruf, Testkauf, Ablehnung und Widerruf im Mitgliederbereich. Die Verwaltung fragt den Authenticator-Code auf demselben Browser 30 Tage lang nicht erneut ab. In der Authenticator-App heißt der Eintrag „Lara&Felix“, außerhalb der Produktion „Lara&Felix (dev)“. Ein bestehender Eintrag übernimmt den Namen erst bei erneuter Einrichtung. - Bibliothek entscheidet je Abruf erneut. Filme mit Datei werden im Player geladen, Texte und PDF über einen kurz signierten Abruf. Der Systemverwalter vergibt diese Rollen und kann sie dem eigenen Konto geben. Ein Konto darf mehrere Rollen tragen. diff --git a/tests/Feature/StaffAuthenticationTest.php b/tests/Feature/StaffAuthenticationTest.php new file mode 100644 index 0000000..7d95127 --- /dev/null +++ b/tests/Feature/StaffAuthenticationTest.php @@ -0,0 +1,75 @@ +assertSame('Lara&Felix (dev)', StaffAuthenticatorName::current()); + + $provider = Filament::getPanel('verwaltung')->getMultiFactorAuthenticationProviders()['app'] ?? null; + $this->assertInstanceOf(AppAuthentication::class, $provider); + $this->assertSame('Lara&Felix (dev)', $provider->getBrandName()); + + $this->app['env'] = 'production'; + + $this->assertSame('Lara&Felix', StaffAuthenticatorName::current()); + } + + public function test_a_trusted_device_skips_the_code_until_the_secret_changes(): void + { + $user = User::factory()->systemverwalter()->create(); + $user->saveAppAuthenticationSecret('JBSWY3DPEHPK3PXP'); + $device = app(TrustedStaffDevice::class); + $request = Request::create('/verwaltung/login', 'POST', cookies: [ + TrustedStaffDevice::COOKIE => $device->token($user), + ]); + $this->app->instance('request', $request); + + $this->assertTrue($device->isTrusted($user, $request)); + $this->assertNull(TrustedDeviceChallenge::make()->getFirstEnabledProvider($user)); + + $user->saveAppAuthenticationSecret('KRSXG5CTMVRXEZLU'); + + $this->assertFalse($device->isTrusted($user, $request)); + $this->assertInstanceOf(AppAuthentication::class, TrustedDeviceChallenge::make()->getFirstEnabledProvider($user)); + } + + public function test_changing_the_own_password_keeps_the_staff_session_hash(): void + { + $admin = User::factory()->systemverwalter()->create(); + $this->actingAs($admin, 'staff'); + session()->put(['password_hash_staff' => $admin->getAuthPassword()]); + + UserResource::persist($admin, [ + 'name' => $admin->name, + 'email' => $admin->email, + 'password' => 'neues-passwort-1', + 'status' => AccountStatus::Aktiv->value, + 'roles' => [StaffRole::Systemverwalter->value], + ], false); + + $admin->refresh(); + + $this->assertTrue(Hash::check('neues-passwort-1', $admin->password)); + $this->assertSame($admin->getAuthPassword(), session('password_hash_staff')); + $this->assertAuthenticatedAs($admin, 'staff'); + } +}