Passed
Pull Request — master (#1094)
by
unknown
02:34 queued 56s
created

RefreshTokenGrant::validateOldRefreshToken()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

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