Skip to content

Commit

Permalink
Rehash password if required (#557)
Browse files Browse the repository at this point in the history
  • Loading branch information
gdebrauwer committed Jul 19, 2024
1 parent 6c30219 commit 6761f04
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Actions/RedirectIfTwoFactorAuthenticatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ protected function validateCredentials($request)

$this->throwFailedAuthenticationException($request);
}

if (config('hashing.rehash_on_login', true) && method_exists($this->guard->getProvider(), 'rehashPasswordIfRequired')) {
$this->guard->getProvider()->rehashPasswordIfRequired($user, ['password' => $request->password]);
}
});
}

Expand Down
53 changes: 53 additions & 0 deletions tests/AuthenticatedSessionControllerWithTwoFactorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

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\Features;
use Laravel\Fortify\Tests\Models\UserWithTwoFactor;
Expand Down Expand Up @@ -100,6 +102,57 @@ public function test_user_can_authenticate_when_two_factor_challenge_is_disabled
$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()
{
$tfaEngine = app(Google2FA::class);
Expand Down

0 comments on commit 6761f04

Please sign in to comment.