Skip to content

Commit d98d475

Browse files
committed
SimpleAuthenticator: passwords can be hashed
1 parent 60b9a12 commit d98d475

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

src/Security/SimpleAuthenticator.php

+4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public function __construct(
2525
private array $passwords,
2626
private array $roles = [],
2727
private array $data = [],
28+
private ?Passwords $verifier = null,
2829
) {
2930
}
3031

@@ -56,6 +57,9 @@ public function authenticate(
5657

5758
protected function verifyPassword(string $password, string $passOrHash): bool
5859
{
60+
if (preg_match('~\$.{50,}~A', $passOrHash)) {
61+
return $this->verifier->verify($password, $passOrHash);
62+
}
5963
return $password === $passOrHash;
6064
}
6165
}

tests/Security/SimpleAuthenticator.phpt

+15-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
declare(strict_types=1);
88

9+
use Nette\Security\Passwords;
910
use Nette\Security\SimpleAuthenticator;
1011
use Tester\Assert;
1112

@@ -14,16 +15,12 @@ require __DIR__ . '/../bootstrap.php';
1415

1516

1617
$users = [
17-
'john' => 'password123!',
18+
'john' => '$2a$12$dliX6LynG/iChDUF7DhKzulN7d3nU.l3/RozE1MmEaxxBWdZXppm2',
1819
'admin' => 'admin',
1920
];
2021

2122
$authenticator = new SimpleAuthenticator($users);
2223

23-
$identity = $authenticator->authenticate('john', 'password123!');
24-
Assert::type(Nette\Security\IIdentity::class, $identity);
25-
Assert::equal('john', $identity->getId());
26-
2724
$identity = $authenticator->authenticate('admin', 'admin');
2825
Assert::type(Nette\Security\IIdentity::class, $identity);
2926
Assert::equal('admin', $identity->getId());
@@ -39,3 +36,16 @@ Assert::exception(
3936
Nette\Security\AuthenticationException::class,
4037
"User 'nobody' not found.",
4138
);
39+
40+
41+
$authenticator = new SimpleAuthenticator($users, verifier: new Passwords);
42+
43+
$identity = $authenticator->authenticate('john', 'password123!');
44+
Assert::type(Nette\Security\IIdentity::class, $identity);
45+
Assert::equal('john', $identity->getId());
46+
47+
Assert::exception(
48+
fn() => $authenticator->authenticate('john', $users['john']),
49+
Nette\Security\AuthenticationException::class,
50+
'Invalid password.',
51+
);

0 commit comments

Comments
 (0)