Completed
Pull Request — master (#925)
by
unknown
01:39
created

AuthorizationServer::setIntrospectionValidator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
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 Defuse\Crypto\Key;
13
use League\Event\EmitterAwareInterface;
14
use League\Event\EmitterAwareTrait;
15
use League\OAuth2\Server\Exception\OAuthServerException;
16
use League\OAuth2\Server\Grant\GrantTypeInterface;
17
use League\OAuth2\Server\IntrospectionValidators\BearerTokenValidator;
18
use League\OAuth2\Server\IntrospectionValidators\IntrospectionValidatorInterface;
19
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
20
use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
21
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
22
use League\OAuth2\Server\RequestTypes\AuthorizationRequest;
23
use League\OAuth2\Server\ResponseTypes\AbstractResponseType;
24
use League\OAuth2\Server\ResponseTypes\BearerTokenIntrospectionResponse;
25
use League\OAuth2\Server\ResponseTypes\BearerTokenResponse;
26
use League\OAuth2\Server\ResponseTypes\IntrospectionResponse;
27
use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface;
28
use Psr\Http\Message\ResponseInterface;
29
use Psr\Http\Message\ServerRequestInterface;
30
31
class AuthorizationServer implements EmitterAwareInterface
32
{
33
    use EmitterAwareTrait;
34
35
    /**
36
     * @var GrantTypeInterface[]
37
     */
38
    protected $enabledGrantTypes = [];
39
40
    /**
41
     * @var \DateInterval[]
42
     */
43
    protected $grantTypeAccessTokenTTL = [];
44
45
    /**
46
     * @var CryptKey
47
     */
48
    protected $privateKey;
49
50
    /**
51
     * @var CryptKey
52
     */
53
    protected $publicKey;
54
55
    /**
56
     * @var ResponseTypeInterface
57
     */
58
    protected $responseTypePrototype;
59
60
    /**
61
     * @var null|IntrospectionResponse
62
     */
63
    protected $introspectionResponseType;
64
65
    /**
66
     * @var null|IntrospectionValidatorInterface
67
     */
68
    protected $introspectionValidator;
69
70
    /**
71
     * @var null|Introspector
72
     */
73
    protected $introspector;
74
75
    /**
76
     * @var ClientRepositoryInterface
77
     */
78
    private $clientRepository;
79
80
    /**
81
     * @var AccessTokenRepositoryInterface
82
     */
83
    private $accessTokenRepository;
84
85
    /**
86
     * @var ScopeRepositoryInterface
87
     */
88
    private $scopeRepository;
89
90
    /**
91
     * @var string|Key
92
     */
93
    private $encryptionKey;
94
95
    /**
96
     * @var string
97
     */
98
    private $defaultScope = '';
99
100
    /**
101
     * New server instance.
102
     *
103
     * @param ClientRepositoryInterface      $clientRepository
104
     * @param AccessTokenRepositoryInterface $accessTokenRepository
105
     * @param ScopeRepositoryInterface       $scopeRepository
106
     * @param CryptKey|string                $privateKey
107
     * @param string|Key                     $encryptionKey
108
     * @param null|ResponseTypeInterface     $responseTypePrototype
109
     */
110 10
    public function __construct(
111
        ClientRepositoryInterface $clientRepository,
112
        AccessTokenRepositoryInterface $accessTokenRepository,
113
        ScopeRepositoryInterface $scopeRepository,
114
        $privateKey,
115
        $encryptionKey,
116
        ResponseTypeInterface $responseTypePrototype = null
117
    ) {
118 10
        $this->clientRepository = $clientRepository;
119 10
        $this->accessTokenRepository = $accessTokenRepository;
120 10
        $this->scopeRepository = $scopeRepository;
121
122 10
        if ($privateKey instanceof CryptKey === false) {
123 10
            $privateKey = new CryptKey($privateKey);
124
        }
125
126 10
        $this->privateKey = $privateKey;
127 10
        $this->encryptionKey = $encryptionKey;
128
129 10
        if ($responseTypePrototype === null) {
130 5
            $responseTypePrototype = new BearerTokenResponse();
131
        } else {
132 5
            $responseTypePrototype = clone $responseTypePrototype;
133
        }
134
135 10
        if ($responseTypePrototype instanceof AbstractResponseType) {
136 10
            $responseTypePrototype->setPrivateKey($this->privateKey);
137
        }
138
139 10
        $responseTypePrototype->setEncryptionKey($this->encryptionKey);
140
141 10
        $this->responseTypePrototype = $responseTypePrototype;
142 10
    }
143
144
    /**
145
     * Enable a grant type on the server.
146
     *
147
     * @param GrantTypeInterface $grantType
148
     * @param null|\DateInterval $accessTokenTTL
149
     */
150 7
    public function enableGrantType(GrantTypeInterface $grantType, \DateInterval $accessTokenTTL = null)
151
    {
152 7
        if ($accessTokenTTL instanceof \DateInterval === false) {
153 4
            $accessTokenTTL = new \DateInterval('PT1H');
154
        }
155
156 7
        $grantType->setAccessTokenRepository($this->accessTokenRepository);
157 7
        $grantType->setClientRepository($this->clientRepository);
158 7
        $grantType->setScopeRepository($this->scopeRepository);
159 7
        $grantType->setDefaultScope($this->defaultScope);
160 7
        $grantType->setPrivateKey($this->privateKey);
161 7
        $grantType->setEmitter($this->getEmitter());
162 7
        $grantType->setEncryptionKey($this->encryptionKey);
163
164 7
        $this->enabledGrantTypes[$grantType->getIdentifier()] = $grantType;
165 7
        $this->grantTypeAccessTokenTTL[$grantType->getIdentifier()] = $accessTokenTTL;
166 7
    }
167
168
    /**
169
     * Validate an authorization request
170
     *
171
     * @param ServerRequestInterface $request
172
     *
173
     * @throws OAuthServerException
174
     *
175
     * @return AuthorizationRequest
176
     */
177 3
    public function validateAuthorizationRequest(ServerRequestInterface $request)
178
    {
179 3
        foreach ($this->enabledGrantTypes as $grantType) {
180 2
            if ($grantType->canRespondToAuthorizationRequest($request)) {
181 2
                return $grantType->validateAuthorizationRequest($request);
182
            }
183
        }
184
185 1
        throw OAuthServerException::unsupportedGrantType();
186
    }
187
188
    /**
189
     * Complete an authorization request
190
     *
191
     * @param AuthorizationRequest $authRequest
192
     * @param ResponseInterface    $response
193
     *
194
     * @return ResponseInterface
195
     */
196 1
    public function completeAuthorizationRequest(AuthorizationRequest $authRequest, ResponseInterface $response)
197
    {
198 1
        return $this->enabledGrantTypes[$authRequest->getGrantTypeId()]
199 1
            ->completeAuthorizationRequest($authRequest)
200 1
            ->generateHttpResponse($response);
201
    }
202
203
    /**
204
     * Return an access token response.
205
     *
206
     * @param ServerRequestInterface $request
207
     * @param ResponseInterface      $response
208
     *
209
     * @throws OAuthServerException
210
     *
211
     * @return ResponseInterface
212
     */
213 4
    public function respondToAccessTokenRequest(ServerRequestInterface $request, ResponseInterface $response)
214
    {
215 4
        foreach ($this->enabledGrantTypes as $grantType) {
216 4
            if (!$grantType->canRespondToAccessTokenRequest($request)) {
217 1
                continue;
218
            }
219 3
            $tokenResponse = $grantType->respondToAccessTokenRequest(
220 3
                $request,
221 3
                $this->newResponseType(),
222 3
                $this->grantTypeAccessTokenTTL[$grantType->getIdentifier()]
223
            );
224
225 2
            if ($tokenResponse instanceof ResponseTypeInterface) {
226 2
                return $tokenResponse->generateHttpResponse($response);
227
            }
228
        }
229
230 1
        throw OAuthServerException::unsupportedGrantType();
231
    }
232
233
    /**
234
     * @param IntrospectionResponse $reponseType
235
     */
236
    public function setIntrospectionReponseType(IntrospectionResponse $reponseType)
237
    {
238
        $this->introspectionResponseType = $reponseType;
239
    }
240
241
    /**
242
     * @param IntrospectionValidatorInterface $introspectionValidator
243
     */
244
    public function setIntrospectionValidator(IntrospectionValidatorInterface $introspectionValidator)
245
    {
246
        $this->introspectionValidator = $introspectionValidator;
247
    }
248
249
    /**
250
     * Get the introspection response
251
     *
252
     * @return IntrospectionResponse
253
     */
254
    protected function getIntrospectionResponseType()
255
    {
256
        if ($this->introspectionResponseType instanceof IntrospectionResponse === false) {
257
            $this->introspectionResponseType = new BearerTokenIntrospectionResponse();
258
        }
259
260
        return $this->introspectionResponseType;
261
    }
262
263
    /**
264
     * Get the introspection response
265
     *
266
     * @return IntrospectionValidatorInterface
267
     */
268
    protected function getIntrospectionValidator()
269
    {
270
        if ($this->introspectionValidator instanceof IntrospectionValidatorInterface === false) {
271
            $this->introspectionValidator = new BearerTokenValidator($this->accessTokenRepository);
272
        }
273
274
        return $this->introspectionValidator;
275
    }
276
277
    /**
278
     * Return an introspection response.
279
     *
280
     * @param ServerRequestInterface $request
281
     * @param ResponseInterface      $response
282
     *
283
     * @return ResponseInterface
284
     */
285
    public function respondToIntrospectionRequest(ServerRequestInterface $request, ResponseInterface $response)
286
    {
287
        $introspector = $this->getIntrospector();
288
289
        $introspectionResponse = $introspector->respondToIntrospectionRequest(
290
            $request,
291
            $this->getIntrospectionResponseType()
292
        );
293
294
        return $introspectionResponse->generateHttpResponse($response);
295
    }
296
297
    /**
298
     * Validate an introspection request.
299
     *
300
     * @param ServerRequestInterface $request
301
     */
302
    public function validateIntrospectionRequest(ServerRequestInterface $request)
303
    {
304
        $introspector = $this->getIntrospector();
305
        $introspector->validateIntrospectionRequest($request);
306
    }
307
308
    /**
309
     * Returns the introspector.
310
     *
311
     * @return Introspector
312
     */
313
    private function getIntrospector()
314
    {
315
        if (!isset($this->introspector)) {
316
            $this->introspector = new Introspector(
317
                $this->accessTokenRepository,
318
                $this->privateKey,
319
                $this->getIntrospectionValidator()
320
            );
321
        }
322
323
        return $this->introspector;
324
    }
325
326
    /**
327
     * Get the token type that grants will return in the HTTP response.
328
     *
329
     * @return ResponseTypeInterface
330
     */
331 5
    protected function newResponseType()
332
    {
333 5
        return clone $this->responseTypePrototype;
334
    }
335
336
    /**
337
     * Set the default scope for the authorization server.
338
     *
339
     * @param string $defaultScope
340
     */
341 3
    public function setDefaultScope($defaultScope)
342
    {
343 3
        $this->defaultScope = $defaultScope;
344 3
    }
345
}
346