Passed
Pull Request — master (#1122)
by Sebastian
02:10
created

RefreshTokenGrant::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
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 10
    public function __construct(RefreshTokenRepositoryInterface $refreshTokenRepository)
31
    {
32 10
        $this->setRefreshTokenRepository($refreshTokenRepository);
33
34 10
        $this->refreshTokenTTL = new DateInterval('P1M');
35 10
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 9
    public function respondToAccessTokenRequest(
41
        ServerRequestInterface $request,
42
        ResponseTypeInterface $responseType,
43
        DateInterval $accessTokenTTL
44
    ) {
45
        // Validate request
46 9
        $client = $this->validateClient($request);
47 9
        $oldRefreshToken = $this->validateOldRefreshToken($request, $client->getIdentifier());
48 4
        $scopes = $this->validateScopes($this->getRequestParameter(
49 4
            'scope',
50 4
            $request,
51 4
            \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 4
        foreach ($scopes as $scope) {
57 4
            if (\in_array($scope->getIdentifier(), $oldRefreshToken['scopes'], true) === false) {
58 1
                throw OAuthServerException::invalidScope($scope->getIdentifier());
59
            }
60
        }
61
62
        // Expire old tokens
63 3
        $this->accessTokenRepository->revokeAccessToken($oldRefreshToken['access_token_id']);
64 3
        $this->refreshTokenRepository->revokeRefreshToken($oldRefreshToken['refresh_token_id']);
65
66 3
        $privateClaims = [];
67 3
        if ($this->claimRepository) {
68
            $privateClaims = $this->claimRepository->getClaims(
69
                $privateClaims,
70
                $this->getIdentifier(),
71
                $client,
72
                $oldRefreshToken['user_id']
73
            );
74
        }
75
76
        // Issue and persist new access token
77 3
        $accessToken = $this->issueAccessToken(
78 3
            $accessTokenTTL,
79 3
            $client,
80 3
            $oldRefreshToken['user_id'],
81 3
            $scopes,
82 3
            $privateClaims
83
        );
84 3
        $this->getEmitter()->emit(new RequestEvent(RequestEvent::ACCESS_TOKEN_ISSUED, $request));
85 3
        $responseType->setAccessToken($accessToken);
86
87
        // Issue and persist new refresh token if given
88 3
        $refreshToken = $this->issueRefreshToken($accessToken);
89
90 3
        if ($refreshToken !== null) {
91 2
            $this->getEmitter()->emit(new RequestEvent(RequestEvent::REFRESH_TOKEN_ISSUED, $request));
92 2
            $responseType->setRefreshToken($refreshToken);
93
        }
94
95 3
        return $responseType;
96
    }
97
98
    /**
99
     * @param ServerRequestInterface $request
100
     * @param string                 $clientId
101
     *
102
     * @throws OAuthServerException
103
     *
104
     * @return array
105
     */
106 9
    protected function validateOldRefreshToken(ServerRequestInterface $request, $clientId)
107
    {
108 9
        $encryptedRefreshToken = $this->getRequestParameter('refresh_token', $request);
109 9
        if (\is_null($encryptedRefreshToken)) {
110 1
            throw OAuthServerException::invalidRequest('refresh_token');
111
        }
112
113
        // Validate refresh token
114
        try {
115 8
            $refreshToken = $this->decrypt($encryptedRefreshToken);
116 1
        } catch (Exception $e) {
117 1
            throw OAuthServerException::invalidRefreshToken('Cannot decrypt the refresh token', $e);
118
        }
119
120 7
        $refreshTokenData = \json_decode($refreshToken, true);
121 7
        if ($refreshTokenData['client_id'] !== $clientId) {
122 1
            $this->getEmitter()->emit(new RequestEvent(RequestEvent::REFRESH_TOKEN_CLIENT_FAILED, $request));
123 1
            throw OAuthServerException::invalidRefreshToken('Token is not linked to client');
124
        }
125
126 6
        if ($refreshTokenData['expire_time'] < \time()) {
127 1
            throw OAuthServerException::invalidRefreshToken('Token has expired');
128
        }
129
130 5
        if ($this->refreshTokenRepository->isRefreshTokenRevoked($refreshTokenData['refresh_token_id']) === true) {
131 1
            throw OAuthServerException::invalidRefreshToken('Token has been revoked');
132
        }
133
134 4
        return $refreshTokenData;
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140 10
    public function getIdentifier()
141
    {
142 10
        return 'refresh_token';
143
    }
144
}
145