-
-
Notifications
You must be signed in to change notification settings - Fork 625
Expand file tree
/
Copy pathResetPasswordTest.php
More file actions
244 lines (204 loc) · 7.54 KB
/
ResetPasswordTest.php
File metadata and controls
244 lines (204 loc) · 7.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
<?php
namespace Tests\Auth;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Password;
use Orchestra\Testbench\Attributes\DefineEnvironment;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\Test;
use Statamic\Auth\File\Passkey;
use Statamic\Auth\Passwords\PasswordReset;
use Statamic\Auth\TwoFactor\RecoveryCode;
use Statamic\Contracts\Auth\TwoFactor\TwoFactorAuthenticationProvider;
use Statamic\Events\TwoFactorAuthenticationChallenged;
use Statamic\Facades\User;
use Symfony\Component\Uid\Uuid;
use Tests\PreventSavingStacheItemsToDisk;
use Tests\TestCase;
use Webauthn\PublicKeyCredentialSource;
use Webauthn\TrustPath\EmptyTrustPath;
class ResetPasswordTest extends TestCase
{
use PreventSavingStacheItemsToDisk;
public static function resetPasswordProvider(): array
{
return [
'cp' => ['cp'],
'web' => ['web'],
];
}
private function resetUrl(string $type): string
{
return match ($type) {
'cp' => cp_route('password.reset.action'),
'web' => route('statamic.password.reset.action'),
};
}
private function twoFactorChallengeUrl(string $type): string
{
return match ($type) {
'cp' => cp_route('two-factor-challenge'),
'web' => route('statamic.two-factor-challenge'),
};
}
private function broker(string $type)
{
$broker = config('statamic.users.passwords.'.PasswordReset::BROKER_RESETS);
if (is_array($broker)) {
$broker = $broker[$type];
}
return Password::broker($broker);
}
#[Test]
#[DataProvider('resetPasswordProvider')]
public function it_resets_password_and_logs_in(string $type)
{
$user = $this->user();
$token = $this->broker($type)->createToken($user);
$this
->assertGuest()
->post($this->resetUrl($type), [
'token' => $token,
'email' => $user->email(),
'password' => 'newpassword',
'password_confirmation' => 'newpassword',
])
->assertRedirect('/');
$this->assertAuthenticatedAs($user);
$this->assertTrue(Hash::check('newpassword', $user->fresh()->password()));
}
#[Test]
#[Group('2fa')]
#[DataProvider('resetPasswordProvider')]
public function it_redirects_to_two_factor_challenge_when_user_has_two_factor_enabled(string $type)
{
Event::fake();
$user = $this->userWithTwoFactorEnabled();
$token = $this->broker($type)->createToken($user);
$this
->assertGuest()
->post($this->resetUrl($type), [
'token' => $token,
'email' => $user->email(),
'password' => 'newpassword',
'password_confirmation' => 'newpassword',
])
->assertRedirect($this->twoFactorChallengeUrl($type))
->assertSessionHas('login.id', $user->id())
->assertSessionHas('login.remember', false);
$this->assertGuest();
$this->assertTrue(Hash::check('newpassword', $user->fresh()->password()));
Event::assertDispatched(TwoFactorAuthenticationChallenged::class, fn ($event) => $event->user->id === $user->id);
}
#[Test]
#[Group('2fa')]
#[DefineEnvironment('disableTwoFactor')]
#[DataProvider('resetPasswordProvider')]
public function it_skips_two_factor_challenge_when_two_factor_is_disabled(string $type)
{
Event::fake();
$user = $this->userWithTwoFactorEnabled();
$token = $this->broker($type)->createToken($user);
$this
->assertGuest()
->post($this->resetUrl($type), [
'token' => $token,
'email' => $user->email(),
'password' => 'newpassword',
'password_confirmation' => 'newpassword',
])
->assertRedirect('/');
$this->assertAuthenticatedAs($user);
$this->assertTrue(Hash::check('newpassword', $user->fresh()->password()));
Event::assertNotDispatched(TwoFactorAuthenticationChallenged::class);
}
#[Test]
#[DefineEnvironment('disallowPasswordLoginWithPasskey')]
#[DataProvider('resetPasswordProvider')]
public function it_does_not_log_in_when_user_has_passkeys_and_password_login_is_disallowed(string $type)
{
$user = $this->userWithPasskey();
$token = $this->broker($type)->createToken($user);
$this
->assertGuest()
->post($this->resetUrl($type), [
'token' => $token,
'email' => $user->email(),
'password' => 'newpassword',
'password_confirmation' => 'newpassword',
])
->assertRedirect($this->loginUrl($type));
$this->assertGuest();
$this->assertTrue(Hash::check('newpassword', $user->fresh()->password()));
}
#[Test]
#[DataProvider('resetPasswordProvider')]
public function it_logs_in_when_user_has_passkeys_and_password_login_is_allowed(string $type)
{
$user = $this->userWithPasskey();
$token = $this->broker($type)->createToken($user);
$this
->assertGuest()
->post($this->resetUrl($type), [
'token' => $token,
'email' => $user->email(),
'password' => 'newpassword',
'password_confirmation' => 'newpassword',
])
->assertRedirect('/');
$this->assertAuthenticatedAs($user);
$this->assertTrue(Hash::check('newpassword', $user->fresh()->password()));
}
protected function disableTwoFactor($app)
{
$app['config']->set('statamic.users.two_factor_enabled', false);
}
protected function disallowPasswordLoginWithPasskey($app)
{
$app['config']->set('statamic.webauthn.allow_password_login_with_passkey', false);
}
private function loginUrl(string $type): string
{
return match ($type) {
'cp' => cp_route('login'),
'web' => route('statamic.site'),
};
}
private function user()
{
return tap(User::make()->makeSuper()->email('david@hasselhoff.com')->password('secret'))->save();
}
private function userWithPasskey()
{
$user = $this->user();
$credential = PublicKeyCredentialSource::create(
publicKeyCredentialId: 'test-credential-id',
type: 'public-key',
transports: ['usb'],
attestationType: 'none',
trustPath: new EmptyTrustPath(),
aaguid: Uuid::fromString('00000000-0000-0000-0000-000000000000'),
credentialPublicKey: 'test-public-key',
userHandle: $user->id(),
counter: 0
);
$passkey = (new Passkey)->setUser($user)->setName('Test Key')->setCredential($credential);
$passkey->save();
return $user->fresh();
}
private function userWithTwoFactorEnabled()
{
$user = $this->user();
$user->merge([
'two_factor_confirmed_at' => now()->timestamp,
'two_factor_secret' => encrypt(app(TwoFactorAuthenticationProvider::class)->generateSecretKey()),
'two_factor_recovery_codes' => encrypt(json_encode(Collection::times(8, function () {
return RecoveryCode::generate();
})->all())),
]);
$user->save();
return $user;
}
}