Test Setup Failed
Pull Request — master (#146)
by
unknown
02:42
created

RefreshTokenAuthenticator::authenticateToken()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 4.5435

Importance

Changes 0
Metric Value
dl 0
loc 32
ccs 4
cts 9
cp 0.4444
rs 9.408
c 0
b 0
f 0
cc 3
nc 3
nop 3
crap 4.5435

1 Method

Rating   Name   Duplication   Size   Complexity  
A RefreshTokenAuthenticator::checkCredentials() 0 8 1
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
/**
28 1
 * Class RefreshTokenAuthenticator.
29
 */
30
class RefreshTokenAuthenticator extends AbstractGuardAuthenticator
31
{
32
33
    /**
34
     * @var UserCheckerInterface
35
     */
36
    private $userChecker;
37
38
    /**
39
     * @var string
40
     */
41
    protected $tokenParameterName;
42
43
    /**
44
     * Constructor.
45
     *
46
     * @param UserCheckerInterface $userChecker
47
     * @param string $tokenParameterName
48
     */
49
    public function __construct(UserCheckerInterface $userChecker, $tokenParameterName)
50
    {
51
        $this->userChecker = $userChecker;
52
        $this->tokenParameterName = $tokenParameterName;
53
    }
54
55
    public function supports(Request $request)
56
    {
57
        return null !== RequestRefreshToken::getRefreshToken($request, $this->tokenParameterName);
58
    }
59
60
    public function getCredentials(Request $request)
61
    {
62
        return [
63
            'token' => RequestRefreshToken::getRefreshToken($request, $this->tokenParameterName),
64
        ];
65
    }
66
67
    public function getUser($credentials, UserProviderInterface $userProvider)
68
    {
69
        if (!$userProvider instanceof RefreshTokenProvider) {
70
            throw new \InvalidArgumentException(
71
                sprintf(
72
                    'The user provider must be an instance of RefreshTokenProvider (%s was given).',
73
                    get_class($userProvider)
74
                )
75
            );
76
        }
77
78
        $refreshToken = $credentials['token'];
79
80 1
        $username = $userProvider->getUsernameForRefreshToken($refreshToken);
81
82 1
        if (null === $username) {
83
            throw new AuthenticationException(
84
                sprintf('Refresh token "%s" does not exist.', $refreshToken)
85 1
            );
86
        }
87 1
88
        $user = $userProvider->loadUserByUsername($username);
89
90
        $this->userChecker->checkPreAuth($user);
91
        $this->userChecker->checkPostAuth($user);
92
93
        return $user;
94
    }
95
96
    public function checkCredentials($credentials, UserInterface $user)
97
    {
98
        // check credentials - e.g. make sure the password is valid
99
        // no credential check is needed in this case
100
101
        // return true to cause authentication success
102
        return true;
103
    }
104
105
    public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
106
    {
107
        // on success, let the request continue
108
        return null;
109
    }
110
111
    public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
112
    {
113
        return new Response('Refresh token authentication failed.', 403);
114
    }
115
116
    public function start(Request $request, AuthenticationException $authException = null)
117
    {
118
        $data = [
119
            // you might translate this message
120
            'message' => 'Authentication Required',
121
        ];
122
123
        return new JsonResponse($data, Response::HTTP_UNAUTHORIZED);
124
    }
125
126
    public function supportsRememberMe()
127
    {
128
        return false;
129
    }
130
131
}
132