From c9dc596efc89931f58d6993b7755c16e3703bc29 Mon Sep 17 00:00:00 2001 From: Baspa Date: Wed, 28 Aug 2024 13:02:34 +0200 Subject: [PATCH 1/4] Validate otp --- src/Pages/TwoFactor.php | 48 ++++++++++++++++++++++++++++------------- 1 file changed, 33 insertions(+), 15 deletions(-) diff --git a/src/Pages/TwoFactor.php b/src/Pages/TwoFactor.php index 4c89c26..75bc675 100644 --- a/src/Pages/TwoFactor.php +++ b/src/Pages/TwoFactor.php @@ -2,22 +2,23 @@ namespace Vormkracht10\TwoFactorAuth\Pages; +use Filament\Forms\Form; +use Filament\Pages\Page; use Filament\Actions\Action; +use Laravel\Fortify\Features; +use Illuminate\Support\Collection; use Filament\Forms\Components\Radio; -use Filament\Forms\Components\TextInput; +use Illuminate\Support\Facades\Auth; use Filament\Forms\Contracts\HasForms; -use Filament\Forms\Form; +use Filament\Forms\Components\TextInput; use Filament\Notifications\Notification; -use Filament\Pages\Page; use Illuminate\Contracts\Support\Htmlable; -use Illuminate\Support\Collection; -use Illuminate\Support\Facades\Auth; +use Illuminate\Validation\ValidationException; +use Vormkracht10\TwoFactorAuth\Enums\TwoFactorType; +use Laravel\Fortify\Actions\GenerateNewRecoveryCodes; +use Laravel\Fortify\Actions\EnableTwoFactorAuthentication; use Laravel\Fortify\Actions\ConfirmTwoFactorAuthentication; use Laravel\Fortify\Actions\DisableTwoFactorAuthentication; -use Laravel\Fortify\Actions\EnableTwoFactorAuthentication; -use Laravel\Fortify\Actions\GenerateNewRecoveryCodes; -use Laravel\Fortify\Features; -use Vormkracht10\TwoFactorAuth\Enums\TwoFactorType; class TwoFactor extends Page implements HasForms { @@ -87,7 +88,7 @@ public function getConfirmationForm(): array return [ TextInput::make('current_password') ->label(__('Password')) - ->dehydrateStateUsing(fn ($state) => filled($state)) + ->dehydrateStateUsing(fn($state) => filled($state)) ->required() ->password() ->inlineLabel() @@ -178,10 +179,23 @@ public function confirmAction(): Action ->label(__('Confirm')) ->color('primary') ->action(function ($data) { + if (count($this->otpCodeData) === 0) { + $this->throwFailureValidationException(); + + return; + } + $this->confirmTwoFactorAuthentication(app(ConfirmTwoFactorAuthentication::class)); }); } + protected function throwFailureValidationException(): never + { + throw ValidationException::withMessages([ + 'otpCodeData.code' => __('The code you entered is invalid.'), + ]); + } + public function regenerateAction(): Action { return Action::make('regenerate') @@ -227,11 +241,15 @@ public function enableTwoFactorAuthentication(EnableTwoFactorAuthentication $ena public function confirmTwoFactorAuthentication(ConfirmTwoFactorAuthentication $confirm): void { - $confirm($this->user, $this->otpCodeData['code']); + try { + $confirm($this->user, $this->otpCodeData['code']); - $this->showingQrCode = false; - $this->showingConfirmation = false; - $this->showingRecoveryCodes = true; + $this->showingQrCode = false; + $this->showingConfirmation = false; + $this->showingRecoveryCodes = true; + } catch (\Exception $e) { + $this->throwFailureValidationException(); + } } public function disableTwoFactorAuthentication(DisableTwoFactorAuthentication $disable): void @@ -256,4 +274,4 @@ public function regenerateRecoveryCodes(GenerateNewRecoveryCodes $generate): voi $this->showingRecoveryCodes = true; } -} +} \ No newline at end of file From f5368bf17fed4f89f23a755c23ae49d718ce3611 Mon Sep 17 00:00:00 2001 From: Baspa Date: Wed, 28 Aug 2024 11:03:01 +0000 Subject: [PATCH 2/4] Fix styling --- src/Pages/TwoFactor.php | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/Pages/TwoFactor.php b/src/Pages/TwoFactor.php index 75bc675..6411688 100644 --- a/src/Pages/TwoFactor.php +++ b/src/Pages/TwoFactor.php @@ -2,23 +2,23 @@ namespace Vormkracht10\TwoFactorAuth\Pages; -use Filament\Forms\Form; -use Filament\Pages\Page; use Filament\Actions\Action; -use Laravel\Fortify\Features; -use Illuminate\Support\Collection; use Filament\Forms\Components\Radio; -use Illuminate\Support\Facades\Auth; -use Filament\Forms\Contracts\HasForms; use Filament\Forms\Components\TextInput; +use Filament\Forms\Contracts\HasForms; +use Filament\Forms\Form; use Filament\Notifications\Notification; +use Filament\Pages\Page; use Illuminate\Contracts\Support\Htmlable; +use Illuminate\Support\Collection; +use Illuminate\Support\Facades\Auth; use Illuminate\Validation\ValidationException; -use Vormkracht10\TwoFactorAuth\Enums\TwoFactorType; -use Laravel\Fortify\Actions\GenerateNewRecoveryCodes; -use Laravel\Fortify\Actions\EnableTwoFactorAuthentication; use Laravel\Fortify\Actions\ConfirmTwoFactorAuthentication; use Laravel\Fortify\Actions\DisableTwoFactorAuthentication; +use Laravel\Fortify\Actions\EnableTwoFactorAuthentication; +use Laravel\Fortify\Actions\GenerateNewRecoveryCodes; +use Laravel\Fortify\Features; +use Vormkracht10\TwoFactorAuth\Enums\TwoFactorType; class TwoFactor extends Page implements HasForms { @@ -88,7 +88,7 @@ public function getConfirmationForm(): array return [ TextInput::make('current_password') ->label(__('Password')) - ->dehydrateStateUsing(fn($state) => filled($state)) + ->dehydrateStateUsing(fn ($state) => filled($state)) ->required() ->password() ->inlineLabel() @@ -274,4 +274,4 @@ public function regenerateRecoveryCodes(GenerateNewRecoveryCodes $generate): voi $this->showingRecoveryCodes = true; } -} \ No newline at end of file +} From 20fac7732189a8f0d2900ec7084c1a21339456a3 Mon Sep 17 00:00:00 2001 From: Baspa Date: Wed, 28 Aug 2024 13:07:17 +0200 Subject: [PATCH 3/4] Remove return --- src/Pages/TwoFactor.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Pages/TwoFactor.php b/src/Pages/TwoFactor.php index 6411688..d533e55 100644 --- a/src/Pages/TwoFactor.php +++ b/src/Pages/TwoFactor.php @@ -88,7 +88,7 @@ public function getConfirmationForm(): array return [ TextInput::make('current_password') ->label(__('Password')) - ->dehydrateStateUsing(fn ($state) => filled($state)) + ->dehydrateStateUsing(fn($state) => filled($state)) ->required() ->password() ->inlineLabel() @@ -181,8 +181,6 @@ public function confirmAction(): Action ->action(function ($data) { if (count($this->otpCodeData) === 0) { $this->throwFailureValidationException(); - - return; } $this->confirmTwoFactorAuthentication(app(ConfirmTwoFactorAuthentication::class)); @@ -274,4 +272,4 @@ public function regenerateRecoveryCodes(GenerateNewRecoveryCodes $generate): voi $this->showingRecoveryCodes = true; } -} +} \ No newline at end of file From 039147763f042e40590120eb4ebfe6ee3f389077 Mon Sep 17 00:00:00 2001 From: Baspa Date: Wed, 28 Aug 2024 11:07:46 +0000 Subject: [PATCH 4/4] Fix styling --- src/Pages/TwoFactor.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Pages/TwoFactor.php b/src/Pages/TwoFactor.php index d533e55..5a915cf 100644 --- a/src/Pages/TwoFactor.php +++ b/src/Pages/TwoFactor.php @@ -88,7 +88,7 @@ public function getConfirmationForm(): array return [ TextInput::make('current_password') ->label(__('Password')) - ->dehydrateStateUsing(fn($state) => filled($state)) + ->dehydrateStateUsing(fn ($state) => filled($state)) ->required() ->password() ->inlineLabel() @@ -272,4 +272,4 @@ public function regenerateRecoveryCodes(GenerateNewRecoveryCodes $generate): voi $this->showingRecoveryCodes = true; } -} \ No newline at end of file +}