-
-
Notifications
You must be signed in to change notification settings - Fork 623
Expand file tree
/
Copy pathResetPasswordTest.php
More file actions
167 lines (141 loc) · 5.09 KB
/
ResetPasswordTest.php
File metadata and controls
167 lines (141 loc) · 5.09 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
<?php
namespace Tests\Auth;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Password;
use PHPUnit\Framework\Attributes\DataProvider;
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\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()
{
return [
'cp' => ['cp'],
'web' => ['web'],
];
}
private function resetUrl($type)
{
return match ($type) {
'cp' => cp_route('password.reset.action'),
'web' => route('statamic.password.reset.action'),
};
}
private function defaultRedirectUrl($type)
{
return match ($type) {
'cp' => cp_route('login'),
'web' => route('statamic.site'),
};
}
private function createUser()
{
return tap(User::make()->makeSuper()->email('san@holo.com')->password('secret'))->save();
}
private function enableTwoFactor($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;
}
private function addPasskey($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 createToken($user, $type)
{
$broker = config('statamic.users.passwords.'.PasswordReset::BROKER_RESETS);
if (is_array($broker)) {
$broker = $broker[$type];
}
return Password::broker($broker)->createToken($user);
}
#[Test]
#[DataProvider('resetPasswordProvider')]
public function it_resets_the_password_and_user_is_not_authenticated($type)
{
$user = $this->createUser();
$token = $this->createToken($user, $type);
$this
->assertGuest()
->post($this->resetUrl($type), [
'token' => $token,
'email' => 'san@holo.com',
'password' => 'newpassword',
'password_confirmation' => 'newpassword',
])
->assertSessionHas('status')
->assertRedirect($this->defaultRedirectUrl($type));
$this->assertGuest();
$this->assertTrue(Hash::check('newpassword', $user->fresh()->password()));
}
#[Test]
#[DataProvider('resetPasswordProvider')]
public function it_resets_password_for_two_factor_user_and_user_is_not_authenticated($type)
{
$user = $this->enableTwoFactor($this->createUser());
$token = $this->createToken($user, $type);
$this
->assertGuest()
->post($this->resetUrl($type), [
'token' => $token,
'email' => 'san@holo.com',
'password' => 'newpassword',
'password_confirmation' => 'newpassword',
])
->assertSessionHas('status')
->assertRedirect($this->defaultRedirectUrl($type));
$this->assertGuest();
$this->assertTrue(Hash::check('newpassword', $user->fresh()->password()));
}
#[Test]
#[DataProvider('resetPasswordProvider')]
public function it_resets_password_for_passkey_user_and_user_is_not_authenticated($type)
{
config(['statamic.webauthn.allow_password_login_with_passkey' => false]);
$user = $this->addPasskey($this->createUser());
$token = $this->createToken($user, $type);
$this
->assertGuest()
->post($this->resetUrl($type), [
'token' => $token,
'email' => 'san@holo.com',
'password' => 'newpassword',
'password_confirmation' => 'newpassword',
])
->assertSessionHas('status')
->assertRedirect($this->defaultRedirectUrl($type));
$this->assertGuest();
$this->assertTrue(Hash::check('newpassword', $user->fresh()->password()));
}
}