Skip to content

Commit 580fb32

Browse files
committed
Require the correct password to rehash it
1 parent c02e325 commit 580fb32

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

src/Illuminate/Auth/SessionGuard.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,26 @@ protected function cycleRememberToken(AuthenticatableContract $user)
573573
$this->provider->updateRememberToken($user, $token);
574574
}
575575

576+
/**
577+
* Rehash the user's password.
578+
*
579+
* @param string $password
580+
* @param string $attribute
581+
* @return bool|null
582+
*
583+
* @throws AuthenticationException If the password is invalid.
584+
*/
585+
protected function rehashUserPassword($password, $attribute)
586+
{
587+
if (! Hash::check($password, $this->user()->$attribute)) {
588+
throw new AuthenticationException('Password mismatch.');
589+
}
590+
591+
return tap($this->user()->forceFill([
592+
$attribute => Hash::make($password),
593+
]))->save();
594+
}
595+
576596
/**
577597
* Invalidate other sessions for the current user.
578598
*
@@ -588,9 +608,7 @@ public function logoutOtherDevices($password, $attribute = 'password')
588608
return;
589609
}
590610

591-
$result = tap($this->user()->forceFill([
592-
$attribute => Hash::make($password),
593-
]))->save();
611+
$result = $this->rehashUserPassword($password, $attribute);
594612

595613
if ($this->recaller() ||
596614
$this->getCookieJar()->hasQueued($this->getRecallerName())) {

tests/Integration/Auth/AuthenticationTest.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Illuminate\Tests\Integration\Auth;
44

5+
use Illuminate\Auth\AuthenticationException;
56
use Illuminate\Auth\EloquentUserProvider;
67
use Illuminate\Auth\Events\Attempting;
78
use Illuminate\Auth\Events\Authenticated;
@@ -211,7 +212,7 @@ public function testLoggingOutOtherDevices()
211212

212213
$this->assertEquals(1, $user->id);
213214

214-
$this->app['auth']->logoutOtherDevices('adifferentpassword');
215+
$this->app['auth']->logoutOtherDevices('password');
215216
$this->assertEquals(1, $user->id);
216217

217218
Event::assertDispatched(OtherDeviceLogout::class, function ($event) {
@@ -222,6 +223,20 @@ public function testLoggingOutOtherDevices()
222223
});
223224
}
224225

226+
public function testPasswordMustBeValidToLogOutOtherDevices()
227+
{
228+
$this->expectException(AuthenticationException::class);
229+
$this->expectExceptionMessage('Password mismatch.');
230+
231+
$this->app['auth']->loginUsingId(1);
232+
233+
$user = $this->app['auth']->user();
234+
235+
$this->assertEquals(1, $user->id);
236+
237+
$this->app['auth']->logoutOtherDevices('adifferentpassword');
238+
}
239+
225240
public function testLoggingInOutViaAttemptRemembering()
226241
{
227242
$this->assertTrue(

0 commit comments

Comments
 (0)