RefreshTokenGrant   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 2 Features 0
Metric Value
eloc 42
dl 0
loc 104
ccs 47
cts 47
cp 1
rs 10
c 3
b 2
f 0
wmc 14

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getIdentifier() 0 3 1
B respondToAccessTokenRequest() 0 48 6
A __construct() 0 5 1
A validateOldRefreshToken() 0 29 6
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 is_string;
30
use function json_decode;
31
use function time;
32
33
/**
34
 * Refresh token grant.
35
 */
36
class RefreshTokenGrant extends AbstractGrant
37
{
38 13
    public function __construct(RefreshTokenRepositoryInterface $refreshTokenRepository)
39
    {
40 13
        $this->setRefreshTokenRepository($refreshTokenRepository);
41
42 13
        $this->refreshTokenTTL = new DateInterval('P1M');
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 12
    public function respondToAccessTokenRequest(
49
        ServerRequestInterface $request,
50
        ResponseTypeInterface $responseType,
51
        DateInterval $accessTokenTTL
52
    ): ResponseTypeInterface {
53
        // Validate request
54 12
        $client = $this->validateClient($request);
55 12
        $oldRefreshToken = $this->validateOldRefreshToken($request, $client->getIdentifier());
56 7
        $scopes = $this->validateScopes(
57 7
            $this->getRequestParameter(
58 7
                'scope',
59 7
                $request,
60 7
                implode(self::SCOPE_DELIMITER_STRING, $oldRefreshToken['scopes'])
61 7
            )
62 7
        );
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 7
        foreach ($scopes as $scope) {
67 7
            if (in_array($scope->getIdentifier(), $oldRefreshToken['scopes'], true) === false) {
68 1
                throw OAuthServerException::invalidScope($scope->getIdentifier());
69
            }
70
        }
71
72 6
        $scopes = $this->scopeRepository->finalizeScopes($scopes, $this->getIdentifier(), $client);
73
74
        // Expire old tokens
75 6
        $this->accessTokenRepository->revokeAccessToken($oldRefreshToken['access_token_id']);
76 6
        if ($this->revokeRefreshTokens) {
77 5
            $this->refreshTokenRepository->revokeRefreshToken($oldRefreshToken['refresh_token_id']);
78
        }
79
80
        // Issue and persist new access token
81 6
        $accessToken = $this->issueAccessToken($accessTokenTTL, $client, $oldRefreshToken['user_id'], $scopes);
82 6
        $this->getEmitter()->emit(new RequestAccessTokenEvent(RequestEvent::ACCESS_TOKEN_ISSUED, $request, $accessToken));
83 6
        $responseType->setAccessToken($accessToken);
84
85
        // Issue and persist new refresh token if given
86 6
        if ($this->revokeRefreshTokens) {
87 5
            $refreshToken = $this->issueRefreshToken($accessToken);
88
89 5
            if ($refreshToken !== null) {
90 3
                $this->getEmitter()->emit(new RequestRefreshTokenEvent(RequestEvent::REFRESH_TOKEN_ISSUED, $request, $refreshToken));
91 3
                $responseType->setRefreshToken($refreshToken);
92
            }
93
        }
94
95 6
        return $responseType;
96
    }
97
98
    /**
99
     * @throws OAuthServerException
100
     *
101
     * @return array<string, mixed>
102
     */
103 12
    protected function validateOldRefreshToken(ServerRequestInterface $request, string $clientId): array
104
    {
105 12
        $encryptedRefreshToken = $this->getRequestParameter('refresh_token', $request);
106 12
        if (!is_string($encryptedRefreshToken)) {
107 1
            throw OAuthServerException::invalidRequest('refresh_token');
108
        }
109
110
        // Validate refresh token
111
        try {
112 11
            $refreshToken = $this->decrypt($encryptedRefreshToken);
113 1
        } catch (Exception $e) {
114 1
            throw OAuthServerException::invalidRefreshToken('Cannot decrypt the refresh token', $e);
115
        }
116
117 10
        $refreshTokenData = json_decode($refreshToken, true);
118 10
        if ($refreshTokenData['client_id'] !== $clientId) {
119 1
            $this->getEmitter()->emit(new RequestEvent(RequestEvent::REFRESH_TOKEN_CLIENT_FAILED, $request));
120 1
            throw OAuthServerException::invalidRefreshToken('Token is not linked to client');
121
        }
122
123 9
        if ($refreshTokenData['expire_time'] < time()) {
124 1
            throw OAuthServerException::invalidRefreshToken('Token has expired');
125
        }
126
127 8
        if ($this->refreshTokenRepository->isRefreshTokenRevoked($refreshTokenData['refresh_token_id']) === true) {
128 1
            throw OAuthServerException::invalidRefreshToken('Token has been revoked');
129
        }
130
131 7
        return $refreshTokenData;
132
    }
133
134
    /**
135
     * {@inheritdoc}
136
     */
137 13
    public function getIdentifier(): string
138
    {
139 13
        return 'refresh_token';
140
    }
141
}
142