RefreshTokenAuthenticator::getUser()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 6.28

Importance

Changes 0
Metric Value
dl 0
loc 28
ccs 4
cts 14
cp 0.2857
rs 9.472
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 6.28
1
<?php
2
3
/*
4
 * This file is part of the GesdinetJWTRefreshTokenBundle package.
5
 *
6
 * (c) Gesdinet <http://www.gesdinet.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Gesdinet\JWTRefreshTokenBundle\Security\Authenticator;
13
14
use Gesdinet\JWTRefreshTokenBundle\Request\RequestRefreshToken;
15
use Symfony\Component\HttpFoundation\JsonResponse;
16
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
17
use Symfony\Component\Security\Core\Exception\AuthenticationException;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\Security\Core\User\UserCheckerInterface;
20
use Symfony\Component\Security\Core\User\UserInterface;
21
use Symfony\Component\Security\Core\User\UserProviderInterface;
22
use Symfony\Component\Security\Guard\AbstractGuardAuthenticator;
23
use Symfony\Component\HttpFoundation\Response;
24 1
use Gesdinet\JWTRefreshTokenBundle\Security\Provider\RefreshTokenProvider;
25
26
/**
27
 * Class RefreshTokenAuthenticator.
28 1
 */
29
class RefreshTokenAuthenticator extends AbstractGuardAuthenticator
30
{
31
    /**
32
     * @var UserCheckerInterface
33
     */
34
    private $userChecker;
35
36
    /**
37
     * @var string
38
     */
39
    protected $tokenParameterName;
40
41
    /**
42
     * Constructor.
43
     *
44
     * @param UserCheckerInterface $userChecker
45
     * @param string               $tokenParameterName
46
     */
47
    public function __construct(UserCheckerInterface $userChecker, $tokenParameterName)
48
    {
49
        $this->userChecker = $userChecker;
50
        $this->tokenParameterName = $tokenParameterName;
51
    }
52
53
    public function supports(Request $request)
54
    {
55
        return null !== RequestRefreshToken::getRefreshToken($request, $this->tokenParameterName);
56
    }
57
58
    public function getCredentials(Request $request)
59
    {
60
        return [
61
            'token' => RequestRefreshToken::getRefreshToken($request, $this->tokenParameterName),
62
        ];
63
    }
64
65
    public function getUser($credentials, UserProviderInterface $userProvider)
66
    {
67
        if (!$userProvider instanceof RefreshTokenProvider) {
68
            throw new \InvalidArgumentException(
69
                sprintf(
70
                    'The user provider must be an instance of RefreshTokenProvider (%s was given).',
71
                    get_class($userProvider)
72
                )
73
            );
74
        }
75
76
        $refreshToken = $credentials['token'];
77
78
        $username = $userProvider->getUsernameForRefreshToken($refreshToken);
79
80 1
        if (null === $username) {
81
            throw new AuthenticationException(
82 1
                sprintf('Refresh token "%s" does not exist.', $refreshToken)
83
            );
84
        }
85 1
86
        $user = $userProvider->loadUserByUsername($username);
87 1
88
        $this->userChecker->checkPreAuth($user);
89
        $this->userChecker->checkPostAuth($user);
90
91
        return $user;
92
    }
93
94
    public function checkCredentials($credentials, UserInterface $user)
95
    {
96
        // check credentials - e.g. make sure the password is valid
97
        // no credential check is needed in this case
98
99
        // return true to cause authentication success
100
        return true;
101
    }
102
103
    public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
104
    {
105
        // on success, let the request continue
106
        return null;
107
    }
108
109
    public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
110
    {
111
        return new Response('Refresh token authentication failed.', 403);
112
    }
113
114
    public function start(Request $request, AuthenticationException $authException = null)
115
    {
116
        $data = [
117
            // you might translate this message
118
            'message' => 'Authentication Required',
119
        ];
120
121
        return new JsonResponse($data, Response::HTTP_UNAUTHORIZED);
122
    }
123
124
    public function supportsRememberMe()
125
    {
126
        return false;
127
    }
128
}
129