Completed
Pull Request — master (#48)
by Philippe
02:42
created

RefreshTokenAuthenticator::setProperty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 6
ccs 0
cts 4
cp 0
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 3
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\UserProviderInterface;
20
use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;
21
use Symfony\Component\HttpFoundation\Response;
22
use Gesdinet\JWTRefreshTokenBundle\Security\Provider\RefreshTokenProvider;
23
24 1
if (interface_exists('Symfony\Component\Security\Http\Authentication\SimplePreAuthenticatorInterface')) {
25
    abstract class RefreshTokenAuthenticatorBase implements \Symfony\Component\Security\Http\Authentication\SimplePreAuthenticatorInterface
26
    {
27
    }
28 1
} else {
29
    abstract class RefreshTokenAuthenticatorBase implements \Symfony\Component\Security\Core\Authentication\SimplePreAuthenticatorInterface
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
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 (L25-27) 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...
30
    {
31
    }
32
}
33
34
/**
35
 * Class RefreshTokenAuthenticator.
36
 */
37
class RefreshTokenAuthenticator extends RefreshTokenAuthenticatorBase implements AuthenticationFailureHandlerInterface
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
38
{
39
    /**
40
     * @var string
41
     */
42
    protected $property;
43
44
    /**
45
     * @param string $property
46
     *
47
     * @return $this
48
     */
49
    public function setProperty($property)
50
    {
51
        $this->property = $property;
52
53
        return $this;
54
    }
55
56
    public function createToken(Request $request, $providerKey)
57
    {
58
        $refreshTokenString = RequestRefreshToken::getRefreshToken($request, $this->property);
59
60
        return new PreAuthenticatedToken(
61
            '',
62
            $refreshTokenString,
63
            $providerKey
64
        );
65
    }
66
67
    public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey)
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 = $token->getCredentials();
79
        $username = $userProvider->getUsernameForRefreshToken($refreshToken);
80 1
81
        if (!$username) {
82 1
            throw new AuthenticationException(
83
                sprintf('Refresh token "%s" does not exist.', $refreshToken)
84
            );
85 1
        }
86
87 1
        $user = $userProvider->loadUserByUsername($username);
88
89
        return new PreAuthenticatedToken(
90
            $user,
91
            $refreshToken,
92
            $providerKey,
93
            $user->getRoles()
94
        );
95
    }
96
97
    public function supportsToken(TokenInterface $token, $providerKey)
98
    {
99
        return $token instanceof PreAuthenticatedToken && $token->getProviderKey() === $providerKey;
100
    }
101
102
    public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
103
    {
104
        return new Response('Refresh token authentication failed.', 403);
105
    }
106
}
107