Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Require the correct password to rehash it when logging out other devices #36608

Merged
merged 4 commits into from
Mar 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions src/Illuminate/Auth/SessionGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,26 @@ protected function cycleRememberToken(AuthenticatableContract $user)
$this->provider->updateRememberToken($user, $token);
}

/**
* Rehash the current user's password.
*
* @param string $password
* @param string $attribute
* @return bool|null
*
* @throws \Illuminate\Auth\AuthenticationException
*/
protected function rehashUserPassword($password, $attribute)
{
if (! Hash::check($password, $this->user()->$attribute)) {
throw new AuthenticationException('Password mismatch.');
}

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

/**
* Invalidate other sessions for the current user.
*
Expand All @@ -581,16 +601,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 Down
17 changes: 16 additions & 1 deletion tests/Integration/Auth/AuthenticationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Tests\Integration\Auth;

use Illuminate\Auth\AuthenticationException;
use Illuminate\Auth\EloquentUserProvider;
use Illuminate\Auth\Events\Attempting;
use Illuminate\Auth\Events\Authenticated;
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(AuthenticationException::class);
$this->expectExceptionMessage('Password mismatch.');

$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