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')
|
||||
|
||||
Reference in new issue
Block a user