Test Setup Failed
Push — master ( 428ef3...604b70 )
by Marcos
03:12
created

RefreshTokenAuthenticator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 0
cts 3
cp 0
cc 1
nc 1
nop 1
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\Token\TokenInterface;
16
use Symfony\Component\Security\Core\Exception\AuthenticationException;
17
use Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\Security\Core\User\UserCheckerInterface;
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 1
25
if (interface_exists('Symfony\Component\Security\Http\Authentication\SimplePreAuthenticatorInterface')) {
26
    abstract class RefreshTokenAuthenticatorBase implements \Symfony\Component\Security\Http\Authentication\SimplePreAuthenticatorInterface
27
    {
28 1
    }
29
} else {
30
    abstract class RefreshTokenAuthenticatorBase implements \Symfony\Component\Security\Core\Authentication\SimplePreAuthenticatorInterface
0 ignored issues
show
Comprehensibility Best Practice introduced by
The type Gesdinet\JWTRefreshToken...hTokenAuthenticatorBase has been defined more than once; this definition is ignored, only the first definition in this file (L26-28) is considered.

This check looks for classes that have been defined more than once in the same file.

If you can, we would recommend to use standard object-oriented programming techniques. For example, to avoid multiple types, it might make sense to create a common interface, and then multiple, different implementations for that interface.

This also has the side-effect of providing you with better IDE auto-completion, static analysis and also better OPCode caching from PHP.

Loading history...
31
    {
32
    }
33
}
34
35
/**
36
 * Class RefreshTokenAuthenticator.
37
 */
38
class RefreshTokenAuthenticator extends RefreshTokenAuthenticatorBase implements AuthenticationFailureHandlerInterface
39
{
40
    /**
41
     * @var UserCheckerInterface
42
     */
43
    private $userChecker;
44
45
    /**
46
     * Constructor.
47
     *
48
     * @param UserCheckerInterface $userChecker
49
     */
50
    public function __construct(UserCheckerInterface $userChecker)
51
    {
52
        $this->userChecker = $userChecker;
53
    }
54
55
    public function createToken(Request $request, $providerKey)
56
    {
57
        $refreshTokenString = RequestRefreshToken::getRefreshToken($request);
58
59
        return new PreAuthenticatedToken(
60
            '',
61
            $refreshTokenString,
62
            $providerKey
63
        );
64
    }
65
66
    public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey)
67
    {
68
        if (!$userProvider instanceof RefreshTokenProvider) {
69
            throw new \InvalidArgumentException(
70
                sprintf(
71
                    'The user provider must be an instance of RefreshTokenProvider (%s was given).',
72
                    get_class($userProvider)
73
                )
74
            );
75
        }
76
77
        $refreshToken = $token->getCredentials();
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 new PreAuthenticatedToken(
92
            $user,
93
            $refreshToken,
94
            $providerKey,
95
            $user->getRoles()
96
        );
97
    }
98
99
    public function supportsToken(TokenInterface $token, $providerKey)
100
    {
101
        return $token instanceof PreAuthenticatedToken && $token->getProviderKey() === $providerKey;
102
    }
103
104
    public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
105
    {
106
        return new Response('Refresh token authentication failed.', 403);
107
    }
108
}
109