Completed
Push — master ( dc4d8f...cabb13 )
by Marcos
07:19
created

RefreshTokenAuthenticator   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 11.11%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 9
c 3
b 0
f 1
lcom 0
cbo 5
dl 0
loc 59
ccs 4
cts 36
cp 0.1111
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B authenticateToken() 0 29 3
A supportsToken() 0 4 2
A onAuthenticationFailure() 0 4 1
A createToken() 0 16 3
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 Symfony\Component\Security\Core\Authentication\SimplePreAuthenticatorInterface;
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\UserProviderInterface;
20
use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;
21
use Symfony\Component\HttpFoundation\Response;
22
use Gesdinet\JWTRefreshTokenBundle\Security\Provider\RefreshTokenProvider;
23
24
/**
25
 * Class RefreshTokenAuthenticator.
26
 */
27
class RefreshTokenAuthenticator implements SimplePreAuthenticatorInterface, AuthenticationFailureHandlerInterface
28
{
29
    public function createToken(Request $request, $providerKey)
30
    {
31
        if ($request->headers->get('content_type') == 'application/json') {
32
            $content = $request->getContent();
33
            $params = !empty($content) ? json_decode($content, true) : array();
34
            $refreshToken = trim($params['refresh_token']);
35
        } else {
36
            $refreshToken = $request->request->get('refresh_token');
37
        }
38
39
        return new PreAuthenticatedToken(
40
            '',
41
            $refreshToken,
42
            $providerKey
43
        );
44
    }
45
46
    public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey)
47
    {
48
        if (!$userProvider instanceof RefreshTokenProvider) {
49
            throw new \InvalidArgumentException(
50
                sprintf(
51
                    'The user provider must be an instance of RefreshTokenProvider (%s was given).',
52
                    get_class($userProvider)
53
                )
54
            );
55
        }
56
57
        $refreshToken = $token->getCredentials();
58
        $username = $userProvider->getUsernameForRefreshToken($refreshToken);
59
60
        if (!$username) {
61
            throw new AuthenticationException(
62
                sprintf('Refresh token "%s" does not exist.', $refreshToken)
63
            );
64
        }
65
66
        $user = $userProvider->loadUserByUsername($username);
67
68
        return new PreAuthenticatedToken(
69
            $user,
70
            $refreshToken,
71
            $providerKey,
72
            $user->getRoles()
73
        );
74
    }
75
76 1
    public function supportsToken(TokenInterface $token, $providerKey)
77
    {
78 1
        return $token instanceof PreAuthenticatedToken && $token->getProviderKey() === $providerKey;
79
    }
80
81 1
    public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
82
    {
83 1
        return new Response('Refresh token authentication failed.', 403);
84
    }
85
}
86