| Total Complexity | 18 |
| Total Lines | 116 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 11 | class TwoFactorController extends AuthController |
||
| 12 | { |
||
| 13 | public function activate(Request $request) |
||
|
1 ignored issue
–
show
|
|||
| 14 | { |
||
| 15 | $user = $request->user(); |
||
| 16 | $google2fa = app('pragmarx.google2fa'); |
||
| 17 | $secret = $google2fa->generateSecretKey(); |
||
| 18 | $qrImage = $google2fa->getQRCodeInline( |
||
| 19 | config('app.name'), |
||
| 20 | $user->email, |
||
| 21 | $secret |
||
| 22 | ); |
||
| 23 | |||
| 24 | return view('auth.two_factor', [ |
||
| 25 | 'qr_image' => $qrImage, |
||
| 26 | 'secret' => $secret, |
||
| 27 | ]); |
||
| 28 | } |
||
| 29 | |||
| 30 | public function deactivate(Request $request) |
||
|
1 ignored issue
–
show
|
|||
| 31 | { |
||
| 32 | $user = $request->user(); |
||
| 33 | $user->google2fa_secret = null; |
||
| 34 | $user->recovery_codes = null; |
||
| 35 | $user->save(); |
||
| 36 | $user->refresh(); |
||
| 37 | |||
| 38 | $profile_url = ''; |
||
| 39 | if (WhichPortal::isApplicantPortal()) { |
||
| 40 | $profile_url = route('settings.edit'); |
||
| 41 | } elseif (WhichPortal::isManagerPortal()) { |
||
| 42 | $profile_url = route('manager.settings.edit'); |
||
| 43 | } elseif (WhichPortal::isAdminPortal()) { |
||
| 44 | $profile_url = backpack_url('2fa'); |
||
| 45 | } |
||
| 46 | |||
| 47 | return redirect($profile_url); |
||
| 48 | } |
||
| 49 | |||
| 50 | public function confirm(Request $request) |
||
|
1 ignored issue
–
show
|
|||
| 51 | { |
||
| 52 | $user = $request->user(); |
||
| 53 | $validatedData = $request->validate([ |
||
| 54 | 'secret' => 'required|string', |
||
| 55 | 'one_time_password' => 'required|string', |
||
| 56 | ]); |
||
| 57 | $secret = $validatedData['secret']; |
||
| 58 | $one_time_password = $validatedData['one_time_password']; |
||
| 59 | |||
| 60 | // A 2fa secret is already set up, no need to do anything. |
||
| 61 | if (!empty($user->google2fa_secret)) { |
||
| 62 | return redirect()->route('home'); |
||
| 63 | } |
||
| 64 | |||
| 65 | // Check that the one time password matches the secret. |
||
| 66 | $authenticator = app(Authenticator::class)->boot($request); |
||
| 67 | $isCorrect = $authenticator->verifyGoogle2FA($secret, $one_time_password); |
||
| 68 | |||
| 69 | if ($isCorrect) { |
||
| 70 | // The password matched the secret! Save the secret, and authenticate. |
||
| 71 | $user->google2fa_secret = $secret; |
||
| 72 | $user->save(); |
||
| 73 | $user->refresh(); |
||
| 74 | $authenticator->login(); |
||
| 75 | |||
| 76 | $this->rememberDevice($request); |
||
| 77 | |||
| 78 | $recovery_codes_url = ''; |
||
| 79 | if (WhichPortal::isApplicantPortal()) { |
||
| 80 | $recovery_codes_url = route('recovery_codes.show'); |
||
| 81 | } elseif (WhichPortal::isManagerPortal()) { |
||
| 82 | $recovery_codes_url = route('manager.recovery_codes.show'); |
||
| 83 | } elseif (WhichPortal::isAdminPortal()) { |
||
| 84 | $recovery_codes_url = route('admin.recovery_codes.show'); |
||
| 85 | } |
||
| 86 | |||
| 87 | return redirect($recovery_codes_url); |
||
| 88 | } else { |
||
| 89 | $activation_url = ''; |
||
| 90 | if (WhichPortal::isApplicantPortal()) { |
||
| 91 | $activation_url = route('two_factor.activate'); |
||
| 92 | } elseif (WhichPortal::isManagerPortal()) { |
||
| 93 | $activation_url = route('manager.two_factor.activate'); |
||
| 94 | } elseif (WhichPortal::isAdminPortal()) { |
||
| 95 | $activation_url = backpack_url('admin.two_factor.activate'); |
||
| 96 | } |
||
| 97 | |||
| 98 | return redirect($activation_url) |
||
| 99 | ->withErrors(['otp' => Lang::get('two_factor.activation_otp_error')]); |
||
| 100 | } |
||
| 101 | } |
||
| 102 | |||
| 103 | public function redirectToExpected(Request $request) |
||
| 112 | } |
||
| 113 | |||
| 114 | protected function rememberDevice(Request $request) |
||
| 127 | ); |
||
| 128 | } |
||
| 129 | } |
||
| 130 | } |
||
| 131 |