|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Statamic\Http\Controllers\Auth; |
| 4 | + |
| 5 | +use Illuminate\Http\Exceptions\HttpResponseException; |
| 6 | +use Illuminate\Http\Request; |
| 7 | +use Illuminate\Support\Facades\Hash; |
| 8 | +use Illuminate\Validation\ValidationException; |
| 9 | +use Inertia\Inertia; |
| 10 | +use Statamic\Auth\WebAuthn\Serializer; |
| 11 | +use Statamic\Facades\User; |
| 12 | +use Statamic\Facades\WebAuthn; |
| 13 | +use Statamic\Http\Controllers\Controller; |
| 14 | +use Statamic\Http\Requests\Auth\ElevatedSessionConfirmationRequest; |
| 15 | + |
| 16 | +class ElevatedSessionController extends Controller |
| 17 | +{ |
| 18 | + public function showForm(Request $request) |
| 19 | + { |
| 20 | + if ($customUrl = config('statamic.users.elevated_session_url')) { |
| 21 | + return redirect()->to($customUrl); |
| 22 | + } |
| 23 | + |
| 24 | + $user = User::current(); |
| 25 | + $method = $user->getElevatedSessionMethod(); |
| 26 | + |
| 27 | + if ($method === 'verification_code') { |
| 28 | + session()->sendElevatedSessionVerificationCodeIfRequired(); |
| 29 | + } |
| 30 | + |
| 31 | + return Inertia::render('auth/ConfirmPassword', [ |
| 32 | + 'outside' => true, |
| 33 | + 'method' => $method, |
| 34 | + 'allowPasskey' => $method !== 'verification_code' && $user->passkeys()->isNotEmpty(), |
| 35 | + 'status' => session('status'), |
| 36 | + 'submitUrl' => route('statamic.elevated-session.confirm'), |
| 37 | + 'resendUrl' => route('statamic.elevated-session.resend-code'), |
| 38 | + 'passkeyOptionsUrl' => route('statamic.elevated-session.passkey-options'), |
| 39 | + ]); |
| 40 | + } |
| 41 | + |
| 42 | + public function confirm(ElevatedSessionConfirmationRequest $request) |
| 43 | + { |
| 44 | + $user = User::current(); |
| 45 | + |
| 46 | + $this->validatePasswordConfirmation($request, $user); |
| 47 | + $this->validateVerificationCodeConfirmation($request); |
| 48 | + $this->validatePasskeyConfirmation($request, $user); |
| 49 | + |
| 50 | + session()->elevate(); |
| 51 | + |
| 52 | + return $this->buildConfirmResponse($request, $user); |
| 53 | + } |
| 54 | + |
| 55 | + protected function buildConfirmResponse(Request $request, $user) |
| 56 | + { |
| 57 | + $message = $user->getElevatedSessionMethod() === 'password_confirmation' |
| 58 | + ? __('Password confirmed') |
| 59 | + : __('Code verified'); |
| 60 | + |
| 61 | + $redirect = redirect()->intended(route('statamic.site')); |
| 62 | + |
| 63 | + if ($request->wantsJson()) { |
| 64 | + return response()->json([ |
| 65 | + 'elevated' => true, |
| 66 | + 'expiry' => $request->getElevatedSessionExpiry(), |
| 67 | + 'redirect' => $redirect->getTargetUrl(), |
| 68 | + ]); |
| 69 | + } |
| 70 | + |
| 71 | + return $request->inertia() |
| 72 | + ? Inertia::location($redirect->getTargetUrl()) |
| 73 | + : $redirect->with('success', $message); |
| 74 | + } |
| 75 | + |
| 76 | + public function options() |
| 77 | + { |
| 78 | + $options = WebAuthn::prepareAssertion(); |
| 79 | + |
| 80 | + return app(Serializer::class)->normalize($options); |
| 81 | + } |
| 82 | + |
| 83 | + public function resendCode() |
| 84 | + { |
| 85 | + if (User::current()->getElevatedSessionMethod() !== 'verification_code') { |
| 86 | + throw ValidationException::withMessages([ |
| 87 | + 'method' => __('statamic::validation.elevated_session_resend_code_unavailable'), |
| 88 | + ]); |
| 89 | + } |
| 90 | + |
| 91 | + session()->sendElevatedSessionVerificationCode(); |
| 92 | + |
| 93 | + return back()->with('status', __('statamic::messages.elevated_session_verification_code_sent')); |
| 94 | + } |
| 95 | + |
| 96 | + private function validatePasswordConfirmation(Request $request, $user): void |
| 97 | + { |
| 98 | + if (! $request->filled('password')) { |
| 99 | + return; |
| 100 | + } |
| 101 | + |
| 102 | + if (Hash::check($request->password, $user->password())) { |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + $this->throwValidationException($request, [ |
| 107 | + 'password' => [__('statamic::validation.current_password')], |
| 108 | + ]); |
| 109 | + } |
| 110 | + |
| 111 | + private function validateVerificationCodeConfirmation(Request $request): void |
| 112 | + { |
| 113 | + if (! $request->filled('verification_code')) { |
| 114 | + return; |
| 115 | + } |
| 116 | + |
| 117 | + $verificationCode = $request->verification_code; |
| 118 | + $storedVerificationCode = $request->getElevatedSessionVerificationCode(); |
| 119 | + |
| 120 | + if ( |
| 121 | + is_string($verificationCode) |
| 122 | + && is_string($storedVerificationCode) |
| 123 | + && hash_equals($storedVerificationCode, $verificationCode) |
| 124 | + ) { |
| 125 | + return; |
| 126 | + } |
| 127 | + |
| 128 | + $this->throwValidationException($request, [ |
| 129 | + 'verification_code' => [__('statamic::validation.elevated_session_verification_code')], |
| 130 | + ]); |
| 131 | + } |
| 132 | + |
| 133 | + protected function throwValidationException(Request $request, array $errors): never |
| 134 | + { |
| 135 | + if ($request->wantsJson() || $request->inertia()) { |
| 136 | + throw ValidationException::withMessages($errors); |
| 137 | + } |
| 138 | + |
| 139 | + throw new HttpResponseException( |
| 140 | + back()->withInput()->withErrors($errors, 'user.elevated_session') |
| 141 | + ); |
| 142 | + } |
| 143 | + |
| 144 | + private function validatePasskeyConfirmation(Request $request, $user): void |
| 145 | + { |
| 146 | + if (! $request->filled('id')) { |
| 147 | + return; |
| 148 | + } |
| 149 | + |
| 150 | + $credentials = $request->only(['id', 'rawId', 'response', 'type']); |
| 151 | + WebAuthn::validateAssertion($user, $credentials); |
| 152 | + } |
| 153 | +} |
0 commit comments