Completed
Push — master ( cabb13...5ef2a7 )
by Marcos
06:14
created

RefreshTokenAuthenticator::supportsToken()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 2
eloc 2
nc 2
nop 2
crap 2
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\Security\Core\Authentication\SimplePreAuthenticatorInterface;
16
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
17
use Symfony\Component\Security\Core\Exception\AuthenticationException;
18
use Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken;
19
use Symfony\Component\HttpFoundation\Request;
20
use Symfony\Component\Security\Core\User\UserProviderInterface;
21
use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;
22
use Symfony\Component\HttpFoundation\Response;
23
use Gesdinet\JWTRefreshTokenBundle\Security\Provider\RefreshTokenProvider;
24
25
/**
26
 * Class RefreshTokenAuthenticator.
27
 */
28
class RefreshTokenAuthenticator implements SimplePreAuthenticatorInterface, AuthenticationFailureHandlerInterface
0 ignored issues
show
Deprecated Code introduced by
The interface Symfony\Component\Securi...eAuthenticatorInterface has been deprecated with message: Since version 2.8, to be removed in 3.0. Use the same interface from Security\Http\Authentication instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
29
{
30
    public function createToken(Request $request, $providerKey)
31
    {
32
        $refreshTokenString = RequestRefreshToken::getRefreshToken($request);
33
34
        return new PreAuthenticatedToken(
35
            '',
36
            $refreshTokenString,
37
            $providerKey
38
        );
39
    }
40
41
    public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey)
42
    {
43
        if (!$userProvider instanceof RefreshTokenProvider) {
44
            throw new \InvalidArgumentException(
45
                sprintf(
46
                    'The user provider must be an instance of RefreshTokenProvider (%s was given).',
47
                    get_class($userProvider)
48
                )
49
            );
50
        }
51
52
        $refreshToken = $token->getCredentials();
53
        $username = $userProvider->getUsernameForRefreshToken($refreshToken);
54
55
        if (!$username) {
56
            throw new AuthenticationException(
57
                sprintf('Refresh token "%s" does not exist.', $refreshToken)
58
            );
59
        }
60
61
        $user = $userProvider->loadUserByUsername($username);
62
63
        return new PreAuthenticatedToken(
64
            $user,
65
            $refreshToken,
66
            $providerKey,
67
            $user->getRoles()
68
        );
69
    }
70
71 1
    public function supportsToken(TokenInterface $token, $providerKey)
72
    {
73 1
        return $token instanceof PreAuthenticatedToken && $token->getProviderKey() === $providerKey;
74
    }
75
76 1
    public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
77
    {
78 1
        return new Response('Refresh token authentication failed.', 403);
79
    }
80
}
81