Total Complexity | 22 |
Total Lines | 134 |
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 | $profile_url = ''; |
||
24 | if (WhichPortal::isApplicantPortal()) { |
||
25 | $profile_url = route('settings.edit'); |
||
26 | } elseif (WhichPortal::isManagerPortal()) { |
||
27 | $profile_url = route('manager.settings.edit'); |
||
28 | } elseif (WhichPortal::isAdminPortal()) { |
||
29 | $profile_url = backpack_url('2fa'); |
||
30 | } |
||
31 | |||
32 | return view('auth.two_factor', [ |
||
33 | 'qr_image' => $qrImage, |
||
34 | 'secret' => $secret, |
||
35 | 'profile_url' => $profile_url, |
||
36 | ]); |
||
37 | } |
||
38 | |||
39 | public function deactivate(Request $request) |
||
1 ignored issue
–
show
|
|||
40 | { |
||
41 | $user = $request->user(); |
||
42 | $user->google2fa_secret = null; |
||
43 | $user->recovery_codes = null; |
||
44 | $user->save(); |
||
45 | $user->refresh(); |
||
46 | |||
47 | $profile_url = ''; |
||
48 | if (WhichPortal::isApplicantPortal()) { |
||
49 | $profile_url = route('settings.edit'); |
||
50 | } elseif (WhichPortal::isManagerPortal()) { |
||
51 | $profile_url = route('manager.settings.edit'); |
||
52 | } elseif (WhichPortal::isAdminPortal()) { |
||
53 | $profile_url = backpack_url('2fa'); |
||
54 | } |
||
55 | |||
56 | return redirect($profile_url)->withSuccess(Lang::get('success.two_factor_deactivate')); |
||
57 | } |
||
58 | |||
59 | public function confirm(Request $request) |
||
1 ignored issue
–
show
|
|||
60 | { |
||
61 | $user = $request->user(); |
||
62 | $validatedData = $request->validate([ |
||
63 | 'secret' => 'required|string', |
||
64 | 'one_time_password' => 'required|string', |
||
65 | ]); |
||
66 | $secret = $validatedData['secret']; |
||
67 | $one_time_password = $validatedData['one_time_password']; |
||
68 | |||
69 | // A 2fa secret is already set up, no need to do anything. |
||
70 | if (!empty($user->google2fa_secret)) { |
||
71 | return redirect()->route('home'); |
||
72 | } |
||
73 | |||
74 | // Check that the one time password matches the secret. |
||
75 | $authenticator = app(Authenticator::class)->boot($request); |
||
76 | $isCorrect = $authenticator->verifyGoogle2FA($secret, $one_time_password); |
||
77 | |||
78 | if ($isCorrect) { |
||
79 | // The password matched the secret! Save the secret, and authenticate. |
||
80 | $user->google2fa_secret = $secret; |
||
81 | $user->save(); |
||
82 | $user->refresh(); |
||
83 | $authenticator->login(); |
||
84 | |||
85 | $this->rememberDevice($request); |
||
86 | |||
87 | $recovery_codes_url = ''; |
||
88 | if (WhichPortal::isApplicantPortal()) { |
||
89 | $recovery_codes_url = route('recovery_codes.show'); |
||
90 | } elseif (WhichPortal::isManagerPortal()) { |
||
91 | $recovery_codes_url = route('manager.recovery_codes.show'); |
||
92 | } elseif (WhichPortal::isAdminPortal()) { |
||
93 | $recovery_codes_url = route('admin.recovery_codes.show'); |
||
94 | } |
||
95 | |||
96 | return redirect($recovery_codes_url); |
||
97 | } else { |
||
98 | $activation_url = ''; |
||
99 | if (WhichPortal::isApplicantPortal()) { |
||
100 | $activation_url = route('two_factor.activate'); |
||
101 | } elseif (WhichPortal::isManagerPortal()) { |
||
102 | $activation_url = route('manager.two_factor.activate'); |
||
103 | } elseif (WhichPortal::isAdminPortal()) { |
||
104 | $activation_url = backpack_url('admin.two_factor.activate'); |
||
105 | } |
||
106 | |||
107 | return redirect($activation_url) |
||
108 | ->withErrors(['otp' => Lang::get('two_factor.activation_otp_error')]); |
||
109 | } |
||
110 | } |
||
111 | |||
112 | public function redirectToExpected(Request $request) |
||
121 | } |
||
122 | |||
123 | protected function rememberDevice(Request $request) |
||
136 | ); |
||
137 | } |
||
138 | } |
||
139 | |||
140 | protected function forget(Request $request) |
||
147 |