Skip to content

Commit bb1d01b

Browse files
claudef3l1x
authored andcommitted
Src: modernize to PHP 8.2+
1 parent 745e811 commit bb1d01b

File tree

2 files changed

+29
-33
lines changed

2 files changed

+29
-33
lines changed

src/Auth/DebugAuthenticator.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,30 @@
1010
class DebugAuthenticator implements Authenticator
1111
{
1212

13-
/** @var bool */
14-
private $pass;
13+
private ?IIdentity $identity = null;
1514

16-
/** @var IIdentity|null */
17-
private $identity;
18-
19-
public function __construct(bool $pass = true)
15+
public function __construct(
16+
private readonly bool $pass = true,
17+
)
2018
{
21-
$this->pass = $pass;
2219
}
2320

2421
public function setIdentity(IIdentity $identity): void
2522
{
2623
$this->identity = $identity;
2724
}
2825

29-
/**
30-
* @throws AuthenticationException
31-
*/
3226
public function authenticate(string $username, string $password): IIdentity
3327
{
3428
if ($this->pass === false) {
35-
throw new AuthenticationException('Cannot login', Authenticator::FAILURE);
29+
throw new AuthenticationException('Cannot login', Authenticator::Failure);
3630
}
3731

3832
if ($this->identity !== null) {
3933
return $this->identity;
4034
}
4135

42-
return new SimpleIdentity(1, null, null);
36+
return new SimpleIdentity(1);
4337
}
4438

4539
}

src/Auth/StaticAuthenticator.php

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,40 +12,38 @@
1212
class StaticAuthenticator implements Authenticator
1313
{
1414

15-
/** @var mixed[][] */
16-
private $list;
17-
18-
/** @var Passwords */
19-
private $passwords;
15+
/** @var array<string, array{password: string, unsecured: bool, identity: IIdentity}> */
16+
private array $list = [];
2017

2118
/**
22-
* @param mixed[] $list
23-
* @throws InvalidArgumentException
19+
* @param array<string, array<string, mixed>> $list
2420
*/
25-
public function __construct(array $list, Passwords $passwords)
21+
public function __construct(
22+
array $list,
23+
private readonly Passwords $passwords,
24+
)
2625
{
2726
foreach ($list as $username => $values) {
2827
if (!isset($values['password'])) {
2928
throw new InvalidArgumentException(sprintf('Missing parameter `password` for user `%s`', $username));
3029
}
3130

31+
if (!is_string($values['password'])) {
32+
throw new InvalidArgumentException(sprintf('Password for user `%s` must be a string', $username));
33+
}
34+
3235
$this->list[$username] = [
3336
'password' => $values['password'],
34-
'unsecured' => $values['unsecured'] ?? false,
37+
'unsecured' => (bool) ($values['unsecured'] ?? false),
3538
'identity' => $this->createIdentity($username, $values),
3639
];
3740
}
38-
39-
$this->passwords = $passwords;
4041
}
4142

42-
/**
43-
* @throws AuthenticationException
44-
*/
4543
public function authenticate(string $username, string $password): IIdentity
4644
{
4745
if (!isset($this->list[$username])) {
48-
throw new AuthenticationException(sprintf('User `%s` not found', $username), Authenticator::IDENTITY_NOT_FOUND);
46+
throw new AuthenticationException(sprintf('User `%s` not found', $username), Authenticator::IdentityNotFound);
4947
}
5048

5149
$user = $this->list[$username];
@@ -54,14 +52,14 @@ public function authenticate(string $username, string $password): IIdentity
5452
($user['unsecured'] === true && !hash_equals($password, $user['password'])) ||
5553
($user['unsecured'] === false && !$this->passwords->verify($password, $user['password']))
5654
) {
57-
throw new AuthenticationException('Invalid password', Authenticator::INVALID_CREDENTIAL);
55+
throw new AuthenticationException('Invalid password', Authenticator::InvalidCredential);
5856
}
5957

6058
return $user['identity'];
6159
}
6260

6361
/**
64-
* @param mixed[] $values
62+
* @param array<string, mixed> $values
6563
*/
6664
private function createIdentity(string $username, array $values): IIdentity
6765
{
@@ -75,11 +73,15 @@ private function createIdentity(string $username, array $values): IIdentity
7573
return $identity;
7674
}
7775

78-
if (is_array($values['identity'])) {
76+
if (is_array($identity)) {
77+
$id = $identity['id'] ?? $username;
78+
$roles = $identity['roles'] ?? null;
79+
$data = $identity['data'] ?? null;
80+
7981
return new SimpleIdentity(
80-
$identity['id'] ?? $username,
81-
$identity['roles'] ?? null,
82-
$identity['date'] ?? null
82+
is_scalar($id) ? (string) $id : $username,
83+
is_array($roles) ? $roles : null,
84+
is_array($data) ? $data : null
8385
);
8486
}
8587

0 commit comments

Comments
 (0)