1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ControleOnline\Security; |
4
|
|
|
|
5
|
|
|
use ControleOnline\Entity\User; |
6
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
|
|
|
7
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
|
|
|
8
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
|
|
|
9
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
|
|
|
10
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
|
|
|
|
11
|
|
|
use Symfony\Component\Security\Core\Exception\AuthenticationException; |
|
|
|
|
12
|
|
|
use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException; |
|
|
|
|
13
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
|
|
|
|
14
|
|
|
use Symfony\Component\Security\Core\User\UserProviderInterface; |
|
|
|
|
15
|
|
|
use Symfony\Component\Security\Http\Authenticator\AuthenticatorInterface; |
|
|
|
|
16
|
|
|
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge; |
|
|
|
|
17
|
|
|
use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\CustomCredentials; |
|
|
|
|
18
|
|
|
use Symfony\Component\Security\Http\Authenticator\Passport\Passport; |
|
|
|
|
19
|
|
|
use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface; |
|
|
|
|
20
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
|
|
|
|
21
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; // Ou outro tipo de token apropriado |
|
|
|
|
22
|
|
|
|
23
|
|
|
class TokenAuthenticator implements AuthenticatorInterface, AuthenticationEntryPointInterface |
24
|
|
|
{ |
25
|
|
|
private TokenStorageInterface $tokenStorage; |
26
|
|
|
|
27
|
|
|
public function __construct(private EntityManagerInterface $em, TokenStorageInterface $tokenStorage) |
28
|
|
|
{ |
29
|
|
|
$this->tokenStorage = $tokenStorage; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function supports(Request $request): ?bool |
33
|
|
|
{ |
34
|
|
|
return $this->getKey($request) !== null; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function authenticate(Request $request): Passport |
38
|
|
|
{ |
39
|
|
|
$apiToken = $this->getKey($request); |
40
|
|
|
if (null === $apiToken) { |
41
|
|
|
throw new CustomUserMessageAuthenticationException('No API token provided'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return new Passport( |
45
|
|
|
new UserBadge($apiToken, function ($apiToken) { |
46
|
|
|
return $this->em->getRepository(User::class)->findOneBy(['apiKey' => $apiToken]); |
47
|
|
|
}), |
48
|
|
|
new CustomCredentials( |
49
|
|
|
function ($credentials, UserInterface $user) { |
|
|
|
|
50
|
|
|
return true; // No need to check credentials for API token |
51
|
|
|
}, |
52
|
|
|
$apiToken |
53
|
|
|
) |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function createAuthenticatedToken(Passport $passport, string $firewallName): TokenInterface |
58
|
|
|
{ |
59
|
|
|
return $passport->createAuthenticatedToken($passport->getUser(), $firewallName); |
60
|
|
|
} |
61
|
|
|
public function createToken(Passport $passport, string $firewallName): TokenInterface |
62
|
|
|
{ |
63
|
|
|
return $this->createAuthenticatedToken($passport, $firewallName); |
64
|
|
|
} |
65
|
|
|
public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response |
66
|
|
|
{ |
67
|
|
|
return null; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response |
71
|
|
|
{ |
72
|
|
|
$data = ['message' => strtr($exception->getMessageKey(), $exception->getMessageData())]; |
73
|
|
|
return new JsonResponse($data, Response::HTTP_UNAUTHORIZED); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function start(Request $request, AuthenticationException $authException = null): Response |
77
|
|
|
{ |
78
|
|
|
$data = ['message' => 'Authentication Required']; |
79
|
|
|
return new JsonResponse($data, Response::HTTP_UNAUTHORIZED); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
private function getKey(Request $request) |
83
|
|
|
{ |
84
|
|
|
return $request->headers->get('Authorization') ?? $request->headers->get('API-TOKEN') ?? $request->headers->get('API-KEY'); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths