diff --git a/docs/guides/strengthen_password.md b/docs/guides/strengthen_password.md index 6aa7f5f09..7682d03b0 100644 --- a/docs/guides/strengthen_password.md +++ b/docs/guides/strengthen_password.md @@ -102,10 +102,10 @@ public int $hashThreads = PASSWORD_ARGON2_DEFAULT_THREADS; ### Update the Dummy Password Hash When you change the password hashing algorithm or its cost settings, update -`Auth::$dummyPasswordHash` as well. Shield uses this hash to -verify a password when the login identifier does not match an account, reducing -timing differences between failed login attempts. The default dummy hash uses -bcrypt with cost 12. +`Auth::$dummyPasswordHash` as well. Shield uses this hash when the login +identifier matches no account or the account has no local password hash. +This reduces timing differences between failed login attempts. The default +dummy hash uses bcrypt with cost 12. After applying the new hash settings in the target environment, run: diff --git a/docs/references/authentication/session.md b/docs/references/authentication/session.md index 9245ebf7e..f72777025 100644 --- a/docs/references/authentication/session.md +++ b/docs/references/authentication/session.md @@ -66,6 +66,12 @@ if (! $validCreds->isOK()) { The Result instance returned contains the valid user as `extraInfo()`. +When a credential lookup finds an account without a local password hash, both +`check()` and `attempt()` return an unsuccessful result with the `Auth.badAttempt` +message. Shield verifies the submitted password against the +[configured dummy hash](../../guides/strengthen_password.md#update-the-dummy-password-hash) +before returning the result. + ### loggedIn() You can determine if a user is currently logged in with the aptly titled method, `loggedIn()`. diff --git a/src/Authentication/Authenticators/Session.php b/src/Authentication/Authenticators/Session.php index 2b7ca3ba3..6133a4ed5 100644 --- a/src/Authentication/Authenticators/Session.php +++ b/src/Authentication/Authenticators/Session.php @@ -363,14 +363,16 @@ public function check(array $credentials): Result unset($credentials['password']); // Find the existing user - $user = $this->provider->findByCredentials($credentials); + $user = $this->provider->findByCredentials($credentials); + $passwordHash = $user?->password_hash; /** @var Passwords $passwords */ $passwords = service('passwords'); - if ($user === null) { - // Verify against a fixed hash so an unknown identifier still does - // the expensive password check. The result is never accepted. + if ($user === null || $passwordHash === null) { + // Verify against a fixed hash to keep the cost of password verification + // for unknown identifiers and accounts without a stored password hash. + // The result is never accepted. $passwords->verify($givenPassword, config('Auth')->dummyPasswordHash); return new Result([ @@ -380,7 +382,7 @@ public function check(array $credentials): Result } // Now, try matching the passwords. - if (! $passwords->verify($givenPassword, $user->password_hash)) { + if (! $passwords->verify($givenPassword, $passwordHash)) { return new Result([ 'success' => false, 'reason' => lang('Auth.badAttempt'), @@ -391,7 +393,7 @@ public function check(array $credentials): Result // This would be due to the hash algorithm or hash // cost changing since the last time that a user // logged in. - if ($passwords->needsRehash($user->password_hash)) { + if ($passwords->needsRehash($passwordHash)) { $user->password_hash = $passwords->hash($givenPassword); $this->provider->save($user); } diff --git a/src/Config/Auth.php b/src/Config/Auth.php index e8d779905..87c6a714e 100644 --- a/src/Config/Auth.php +++ b/src/Config/Auth.php @@ -380,9 +380,9 @@ class Auth extends BaseConfig public int $hashCost = 12; /** - * Hash used when a login identifier does not match an account. It must use - * the same password algorithm and cost as real accounts so that failed - * logins take comparable time. Override this when changing hash settings. + * Hash used when a login identifier has no matching account or the account + * has no password hash. Use the same algorithm and cost as for real passwords + * so failed logins take comparable time. Override this when changing hash settings. * Run `php spark shield:generate-dummy-hash` to generate a matching hash. * The corresponding plaintext password is never accepted. */ diff --git a/tests/Authentication/Authenticators/SessionAuthenticatorTest.php b/tests/Authentication/Authenticators/SessionAuthenticatorTest.php index 644d54a5f..f523834d3 100644 --- a/tests/Authentication/Authenticators/SessionAuthenticatorTest.php +++ b/tests/Authentication/Authenticators/SessionAuthenticatorTest.php @@ -377,6 +377,32 @@ public function testAttemptCannotFindUser(): void ]); } + public function testAttemptUsernameWithoutPasswordHash(): void + { + $config = config('Auth'); + $config->validFields = ['email', 'username']; + Factories::injectMock('config', 'Auth', $config); + + $user = fake(UserModel::class, ['username' => 'directory-user']); + $this->assertNull($user->getPasswordHash()); + + $passwords = $this->createMock(Passwords::class); + $passwords->expects($this->once()) + ->method('verify') + ->with('secret', $config->dummyPasswordHash) + ->willReturn(true); + Services::injectMock('passwords', $passwords); + + $result = $this->auth->attempt([ + 'username' => $user->username, + 'password' => 'secret', + ]); + + $this->assertFalse($result->isOK()); + $this->assertSame(lang('Auth.badAttempt'), $result->reason()); + $this->assertFalse($this->auth->loggedIn()); + } + public function testAttemptSuccess(): void { $this->user->createEmailIdentity([