1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* NOTICE OF LICENSE |
5
|
|
|
* |
6
|
|
|
* Part of the Rinvex Fort Package. |
7
|
|
|
* |
8
|
|
|
* This source file is subject to The MIT License (MIT) |
9
|
|
|
* that is bundled with this package in the LICENSE file. |
10
|
|
|
* |
11
|
|
|
* Package: Rinvex Fort Package |
12
|
|
|
* License: The MIT License (MIT) |
13
|
|
|
* Link: https://rinvex.com |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace Rinvex\Fort\Http\Controllers\Frontend; |
17
|
|
|
|
18
|
|
|
use Carbon\Carbon; |
19
|
|
|
use Illuminate\Support\MessageBag; |
20
|
|
|
use Illuminate\Support\Facades\Auth; |
21
|
|
|
use Illuminate\Support\ViewErrorBag; |
22
|
|
|
use Rinvex\Fort\Services\TwoFactorTotpProvider; |
23
|
|
|
use Rinvex\Fort\Contracts\UserRepositoryContract; |
24
|
|
|
use Rinvex\Fort\Http\Requests\Frontend\TwoFactorTotp; |
25
|
|
|
use Rinvex\Fort\Http\Requests\Frontend\TwoFactorPhone; |
26
|
|
|
use Rinvex\Fort\Http\Controllers\AuthorizedController; |
27
|
|
|
|
28
|
|
|
class TwoFactorSettingsController extends AuthorizedController |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* The user repository instance. |
32
|
|
|
* |
33
|
|
|
* @var \Rinvex\Fort\Contracts\UserRepositoryContract |
34
|
|
|
*/ |
35
|
|
|
protected $userRepository; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Create a new Two-Factor update controller instance. |
39
|
|
|
* |
40
|
|
|
* @param \Rinvex\Fort\Contracts\UserRepositoryContract $userRepository |
41
|
|
|
* |
42
|
|
|
* @return void |
|
|
|
|
43
|
|
|
*/ |
44
|
|
|
public function __construct(UserRepositoryContract $userRepository) |
45
|
|
|
{ |
46
|
|
|
parent::__construct(); |
47
|
|
|
|
48
|
|
|
$this->userRepository = $userRepository; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Show the Two-Factor TOTP enable form. |
53
|
|
|
* |
54
|
|
|
* @param \Rinvex\Fort\Http\Requests\Frontend\TwoFactorTotp $request |
55
|
|
|
* @param \Rinvex\Fort\Services\TwoFactorTotpProvider $totpProvider |
56
|
|
|
* |
57
|
|
|
* @return \Illuminate\Http\Response |
|
|
|
|
58
|
|
|
*/ |
59
|
|
|
public function enableTotp(TwoFactorTotp $request, TwoFactorTotpProvider $totpProvider) |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
$currentUser = $this->currentUser(); |
62
|
|
|
$settings = $currentUser->getTwoFactor(); |
63
|
|
|
|
64
|
|
|
if (array_get($settings, 'totp.enabled') && ! session()->get('rinvex.fort.alert.success') && ! session()->get('errors')) { |
|
|
|
|
65
|
|
|
$messageBag = new MessageBag([trans('rinvex.fort::frontend/messages.verification.twofactor.totp.already')]); |
66
|
|
|
$errors = (new ViewErrorBag())->put('default', $messageBag); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if (! $secret = array_get($settings, 'totp.secret')) { |
70
|
|
|
array_set($settings, 'totp.enabled', false); |
71
|
|
|
array_set($settings, 'totp.secret', $secret = $totpProvider->generateSecretKey()); |
72
|
|
|
|
73
|
|
|
$this->userRepository->update($currentUser, [ |
74
|
|
|
'two_factor' => $settings, |
75
|
|
|
]); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$qrCode = $totpProvider->getQRCodeInline(config('rinvex.fort.twofactor.issuer'), $currentUser->email, $secret); |
|
|
|
|
79
|
|
|
|
80
|
|
|
return view('rinvex.fort::frontend.user.twofactor', compact('secret', 'qrCode', 'settings', 'errors')); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Process the Two-Factor TOTP enable form. |
85
|
|
|
* |
86
|
|
|
* @param \Rinvex\Fort\Http\Requests\Frontend\TwoFactorTotp $request |
87
|
|
|
* @param \Rinvex\Fort\Services\TwoFactorTotpProvider $totpProvider |
88
|
|
|
* |
89
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
90
|
|
|
*/ |
91
|
|
|
public function updateTotp(TwoFactorTotp $request, TwoFactorTotpProvider $totpProvider) |
92
|
|
|
{ |
93
|
|
|
$currentUser = $this->currentUser(); |
94
|
|
|
$settings = $currentUser->getTwoFactor(); |
95
|
|
|
$secret = array_get($settings, 'totp.secret'); |
96
|
|
|
$backup = array_get($settings, 'totp.backup'); |
97
|
|
|
$backupAt = array_get($settings, 'totp.backup_at'); |
98
|
|
|
|
99
|
|
|
if ($totpProvider->verifyKey($secret, $request->get('token'))) { |
100
|
|
|
array_set($settings, 'totp.enabled', true); |
101
|
|
|
array_set($settings, 'totp.secret', $secret); |
102
|
|
|
array_set($settings, 'totp.backup', $backup ?: $this->generateTotpBackups()); |
103
|
|
|
array_set($settings, 'totp.backup_at', $backupAt ?: (new Carbon())->toDateTimeString()); |
104
|
|
|
|
105
|
|
|
// Update Two-Factor settings |
106
|
|
|
$this->userRepository->update($currentUser, [ |
107
|
|
|
'two_factor' => $settings, |
108
|
|
|
]); |
109
|
|
|
|
110
|
|
|
return intend([ |
111
|
|
|
'back' => true, |
112
|
|
|
'with' => ['rinvex.fort.alert.success' => trans('rinvex.fort::frontend/messages.verification.twofactor.totp.enabled')], |
|
|
|
|
113
|
|
|
]); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return intend([ |
117
|
|
|
'back' => true, |
118
|
|
|
'withErrors' => ['token' => trans('rinvex.fort::frontend/messages.verification.twofactor.totp.invalid_token')], |
|
|
|
|
119
|
|
|
]); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Process the Two-Factor TOTP disable. |
124
|
|
|
* |
125
|
|
|
* @param \Rinvex\Fort\Http\Requests\Frontend\TwoFactorTotp $request |
126
|
|
|
* |
127
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
128
|
|
|
*/ |
129
|
|
|
public function disableTotp(TwoFactorTotp $request) |
|
|
|
|
130
|
|
|
{ |
131
|
|
|
$currentUser = $this->currentUser(); |
132
|
|
|
$settings = $currentUser->getTwoFactor(); |
133
|
|
|
|
134
|
|
|
array_set($settings, 'totp', []); |
135
|
|
|
|
136
|
|
|
$this->userRepository->update($currentUser, [ |
137
|
|
|
'two_factor' => $settings, |
138
|
|
|
]); |
139
|
|
|
|
140
|
|
|
return intend([ |
141
|
|
|
'intended' => route('rinvex.fort.frontend.user.settings'), |
142
|
|
|
'with' => ['rinvex.fort.alert.success' => trans('rinvex.fort::frontend/messages.verification.twofactor.totp.disabled')], |
|
|
|
|
143
|
|
|
]); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Process the Two-Factor Phone enable. |
148
|
|
|
* |
149
|
|
|
* @param \Rinvex\Fort\Http\Requests\Frontend\TwoFactorPhone $request |
150
|
|
|
* |
151
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
152
|
|
|
*/ |
153
|
|
|
public function enablePhone(TwoFactorPhone $request) |
|
|
|
|
154
|
|
|
{ |
155
|
|
|
$currentUser = $this->currentUser(); |
156
|
|
|
|
157
|
|
|
if (! $currentUser->phone || ! $currentUser->phone_verified) { |
|
|
|
|
158
|
|
|
return intend([ |
159
|
|
|
'intended' => route('rinvex.fort.frontend.user.settings'), |
160
|
|
|
'withErrors' => ['phone' => trans('rinvex.fort::frontend/messages.account.phone_required')], |
161
|
|
|
]); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
$settings = $currentUser->getTwoFactor(); |
165
|
|
|
|
166
|
|
|
array_set($settings, 'phone.enabled', true); |
167
|
|
|
|
168
|
|
|
$this->userRepository->update($currentUser, [ |
169
|
|
|
'two_factor' => $settings, |
170
|
|
|
]); |
171
|
|
|
|
172
|
|
|
return intend([ |
173
|
|
|
'intended' => route('rinvex.fort.frontend.user.settings'), |
174
|
|
|
'with' => ['rinvex.fort.alert.success' => trans('rinvex.fort::frontend/messages.verification.twofactor.phone.enabled')], |
|
|
|
|
175
|
|
|
]); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Process the Two-Factor Phone disable. |
180
|
|
|
* |
181
|
|
|
* @param \Rinvex\Fort\Http\Requests\Frontend\TwoFactorPhone $request |
182
|
|
|
* |
183
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
184
|
|
|
*/ |
185
|
|
|
public function disablePhone(TwoFactorPhone $request) |
|
|
|
|
186
|
|
|
{ |
187
|
|
|
$currentUser = $this->currentUser(); |
188
|
|
|
$settings = $currentUser->getTwoFactor(); |
189
|
|
|
|
190
|
|
|
array_set($settings, 'phone.enabled', false); |
191
|
|
|
|
192
|
|
|
$this->userRepository->update($currentUser, [ |
193
|
|
|
'two_factor' => $settings, |
194
|
|
|
]); |
195
|
|
|
|
196
|
|
|
return intend([ |
197
|
|
|
'intended' => route('rinvex.fort.frontend.user.settings'), |
198
|
|
|
'with' => ['rinvex.fort.alert.success' => trans('rinvex.fort::frontend/messages.verification.twofactor.phone.disabled')], |
|
|
|
|
199
|
|
|
]); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Process the Two-Factor OTP backup. |
204
|
|
|
* |
205
|
|
|
* @param \Rinvex\Fort\Http\Requests\Frontend\TwoFactorTotp $request |
206
|
|
|
* |
207
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
208
|
|
|
*/ |
209
|
|
|
public function backupTotp(TwoFactorTotp $request) |
|
|
|
|
210
|
|
|
{ |
211
|
|
|
$currentUser = $this->currentUser(); |
212
|
|
|
$settings = $currentUser->getTwoFactor(); |
213
|
|
|
|
214
|
|
|
if (! array_get($settings, 'totp.enabled')) { |
215
|
|
|
return intend([ |
216
|
|
|
'intended' => route('rinvex.fort.frontend.user.settings'), |
217
|
|
|
'withErrors' => ['rinvex.fort.verification.twofactor.totp.cant_backup' => trans('rinvex.fort::frontend/messages.verification.twofactor.totp.cant_backup')], |
|
|
|
|
218
|
|
|
]); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
array_set($settings, 'totp.backup', $this->generateTotpBackups()); |
222
|
|
|
array_set($settings, 'totp.backup_at', (new Carbon())->toDateTimeString()); |
223
|
|
|
|
224
|
|
|
$this->userRepository->update($currentUser, [ |
225
|
|
|
'two_factor' => $settings, |
226
|
|
|
]); |
227
|
|
|
|
228
|
|
|
return intend([ |
229
|
|
|
'back' => true, |
230
|
|
|
'with' => ['rinvex.fort.alert.success' => trans('rinvex.fort::frontend/messages.verification.twofactor.totp.rebackup')], |
|
|
|
|
231
|
|
|
]); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Generate Two-Factor OTP backup codes. |
236
|
|
|
* |
237
|
|
|
* @return array |
238
|
|
|
*/ |
239
|
|
|
protected function generateTotpBackups() |
240
|
|
|
{ |
241
|
|
|
$backup = []; |
242
|
|
|
|
243
|
|
|
for ($x = 0; $x <= 9; $x++) { |
244
|
|
|
$backup[] = str_pad(random_int(0, 9999999999), 10, 0, STR_PAD_BOTH); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
return $backup; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Get current user. |
252
|
|
|
* |
253
|
|
|
* @return \Rinvex\Fort\Contracts\AuthenticatableContract |
254
|
|
|
*/ |
255
|
|
|
protected function currentUser() |
256
|
|
|
{ |
257
|
|
|
return Auth::guard($this->getGuard())->user(); |
258
|
|
|
} |
259
|
|
|
} |
260
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.