From ffa4aa88c6a897487c1c10eeca4a0f1aebc25894 Mon Sep 17 00:00:00 2001 From: Dennis Elsinga Date: Mon, 9 Sep 2024 10:28:21 +0200 Subject: [PATCH] Add `forced()` method and middleware --- src/Http/Middleware/Force2Factor.php | 24 ++++++++++++++++++++++++ src/TwoFactorAuthPlugin.php | 21 +++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 src/Http/Middleware/Force2Factor.php diff --git a/src/Http/Middleware/Force2Factor.php b/src/Http/Middleware/Force2Factor.php new file mode 100644 index 0000000..98020b1 --- /dev/null +++ b/src/Http/Middleware/Force2Factor.php @@ -0,0 +1,24 @@ +auth()->user(); + + if ($request->is('*/two-factor') || $request->is('*/logout')) { + return $next($request); + } + + if (! $user->two_factor_confirmed_at) { + // TODO: Redirect to the two-factor page. + } + + return $next($request); + } +} diff --git a/src/TwoFactorAuthPlugin.php b/src/TwoFactorAuthPlugin.php index 7e57d9f..782a5af 100644 --- a/src/TwoFactorAuthPlugin.php +++ b/src/TwoFactorAuthPlugin.php @@ -5,15 +5,30 @@ use Filament\Contracts\Plugin; use Filament\Navigation\MenuItem; use Filament\Panel; +use Vormkracht10\TwoFactorAuth\Http\Middleware\Force2Factor; use Vormkracht10\TwoFactorAuth\Pages\TwoFactor; class TwoFactorAuthPlugin implements Plugin { + private bool $forced = false; + public function getId(): string { return 'filament-two-factor-auth'; } + public function forced(bool $forced = true): self + { + $this->forced = $forced; + + return $this; + } + + public function isForced(): bool + { + return $this->forced; + } + public function register(Panel $panel): void { $panel @@ -24,6 +39,12 @@ public function register(Panel $panel): void ]) ->viteTheme('vendor/vormkracht10/filament-2fa/resources/dist/filament-two-factor-auth.css'); + if ($this->isForced()) { + $panel->authMiddleware([ + Force2Factor::class, + ]); + } + if (! config('filament-two-factor-auth.enabled_features.multi_tenancy')) { $panel->userMenuItems([ 'two-factor-authentication' => MenuItem::make()