Passed
Push — master ( 8c33b5...d7634f )
by Andrew
05:35
created

AuthorizationServer::revokeRefreshTokens()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author      Alex Bilbie <[email protected]>
4
 * @copyright   Copyright (c) Alex Bilbie
5
 * @license     http://mit-license.org/
6
 *
7
 * @link        https://github.com/thephpleague/oauth2-server
8
 */
9
10
namespace League\OAuth2\Server;
11
12
use DateInterval;
13
use Defuse\Crypto\Key;
14
use League\Event\EmitterAwareInterface;
15
use League\Event\EmitterAwareTrait;
16
use League\OAuth2\Server\Exception\OAuthServerException;
17
use League\OAuth2\Server\Grant\GrantTypeInterface;
18
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
19
use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
20
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
21
use League\OAuth2\Server\RequestTypes\AuthorizationRequest;
22
use League\OAuth2\Server\ResponseTypes\AbstractResponseType;
23
use League\OAuth2\Server\ResponseTypes\BearerTokenResponse;
24
use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface;
25
use Psr\Http\Message\ResponseInterface;
26
use Psr\Http\Message\ServerRequestInterface;
27
28
class AuthorizationServer implements EmitterAwareInterface
29
{
30
    use EmitterAwareTrait;
31
32
    /**
33
     * @var GrantTypeInterface[]
34
     */
35
    protected $enabledGrantTypes = [];
36
37
    /**
38
     * @var DateInterval[]
39
     */
40
    protected $grantTypeAccessTokenTTL = [];
41
42
    /**
43
     * @var CryptKey
44
     */
45
    protected $privateKey;
46
47
    /**
48
     * @var CryptKey
49
     */
50
    protected $publicKey;
51
52
    /**
53
     * @var ResponseTypeInterface
54
     */
55
    protected $responseType;
56
57
    /**
58
     * @var ClientRepositoryInterface
59
     */
60
    private $clientRepository;
61
62
    /**
63
     * @var AccessTokenRepositoryInterface
64
     */
65
    private $accessTokenRepository;
66
67
    /**
68
     * @var ScopeRepositoryInterface
69
     */
70
    private $scopeRepository;
71
72
    /**
73
     * @var string|Key
74
     */
75
    private $encryptionKey;
76
77
    /**
78
     * @var string
79
     */
80
    private $defaultScope = '';
81
82
    /**
83
     * @var bool
84
     */
85
    private $revokeRefreshTokens = true;
86
87
    /**
88
     * New server instance.
89
     *
90
     * @param ClientRepositoryInterface      $clientRepository
91
     * @param AccessTokenRepositoryInterface $accessTokenRepository
92
     * @param ScopeRepositoryInterface       $scopeRepository
93
     * @param CryptKey|string                $privateKey
94
     * @param string|Key                     $encryptionKey
95
     * @param null|ResponseTypeInterface     $responseType
96
     */
97 11
    public function __construct(
98
        ClientRepositoryInterface $clientRepository,
99
        AccessTokenRepositoryInterface $accessTokenRepository,
100
        ScopeRepositoryInterface $scopeRepository,
101
        $privateKey,
102
        $encryptionKey,
103
        ResponseTypeInterface $responseType = null
104
    ) {
105 11
        $this->clientRepository = $clientRepository;
106 11
        $this->accessTokenRepository = $accessTokenRepository;
107 11
        $this->scopeRepository = $scopeRepository;
108
109 11
        if ($privateKey instanceof CryptKey === false) {
110 11
            $privateKey = new CryptKey($privateKey);
111
        }
112
113 11
        $this->privateKey = $privateKey;
114 11
        $this->encryptionKey = $encryptionKey;
115
116 11
        if ($responseType === null) {
117 6
            $responseType = new BearerTokenResponse();
118
        } else {
119 5
            $responseType = clone $responseType;
120
        }
121
122 11
        $this->responseType = $responseType;
123 11
    }
124
125
    /**
126
     * Enable a grant type on the server.
127
     *
128
     * @param GrantTypeInterface $grantType
129
     * @param null|DateInterval  $accessTokenTTL
130
     */
131 7
    public function enableGrantType(GrantTypeInterface $grantType, DateInterval $accessTokenTTL = null)
132
    {
133 7
        if ($accessTokenTTL === null) {
134 4
            $accessTokenTTL = new DateInterval('PT1H');
135
        }
136
137 7
        $grantType->setAccessTokenRepository($this->accessTokenRepository);
138 7
        $grantType->setClientRepository($this->clientRepository);
139 7
        $grantType->setScopeRepository($this->scopeRepository);
140 7
        $grantType->setDefaultScope($this->defaultScope);
141 7
        $grantType->setPrivateKey($this->privateKey);
142 7
        $grantType->setEmitter($this->getEmitter());
143 7
        $grantType->setEncryptionKey($this->encryptionKey);
144 7
        $grantType->revokeRefreshTokens($this->revokeRefreshTokens);
0 ignored issues
show
Bug introduced by
The method revokeRefreshTokens() does not exist on League\OAuth2\Server\Grant\GrantTypeInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to League\OAuth2\Server\Grant\GrantTypeInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

144
        $grantType->/** @scrutinizer ignore-call */ 
145
                    revokeRefreshTokens($this->revokeRefreshTokens);
Loading history...
145
146 7
        $this->enabledGrantTypes[$grantType->getIdentifier()] = $grantType;
147 7
        $this->grantTypeAccessTokenTTL[$grantType->getIdentifier()] = $accessTokenTTL;
148 7
    }
149
150
    /**
151
     * Validate an authorization request
152
     *
153
     * @param ServerRequestInterface $request
154
     *
155
     * @throws OAuthServerException
156
     *
157
     * @return AuthorizationRequest
158
     */
159 3
    public function validateAuthorizationRequest(ServerRequestInterface $request)
160
    {
161 3
        foreach ($this->enabledGrantTypes as $grantType) {
162 2
            if ($grantType->canRespondToAuthorizationRequest($request)) {
163 2
                return $grantType->validateAuthorizationRequest($request);
164
            }
165
        }
166
167 1
        throw OAuthServerException::unsupportedGrantType();
168
    }
169
170
    /**
171
     * Complete an authorization request
172
     *
173
     * @param AuthorizationRequest $authRequest
174
     * @param ResponseInterface    $response
175
     *
176
     * @return ResponseInterface
177
     */
178 1
    public function completeAuthorizationRequest(AuthorizationRequest $authRequest, ResponseInterface $response)
179
    {
180 1
        return $this->enabledGrantTypes[$authRequest->getGrantTypeId()]
181 1
            ->completeAuthorizationRequest($authRequest)
182 1
            ->generateHttpResponse($response);
183
    }
184
185
    /**
186
     * Return an access token response.
187
     *
188
     * @param ServerRequestInterface $request
189
     * @param ResponseInterface      $response
190
     *
191
     * @throws OAuthServerException
192
     *
193
     * @return ResponseInterface
194
     */
195 4
    public function respondToAccessTokenRequest(ServerRequestInterface $request, ResponseInterface $response)
196
    {
197 4
        foreach ($this->enabledGrantTypes as $grantType) {
198 4
            if (!$grantType->canRespondToAccessTokenRequest($request)) {
199 1
                continue;
200
            }
201 3
            $tokenResponse = $grantType->respondToAccessTokenRequest(
202
                $request,
203 3
                $this->getResponseType(),
204 3
                $this->grantTypeAccessTokenTTL[$grantType->getIdentifier()]
205
            );
206
207 2
            if ($tokenResponse instanceof ResponseTypeInterface) {
208 2
                return $tokenResponse->generateHttpResponse($response);
209
            }
210
        }
211
212 1
        throw OAuthServerException::unsupportedGrantType();
213
    }
214
215
    /**
216
     * Get the token type that grants will return in the HTTP response.
217
     *
218
     * @return ResponseTypeInterface
219
     */
220 6
    protected function getResponseType()
221
    {
222 6
        $responseType = clone $this->responseType;
223
224 6
        if ($responseType instanceof AbstractResponseType) {
225 6
            $responseType->setPrivateKey($this->privateKey);
226
        }
227
228 6
        $responseType->setEncryptionKey($this->encryptionKey);
229
230 6
        return $responseType;
231
    }
232
233
    /**
234
     * Set the default scope for the authorization server.
235
     *
236
     * @param string $defaultScope
237
     */
238 3
    public function setDefaultScope($defaultScope)
239
    {
240 3
        $this->defaultScope = $defaultScope;
241 3
    }
242
243
    /**
244
     * Sets wether to revoke refresh tokens or not (for all grant types).
245
     *
246
     * @param bool $revokeRefreshTokens
247
     */
248
    public function revokeRefreshTokens(bool $revokeRefreshTokens): void
249
    {
250
        $this->revokeRefreshTokens = $revokeRefreshTokens;
251
    }
252
}
253