Test Failed
Pull Request — master (#62)
by
unknown
03:26
created

RefreshTokenAuthenticator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
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 Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
15
use Symfony\Component\Security\Core\Exception\AuthenticationException;
16
use Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\Security\Core\User\UserProviderInterface;
19
use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;
20
use Symfony\Component\HttpFoundation\Response;
21
use Gesdinet\JWTRefreshTokenBundle\Security\Provider\RefreshTokenProvider;
22
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
23
use Gesdinet\JWTRefreshTokenBundle\Event\GetTokenRequestEvent;
24 1
use Gesdinet\JWTRefreshTokenBundle\Events;
25
26
if (interface_exists('Symfony\Component\Security\Http\Authentication\SimplePreAuthenticatorInterface')) {
27
    abstract class RefreshTokenAuthenticatorBase implements \Symfony\Component\Security\Http\Authentication\SimplePreAuthenticatorInterface
28 1
    {
29
    }
30
} else {
31
    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 (L27-29) 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...
32
    {
33
    }
34
}
35
36
/**
37
 * Class RefreshTokenAuthenticator.
38
 */
39
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...
40
{
41
    public function __construct(EventDispatcherInterface $dispatcher){
42
        $this->dispatcher = $dispatcher;
0 ignored issues
show
Bug introduced by
The property dispatcher does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
43
    }
44
    public function createToken(Request $request, $providerKey)
45
    {
46
        $event = new GetTokenRequestEvent($request);
47
        $this->dispatcher->dispatch(Events::GET_TOKEN_REQUEST, $event);
48
        $refreshTokenString = $event->getToken();
49
50
        return new PreAuthenticatedToken(
51
            '',
52
            $refreshTokenString,
53
            $providerKey
54
        );
55
    }
56
57
    public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey)
58
    {
59
        if (!$userProvider instanceof RefreshTokenProvider) {
60
            throw new \InvalidArgumentException(
61
                sprintf(
62
                    'The user provider must be an instance of RefreshTokenProvider (%s was given).',
63
                    get_class($userProvider)
64
                )
65
            );
66
        }
67
68
        $refreshToken = $token->getCredentials();
69
        $username = $userProvider->getUsernameForRefreshToken($refreshToken);
70
71
        if (!$username) {
72
            throw new AuthenticationException(
73
                sprintf('Refresh token "%s" does not exist.', $refreshToken)
74
            );
75
        }
76
77
        $user = $userProvider->loadUserByUsername($username);
78
79
        return new PreAuthenticatedToken(
80 1
            $user,
81
            $refreshToken,
82 1
            $providerKey,
83
            $user->getRoles()
84
        );
85 1
    }
86
87 1
    public function supportsToken(TokenInterface $token, $providerKey)
88
    {
89
        return $token instanceof PreAuthenticatedToken && $token->getProviderKey() === $providerKey;
90
    }
91
92
    public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
93
    {
94
        return new Response('Refresh token authentication failed.', 403);
95
    }
96
}
97