Skip to content
Open
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
8 changes: 4 additions & 4 deletions docs/guides/strengthen_password.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
6 changes: 6 additions & 0 deletions docs/references/authentication/session.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()`.
Expand Down
14 changes: 8 additions & 6 deletions src/Authentication/Authenticators/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,14 +363,16 @@
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([
Expand All @@ -380,7 +382,7 @@
}

// 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'),
Expand All @@ -391,7 +393,7 @@
// 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)) {

Check warning on line 396 in src/Authentication/Authenticators/Session.php

View workflow job for this annotation

GitHub Actions / Infection

Escaped Mutant for Mutator "IfNegation": @@ @@ // This would be due to the hash algorithm or hash // cost changing since the last time that a user // logged in. - if ($passwords->needsRehash($passwordHash)) { + if (!$passwords->needsRehash($passwordHash)) { $user->password_hash = $passwords->hash($givenPassword); $this->provider->save($user); }
$user->password_hash = $passwords->hash($givenPassword);
$this->provider->save($user);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Config/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
26 changes: 26 additions & 0 deletions tests/Authentication/Authenticators/SessionAuthenticatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down
Loading