-
-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathSimpleAuthenticator.php
More file actions
56 lines (47 loc) · 1.37 KB
/
SimpleAuthenticator.php
File metadata and controls
56 lines (47 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php declare(strict_types=1);
/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/
namespace Nette\Security;
/**
* Trivial implementation of Authenticator.
*/
class SimpleAuthenticator implements Authenticator
{
public function __construct(
/** @var array<string, string> */
#[\SensitiveParameter]
private array $passwords,
/** @var array<string, string|list<string>|null> */
private array $roles = [],
/** @var array<string, array<string, mixed>> */
private array $data = [],
) {
}
/**
* Authenticates against the in-memory list of users (case-insensitive username).
* @throws AuthenticationException
*/
public function authenticate(
string $username,
#[\SensitiveParameter]
string $password,
): IIdentity
{
foreach ($this->passwords as $name => $pass) {
if (strcasecmp($name, $username) === 0) {
if ($this->verifyPassword($password, $pass)) {
return new SimpleIdentity($name, $this->roles[$name] ?? null, $this->data[$name] ?? []);
} else {
throw new AuthenticationException('Invalid password.', self::InvalidCredential);
}
}
}
throw new AuthenticationException("User '$username' not found.", self::IdentityNotFound);
}
protected function verifyPassword(string $password, string $passOrHash): bool
{
return $password === $passOrHash;
}
}