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\Authentication\Token\TokenValidator; |
13
|
|
|
|
14
|
|
|
use Slick\WebStack\Domain\Security\Authentication\Token\TokenValidatorInterface; |
15
|
|
|
use Slick\WebStack\Domain\Security\Authentication\TokenInterface; |
16
|
|
|
use Slick\WebStack\Domain\Security\Exception\UserNotFoundException; |
17
|
|
|
use Slick\WebStack\Domain\Security\SecurityException; |
18
|
|
|
use Slick\WebStack\Domain\Security\User\PasswordAuthenticatedUserInterface; |
19
|
|
|
use Slick\WebStack\Domain\Security\User\UserProviderInterface; |
20
|
|
|
use Slick\WebStack\Domain\Security\UserInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* UserIntegrityTokenValidator |
24
|
|
|
* |
25
|
|
|
* @package Slick\WebStack\Domain\Security\Authentication\Token\TokenValidator |
26
|
|
|
* @template-covariant TUser of UserInterface |
27
|
|
|
*/ |
28
|
|
|
final class UserIntegrityTokenValidator implements TokenValidatorInterface |
29
|
|
|
{ |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Creates a UserIntegrityTokenValidator |
33
|
|
|
* |
34
|
|
|
* @param UserProviderInterface<TUser> $provider |
35
|
|
|
*/ |
36
|
|
|
public function __construct(private readonly UserProviderInterface $provider) |
37
|
|
|
{ |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @inheritDoc |
42
|
|
|
* @throws SecurityException |
43
|
|
|
* @throws UserNotFoundException |
44
|
|
|
*/ |
45
|
|
|
public function validate(TokenInterface $token): bool |
46
|
|
|
{ |
47
|
|
|
$storedUser = $token->user(); |
48
|
|
|
if (!$storedUser) { |
49
|
|
|
return false; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** @var PasswordAuthenticatedUserInterface $user */ |
53
|
|
|
$user = $this->provider->loadUserByIdentifier($storedUser->userIdentifier()); |
54
|
|
|
|
55
|
|
|
if ($storedUser instanceof PasswordAuthenticatedUserInterface && |
56
|
|
|
$user->password() !== $storedUser->password() |
57
|
|
|
) { |
58
|
|
|
return false; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return $user && $user->roles() === $token->roleNames(); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|