Skip to content

Commit eb622f0

Browse files
authored
[2FA] Change to use timestamp for confirming 2fa (#359)
* Change to use timestamp for confirming 2fa * Fix nullable column * Fix nullable column in tests * Add unsaved changes
1 parent 37f1216 commit eb622f0

6 files changed

+13
-13
lines changed

database/migrations/2014_10_12_200000_add_two_factor_columns_to_users_table.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ public function up()
2424
->nullable();
2525

2626
if (Fortify::confirmsTwoFactorAuthentication()) {
27-
$table->boolean('two_factor_confirmed')
27+
$table->timestamp('two_factor_confirmed_at')
2828
->after('two_factor_recovery_codes')
29-
->default(false);
29+
->nullable();
3030
}
3131
});
3232
}
@@ -43,7 +43,7 @@ public function down()
4343
'two_factor_secret',
4444
'two_factor_recovery_codes',
4545
] + Fortify::confirmsTwoFactorAuthentication() ? [
46-
'two_factor_confirmed',
46+
'two_factor_confirmed_at',
4747
] : []);
4848
});
4949
}

src/Actions/ConfirmTwoFactorAuthentication.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function __invoke($user, $code)
4343
}
4444

4545
$user->forceFill([
46-
'two_factor_confirmed' => true,
46+
'two_factor_confirmed_at' => now(),
4747
])->save();
4848

4949
TwoFactorAuthenticationConfirmed::dispatch($user);

src/Actions/DisableTwoFactorAuthentication.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function __invoke($user)
1919
'two_factor_secret' => null,
2020
'two_factor_recovery_codes' => null,
2121
] + (Fortify::confirmsTwoFactorAuthentication() ? [
22-
'two_factor_confirmed' => false,
22+
'two_factor_confirmed_at' => null,
2323
] : []))->save();
2424

2525
TwoFactorAuthenticationDisabled::dispatch($user);

src/Actions/RedirectIfTwoFactorAuthenticatable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function handle($request, $next)
5252

5353
if (Fortify::confirmsTwoFactorAuthentication()) {
5454
if (optional($user)->two_factor_secret &&
55-
optional($user)->two_factor_confirmed &&
55+
! is_null(optional($user)->two_factor_confirmed_at) &&
5656
in_array(TwoFactorAuthenticatable::class, class_uses_recursive($user))) {
5757
return $this->twoFactorChallengeResponse($request, $user);
5858
} else {

tests/AuthenticatedSessionControllerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,15 +124,15 @@ public function test_user_is_redirected_to_challenge_when_using_two_factor_authe
124124

125125
Schema::table('users', function ($table) {
126126
$table->text('two_factor_secret')->nullable();
127-
$table->boolean('two_factor_confirmed')->default(true);
127+
$table->timestamp('two_factor_confirmed_at')->nullable();
128128
});
129129

130130
TestTwoFactorAuthenticationSessionUser::forceCreate([
131131
'name' => 'Taylor Otwell',
132132
'email' => '[email protected]',
133133
'password' => bcrypt('secret'),
134134
'two_factor_secret' => 'test-secret',
135-
'two_factor_confirmed' => true,
135+
'two_factor_confirmed_at' => now(),
136136
]);
137137

138138
$response = $this->withoutExceptionHandling()->post('/login', [

tests/TwoFactorAuthenticationControllerTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function test_two_factor_authentication_can_be_enabled()
3939

4040
$this->assertNotNull($user->two_factor_secret);
4141
$this->assertNotNull($user->two_factor_recovery_codes);
42-
$this->assertEquals(0, $user->two_factor_confirmed);
42+
$this->assertNull($user->two_factor_confirmed_at);
4343
$this->assertIsArray(json_decode(decrypt($user->two_factor_recovery_codes), true));
4444
$this->assertNotNull($user->twoFactorQrCodeSvg());
4545
}
@@ -64,7 +64,7 @@ public function test_two_factor_authentication_can_be_confirmed()
6464
'email' => '[email protected]',
6565
'password' => bcrypt('secret'),
6666
'two_factor_secret' => encrypt($userSecret),
67-
'two_factor_confirmed' => false,
67+
'two_factor_confirmed_at' => null,
6868
]);
6969

7070
$response = $this->withoutExceptionHandling()->actingAs($user)->postJson(
@@ -77,7 +77,7 @@ public function test_two_factor_authentication_can_be_confirmed()
7777

7878
$user = $user->fresh();
7979

80-
$this->assertEquals(1, $user->two_factor_confirmed);
80+
$this->assertNotNull($user->two_factor_confirmed_at);
8181
}
8282

8383
public function test_two_factor_authentication_can_not_be_confirmed_with_invalid_code()
@@ -99,7 +99,7 @@ public function test_two_factor_authentication_can_not_be_confirmed_with_invalid
9999
'email' => '[email protected]',
100100
'password' => bcrypt('secret'),
101101
'two_factor_secret' => encrypt($userSecret),
102-
'two_factor_confirmed' => false,
102+
'two_factor_confirmed_at' => null,
103103
]);
104104

105105
$response = $this->withExceptionHandling()->actingAs($user)->postJson(
@@ -112,7 +112,7 @@ public function test_two_factor_authentication_can_not_be_confirmed_with_invalid
112112

113113
$user = $user->fresh();
114114

115-
$this->assertEquals(0, $user->two_factor_confirmed);
115+
$this->assertNull($user->two_factor_confirmed_at);
116116
}
117117

118118
public function test_two_factor_authentication_can_be_disabled()

0 commit comments

Comments
 (0)