|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
/** |
|
4
|
|
|
* /src/Security/Authenticator/ApiKeyAuthenticator.php |
|
5
|
|
|
* |
|
6
|
|
|
* @author TLe, Tarmo Leppänen <[email protected]> |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace App\Security\Authenticator; |
|
10
|
|
|
|
|
11
|
|
|
use App\Security\Provider\ApiKeyUserProvider; |
|
12
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
13
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
14
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
15
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
|
16
|
|
|
use Symfony\Component\Security\Core\Exception\AuthenticationException; |
|
17
|
|
|
use Symfony\Component\Security\Core\Exception\UserNotFoundException; |
|
18
|
|
|
use Symfony\Component\Security\Http\Authenticator\AbstractAuthenticator; |
|
19
|
|
|
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge; |
|
20
|
|
|
use Symfony\Component\Security\Http\Authenticator\Passport\PassportInterface; |
|
21
|
|
|
use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport; |
|
22
|
|
|
use function preg_match; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Class ApiKeyAuthenticator |
|
26
|
|
|
* |
|
27
|
|
|
* @package App\Security\Authenticator |
|
28
|
|
|
* @author TLe, Tarmo Leppänen <[email protected]> |
|
29
|
|
|
*/ |
|
30
|
|
|
class ApiKeyAuthenticator extends AbstractAuthenticator |
|
31
|
|
|
{ |
|
32
|
337 |
|
public function __construct( |
|
33
|
|
|
private ApiKeyUserProvider $apiKeyUserProvider, |
|
34
|
|
|
) { |
|
35
|
337 |
|
} |
|
36
|
|
|
|
|
37
|
333 |
|
public function supports(Request $request): ?bool |
|
38
|
|
|
{ |
|
39
|
333 |
|
return $this->getToken($request) !== ''; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
7 |
|
public function authenticate(Request $request): PassportInterface |
|
43
|
|
|
{ |
|
44
|
7 |
|
$token = $this->getToken($request); |
|
45
|
7 |
|
$apiKey = $this->apiKeyUserProvider->getApiKeyForToken($token); |
|
46
|
|
|
|
|
47
|
7 |
|
if ($apiKey === null) { |
|
48
|
1 |
|
throw new UserNotFoundException('API key not found'); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
6 |
|
return new SelfValidatingPassport(new UserBadge($token)); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
6 |
|
public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response |
|
55
|
|
|
{ |
|
56
|
6 |
|
return null; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
1 |
|
public function onAuthenticationFailure(Request $request, AuthenticationException $exception): Response |
|
60
|
|
|
{ |
|
61
|
1 |
|
$data = [ |
|
62
|
|
|
'code' => 401, |
|
63
|
|
|
'message' => 'Invalid API key', |
|
64
|
|
|
]; |
|
65
|
|
|
|
|
66
|
1 |
|
return new JsonResponse($data, Response::HTTP_UNAUTHORIZED); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
335 |
|
private function getToken(Request $request): string |
|
70
|
|
|
{ |
|
71
|
335 |
|
preg_match('#^ApiKey (\w+)$#', $request->headers->get('Authorization', ''), $matches); |
|
72
|
|
|
|
|
73
|
335 |
|
return $matches[1] ?? ''; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|