RefreshToken::refresh()   B
last analyzed

Complexity

Conditions 6
Paths 7

Size

Total Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 38
c 0
b 0
f 0
rs 8.6897
ccs 0
cts 0
cp 0
cc 6
nc 7
nop 1
crap 42
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\Service;
13
14
use Gesdinet\JWTRefreshTokenBundle\Event\RefreshEvent;
15
use Gesdinet\JWTRefreshTokenBundle\Security\Authenticator\RefreshTokenAuthenticator;
16
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
17
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface as ContractsEventDispatcherInterface;
18
use InvalidArgumentException;
19
use Symfony\Component\HttpFoundation\Request;
20
use Symfony\Component\Security\Core\Exception\AuthenticationException;
21
use Gesdinet\JWTRefreshTokenBundle\Model\RefreshTokenManagerInterface;
22
use Gesdinet\JWTRefreshTokenBundle\Security\Provider\RefreshTokenProvider;
23
use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;
24
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
25
26
/**
27
 * Class RefreshToken.
28
 */
29
class RefreshToken
30
{
31
    /**
32
     * @var RefreshTokenAuthenticator
33
     */
34
    private $authenticator;
35 4
36
    /**
37 4
     * @var RefreshTokenProvider
38 4
     */
39 4
    private $provider;
40 4
41 4
    /**
42 4
     * @var AuthenticationSuccessHandlerInterface
43 4
     */
44 4
    private $successHandler;
45 4
46
    /**
47
     * @var AuthenticationFailureHandlerInterface
48
     */
49
    private $failureHandler;
50
51
    /**
52
     * @var RefreshTokenManagerInterface
53
     */
54
    private $refreshTokenManager;
55
56 3
    /**
57
     * @var int
58
     */
59 3
    private $ttl;
60 3
61 3
    /**
62 3
     * @var string
63
     */
64
    private $providerKey;
65
66 3
    /**
67
     * @var bool
68 3
     */
69 1
    private $ttlUpdate;
70 1
71 1
    /**
72 1
     * @var EventDispatcherInterface
73
     */
74
    private $eventDispatcher;
75 2
76 1
    /**
77 1
     * RefreshToken constructor.
78 1
     *
79
     * @param RefreshTokenAuthenticator             $authenticator
80 1
     * @param RefreshTokenProvider                  $provider
81 1
     * @param AuthenticationSuccessHandlerInterface $successHandler
82
     * @param AuthenticationFailureHandlerInterface $failureHandler
83 2
     * @param RefreshTokenManagerInterface          $refreshTokenManager
84
     * @param int                                   $ttl
85
     * @param string                                $providerKey
86
     * @param bool                                  $ttlUpdate
87
     * @param EventDispatcherInterface              $eventDispatcher
88
     */
89
    public function __construct(
90
        RefreshTokenAuthenticator $authenticator,
91
        RefreshTokenProvider $provider,
92
        AuthenticationSuccessHandlerInterface $successHandler,
93
        AuthenticationFailureHandlerInterface $failureHandler,
94
        RefreshTokenManagerInterface $refreshTokenManager,
95
        $ttl,
96
        $providerKey,
97
        $ttlUpdate,
98
        EventDispatcherInterface $eventDispatcher
99
    ) {
100
        $this->authenticator = $authenticator;
101
        $this->provider = $provider;
102
        $this->successHandler = $successHandler;
103
        $this->failureHandler = $failureHandler;
104
        $this->refreshTokenManager = $refreshTokenManager;
105
        $this->ttl = $ttl;
106
        $this->providerKey = $providerKey;
107
        $this->ttlUpdate = $ttlUpdate;
108
        $this->eventDispatcher = $eventDispatcher;
109
    }
110
111
    /**
112
     * Refresh token.
113
     *
114
     * @param Request $request
115
     *
116
     * @return mixed
117
     *
118
     * @throws InvalidArgumentException
119
     * @throws AuthenticationException
120
     */
121
    public function refresh(Request $request)
122
    {
123
        try {
124
            $user = $this->authenticator->getUser(
125
                $this->authenticator->getCredentials($request),
126
                $this->provider
127
            );
128
129
            $postAuthenticationToken = $this->authenticator->createAuthenticatedToken($user, $this->providerKey);
0 ignored issues
show
Bug introduced by
It seems like $user defined by $this->authenticator->ge...uest), $this->provider) on line 124 can be null; however, Symfony\Component\Securi...ateAuthenticatedToken() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
130
        } catch (AuthenticationException $e) {
131
            return $this->failureHandler->onAuthenticationFailure($request, $e);
132
        }
133
134
        $refreshToken = $this->refreshTokenManager->get($this->authenticator->getCredentials($request));
0 ignored issues
show
Documentation introduced by
$this->authenticator->getCredentials($request) is of type array<string,*,{"token":"*"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
135
136
        if (null === $refreshToken || !$refreshToken->isValid()) {
137
            return $this->failureHandler->onAuthenticationFailure($request, new AuthenticationException(
138
                    sprintf('Refresh token "%s" is invalid.', $refreshToken)
139
                )
140
            );
141
        }
142
143
        if ($this->ttlUpdate) {
144
            $expirationDate = new \DateTime();
145
            $expirationDate->modify(sprintf('+%d seconds', $this->ttl));
146
            $refreshToken->setValid($expirationDate);
147
148
            $this->refreshTokenManager->save($refreshToken);
149
        }
150
151
        if ($this->eventDispatcher instanceof ContractsEventDispatcherInterface) {
0 ignored issues
show
Bug introduced by
The class Symfony\Contracts\EventD...ventDispatcherInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
152
            $this->eventDispatcher->dispatch(new RefreshEvent($refreshToken, $postAuthenticationToken), 'gesdinet.refresh_token');
153
        } else {
154
            $this->eventDispatcher->dispatch('gesdinet.refresh_token', new RefreshEvent($refreshToken, $postAuthenticationToken));
155
        }
156
157
        return $this->successHandler->onAuthenticationSuccess($request, $postAuthenticationToken);
158
    }
159
}
160