1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Security; |
4
|
|
|
|
5
|
|
|
use App\Entity\User; |
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\User\UserInterface; |
12
|
|
|
use Symfony\Component\Security\Core\User\UserProviderInterface; |
13
|
|
|
use Symfony\Component\Security\Guard\AbstractGuardAuthenticator; |
14
|
|
|
|
15
|
|
|
final class IntegrationApiKeyAuthenticator extends AbstractGuardAuthenticator |
16
|
|
|
{ |
17
|
|
|
private $integrationApiKey; |
18
|
|
|
|
19
|
|
|
public function __construct(string $integrationApiKey) |
20
|
|
|
{ |
21
|
|
|
$this->integrationApiKey = $integrationApiKey; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function supports(Request $request) |
25
|
|
|
{ |
26
|
|
|
return $request->headers->has('X-API-KEY'); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function getCredentials(Request $request): Request |
30
|
|
|
{ |
31
|
|
|
return $request; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function getUser($credentials, UserProviderInterface $userProvider) |
35
|
|
|
{ |
36
|
|
|
return new User(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function checkCredentials($request, UserInterface $user): bool |
40
|
|
|
{ |
41
|
|
|
return $this->integrationApiKey === $request->headers->get('X-API-KEY', $request->request->get('api_key')); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey) |
45
|
|
|
{ |
46
|
|
|
return null; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function onAuthenticationFailure(Request $request, AuthenticationException $exception) |
50
|
|
|
{ |
51
|
|
|
$data = [ |
52
|
|
|
'message' => strtr($exception->getMessageKey(), $exception->getMessageData()), |
53
|
|
|
]; |
54
|
|
|
|
55
|
|
|
return new JsonResponse($data, Response::HTTP_FORBIDDEN); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function start(Request $request, AuthenticationException $authException = null) |
59
|
|
|
{ |
60
|
|
|
$data = [ |
61
|
|
|
'message' => 'Authentication Required', |
62
|
|
|
]; |
63
|
|
|
|
64
|
|
|
return new JsonResponse($data, Response::HTTP_UNAUTHORIZED); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function supportsRememberMe() |
68
|
|
|
{ |
69
|
|
|
return false; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|