|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Silverback\ApiComponentBundle\Security; |
|
4
|
|
|
|
|
5
|
|
|
use Silverback\ApiComponentBundle\Entity\User\TokenUser; |
|
6
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
7
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
8
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
9
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
|
10
|
|
|
use Symfony\Component\Security\Core\Exception\AuthenticationException; |
|
11
|
|
|
use Symfony\Component\Security\Core\Security; |
|
12
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
|
13
|
|
|
use Symfony\Component\Security\Core\User\UserProviderInterface; |
|
14
|
|
|
use Symfony\Component\Security\Guard\AbstractGuardAuthenticator; |
|
15
|
|
|
|
|
16
|
|
|
class TokenAuthenticator extends AbstractGuardAuthenticator |
|
17
|
|
|
{ |
|
18
|
|
|
private $security; |
|
19
|
|
|
private $tokens; |
|
20
|
|
|
|
|
21
|
|
|
public function __construct(Security $security, array $tokens = []) |
|
22
|
|
|
{ |
|
23
|
|
|
$this->security = $security; |
|
24
|
|
|
$this->tokens = $tokens; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Called on every request to decide if this authenticator should be |
|
29
|
|
|
* used for the request. Returning false will cause this authenticator |
|
30
|
|
|
* to be skipped. |
|
31
|
|
|
* @param Request $request |
|
32
|
|
|
* @return bool |
|
33
|
|
|
*/ |
|
34
|
|
|
public function supports(Request $request): bool |
|
35
|
|
|
{ |
|
36
|
|
|
if ($this->security->getUser()) { |
|
37
|
|
|
return false; |
|
38
|
|
|
} |
|
39
|
|
|
return $request->headers->has('X-AUTH-TOKEN'); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Called on every request. Return whatever credentials you want to |
|
44
|
|
|
* be passed to getUser() as $credentials. |
|
45
|
|
|
* @param Request $request |
|
46
|
|
|
* @return array |
|
47
|
|
|
*/ |
|
48
|
|
|
public function getCredentials(Request $request): array |
|
49
|
|
|
{ |
|
50
|
|
|
return array( |
|
51
|
|
|
'token' => $request->headers->get('X-AUTH-TOKEN'), |
|
52
|
|
|
); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function getUser($credentials, UserProviderInterface $userProvider = null): ?TokenUser |
|
56
|
|
|
{ |
|
57
|
|
|
$apiToken = $credentials['token']; |
|
58
|
|
|
if (null === $apiToken || !\in_array($apiToken, $this->tokens, true)) { |
|
59
|
|
|
return null; |
|
60
|
|
|
} |
|
61
|
|
|
return new TokenUser(); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function checkCredentials($credentials, UserInterface $user): bool |
|
65
|
|
|
{ |
|
66
|
|
|
return true; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey): void |
|
70
|
|
|
{ |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function onAuthenticationFailure(Request $request, AuthenticationException $exception): JsonResponse |
|
74
|
|
|
{ |
|
75
|
|
|
$data = array( |
|
76
|
|
|
'message' => strtr($exception->getMessageKey(), $exception->getMessageData()) |
|
77
|
|
|
); |
|
78
|
|
|
|
|
79
|
|
|
return new JsonResponse($data, Response::HTTP_FORBIDDEN); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Called when authentication is needed, but it's not sent |
|
84
|
|
|
* @param Request $request |
|
85
|
|
|
* @param AuthenticationException|null $authException |
|
86
|
|
|
* @return JsonResponse |
|
87
|
|
|
*/ |
|
88
|
|
|
public function start(Request $request, AuthenticationException $authException = null): JsonResponse |
|
89
|
|
|
{ |
|
90
|
|
|
$data = [ |
|
91
|
|
|
'message' => 'Token Authentication Required' |
|
92
|
|
|
]; |
|
93
|
|
|
return new JsonResponse($data, Response::HTTP_UNAUTHORIZED); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function supportsRememberMe(): bool |
|
97
|
|
|
{ |
|
98
|
|
|
return false; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|