Skip to content

Commit

Permalink
Merge branch 'hotfix/password-rehash' into 8.x
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Mar 16, 2021
2 parents d98cf8b + 1e61612 commit eacabc7
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
27 changes: 24 additions & 3 deletions src/Illuminate/Auth/SessionGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
use Illuminate\Support\Traits\Macroable;
use InvalidArgumentException;
use RuntimeException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
Expand Down Expand Up @@ -581,16 +582,16 @@ protected function cycleRememberToken(AuthenticatableContract $user)
* @param string $password
* @param string $attribute
* @return bool|null
*
* @throws \Illuminate\Auth\AuthenticationException
*/
public function logoutOtherDevices($password, $attribute = 'password')
{
if (! $this->user()) {
return;
}

$result = tap($this->user()->forceFill([
$attribute => Hash::make($password),
]))->save();
$result = $this->rehashUserPassword($password, $attribute);

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

/**
* Rehash the current user's password.
*
* @param string $password
* @param string $attribute
* @return bool|null
*
* @throws \InvalidArgumentException
*/
protected function rehashUserPassword($password, $attribute)
{
if (! Hash::check($password, $this->user()->{$attribute})) {
throw new InvalidArgumentException("The given password does not match the current password.");
}

return tap($this->user()->forceFill([
$attribute => Hash::make($password),
]))->save();
}

/**
* Register an authentication attempt event listener.
*
Expand Down
17 changes: 16 additions & 1 deletion tests/Integration/Auth/AuthenticationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Illuminate\Support\Str;
use Illuminate\Support\Testing\Fakes\EventFake;
use Illuminate\Tests\Integration\Auth\Fixtures\AuthenticationTestUser;
use InvalidArgumentException;
use Orchestra\Testbench\TestCase;

/**
Expand Down Expand Up @@ -211,7 +212,7 @@ public function testLoggingOutOtherDevices()

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

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

Event::assertDispatched(OtherDeviceLogout::class, function ($event) {
Expand All @@ -222,6 +223,20 @@ public function testLoggingOutOtherDevices()
});
}

public function testPasswordMustBeValidToLogOutOtherDevices()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('current password');

$this->app['auth']->loginUsingId(1);

$user = $this->app['auth']->user();

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

$this->app['auth']->logoutOtherDevices('adifferentpassword');
}

public function testLoggingInOutViaAttemptRemembering()
{
$this->assertTrue(
Expand Down

0 comments on commit eacabc7

Please sign in to comment.