Passed
Push — master ( 9a636e...af59f2 )
by Alexandre
03:34
created

RefreshTokenGrantType::handleAccessTokenRequest()   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 38
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
cc 7
eloc 24
nc 6
nop 2
dl 0
loc 38
ccs 0
cts 8
cp 0
crap 56
rs 6.7272
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: GCC-MED
5
 * Date: 12/03/2018
6
 * Time: 14:36
7
 */
8
9
namespace OAuth2\GrantTypes;
10
11
12
use OAuth2\Endpoints\TokenEndpoint;
13
use OAuth2\Exceptions\OAuthException;
14
use OAuth2\Storages\AccessTokenStorageInterface;
15
use OAuth2\Storages\RefreshTokenStorageInterface;
16
17
class RefreshTokenGrantType extends AbstractGrantType implements GrantTypeInterface
18
{
19
    function handleAccessTokenRequest(TokenEndpoint $tokenEndpoint, array $requestData): array
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
20
    {
21
        if(empty($requestData['refresh_token'])) {
22
            throw new OAuthException('invalid_request',
23
                'The request is missing the required parameter refresh_token.',
24
                'https://tools.ietf.org/html/rfc7636#section-4.4');
25
        }
26
27
        $refreshToken = $this->refreshTokenStorage->get($requestData['refresh_token']);
28
        if(!$refreshToken || $refreshToken->getClientIdentifier() !== $tokenEndpoint->getClient()->getIdentifier()) {
29
            throw new OAuthException('invalid_grant',
30
                'The request includes the invalid parameter refresh_token.',
31
                'https://tools.ietf.org/html/rfc7636#section-4.4');
32
        }
33
34
        // TODO Config alwaysRevokeRefreshTokenOnUse
35
        $this->refreshTokenStorage->revoke($refreshToken->getToken());
36
37
        if($this->refreshTokenStorage->hasExpired($refreshToken)) {
38
            throw new OAuthException('invalid_grant',
39
                'The request includes the invalid parameter refresh_token. The token has expired.',
40
                'https://tools.ietf.org/html/rfc7636#section-4.4');
41
        }
42
43
        $scope = $refreshToken->getScope();
44
        if(!empty($requestData['scope'])) {
45
            if(!empty(array_diff(
46
                explode(' ', $requestData['scope']),
47
                explode(' ', $refreshToken->getScope())))) {
48
                throw new OAuthException('invalid_request',
49
                    'The request includes the invalid parameter scope.',
50
                    'https://tools.ietf.org/html/rfc7636#section-4.4');
51
            }
52
            $scope = $requestData['scope'];
53
        }
54
55
        // TODO Config issueTokens or only accessToken
56
        return $this->issueTokens($scope, $refreshToken->getClientIdentifier(), $refreshToken->getResourceOwnerIdentifier());
57
    }
58
}