Passed
Push — master ( 4a33b4...375bf7 )
by Luiz Kim
02:48
created

TokenAuthenticator   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 11
eloc 21
c 4
b 0
f 0
dl 0
loc 58
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getKey() 0 3 1
A onAuthenticationFailure() 0 3 1
A authenticate() 0 18 3
A start() 0 3 1
A onAuthenticationSuccess() 0 3 1
A createToken() 0 4 1
A __construct() 0 3 1
A supports() 0 4 2
1
<?php
2
3
namespace ControleOnline\Security;
4
5
use ControleOnline\Entity\User;
6
use Doctrine\ORM\EntityManagerInterface;
0 ignored issues
show
Bug introduced by
The type Doctrine\ORM\EntityManagerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Symfony\Component\HttpFoundation\JsonResponse;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\HttpFoundation\JsonResponse was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Symfony\Component\HttpFoundation\Request;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\HttpFoundation\Request was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Symfony\Component\HttpFoundation\Response;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\HttpFoundation\Response was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Securi...on\Token\TokenInterface was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Securi...n\UsernamePasswordToken was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Symfony\Component\Security\Core\Exception\AuthenticationException;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Securi...AuthenticationException was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Securi...AuthenticationException was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use Symfony\Component\Security\Http\Authenticator\AbstractAuthenticator;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Securi...r\AbstractAuthenticator was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Securi...assport\Badge\UserBadge was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\CustomCredentials;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Securi...tials\CustomCredentials was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Securi...cator\Passport\Passport was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Securi...tionEntryPointInterface was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
20
class TokenAuthenticator extends AbstractAuthenticator implements AuthenticationEntryPointInterface
21
{
22
    public function __construct(
23
        private EntityManagerInterface $em
24
    ) {}
25
26
    public function supports(Request $request): ?bool
27
    {
28
        $key = $this->getKey($request);
29
        return $key !== null && !empty(trim($key));
30
    }
31
32
    public function authenticate(Request $request): Passport
33
    {
34
        $apiToken = $this->getKey($request);
35
        if (null === $apiToken) {
36
            throw new CustomUserMessageAuthenticationException('No API token provided');
37
        }
38
39
        return new Passport(
40
            new UserBadge($apiToken, function ($apiToken) {
41
                $user = $this->em->getRepository(User::class)->findOneBy(['apiKey' => $apiToken]);
42
                if (null === $user) {
43
                    throw new CustomUserMessageAuthenticationException('Invalid API token');
44
                }
45
                return $user;
46
            }),
47
            new CustomCredentials(
48
                fn($credentials, $user) => true,
0 ignored issues
show
Unused Code introduced by
The parameter $credentials is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

48
                fn(/** @scrutinizer ignore-unused */ $credentials, $user) => true,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $user is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

48
                fn($credentials, /** @scrutinizer ignore-unused */ $user) => true,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
49
                $apiToken
50
            )
51
        );
52
    }
53
54
    public function createToken(Passport $passport, string $firewallName): TokenInterface
55
    {
56
        $user = $passport->getUser();
57
        return new UsernamePasswordToken($user, $firewallName, $user->getRoles());
58
    }
59
60
    public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response
61
    {
62
        return null;
63
    }
64
65
    public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response
66
    {
67
        return new JsonResponse(['message' => 'Authentication failed'], Response::HTTP_UNAUTHORIZED);
68
    }
69
70
    public function start(Request $request, AuthenticationException $authException = null): Response
71
    {
72
        return new JsonResponse(['message' => 'Authentication required'], Response::HTTP_UNAUTHORIZED);
73
    }
74
75
    private function getKey(Request $request): ?string
76
    {
77
        return $request->headers->get('API-KEY') ?? $request->headers->get('API-TOKEN');
78
    }
79
}
80