Benenne den Authenticator und merke denselben Browser für 30 Tage.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
cd79fb99d7
commit
b374335b3a
10 files changed
+241
-4
No files matched your search
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace App\Auth;
|
||||
|
||||
class StaffAuthenticatorName
|
||||
{
|
||||
public static function current(): string
|
||||
{
|
||||
return app()->isProduction() ? 'Lara&Felix' : 'Lara&Felix (dev)';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
namespace App\Auth;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Cookie;
|
||||
use Illuminate\Support\Facades\Crypt;
|
||||
|
||||
class TrustedStaffDevice
|
||||
{
|
||||
public const COOKIE = 'luf_staff_mfa';
|
||||
|
||||
public const DAYS = 30;
|
||||
|
||||
public function remember(User $user): void
|
||||
{
|
||||
Cookie::queue(cookie(
|
||||
self::COOKIE,
|
||||
$this->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());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Auth;
|
||||
|
||||
use Filament\Auth\MultiFactor\MultiFactorChallenge;
|
||||
use Filament\Auth\Pages\Login;
|
||||
|
||||
class StaffLogin extends Login
|
||||
{
|
||||
protected function getMultiFactorChallenge(): MultiFactorChallenge
|
||||
{
|
||||
return TrustedDeviceChallenge::make();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Auth;
|
||||
|
||||
use App\Auth\TrustedStaffDevice;
|
||||
use App\Models\User;
|
||||
use Filament\Auth\MultiFactor\Contracts\MultiFactorAuthenticationProvider;
|
||||
use Filament\Auth\MultiFactor\MultiFactorChallenge;
|
||||
use Illuminate\Contracts\Auth\Authenticatable;
|
||||
|
||||
class TrustedDeviceChallenge extends MultiFactorChallenge
|
||||
{
|
||||
public function getFirstEnabledProvider(Authenticatable $user): ?MultiFactorAuthenticationProvider
|
||||
{
|
||||
if ($user instanceof User && app(TrustedStaffDevice::class)->isTrusted($user)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return parent::getFirstEnabledProvider($user);
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Responses;
|
||||
|
||||
use App\Auth\TrustedStaffDevice;
|
||||
use App\Models\User;
|
||||
use Filament\Auth\Http\Responses\Contracts\LoginResponse as Responsable;
|
||||
use Filament\Facades\Filament;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Livewire\Features\SupportRedirects\Redirector;
|
||||
|
||||
class StaffLoginResponse implements Responsable
|
||||
{
|
||||
public function toResponse($request): RedirectResponse|Redirector
|
||||
{
|
||||
$user = Filament::auth()->user();
|
||||
|
||||
if ($user instanceof User && filled($user->getAppAuthenticationSecret())) {
|
||||
app(TrustedStaffDevice::class)->remember($user);
|
||||
}
|
||||
|
||||
return redirect()->intended(Filament::getUrl());
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
@@ -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')
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Auth\StaffAuthenticatorName;
|
||||
use App\Auth\TrustedStaffDevice;
|
||||
use App\Domain\Identity\AccountStatus;
|
||||
use App\Domain\Identity\StaffRole;
|
||||
use App\Filament\Auth\TrustedDeviceChallenge;
|
||||
use App\Filament\Resources\Users\UserResource;
|
||||
use App\Models\User;
|
||||
use Filament\Auth\MultiFactor\App\AppAuthentication;
|
||||
use Filament\Facades\Filament;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Tests\TestCase;
|
||||
|
||||
class StaffAuthenticationTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_authenticator_name_distinguishes_production_from_development(): void
|
||||
{
|
||||
$this->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');
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user