RefreshTokenGrant::respondToAccessTokenRequest()   B
last analyzed

Complexity

Conditions 6
Paths 17

Size

Total Lines 52
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 6

Importance

Changes 6
Bugs 0 Features 0
Metric Value
cc 6
eloc 25
c 6
b 0
f 0
nc 17
nop 3
dl 0
loc 52
ccs 28
cts 28
cp 1
crap 6
rs 8.8977

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * OAuth 2.0 Refresh token grant.
5
 *
6
 * @author      Alex Bilbie <[email protected]>
7
 * @copyright   Copyright (c) Alex Bilbie
8
 * @license     http://mit-license.org/
9
 *
10
 * @link        https://github.com/thephpleague/oauth2-server
11
 */
12
13
declare(strict_types=1);
14
15
namespace League\OAuth2\Server\Grant;
16
17
use DateInterval;
18
use Exception;
19
use League\OAuth2\Server\Exception\OAuthServerException;
20
use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface;
21
use League\OAuth2\Server\RequestAccessTokenEvent;
22
use League\OAuth2\Server\RequestEvent;
23
use League\OAuth2\Server\RequestRefreshTokenEvent;
24
use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface;
25
use Psr\Http\Message\ServerRequestInterface;
26
27
use function implode;
28
use function in_array;
29
use function json_decode;
30
use function time;
31
32
/**
33
 * Refresh token grant.
34
 */
35
class RefreshTokenGrant extends AbstractGrant
36
{
37 14
    public function __construct(RefreshTokenRepositoryInterface $refreshTokenRepository)
38
    {
39 14
        $this->setRefreshTokenRepository($refreshTokenRepository);
40
41 14
        $this->refreshTokenTTL = new DateInterval('P1M');
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 13
    public function respondToAccessTokenRequest(
48
        ServerRequestInterface $request,
49
        ResponseTypeInterface $responseType,
50
        DateInterval $accessTokenTTL
51
    ): ResponseTypeInterface {
52
        // Validate request
53 13
        $client = $this->validateClient($request);
54 13
        $oldRefreshToken = $this->validateOldRefreshToken($request, $client->getIdentifier());
55
56 8
        $scopes = $this->validateScopes(
57 8
            $this->getRequestParameter(
58 8
                'scope',
59 8
                $request,
60 8
                implode(self::SCOPE_DELIMITER_STRING, $oldRefreshToken['scopes'])
61 8
            )
62 8
        );
63
64
        // The OAuth spec says that a refreshed access token can have the original scopes or fewer so ensure
65
        // the request doesn't include any new scopes
66 8
        foreach ($scopes as $scope) {
67 8
            if (in_array($scope->getIdentifier(), $oldRefreshToken['scopes'], true) === false) {
68 1
                throw OAuthServerException::invalidScope($scope->getIdentifier());
69
            }
70
        }
71
72 7
        $userId = $oldRefreshToken['user_id'];
73 7
        if (is_int($userId)) {
74 1
            $userId = (string) $userId;
75
        }
76
77 7
        $scopes = $this->scopeRepository->finalizeScopes($scopes, $this->getIdentifier(), $client, $userId);
78
79
        // Expire old tokens
80 7
        $this->accessTokenRepository->revokeAccessToken($oldRefreshToken['access_token_id']);
81 7
        if ($this->revokeRefreshTokens) {
82 6
            $this->refreshTokenRepository->revokeRefreshToken($oldRefreshToken['refresh_token_id']);
83
        }
84
85
        // Issue and persist new access token
86 7
        $accessToken = $this->issueAccessToken($accessTokenTTL, $client, $userId, $scopes);
87 7
        $this->getEmitter()->emit(new RequestAccessTokenEvent(RequestEvent::ACCESS_TOKEN_ISSUED, $request, $accessToken));
88 7
        $responseType->setAccessToken($accessToken);
89
90
        // Issue and persist new refresh token if given
91 7
        $refreshToken = $this->issueRefreshToken($accessToken);
92
93 7
        if ($refreshToken !== null) {
94 5
            $this->getEmitter()->emit(new RequestRefreshTokenEvent(RequestEvent::REFRESH_TOKEN_ISSUED, $request, $refreshToken));
95 5
            $responseType->setRefreshToken($refreshToken);
96
        }
97
98 7
        return $responseType;
99
    }
100
101
    /**
102
     * @throws OAuthServerException
103
     *
104
     * @return array<string, mixed>
105
     */
106 13
    protected function validateOldRefreshToken(ServerRequestInterface $request, string $clientId): array
107
    {
108 13
        $encryptedRefreshToken = $this->getRequestParameter('refresh_token', $request)
109 1
            ?? throw OAuthServerException::invalidRequest('refresh_token');
110
111
        // Validate refresh token
112
        try {
113 12
            $refreshToken = $this->decrypt($encryptedRefreshToken);
114 1
        } catch (Exception $e) {
115 1
            throw OAuthServerException::invalidRefreshToken('Cannot decrypt the refresh token', $e);
116
        }
117
118 11
        $refreshTokenData = json_decode($refreshToken, true);
119 11
        if ($refreshTokenData['client_id'] !== $clientId) {
120 1
            $this->getEmitter()->emit(new RequestEvent(RequestEvent::REFRESH_TOKEN_CLIENT_FAILED, $request));
121 1
            throw OAuthServerException::invalidRefreshToken('Token is not linked to client');
122
        }
123
124 10
        if ($refreshTokenData['expire_time'] < time()) {
125 1
            throw OAuthServerException::invalidRefreshToken('Token has expired');
126
        }
127
128 9
        if ($this->refreshTokenRepository->isRefreshTokenRevoked($refreshTokenData['refresh_token_id']) === true) {
129 1
            throw OAuthServerException::invalidRefreshToken('Token has been revoked');
130
        }
131
132 8
        return $refreshTokenData;
133
    }
134
135
    /**
136
     * {@inheritdoc}
137
     */
138 14
    public function getIdentifier(): string
139
    {
140 14
        return 'refresh_token';
141
    }
142
}
143