RefreshTokenGrant::createAuthorizationResponse()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 6
rs 10
1
<?php
2
3
/**
4
 * Platine OAuth2
5
 *
6
 * Platine OAuth2 is a library that implements the OAuth2 specification
7
 *
8
 * This content is released under the MIT License (MIT)
9
 *
10
 * Copyright (c) 2020 Platine OAuth2
11
 *
12
 * Permission is hereby granted, free of charge, to any person obtaining a copy
13
 * of this software and associated documentation files (the "Software"), to deal
14
 * in the Software without restriction, including without limitation the rights
15
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
 * copies of the Software, and to permit persons to whom the Software is
17
 * furnished to do so, subject to the following conditions:
18
 *
19
 * The above copyright notice and this permission notice shall be included in all
20
 * copies or substantial portions of the Software.
21
 *
22
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
 * SOFTWARE.
29
 */
30
31
declare(strict_types=1);
32
33
namespace Platine\OAuth2\Grant;
34
35
use Platine\Http\ResponseInterface;
36
use Platine\Http\ServerRequestInterface;
37
use Platine\OAuth2\Configuration;
38
use Platine\OAuth2\Entity\Client;
39
use Platine\OAuth2\Entity\RefreshToken;
40
use Platine\OAuth2\Entity\TokenOwnerInterface;
41
use Platine\OAuth2\Exception\OAuth2Exception;
42
use Platine\OAuth2\Service\AccessTokenService;
43
use Platine\OAuth2\Service\RefreshTokenService;
44
45
/**
46
 * @class RefreshTokenGrant
47
 * @package Platine\OAuth2\Grant
48
 */
49
class RefreshTokenGrant extends BaseGrant
50
{
51
    public const GRANT_TYPE = 'refresh_token';
52
    public const GRANT_RESPONSE_TYPE = '';
53
54
    /**
55
     * Create new instance
56
     * @param AccessTokenService $accessTokenService
57
     * @param RefreshTokenService $refreshTokenService
58
     * @param Configuration $configuration
59
     */
60
    public function __construct(
61
        protected AccessTokenService $accessTokenService,
62
        protected RefreshTokenService $refreshTokenService,
63
        protected Configuration $configuration
64
    ) {
65
    }
66
67
        /**
68
     * {@inheritdoc}
69
     */
70
    public function createAuthorizationResponse(
71
        ServerRequestInterface $request,
72
        Client $client,
73
        ?TokenOwnerInterface $owner = null
74
    ): ResponseInterface {
75
        throw OAuth2Exception::invalidRequest('Refresh token grant does not support authorization');
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function createTokenResponse(
82
        ServerRequestInterface $request,
83
        ?Client $client = null,
84
        ?TokenOwnerInterface $owner = null
85
    ): ResponseInterface {
86
        $postParams = (array) $request->getParsedBody();
87
        $refreshTokenValue = $postParams['refresh_token'] ?? null;
88
        if ($refreshTokenValue === null) {
89
            throw OAuth2Exception::invalidRequest('Refresh token is missing in request');
90
        }
91
92
        // We can fetch the actual token, and validate it
93
        /** @var RefreshToken|null $refreshToken */
94
        $refreshToken = $this->refreshTokenService->getToken((string) $refreshTokenValue);
95
        if ($refreshToken === null || $refreshToken->isExpired()) {
96
            throw OAuth2Exception::invalidGrant('Refresh token is expired');
97
        }
98
99
        // We can now create a new access token! First, we need to make some checks on the asked scopes,
100
        // because according to the spec, a refresh token can create an access token
101
        // with an equal or lesser scope, but not more
102
        $scope = $postParams['scope'] ?? null;
103
        $scopes = is_string($scope) ? explode(' ', $scope) : $refreshToken->getScopes();
104
        if ($refreshToken->matchScopes($scopes) === false) {
105
            throw OAuth2Exception::invalidScope(
106
                'The scope of the new access token exceeds the scope(s) of the refresh token'
107
            );
108
        }
109
110
        $refreshTokenOwner = $refreshToken->getOwner();
111
        $accessToken = $this->accessTokenService->createToken($refreshTokenOwner, $client, $scopes);
112
        // We may want to revoke the old refresh token
113
        if ($this->configuration->isRotateRefreshToken()) {
114
            if ($this->configuration->isRevokeRotatedRefreshToken()) {
115
                $this->refreshTokenService->delete($refreshToken);
116
            }
117
118
            /** @var RefreshToken $refreshToken */
119
            $refreshToken = $this->refreshTokenService->createToken($refreshTokenOwner, $client, $scopes);
120
        }
121
122
        return $this->generateTokenResponse($accessToken, $refreshToken, true);
123
    }
124
125
126
    /**
127
     * {@inheritdoc}
128
     */
129
    public function allowPublicClients(): bool
130
    {
131
        return true;
132
    }
133
}
134