1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of web-stack |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Slick\WebStack\Domain\Security\Http\Authenticator\Passport\Badge; |
13
|
|
|
|
14
|
|
|
use Slick\WebStack\Domain\Security\Exception\AuthenticationServiceException; |
15
|
|
|
use Slick\WebStack\Domain\Security\Exception\BadCredentialsException; |
16
|
|
|
use Slick\WebStack\Domain\Security\Exception\UserNotFoundException; |
17
|
|
|
use Slick\WebStack\Domain\Security\Http\Authenticator\Passport\BadgeInterface; |
18
|
|
|
use Slick\WebStack\Domain\Security\SecurityException; |
19
|
|
|
use Slick\WebStack\Domain\Security\User\UserProviderInterface; |
20
|
|
|
use Slick\WebStack\Domain\Security\UserInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* UserBadge |
24
|
|
|
* |
25
|
|
|
* @package Slick\WebStack\Domain\Security\Http\Authenticator\Passport\Badge |
26
|
|
|
* @template-covariant TUser of UserInterface |
27
|
|
|
*/ |
28
|
|
|
class UserBadge implements BadgeInterface |
29
|
|
|
{ |
30
|
|
|
public const MAX_USERNAME_LENGTH = 4096; |
31
|
|
|
|
32
|
|
|
private ?UserInterface $user = null; |
33
|
|
|
|
34
|
|
|
/** @var callable|UserProviderInterface<TUser> */ |
35
|
|
|
private readonly mixed $provider; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Creates a UserBadge |
39
|
|
|
* |
40
|
|
|
* @param string $userIdentifier |
41
|
|
|
* @param UserProviderInterface<TUser>|callable $provider |
42
|
|
|
* @throws BadCredentialsException |
43
|
|
|
*/ |
44
|
|
|
public function __construct( |
45
|
|
|
private readonly string $userIdentifier, |
46
|
|
|
UserProviderInterface|callable $provider |
47
|
|
|
) { |
48
|
|
|
if (strlen($this->userIdentifier) > self::MAX_USERNAME_LENGTH) { |
49
|
|
|
throw new BadCredentialsException('Username too long.'); |
50
|
|
|
} |
51
|
|
|
$this->provider = $provider; |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* UserBadge user |
56
|
|
|
* |
57
|
|
|
* @return UserInterface |
58
|
|
|
* @throws SecurityException |
59
|
|
|
*/ |
60
|
|
|
public function user(): UserInterface |
61
|
|
|
{ |
62
|
|
|
if (null === $this->user) { |
63
|
|
|
$this->user = $this->retrieveUser(); |
64
|
|
|
} |
65
|
|
|
return $this->user; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @inheritDoc |
70
|
|
|
*/ |
71
|
|
|
public function isResolved(): bool |
72
|
|
|
{ |
73
|
|
|
return true; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* UserBadge userIdentifier |
78
|
|
|
* |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
|
|
public function userIdentifier(): string |
82
|
|
|
{ |
83
|
|
|
return $this->userIdentifier; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* UserBadge provider |
88
|
|
|
* |
89
|
|
|
* @return callable|UserProviderInterface<TUser> |
90
|
|
|
*/ |
91
|
|
|
public function provider(): callable|UserProviderInterface |
92
|
|
|
{ |
93
|
|
|
return $this->provider; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @throws SecurityException|UserNotFoundException |
98
|
|
|
* @throws SecurityException|AuthenticationServiceException |
99
|
|
|
*/ |
100
|
|
|
private function retrieveUser(): UserInterface |
101
|
|
|
{ |
102
|
|
|
if ($this->provider instanceof UserProviderInterface) { |
103
|
|
|
return $this->provider->loadUserByIdentifier($this->userIdentifier); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** @phpstan-var TUser $user */ |
107
|
|
|
$user = ($this->provider)($this->userIdentifier); |
108
|
|
|
if (null === $user) { |
109
|
|
|
throw new UserNotFoundException(sprintf('User with identifier "%s" not found.', $this->userIdentifier)); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
if (!$user instanceof UserInterface) { |
113
|
|
|
throw new AuthenticationServiceException( |
114
|
|
|
sprintf('The user provider must return a UserInterface object, "%s" given.', get_debug_type($user)) |
115
|
|
|
); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $user; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|