Skip to content

Commit eacabc7

Browse files
committed
Merge branch 'hotfix/password-rehash' into 8.x
2 parents d98cf8b + 1e61612 commit eacabc7

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

src/Illuminate/Auth/SessionGuard.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Illuminate\Support\Facades\Hash;
2121
use Illuminate\Support\Str;
2222
use Illuminate\Support\Traits\Macroable;
23+
use InvalidArgumentException;
2324
use RuntimeException;
2425
use Symfony\Component\HttpFoundation\Request;
2526
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
@@ -581,16 +582,16 @@ protected function cycleRememberToken(AuthenticatableContract $user)
581582
* @param string $password
582583
* @param string $attribute
583584
* @return bool|null
585+
*
586+
* @throws \Illuminate\Auth\AuthenticationException
584587
*/
585588
public function logoutOtherDevices($password, $attribute = 'password')
586589
{
587590
if (! $this->user()) {
588591
return;
589592
}
590593

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

595596
if ($this->recaller() ||
596597
$this->getCookieJar()->hasQueued($this->getRecallerName())) {
@@ -602,6 +603,26 @@ public function logoutOtherDevices($password, $attribute = 'password')
602603
return $result;
603604
}
604605

606+
/**
607+
* Rehash the current user's password.
608+
*
609+
* @param string $password
610+
* @param string $attribute
611+
* @return bool|null
612+
*
613+
* @throws \InvalidArgumentException
614+
*/
615+
protected function rehashUserPassword($password, $attribute)
616+
{
617+
if (! Hash::check($password, $this->user()->{$attribute})) {
618+
throw new InvalidArgumentException("The given password does not match the current password.");
619+
}
620+
621+
return tap($this->user()->forceFill([
622+
$attribute => Hash::make($password),
623+
]))->save();
624+
}
625+
605626
/**
606627
* Register an authentication attempt event listener.
607628
*

tests/Integration/Auth/AuthenticationTest.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Illuminate\Support\Str;
2020
use Illuminate\Support\Testing\Fakes\EventFake;
2121
use Illuminate\Tests\Integration\Auth\Fixtures\AuthenticationTestUser;
22+
use InvalidArgumentException;
2223
use Orchestra\Testbench\TestCase;
2324

2425
/**
@@ -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(InvalidArgumentException::class);
229+
$this->expectExceptionMessage('current password');
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)