|
2 | 2 |
|
3 | 3 | namespace Laravel\Fortify\Tests;
|
4 | 4 |
|
| 5 | +use Illuminate\Foundation\Application; |
5 | 6 | use Illuminate\Foundation\Testing\RefreshDatabase;
|
6 | 7 | use Illuminate\Support\Facades\Auth;
|
7 | 8 | use Illuminate\Support\Facades\Event;
|
| 9 | +use Illuminate\Support\Facades\Hash; |
8 | 10 | use Laravel\Fortify\Events\TwoFactorAuthenticationChallenged;
|
9 | 11 | use Laravel\Fortify\Features;
|
10 | 12 | use Laravel\Fortify\Tests\Models\UserWithTwoFactor;
|
@@ -100,6 +102,57 @@ public function test_user_can_authenticate_when_two_factor_challenge_is_disabled
|
100 | 102 | $response->assertRedirect('/home');
|
101 | 103 | }
|
102 | 104 |
|
| 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 | + |
| 116 | + 'password' => Hash::make('secret', ['rounds' => 6]), |
| 117 | + 'two_factor_secret' => 'test-secret', |
| 118 | + ]); |
| 119 | + |
| 120 | + $response = $this->withoutExceptionHandling()->post('/login', [ |
| 121 | + |
| 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 | + |
| 142 | + 'password' => Hash::make('secret', ['rounds' => 6]), |
| 143 | + 'two_factor_secret' => 'test-secret', |
| 144 | + ]); |
| 145 | + |
| 146 | + $response = $this->withoutExceptionHandling()->post('/login', [ |
| 147 | + |
| 148 | + 'password' => 'secret', |
| 149 | + ]); |
| 150 | + |
| 151 | + $response->assertRedirect('/two-factor-challenge'); |
| 152 | + |
| 153 | + $this->assertSame($user->password, $user->fresh()->password); |
| 154 | + } |
| 155 | + |
103 | 156 | public function test_two_factor_challenge_can_be_passed_via_code()
|
104 | 157 | {
|
105 | 158 | $tfaEngine = app(Google2FA::class);
|
|
0 commit comments