-
Notifications
You must be signed in to change notification settings - Fork 308
/
Copy pathAuthenticatedSessionControllerWithTwoFactorTest.php
295 lines (237 loc) · 10.2 KB
/
AuthenticatedSessionControllerWithTwoFactorTest.php
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
<?php
namespace Laravel\Fortify\Tests;
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Hash;
use Laravel\Fortify\Events\TwoFactorAuthenticationChallenged;
use Laravel\Fortify\Events\TwoFactorAuthenticationFailed;
use Laravel\Fortify\Events\ValidTwoFactorAuthenticationCodeProvided;
use Laravel\Fortify\Features;
use Laravel\Fortify\Tests\Models\UserWithTwoFactor;
use Orchestra\Testbench\Attributes\DefineEnvironment;
use Orchestra\Testbench\Attributes\WithConfig;
use Orchestra\Testbench\Attributes\WithMigration;
use PragmaRX\Google2FA\Google2FA;
#[WithMigration]
#[DefineEnvironment('withTwoFactorAuthentication')]
#[WithConfig('auth.providers.users.model', UserWithTwoFactor::class)]
class AuthenticatedSessionControllerWithTwoFactorTest extends OrchestraTestCase
{
use RefreshDatabase;
public function test_user_is_redirected_to_challenge_when_using_two_factor_authentication()
{
Event::fake();
UserWithTwoFactor::forceCreate([
'name' => 'Taylor Otwell',
'email' => '[email protected]',
'password' => bcrypt('secret'),
'two_factor_secret' => 'test-secret',
]);
$response = $this->withoutExceptionHandling()->post('/login', [
'email' => '[email protected]',
'password' => 'secret',
]);
$response->assertRedirect('/two-factor-challenge');
Event::assertDispatched(TwoFactorAuthenticationChallenged::class);
}
#[DefineEnvironment('withConfirmedTwoFactorAuthentication')]
public function test_user_is_not_redirected_to_challenge_when_using_two_factor_authentication_that_has_not_been_confirmed_and_confirmation_is_enabled()
{
Event::fake();
UserWithTwoFactor::forceCreate([
'name' => 'Taylor Otwell',
'email' => '[email protected]',
'password' => bcrypt('secret'),
'two_factor_secret' => 'test-secret',
]);
$response = $this->withoutExceptionHandling()->post('/login', [
'email' => '[email protected]',
'password' => 'secret',
]);
$response->assertRedirect('/home');
}
#[DefineEnvironment('withConfirmedTwoFactorAuthentication')]
public function test_user_is_redirected_to_challenge_when_using_two_factor_authentication_that_has_been_confirmed_and_confirmation_is_enabled()
{
Event::fake();
UserWithTwoFactor::forceCreate([
'name' => 'Taylor Otwell',
'email' => '[email protected]',
'password' => bcrypt('secret'),
'two_factor_secret' => 'test-secret',
'two_factor_confirmed_at' => now(),
]);
$response = $this->withoutExceptionHandling()->post('/login', [
'email' => '[email protected]',
'password' => 'secret',
]);
$response->assertRedirect('/two-factor-challenge');
}
#[DefineEnvironment('withoutTwoFactorAuthentication')]
public function test_user_can_authenticate_when_two_factor_challenge_is_disabled()
{
UserWithTwoFactor::forceCreate([
'name' => 'Taylor Otwell',
'email' => '[email protected]',
'password' => bcrypt('secret'),
'two_factor_secret' => 'test-secret',
]);
$response = $this->withoutExceptionHandling()->post('/login', [
'email' => '[email protected]',
'password' => 'secret',
]);
$response->assertRedirect('/home');
}
public function test_rehash_user_password_when_redirecting_to_two_factor_challenge_if_rehashing_on_login_is_enabled()
{
if (version_compare(Application::VERSION, '11.0.0', '<')) {
$this->markTestSkipped('Only on Laravel 11 and later');
}
$this->app['config']->set('hashing.rehash_on_login', true);
$user = UserWithTwoFactor::forceCreate([
'name' => 'Taylor Otwell',
'email' => '[email protected]',
'password' => Hash::make('secret', ['rounds' => 6]),
'two_factor_secret' => 'test-secret',
]);
$response = $this->withoutExceptionHandling()->post('/login', [
'email' => '[email protected]',
'password' => 'secret',
]);
$response->assertRedirect('/two-factor-challenge');
$this->assertNotSame($user->password, $user->fresh()->password);
$this->assertTrue(Hash::check('secret', $user->fresh()->password));
}
public function test_does_not_rehash_user_password_when_redirecting_to_two_factor_challenge_if_rehashing_on_login_is_disabled()
{
if (version_compare(Application::VERSION, '11.0.0', '<')) {
$this->markTestSkipped('Only on Laravel 11 and later');
}
$this->app['config']->set('hashing.rehash_on_login', false);
$user = UserWithTwoFactor::forceCreate([
'name' => 'Taylor Otwell',
'email' => '[email protected]',
'password' => Hash::make('secret', ['rounds' => 6]),
'two_factor_secret' => 'test-secret',
]);
$response = $this->withoutExceptionHandling()->post('/login', [
'email' => '[email protected]',
'password' => 'secret',
]);
$response->assertRedirect('/two-factor-challenge');
$this->assertSame($user->password, $user->fresh()->password);
}
public function test_two_factor_challenge_can_be_passed_via_code()
{
Event::fake();
$tfaEngine = app(Google2FA::class);
$userSecret = $tfaEngine->generateSecretKey();
$validOtp = $tfaEngine->getCurrentOtp($userSecret);
$user = UserWithTwoFactor::forceCreate([
'name' => 'Taylor Otwell',
'email' => '[email protected]',
'password' => bcrypt('secret'),
'two_factor_secret' => encrypt($userSecret),
]);
$response = $this->withSession([
'login.id' => $user->id,
'login.remember' => false,
])->withoutExceptionHandling()->post('/two-factor-challenge', [
'code' => $validOtp,
]);
Event::assertDispatched(ValidTwoFactorAuthenticationCodeProvided::class);
$response->assertRedirect('/home')
->assertSessionMissing('login.id');
}
public function test_two_factor_authentication_preserves_remember_me_selection(): void
{
Event::fake();
UserWithTwoFactor::forceCreate([
'name' => 'Taylor Otwell',
'email' => '[email protected]',
'password' => bcrypt('secret'),
'two_factor_secret' => 'test-secret',
]);
$response = $this->withoutExceptionHandling()->post('/login', [
'email' => '[email protected]',
'password' => 'secret',
'remember' => false,
]);
$response->assertRedirect('/two-factor-challenge')
->assertSessionHas('login.remember', false);
}
public function test_two_factor_challenge_fails_for_old_otp_and_zero_window()
{
Event::fake();
// Setting window to 0 should mean any old OTP is instantly invalid
Features::twoFactorAuthentication(['window' => 0]);
$tfaEngine = app(Google2FA::class);
$userSecret = $tfaEngine->generateSecretKey();
$currentTs = $tfaEngine->getTimestamp();
$previousOtp = $tfaEngine->oathTotp($userSecret, $currentTs - 1);
$user = UserWithTwoFactor::forceCreate([
'name' => 'Taylor Otwell',
'email' => '[email protected]',
'password' => bcrypt('secret'),
'two_factor_secret' => encrypt($userSecret),
]);
$response = $this->withSession([
'login.id' => $user->id,
'login.remember' => false,
])->withoutExceptionHandling()->post('/two-factor-challenge', [
'code' => $previousOtp,
]);
Event::assertDispatched(TwoFactorAuthenticationFailed::class);
$response->assertRedirect('/two-factor-challenge')
->assertSessionHas('login.id')
->assertSessionHasErrors(['code']);
}
public function test_two_factor_challenge_can_be_passed_via_recovery_code()
{
Event::fake();
$user = UserWithTwoFactor::forceCreate([
'name' => 'Taylor Otwell',
'email' => '[email protected]',
'password' => bcrypt('secret'),
'two_factor_recovery_codes' => encrypt(json_encode(['invalid-code', 'valid-code'])),
]);
$response = $this->withSession([
'login.id' => $user->id,
'login.remember' => false,
])->withoutExceptionHandling()->post('/two-factor-challenge', [
'recovery_code' => 'valid-code',
]);
Event::assertDispatched(ValidTwoFactorAuthenticationCodeProvided::class);
$response->assertRedirect('/home')
->assertSessionMissing('login.id');
$this->assertNotNull(Auth::getUser());
$this->assertNotContains('valid-code', json_decode(decrypt($user->fresh()->two_factor_recovery_codes), true));
}
public function test_two_factor_challenge_can_fail_via_recovery_code()
{
$user = UserWithTwoFactor::forceCreate([
'name' => 'Taylor Otwell',
'email' => '[email protected]',
'password' => bcrypt('secret'),
'two_factor_recovery_codes' => encrypt(json_encode(['invalid-code', 'valid-code'])),
]);
$response = $this->withSession([
'login.id' => $user->id,
'login.remember' => false,
])->withoutExceptionHandling()->post('/two-factor-challenge', [
'recovery_code' => 'missing-code',
]);
$response->assertRedirect('/two-factor-challenge')
->assertSessionHas('login.id')
->assertSessionHasErrors(['recovery_code']);
$this->assertNull(Auth::getUser());
}
public function test_two_factor_challenge_requires_a_challenged_user()
{
$response = $this->withSession([])->withoutExceptionHandling()->get('/two-factor-challenge');
$response->assertRedirect('/login');
$this->assertNull(Auth::getUser());
}
}