-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwoFactorAuthentication.php
More file actions
298 lines (246 loc) · 9.54 KB
/
Copy pathTwoFactorAuthentication.php
File metadata and controls
298 lines (246 loc) · 9.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<?php
declare(strict_types=1);
namespace NyonCode\WireModuleUsers\Livewire;
use Illuminate\Contracts\View\View;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;
use Livewire\Component;
use NyonCode\WireCore\Core\Plugin\Contracts\IdentifiesHookTarget;
use NyonCode\WireCore\Foundation\Concerns\InteractsWithPasswordConfirmation;
use NyonCode\WireCore\Notifications\NotificationManager;
use NyonCode\WireForms\Components\OtpInput;
use NyonCode\WireForms\Forms\Form;
use NyonCode\WireModuleUsers\Support\TwoFactor;
use Throwable;
/**
* The two-factor card: a screen over Fortify, never a second implementation.
*
* Every button here resolves one of Fortify's own actions out of the container
* and calls it. Nothing in this class generates a secret, formats an
* `otpauth://` URI, checks a code against a time window or writes a recovery
* code — all of that is Fortify's, along with the responsibility for getting it
* right. What was missing was somewhere to press the buttons, which is what an
* admin panel is for.
*
* The three states this card can be in are worth naming, because they are not
* two:
*
* - **off** — no secret. One button: turn it on.
* - **pending** — a secret exists and has not been confirmed. This is where a
* QR code and a code box belong, and it is also the state a user is left in
* by opening the panel and closing the tab. It must be possible to leave it
* in both directions.
* - **on** — confirmed. Recovery codes, and a way off.
*
* A panel that modelled this as a boolean would strand every user in the middle
* one, protected by nothing while the badge says they are safe.
*/
class TwoFactorAuthentication extends Component implements IdentifiesHookTarget
{
/**
* Every state change and every reveal on this card sits behind a recently
* confirmed password, because Fortify's own routes for these actions do.
* Driving its actions out of the container reaches them around that guard,
* so the guard is re-stated here rather than lost. See the trait.
*/
use InteractsWithPasswordConfirmation;
/** The code typed in to finish the setup. */
public string $code = '';
/** Whether the recovery codes are on screen. Never on by default. */
public bool $showingRecoveryCodes = false;
/**
* The box the setup code is typed into — the same field as the challenge.
*
* It used to be a hand-written `<input autocomplete="one-time-code">`, three
* clicks away from the screen on the way in that draws six boxes for the
* same six digits (ADR 0037 §6). One field, one behaviour: the boxes advance
* themselves, a pasted code fills the row, and the value still arrives in
* `$code` because with no state path the binding is this component's own
* property.
*/
public function mount(): void
{
// The card links to the confirmation screen when the password is stale;
// this is what brings the person back to it afterwards.
$this->returnHereAfterPasswordConfirmation();
}
public function codeField(): Form
{
return Form::make()->schema([
OtpInput::make('code')
->label(__('wire-module-users::messages.two_factor_code'))
->length(6)
->numericOnly(),
]);
}
public function enable(): void
{
if (! $this->ensurePasswordConfirmed()) {
return;
}
if (! $this->run(TwoFactor::ENABLE_ACTION)) {
return;
}
$this->showingRecoveryCodes = false;
// No success notification: the card has just grown a QR code and a code
// box, and "Two-factor enabled" over the top of a setup that is not
// finished is exactly the lie the pending state exists to prevent.
}
public function confirm(): void
{
if (! $this->ensurePasswordConfirmed()) {
return;
}
$user = $this->user();
if ($user === null) {
return;
}
try {
/** @var object{__invoke: callable} $action */
$action = app(TwoFactor::CONFIRM_ACTION);
$action($user, $this->code);
} catch (Throwable $e) {
// Fortify throws a ValidationException for a wrong code, which
// Livewire turns into an error bag on `code` — that is the message
// the user should read, so it is re-thrown rather than swallowed.
$this->code = '';
throw $e;
}
$this->code = '';
$this->showingRecoveryCodes = true;
NotificationManager::success(__('wire-module-users::messages.two_factor_enabled'));
}
public function disable(): void
{
if (! $this->ensurePasswordConfirmed()) {
return;
}
if (! $this->run(TwoFactor::DISABLE_ACTION)) {
return;
}
$this->code = '';
$this->showingRecoveryCodes = false;
NotificationManager::success(__('wire-module-users::messages.two_factor_disabled'));
}
public function regenerateRecoveryCodes(): void
{
if (! $this->ensurePasswordConfirmed()) {
return;
}
if (! $this->run(TwoFactor::GENERATE_CODES_ACTION)) {
return;
}
$this->showingRecoveryCodes = true;
NotificationManager::success(__('wire-module-users::messages.recovery_codes_regenerated'));
}
public function toggleRecoveryCodes(): void
{
if (! $this->ensurePasswordConfirmed()) {
return;
}
$this->showingRecoveryCodes = ! $this->showingRecoveryCodes;
}
/** The QR code Fortify renders, as inline SVG, or null when there is nothing to scan. */
public function qrCodeSvg(): ?string
{
$user = $this->user();
if ($user === null || ! method_exists($user, 'twoFactorQrCodeSvg') || ! TwoFactor::pending($user)) {
return null;
}
try {
return (string) $user->twoFactorQrCodeSvg();
} catch (Throwable) {
return null;
}
}
/**
* The secret in the form somebody types into an authenticator by hand.
*
* Offered beside the QR code rather than instead of it: a phone that cannot
* scan — a desktop authenticator, a locked-down camera — is common enough
* that a setup screen with only a picture on it is a setup screen some people
* cannot finish.
*/
public function setupKey(): ?string
{
$user = $this->user();
// Read on every render, so it asks rather than redirects: a stale window
// hides the secret and the card says why, instead of bouncing somebody
// off a page they only opened to look at.
if ($user === null || ! TwoFactor::pending($user) || ! $this->hasConfirmedPasswordRecently()) {
return null;
}
try {
// The column, not a method: Fortify exposes the secret as an
// encrypted attribute and offers no accessor for the plain one.
return (string) decrypt((string) $user->getAttribute('two_factor_secret'));
} catch (Throwable) {
return null;
}
}
/**
* The recovery codes, or an empty list when there are none to show.
*
* @return array<int, string>
*/
public function recoveryCodes(): array
{
$user = $this->user();
if ($user === null || ! $this->showingRecoveryCodes || ! $this->hasConfirmedPasswordRecently()) {
return [];
}
if ($user->getAttribute('two_factor_recovery_codes') === null) {
return [];
}
try {
$codes = json_decode(decrypt((string) $user->getAttribute('two_factor_recovery_codes')), true);
return is_array($codes) ? array_values(array_filter($codes, 'is_string')) : [];
} catch (Throwable) {
return [];
}
}
public function hookKey(): ?string
{
return 'users.two-factor';
}
public function render(): View
{
$user = $this->user();
return view('wire-module-users::livewire.two-factor', [
'confirmed' => TwoFactor::confirmed($user),
'pending' => TwoFactor::pending($user) && ! TwoFactor::confirmed($user),
'qrCode' => $this->qrCodeSvg(),
'setupKey' => $this->setupKey(),
'codes' => $this->recoveryCodes(),
// The card explains a stale window rather than silently missing a QR.
'needsPasswordConfirmation' => ! $this->hasConfirmedPasswordRecently(),
'passwordConfirmationUrl' => $this->passwordConfirmationUrl(),
]);
}
/**
* Resolve one of Fortify's actions and run it against the signed-in user.
*
* False means there was nobody to run it for, or no Fortify to run — both of
* which are states this card can be rendered in and neither of which is an
* error worth an exception on a profile page.
*/
protected function run(string $action): bool
{
$user = $this->user();
if ($user === null || ! class_exists($action)) {
return false;
}
/** @var object{__invoke: callable} $resolved */
$resolved = app($action);
$resolved($user);
// The action wrote columns straight to the database; this instance is
// the one the view is about to read.
$user->refresh();
return true;
}
protected function user(): ?Model
{
$user = Auth::user();
return $user instanceof Model ? $user : null;
}
}