|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Http\Requests\Auth; |
| 4 | + |
| 5 | +use Illuminate\Foundation\Http\FormRequest; |
| 6 | +use Illuminate\Support\Facades\Auth; |
| 7 | +use Laravel\Fortify\Actions\DisableTwoFactorAuthentication; |
| 8 | +use Laravel\Fortify\Features; |
| 9 | + |
| 10 | +class TwoFactorAuthenticationRequest extends FormRequest |
| 11 | +{ |
| 12 | + /** |
| 13 | + * Determine if the user is authorized to make this request. |
| 14 | + */ |
| 15 | + public function authorize(): bool |
| 16 | + { |
| 17 | + return Features::enabled(Features::twoFactorAuthentication()); |
| 18 | + } |
| 19 | + |
| 20 | + /** |
| 21 | + * Get the validation rules that apply to the request. |
| 22 | + * |
| 23 | + * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string> |
| 24 | + */ |
| 25 | + public function rules(): array |
| 26 | + { |
| 27 | + return []; |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Validate the two-factor authentication state for the request. |
| 32 | + */ |
| 33 | + public function validateState(): void |
| 34 | + { |
| 35 | + if (! Features::optionEnabled(Features::twoFactorAuthentication(), 'confirm')) { |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + $currentTime = time(); |
| 40 | + |
| 41 | + // Notate totally disabled state in session... |
| 42 | + if ($this->twoFactorAuthenticationDisabled()) { |
| 43 | + $this->session()->put('two_factor_empty_at', $currentTime); |
| 44 | + } |
| 45 | + |
| 46 | + // If was previously totally disabled this session but is now confirming, notate time... |
| 47 | + if ($this->hasJustBegunConfirmingTwoFactorAuthentication()) { |
| 48 | + $this->session()->put('two_factor_confirming_at', $currentTime); |
| 49 | + } |
| 50 | + |
| 51 | + // If the profile is reloaded and is not confirmed but was previously in confirming state, disable... |
| 52 | + if ($this->neverFinishedConfirmingTwoFactorAuthentication($currentTime)) { |
| 53 | + app(DisableTwoFactorAuthentication::class)(Auth::user()); |
| 54 | + |
| 55 | + $this->session()->put('two_factor_empty_at', $currentTime); |
| 56 | + $this->session()->remove('two_factor_confirming_at'); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Determine if two-factor authentication is totally disabled. |
| 62 | + */ |
| 63 | + protected function twoFactorAuthenticationDisabled(): bool |
| 64 | + { |
| 65 | + return is_null($this->user()->two_factor_secret) && |
| 66 | + is_null($this->user()->two_factor_confirmed_at); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Determine if two-factor authentication is just now being confirmed within the last request cycle. |
| 71 | + */ |
| 72 | + protected function hasJustBegunConfirmingTwoFactorAuthentication(): bool |
| 73 | + { |
| 74 | + return ! is_null($this->user()->two_factor_secret) && |
| 75 | + is_null($this->user()->two_factor_confirmed_at) && |
| 76 | + $this->session()->has('two_factor_empty_at') && |
| 77 | + is_null($this->session()->get('two_factor_confirming_at')); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Determine if two-factor authentication was never totally confirmed once confirmation started. |
| 82 | + */ |
| 83 | + protected function neverFinishedConfirmingTwoFactorAuthentication(int $currentTime): bool |
| 84 | + { |
| 85 | + return ! array_key_exists('code', $this->session()->getOldInput()) && |
| 86 | + is_null($this->user()->two_factor_confirmed_at) && |
| 87 | + $this->session()->get('two_factor_confirming_at', 0) != $currentTime; |
| 88 | + } |
| 89 | +} |
0 commit comments