Skip to content

Commit a28522b

Browse files
committed
Refactored 2fa challenge from hash to decrypt usage
1 parent 7699740 commit a28522b

File tree

6 files changed

+18
-31
lines changed

6 files changed

+18
-31
lines changed

resources/lang/en/actions.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
<?php
2-
3-
// translations for Stephenjude/FilamentTwoFactorAuthentication
42
return [
53
'confirm_two_factor_authentication' => [
64
'wrong_code' => 'The provided two factor authentication code was invalid.',

resources/lang/en/pages.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
return [
44
'subheading' => 'Or',
55
'challenge' => [
6-
'action_label' => 'use a recovery code',
6+
'action_label' => 'Use a recovery code',
77
'confirm' => 'Please confirm access to your account by entering the authentication code provided by your authenticator application.',
88
'code' => 'Code',
99
'error' => 'The provided two factor authentication code was invalid.',
1010
],
1111
'recovery' => [
12-
'action_label' => 'use an authentication code',
12+
'action_label' => 'Use an authentication code',
1313
'form_hint' => 'Please confirm access to your account by entering one of your emergency recovery codes.',
1414
'error' => 'The provided two factor authentication code was invalid.',
1515
'title' => 'Recovery Code',

resources/views/components/logout.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<div class="flex justify-center w-full">
22
<form method="POST" action="{{ filament()->getCurrentOrDefaultPanel()->getLogoutUrl() }}">
33
@csrf
4-
<x-filament::link tag="button" type="submit" weight="semibold">
4+
<x-filament::link style="align-center" tag="button" type="submit" weight="semibold">
55
{{__('filament-two-factor-authentication::components.logout.button')}}
66
</x-filament::link>
77
</form>

src/Pages/Challenge.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ class Challenge extends BaseSimplePage
1919

2020
public ?array $data = [];
2121

22-
public function getTitle(): string | Htmlable
22+
public function getTitle(): string|Htmlable
2323
{
2424
return __('filament-two-factor-authentication::section.header');
2525
}
2626

2727
public function mount(): void
2828
{
29-
if (! Filament::auth()->check()) {
29+
if (!Filament::auth()->check()) {
3030
redirect()->to(filament()->getCurrentOrDefaultPanel()?->getLoginUrl());
3131

3232
return;
@@ -44,11 +44,7 @@ public function recoveryAction(): Action
4444
return Action::make('recovery')
4545
->link()
4646
->label(__('filament-two-factor-authentication::pages.challenge.action_label'))
47-
->url(
48-
filament()->getCurrentOrDefaultPanel()->route(
49-
'two-factor.recovery'
50-
)
51-
);
47+
->url(filament()->getCurrentOrDefaultPanel()->route('two-factor.recovery'));
5248
}
5349

5450
public function authenticate()
@@ -85,8 +81,7 @@ public function form(Schema $schema): Schema
8581
->required()
8682
->autocomplete()
8783
->rules([
88-
fn () => function (string $attribute, $value, $fail) {
89-
84+
fn() => function (string $attribute, $value, $fail) {
9085
$user = Filament::auth()->user();
9186
if (is_null($user)) {
9287
$fail(__('filament-two-factor-authentication::pages.challenge.error'));
@@ -101,7 +96,7 @@ public function form(Schema $schema): Schema
10196
code: $value
10297
);
10398

104-
if (! $isValidCode) {
99+
if (!$isValidCode) {
105100
$fail(__('filament-two-factor-authentication::pages.challenge.error'));
106101

107102
event(new TwoFactorAuthenticationFailed($user));
@@ -122,7 +117,7 @@ public function getFormActions(): array
122117
protected function getAuthenticateFormAction(): Action
123118
{
124119
return Action::make('authenticate')
125-
->label(__('filament-panels::pages/auth/login.form.actions.authenticate.label'))
120+
->label(__('filament-panels::auth/pages/login.form.actions.authenticate.label'))
126121
->submit('authenticate');
127122
}
128123

src/Pages/Recovery.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,7 @@ public function form(Schema $schema): Schema
6666
->schema([
6767
TextInput::make('recovery_code')
6868
->hiddenLabel()
69-
->hint(
70-
__(
71-
'filament-two-factor-authentication::pages.recovery.form_hint'
72-
)
73-
)
69+
->hint(__('filament-two-factor-authentication::pages.recovery.form_hint'))
7470
->required()
7571
->autocomplete()
7672
->autofocus()
@@ -101,7 +97,7 @@ public function getFormActions(): array
10197
protected function getAuthenticateFormAction(): Action
10298
{
10399
return Action::make('authenticate')
104-
->label(__('filament-panels::pages/auth/login.form.actions.authenticate.label'))
100+
->label(__('filament-panels::auth/pages/login.form.actions.authenticate.label'))
105101
->submit('authenticate');
106102
}
107103

src/TwoFactorAuthenticatable.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use BaconQrCode\Renderer\RendererStyle\RendererStyle;
1010
use BaconQrCode\Writer;
1111
use Illuminate\Support\Facades\Cache;
12-
use Illuminate\Support\Facades\Hash;
1312
use Spatie\LaravelPasskeys\Models\Concerns\InteractsWithPasskeys;
1413
use Stephenjude\FilamentTwoFactorAuthentication\Events\RecoveryCodeReplaced;
1514

@@ -22,8 +21,8 @@ trait TwoFactorAuthenticatable
2221
*/
2322
public function hasEnabledTwoFactorAuthentication(): bool
2423
{
25-
return ! is_null($this->two_factor_secret) &&
26-
! is_null($this->two_factor_confirmed_at);
24+
return !is_null($this->two_factor_secret) &&
25+
!is_null($this->two_factor_confirmed_at);
2726
}
2827

2928
public function hasEnabledPasskeyAuthentication(): bool
@@ -44,17 +43,16 @@ public function passkeyAuthenticated(): bool
4443

4544
public function isTwoFactorChallengePassed(): bool
4645
{
47-
$sessionKey = 'login_2fa_challenge_passed_' . $this->id;
46+
if ($twoFactorSecretFromSession = session()->get("login:challenge:secret:$this->id")) {
47+
return decrypt($this->two_factor_secret) === decrypt($twoFactorSecretFromSession);
48+
}
4849

49-
return Hash::check($this->two_factor_secret, session()->get($sessionKey));
50+
return false;
5051
}
5152

5253
public function setTwoFactorChallengePassed(): void
5354
{
54-
$sessionKey = 'login_2fa_challenge_passed_' . $this->id;
55-
$sessionValue = Hash::make($this->two_factor_secret);
56-
57-
session()->put($sessionKey, $sessionValue);
55+
session()->put("login:challenge:secret:$this->id", $this->two_factor_secret);
5856
}
5957

6058
/**

0 commit comments

Comments
 (0)