From a06cd105eb888f6a2c6815d36e7b500f6fc89a60 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Fri, 11 Sep 2020 14:50:33 -0500 Subject: [PATCH 01/10] confirmable --- .../components/confirms-password.blade.php | 44 +++++++ src/ConfirmsPasswords.php | 111 ++++++++++++++++++ .../Livewire/TwoFactorAuthenticationForm.php | 17 +++ src/JetstreamServiceProvider.php | 1 + .../two-factor-authentication-form.blade.php | 16 ++- 5 files changed, 183 insertions(+), 6 deletions(-) create mode 100644 resources/views/components/confirms-password.blade.php create mode 100644 src/ConfirmsPasswords.php diff --git a/resources/views/components/confirms-password.blade.php b/resources/views/components/confirms-password.blade.php new file mode 100644 index 000000000..6692e3dd3 --- /dev/null +++ b/resources/views/components/confirms-password.blade.php @@ -0,0 +1,44 @@ +@php + $confirmableId = md5($attributes->wire('then')); +@endphp + +wire('then') }} + x-data + x-ref="span" + x-on:click="$wire.startConfirmingPassword('{{ $confirmableId }}')" + x-on:password-confirmed.window="setTimeout(() => $event.detail.id === '{{ $confirmableId }}' && $refs.span.dispatchEvent(new CustomEvent('then', { bubbles: false })), 250);" +> + {{ $slot }} + + +@once + + + Confirm Password + + + + {{ __('For your security, please enter your password to continue.') }} + +
+ + + +
+
+ + + + Nevermind + + + + Confirm + + +
+@endonce diff --git a/src/ConfirmsPasswords.php b/src/ConfirmsPasswords.php new file mode 100644 index 000000000..a76e965f5 --- /dev/null +++ b/src/ConfirmsPasswords.php @@ -0,0 +1,111 @@ +resetErrorBag(); + + if ($this->passwordIsConfirmed()) { + return $this->dispatchBrowserEvent('password-confirmed', [ + 'id' => $confirmableId + ]); + } + + $this->confirmingPassword = true; + $this->confirmableId = $confirmableId; + $this->confirmablePassword = ''; + + $this->dispatchBrowserEvent('confirming-password'); + } + + /** + * Stop confirming the user's password. + * + * @return void + */ + public function stopConfirmingPassword() + { + $this->confirmingPassword = false; + $this->confirmableId = null; + $this->confirmablePassword = ''; + } + + /** + * Confirm the user's password. + * + * @return void + */ + public function confirmPassword() + { + if (! app(ConfirmPassword::class)(app(StatefulGuard::class), Auth::user(), $this->confirmablePassword)) { + throw ValidationException::withMessages([ + 'confirmable_password' => [__('This password does not match our records.')], + ]); + } + + session(['auth.password_confirmed_at' => time()]); + + $this->dispatchBrowserEvent('password-confirmed', [ + 'id' => $this->confirmableId + ]); + + $this->stopConfirmingPassword(); + } + + /** + * Ensure that the user's password has been recently confirmed. + * + * @param int $maximumSecondsSinceConfirmation + * @return void + */ + protected function ensurePasswordIsConfirmed($maximumSecondsSinceConfirmation = 900) + { + return $this->passwordIsConfirmed($maximumSecondsSinceConfirmation) ? null : abort(403); + } + + /** + * Determine if the user's password has been recently confirmed. + * + * @param int $maximumSecondsSinceConfirmation + * @return bool + */ + protected function passwordIsConfirmed($maximumSecondsSinceConfirmation = 900) + { + return (time() - session('auth.password_confirmed_at', 0)) < $maximumSecondsSinceConfirmation; + } +} diff --git a/src/Http/Livewire/TwoFactorAuthenticationForm.php b/src/Http/Livewire/TwoFactorAuthenticationForm.php index 74d05bff4..b66220449 100644 --- a/src/Http/Livewire/TwoFactorAuthenticationForm.php +++ b/src/Http/Livewire/TwoFactorAuthenticationForm.php @@ -6,10 +6,13 @@ use Laravel\Fortify\Actions\DisableTwoFactorAuthentication; use Laravel\Fortify\Actions\EnableTwoFactorAuthentication; use Laravel\Fortify\Actions\GenerateNewRecoveryCodes; +use Laravel\Jetstream\ConfirmsPasswords; use Livewire\Component; class TwoFactorAuthenticationForm extends Component { + use ConfirmsPasswords; + /** * Indicates if two factor authentication QR code is being displayed. * @@ -38,6 +41,18 @@ public function enableTwoFactorAuthentication(EnableTwoFactorAuthentication $ena $this->showingRecoveryCodes = true; } + /** + * Display the user's recovery codes. + * + * @return void + */ + public function showRecoveryCodes() + { + $this->ensurePasswordIsConfirmed(); + + $this->showingRecoveryCodes = true; + } + /** * Generate new recovery codes for the user. * @@ -46,6 +61,8 @@ public function enableTwoFactorAuthentication(EnableTwoFactorAuthentication $ena */ public function regenerateRecoveryCodes(GenerateNewRecoveryCodes $generate) { + $this->ensurePasswordIsConfirmed(); + $generate(Auth::user()); $this->showingRecoveryCodes = true; diff --git a/src/JetstreamServiceProvider.php b/src/JetstreamServiceProvider.php index fe2eb4fe0..7c7582ccd 100644 --- a/src/JetstreamServiceProvider.php +++ b/src/JetstreamServiceProvider.php @@ -94,6 +94,7 @@ protected function configureComponents() $this->registerComponent('authentication-card-logo'); $this->registerComponent('button'); $this->registerComponent('confirmation-modal'); + $this->registerComponent('confirms-password'); $this->registerComponent('danger-button'); $this->registerComponent('dialog-modal'); $this->registerComponent('dropdown'); diff --git a/stubs/livewire/resources/views/profile/two-factor-authentication-form.blade.php b/stubs/livewire/resources/views/profile/two-factor-authentication-form.blade.php index cdc531ecf..d26a4b5fb 100644 --- a/stubs/livewire/resources/views/profile/two-factor-authentication-form.blade.php +++ b/stubs/livewire/resources/views/profile/two-factor-authentication-form.blade.php @@ -57,13 +57,17 @@ @else @if ($showingRecoveryCodes) - - {{ __('Regenerate Recovery Codes') }} - + + + {{ __('Regenerate Recovery Codes') }} + + @else - - {{ __('Show Recovery Codes') }} - + + + {{ __('Show Recovery Codes') }} + + @endif From 2aaa0dbf2dfaf96dfbaab50ed09b501c826f6950 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Fri, 11 Sep 2020 14:55:15 -0500 Subject: [PATCH 02/10] updates to confirmable --- resources/views/components/confirms-password.blade.php | 6 ++++-- src/Http/Livewire/TwoFactorAuthenticationForm.php | 2 ++ .../profile/two-factor-authentication-form.blade.php | 8 +++++--- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/resources/views/components/confirms-password.blade.php b/resources/views/components/confirms-password.blade.php index 6692e3dd3..a144f1d99 100644 --- a/resources/views/components/confirms-password.blade.php +++ b/resources/views/components/confirms-password.blade.php @@ -1,3 +1,5 @@ +@props(['title' => 'Confirm Password', 'content' => __('For your security, please enter your password to continue.')]) + @php $confirmableId = md5($attributes->wire('then')); @endphp @@ -15,11 +17,11 @@ @once - Confirm Password + {{ $title }} - {{ __('For your security, please enter your password to continue.') }} + {{ $content }}
ensurePasswordIsConfirmed(); + $disable(Auth::user()); } diff --git a/stubs/livewire/resources/views/profile/two-factor-authentication-form.blade.php b/stubs/livewire/resources/views/profile/two-factor-authentication-form.blade.php index d26a4b5fb..1f99886f1 100644 --- a/stubs/livewire/resources/views/profile/two-factor-authentication-form.blade.php +++ b/stubs/livewire/resources/views/profile/two-factor-authentication-form.blade.php @@ -70,9 +70,11 @@ @endif - - {{ __('Disable') }} - + + + {{ __('Disable') }} + + @endif
From d60e8e11aa6ddddb039db414a75a86540efd8089 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Fri, 11 Sep 2020 14:58:16 -0500 Subject: [PATCH 03/10] tweak wording --- resources/views/components/confirms-password.blade.php | 4 ++-- src/Http/Livewire/TwoFactorAuthenticationForm.php | 2 ++ .../profile/two-factor-authentication-form.blade.php | 8 +++++--- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/resources/views/components/confirms-password.blade.php b/resources/views/components/confirms-password.blade.php index a144f1d99..b26c63b75 100644 --- a/resources/views/components/confirms-password.blade.php +++ b/resources/views/components/confirms-password.blade.php @@ -1,4 +1,4 @@ -@props(['title' => 'Confirm Password', 'content' => __('For your security, please enter your password to continue.')]) +@props(['title' => 'Confirm Password', 'content' => __('For your security, please confirm your password to continue.'), 'button' => __('Confirm')]) @php $confirmableId = md5($attributes->wire('then')); @@ -39,7 +39,7 @@ - Confirm + {{ $button }}
diff --git a/src/Http/Livewire/TwoFactorAuthenticationForm.php b/src/Http/Livewire/TwoFactorAuthenticationForm.php index 651c5a0b9..58688fa66 100644 --- a/src/Http/Livewire/TwoFactorAuthenticationForm.php +++ b/src/Http/Livewire/TwoFactorAuthenticationForm.php @@ -35,6 +35,8 @@ class TwoFactorAuthenticationForm extends Component */ public function enableTwoFactorAuthentication(EnableTwoFactorAuthentication $enable) { + $this->ensurePasswordIsConfirmed(); + $enable(Auth::user()); $this->showingQrCode = true; diff --git a/stubs/livewire/resources/views/profile/two-factor-authentication-form.blade.php b/stubs/livewire/resources/views/profile/two-factor-authentication-form.blade.php index 1f99886f1..88d8f3865 100644 --- a/stubs/livewire/resources/views/profile/two-factor-authentication-form.blade.php +++ b/stubs/livewire/resources/views/profile/two-factor-authentication-form.blade.php @@ -52,9 +52,11 @@
@if (! $this->enabled) - - {{ __('Enable') }} - + + + {{ __('Enable') }} + + @else @if ($showingRecoveryCodes) From 9adbcee8302957562b4a2eb61cafbf780ca0b98d Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Fri, 11 Sep 2020 14:58:56 -0500 Subject: [PATCH 04/10] Apply fixes from StyleCI (#156) --- src/ConfirmsPasswords.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ConfirmsPasswords.php b/src/ConfirmsPasswords.php index a76e965f5..a0e94cfbf 100644 --- a/src/ConfirmsPasswords.php +++ b/src/ConfirmsPasswords.php @@ -42,7 +42,7 @@ public function startConfirmingPassword(string $confirmableId) if ($this->passwordIsConfirmed()) { return $this->dispatchBrowserEvent('password-confirmed', [ - 'id' => $confirmableId + 'id' => $confirmableId, ]); } @@ -81,7 +81,7 @@ public function confirmPassword() session(['auth.password_confirmed_at' => time()]); $this->dispatchBrowserEvent('password-confirmed', [ - 'id' => $this->confirmableId + 'id' => $this->confirmableId, ]); $this->stopConfirmingPassword(); From 176c9ff56af4de9df75b307fe761521bda0f2767 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Fri, 11 Sep 2020 16:37:32 -0500 Subject: [PATCH 05/10] work on inertia confirmations --- .../js/Jetstream/ConfirmsPassword.vue | 119 ++++++++++++++++++ .../Profile/TwoFactorAuthenticationForm.vue | 49 ++++---- 2 files changed, 146 insertions(+), 22 deletions(-) create mode 100644 stubs/inertia/resources/js/Jetstream/ConfirmsPassword.vue diff --git a/stubs/inertia/resources/js/Jetstream/ConfirmsPassword.vue b/stubs/inertia/resources/js/Jetstream/ConfirmsPassword.vue new file mode 100644 index 000000000..79decfabf --- /dev/null +++ b/stubs/inertia/resources/js/Jetstream/ConfirmsPassword.vue @@ -0,0 +1,119 @@ + + + diff --git a/stubs/inertia/resources/js/Pages/Profile/TwoFactorAuthenticationForm.vue b/stubs/inertia/resources/js/Pages/Profile/TwoFactorAuthenticationForm.vue index 655c39fff..ea087903a 100644 --- a/stubs/inertia/resources/js/Pages/Profile/TwoFactorAuthenticationForm.vue +++ b/stubs/inertia/resources/js/Pages/Profile/TwoFactorAuthenticationForm.vue @@ -52,31 +52,34 @@
- - Enable - + + + Enable + +
- - Regenerate Recovery Codes - - - - Show Recovery Codes - - - - Disable - + + + Regenerate Recovery Codes + + + + + + Show Recovery Codes + + + + + + Disable + +
@@ -86,6 +89,7 @@