TokenAuthenticator::getUser()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 7
cts 7
cp 1
rs 9.7666
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2
1
<?php
2
3
namespace App\Users\Security;
4
5
use Symfony\Component\HttpFoundation\JsonResponse;
6
use Symfony\Component\HttpFoundation\Request;
7
use Symfony\Component\HttpFoundation\Response;
8
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
9
use Symfony\Component\Security\Core\Exception\AuthenticationException;
10
use Symfony\Component\Security\Core\User\UserInterface;
11
use Symfony\Component\Security\Core\User\UserProviderInterface;
12
use Symfony\Component\Security\Guard\AbstractGuardAuthenticator;
13
use Symfony\Component\Translation\TranslatorInterface;
14
15
class TokenAuthenticator extends AbstractGuardAuthenticator
16
{
17
    /**
18
     * @var TranslatorInterface
19
     */
20
    private $translator;
21
22 96
    public function __construct(TranslatorInterface $translator)
23
    {
24 96
        $this->translator = $translator;
25 96
    }
26
27 93
    public function supports(Request $request)
28
    {
29 93
        return $request->query->has('api_token') && !empty($request->query->get('api_token'));
30
    }
31
32 38
    public function getCredentials(Request $request)
33
    {
34 38
        return $request->query->get('api_token');
35
    }
36
37 38
    public function getUser($apiToken, UserProviderInterface $userProvider)
38
    {
39 38
        if (!$userProvider instanceof UserProvider) {
40 1
            throw new \InvalidArgumentException(
41 1
                $this->translator->trans('invalid_user_provider', [
42 1
                    'actual' => \get_class($userProvider),
43 1
                ], 'exceptions')
44
            );
45
        }
46
47
        /*
48
         * @var $userProvider UserProvider
49
         */
50 37
        return $userProvider->loadUserByToken($apiToken);
51
    }
52
53 36
    public function checkCredentials($credentials, UserInterface $user)
54
    {
55
        // check credentials - e.g. make sure the password is valid
56
        // no credential check is needed in this case
57
58
        // return true to cause authentication success
59 36
        return true;
60
    }
61
62 1
    public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
63
    {
64
        $data = [
65 1
            'error' => $this->translator->trans('api_token_authentication_failure', [], 'error'),
66 1
            'error_description' => $this->translator->trans('api_token_authentication_failure_description', [], 'error'),
67
        ];
68
69 1
        return new JsonResponse($data, Response::HTTP_UNAUTHORIZED);
70
    }
71
72 6
    public function start(Request $request, AuthenticationException $authException = null)
73
    {
74
        $data = [
75 6
            'error' => $this->translator->trans('api_token_authentication_required', [], 'error'),
76 6
            'error_description' => $this->translator->trans('api_token_authentication_required_description', [], 'error'),
77
        ];
78
79 6
        return new JsonResponse($data, Response::HTTP_UNAUTHORIZED);
80
    }
81
82 1
    public function supportsRememberMe()
83
    {
84 1
        return false;
85
    }
86
87 36
    public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
88
    {
89 36
        return null;
90
    }
91
}
92