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\Authentication\Token\UsernamePasswordToken; |
|
|
|
|
12
|
|
|
use Symfony\Component\Security\Core\Exception\AuthenticationException; |
|
|
|
|
13
|
|
|
use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException; |
|
|
|
|
14
|
|
|
use Symfony\Component\Security\Http\Authenticator\AbstractAuthenticator; |
|
|
|
|
15
|
|
|
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge; |
|
|
|
|
16
|
|
|
use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\CustomCredentials; |
|
|
|
|
17
|
|
|
use Symfony\Component\Security\Http\Authenticator\Passport\Passport; |
|
|
|
|
18
|
|
|
use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface; |
|
|
|
|
19
|
|
|
|
20
|
|
|
class TokenAuthenticator extends AbstractAuthenticator implements AuthenticationEntryPointInterface |
21
|
|
|
{ |
22
|
|
|
private $em; |
23
|
|
|
|
24
|
|
|
public function __construct(EntityManagerInterface $em) |
25
|
|
|
{ |
26
|
|
|
$this->em = $em; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function supports(Request $request): ?bool |
30
|
|
|
{ |
31
|
|
|
$key = $this->getKey($request); |
32
|
|
|
return $key !== null && !empty(trim($key)); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function authenticate(Request $request): Passport |
36
|
|
|
{ |
37
|
|
|
$apiToken = $this->getKey($request); |
38
|
|
|
if (null === $apiToken) |
39
|
|
|
throw new CustomUserMessageAuthenticationException('No API token provided'); |
40
|
|
|
|
41
|
|
|
return new Passport( |
42
|
|
|
new UserBadge($apiToken, function ($apiToken) { |
43
|
|
|
$user = $this->em->getRepository(User::class)->findOneBy(['apiKey' => $apiToken]); |
44
|
|
|
if (null === $user) |
45
|
|
|
throw new CustomUserMessageAuthenticationException('Invalid API token'); |
46
|
|
|
return $user; |
47
|
|
|
}), |
48
|
|
|
new CustomCredentials( |
49
|
|
|
fn($credentials, $user) => true, |
|
|
|
|
50
|
|
|
$apiToken |
51
|
|
|
) |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function createToken(Passport $passport, string $firewallName): TokenInterface |
56
|
|
|
{ |
57
|
|
|
$user = $passport->getUser(); |
58
|
|
|
return new UsernamePasswordToken($user, $firewallName, $user->getRoles()); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response |
62
|
|
|
{ |
63
|
|
|
return null; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response |
67
|
|
|
{ |
68
|
|
|
return new JsonResponse(['message' => 'Authentication failed'], Response::HTTP_UNAUTHORIZED); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function start(Request $request, AuthenticationException $authException = null): Response |
72
|
|
|
{ |
73
|
|
|
return new JsonResponse(['message' => 'Authentication required'], Response::HTTP_UNAUTHORIZED); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
private function getKey(Request $request): ?string |
77
|
|
|
{ |
78
|
|
|
return $request->headers->get('API-KEY') ?? $request->headers->get('API-TOKEN'); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
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