Skip to content

Commit 6761f04

Browse files
authored
Rehash password if required (#557)
1 parent 6c30219 commit 6761f04

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

src/Actions/RedirectIfTwoFactorAuthenticatable.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ protected function validateCredentials($request)
9494

9595
$this->throwFailedAuthenticationException($request);
9696
}
97+
98+
if (config('hashing.rehash_on_login', true) && method_exists($this->guard->getProvider(), 'rehashPasswordIfRequired')) {
99+
$this->guard->getProvider()->rehashPasswordIfRequired($user, ['password' => $request->password]);
100+
}
97101
});
98102
}
99103

tests/AuthenticatedSessionControllerWithTwoFactorTest.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
namespace Laravel\Fortify\Tests;
44

5+
use Illuminate\Foundation\Application;
56
use Illuminate\Foundation\Testing\RefreshDatabase;
67
use Illuminate\Support\Facades\Auth;
78
use Illuminate\Support\Facades\Event;
9+
use Illuminate\Support\Facades\Hash;
810
use Laravel\Fortify\Events\TwoFactorAuthenticationChallenged;
911
use Laravel\Fortify\Features;
1012
use Laravel\Fortify\Tests\Models\UserWithTwoFactor;
@@ -100,6 +102,57 @@ public function test_user_can_authenticate_when_two_factor_challenge_is_disabled
100102
$response->assertRedirect('/home');
101103
}
102104

105+
public function test_rehash_user_password_when_redirecting_to_two_factor_challenge_if_rehashing_on_login_is_enabled()
106+
{
107+
if (version_compare(Application::VERSION, '11.0.0', '<')) {
108+
$this->markTestSkipped('Only on Laravel 11 and later');
109+
}
110+
111+
$this->app['config']->set('hashing.rehash_on_login', true);
112+
113+
$user = UserWithTwoFactor::forceCreate([
114+
'name' => 'Taylor Otwell',
115+
'email' => '[email protected]',
116+
'password' => Hash::make('secret', ['rounds' => 6]),
117+
'two_factor_secret' => 'test-secret',
118+
]);
119+
120+
$response = $this->withoutExceptionHandling()->post('/login', [
121+
'email' => '[email protected]',
122+
'password' => 'secret',
123+
]);
124+
125+
$response->assertRedirect('/two-factor-challenge');
126+
127+
$this->assertNotSame($user->password, $user->fresh()->password);
128+
$this->assertTrue(Hash::check('secret', $user->fresh()->password));
129+
}
130+
131+
public function test_does_not_rehash_user_password_when_redirecting_to_two_factor_challenge_if_rehashing_on_login_is_disabled()
132+
{
133+
if (version_compare(Application::VERSION, '11.0.0', '<')) {
134+
$this->markTestSkipped('Only on Laravel 11 and later');
135+
}
136+
137+
$this->app['config']->set('hashing.rehash_on_login', false);
138+
139+
$user = UserWithTwoFactor::forceCreate([
140+
'name' => 'Taylor Otwell',
141+
'email' => '[email protected]',
142+
'password' => Hash::make('secret', ['rounds' => 6]),
143+
'two_factor_secret' => 'test-secret',
144+
]);
145+
146+
$response = $this->withoutExceptionHandling()->post('/login', [
147+
'email' => '[email protected]',
148+
'password' => 'secret',
149+
]);
150+
151+
$response->assertRedirect('/two-factor-challenge');
152+
153+
$this->assertSame($user->password, $user->fresh()->password);
154+
}
155+
103156
public function test_two_factor_challenge_can_be_passed_via_code()
104157
{
105158
$tfaEngine = app(Google2FA::class);

0 commit comments

Comments
 (0)