1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Silverback API Components Bundle Project |
5
|
|
|
* |
6
|
|
|
* (c) Daniel West <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Silverback\ApiComponentsBundle\Security; |
15
|
|
|
|
16
|
|
|
use Silverback\ApiComponentsBundle\Entity\User\TokenUser; |
17
|
|
|
use Silverback\ApiComponentsBundle\Exception\ApiPlatformAuthenticationException; |
18
|
|
|
use Silverback\ApiComponentsBundle\Exception\TokenAuthenticationException; |
19
|
|
|
use Silverback\ApiComponentsBundle\Serializer\SerializeFormatResolverInterface; |
20
|
|
|
use Symfony\Component\HttpFoundation\Request; |
21
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
22
|
|
|
use Symfony\Component\Security\Core\Exception\AuthenticationException; |
23
|
|
|
use Symfony\Component\Security\Core\Security; |
24
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
25
|
|
|
use Symfony\Component\Security\Core\User\UserProviderInterface; |
26
|
|
|
use Symfony\Component\Security\Guard\AbstractGuardAuthenticator; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @author Daniel West <[email protected]> |
30
|
|
|
*/ |
31
|
|
|
class TokenAuthenticator extends AbstractGuardAuthenticator |
32
|
|
|
{ |
33
|
|
|
private Security $security; |
34
|
|
|
private SerializeFormatResolverInterface $formatResolver; |
35
|
|
|
private array $tokens; |
36
|
|
|
|
37
|
12 |
|
public function __construct(Security $security, SerializeFormatResolverInterface $formatResolver, array $tokens = []) |
38
|
|
|
{ |
39
|
12 |
|
$this->security = $security; |
40
|
12 |
|
$this->formatResolver = $formatResolver; |
41
|
12 |
|
$this->tokens = $tokens; |
42
|
12 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Called on every request to decide if this authenticator should be |
46
|
|
|
* used for the request. Returning false will cause this authenticator |
47
|
|
|
* to be skipped. |
48
|
|
|
*/ |
49
|
2 |
|
public function supports(Request $request): bool |
50
|
|
|
{ |
51
|
2 |
|
return !$this->security->getUser(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Called on every request. Return whatever credentials you want to |
56
|
|
|
* be passed to getUser() as $credentials. |
57
|
|
|
*/ |
58
|
1 |
|
public function getCredentials(Request $request): array |
59
|
|
|
{ |
60
|
|
|
return [ |
61
|
1 |
|
'token' => $request->headers->get('X-AUTH-TOKEN'), |
62
|
|
|
]; |
63
|
|
|
} |
64
|
|
|
|
65
|
3 |
|
public function getUser($credentials, UserProviderInterface $userProvider = null): ?TokenUser |
66
|
|
|
{ |
67
|
3 |
|
$apiToken = $credentials['token']; |
68
|
3 |
|
if (null === $apiToken) { |
69
|
1 |
|
throw new TokenAuthenticationException('Token Authentication Required'); |
70
|
|
|
} |
71
|
2 |
|
if (!\in_array($apiToken, $this->tokens, true)) { |
72
|
1 |
|
throw new TokenAuthenticationException('The authentication token provided in the X-AUTH-TOKEN header is invalid'); |
73
|
|
|
} |
74
|
|
|
|
75
|
1 |
|
return new TokenUser(); |
76
|
|
|
} |
77
|
|
|
|
78
|
1 |
|
public function checkCredentials($credentials, UserInterface $user): bool |
79
|
|
|
{ |
80
|
1 |
|
return true; |
81
|
|
|
} |
82
|
|
|
|
83
|
1 |
|
public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey): void |
84
|
|
|
{ |
85
|
1 |
|
} |
86
|
|
|
|
87
|
1 |
|
public function onAuthenticationFailure(Request $request, AuthenticationException $exception): void |
88
|
|
|
{ |
89
|
1 |
|
$this->throwApiPlatformAuthenticationException($request, strtr($exception->getMessageKey(), $exception->getMessageData())); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Called when authentication is needed, but it's not sent. |
94
|
|
|
*/ |
95
|
2 |
|
public function start(Request $request, AuthenticationException $authException = null): void |
96
|
|
|
{ |
97
|
2 |
|
$this->throwApiPlatformAuthenticationException($request, $authException ? $authException->getMessage() : 'Token Authentication Required.'); |
98
|
|
|
} |
99
|
|
|
|
100
|
1 |
|
public function supportsRememberMe(): bool |
101
|
|
|
{ |
102
|
1 |
|
return false; |
103
|
|
|
} |
104
|
|
|
|
105
|
3 |
|
private function throwApiPlatformAuthenticationException(Request $request, string $message): void |
106
|
|
|
{ |
107
|
3 |
|
$request->attributes->set('_api_respond', true); |
108
|
3 |
|
$request->attributes->set('_format', $this->formatResolver->getFormatFromRequest($request)); |
109
|
3 |
|
throw new ApiPlatformAuthenticationException($message); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|