Completed
Pull Request — master (#869)
by
unknown
01:50
created

respondToIntrospectionRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
ccs 0
cts 4
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
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\Repositories\AccessTokenRepositoryInterface;
18
use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
19
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
20
use League\OAuth2\Server\RequestTypes\AuthorizationRequest;
21
use League\OAuth2\Server\ResponseTypes\AbstractResponseType;
22
use League\OAuth2\Server\ResponseTypes\BearerTokenResponse;
23
use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface;
24
use Psr\Http\Message\ResponseInterface;
25
use Psr\Http\Message\ServerRequestInterface;
26
27
class AuthorizationServer implements EmitterAwareInterface
28
{
29
    use EmitterAwareTrait;
30
31
    /**
32
     * @var GrantTypeInterface[]
33
     */
34
    protected $enabledGrantTypes = [];
35
36
    /**
37
     * @var \DateInterval[]
38
     */
39
    protected $grantTypeAccessTokenTTL = [];
40
41
    /**
42
     * @var CryptKey
43
     */
44
    protected $privateKey;
45
46
    /**
47
     * @var CryptKey
48
     */
49
    protected $publicKey;
50
51
    /**
52
     * @var null|ResponseTypeInterface
53
     */
54
    protected $responseType;
55
56
    /**
57
     * @var ClientRepositoryInterface
58
     */
59
    private $clientRepository;
60
61
    /**
62
     * @var AccessTokenRepositoryInterface
63
     */
64
    private $accessTokenRepository;
65
66
    /**
67
     * @var ScopeRepositoryInterface
68
     */
69
    private $scopeRepository;
70
71
    /**
72
     * @var string|Key
73
     */
74
    private $encryptionKey;
75
76
    /**
77
     * @var string
78
     */
79
    private $defaultScope = '';
80
81
    /**
82
     * New server instance.
83
     *
84
     * @param ClientRepositoryInterface      $clientRepository
85
     * @param AccessTokenRepositoryInterface $accessTokenRepository
86
     * @param ScopeRepositoryInterface       $scopeRepository
87
     * @param CryptKey|string                $privateKey
88
     * @param string|Key                     $encryptionKey
89
     * @param null|ResponseTypeInterface     $responseType
90
     */
91 9
    public function __construct(
92
        ClientRepositoryInterface $clientRepository,
93
        AccessTokenRepositoryInterface $accessTokenRepository,
94
        ScopeRepositoryInterface $scopeRepository,
95
        $privateKey,
96
        $encryptionKey,
97
        ResponseTypeInterface $responseType = null
98
    ) {
99 9
        $this->clientRepository = $clientRepository;
100 9
        $this->accessTokenRepository = $accessTokenRepository;
101 9
        $this->scopeRepository = $scopeRepository;
102
103 9
        if ($privateKey instanceof CryptKey === false) {
104 9
            $privateKey = new CryptKey($privateKey);
105
        }
106 9
        $this->privateKey = $privateKey;
107 9
        $this->encryptionKey = $encryptionKey;
108 9
        $this->responseType = $responseType;
109 9
    }
110
111
    /**
112
     * Enable a grant type on the server.
113
     *
114
     * @param GrantTypeInterface $grantType
115
     * @param null|\DateInterval $accessTokenTTL
116
     */
117 7
    public function enableGrantType(GrantTypeInterface $grantType, \DateInterval $accessTokenTTL = null)
118
    {
119 7
        if ($accessTokenTTL instanceof \DateInterval === false) {
120 4
            $accessTokenTTL = new \DateInterval('PT1H');
121
        }
122
123 7
        $grantType->setAccessTokenRepository($this->accessTokenRepository);
124 7
        $grantType->setClientRepository($this->clientRepository);
125 7
        $grantType->setScopeRepository($this->scopeRepository);
126 7
        $grantType->setDefaultScope($this->defaultScope);
127 7
        $grantType->setPrivateKey($this->privateKey);
128 7
        $grantType->setEmitter($this->getEmitter());
129 7
        $grantType->setEncryptionKey($this->encryptionKey);
130
131 7
        $this->enabledGrantTypes[$grantType->getIdentifier()] = $grantType;
132 7
        $this->grantTypeAccessTokenTTL[$grantType->getIdentifier()] = $accessTokenTTL;
133 7
    }
134
135
    /**
136
     * Validate an authorization request
137
     *
138
     * @param ServerRequestInterface $request
139
     *
140
     * @throws OAuthServerException
141
     *
142
     * @return AuthorizationRequest
143
     */
144 3
    public function validateAuthorizationRequest(ServerRequestInterface $request)
145
    {
146 3
        foreach ($this->enabledGrantTypes as $grantType) {
147 2
            if ($grantType->canRespondToAuthorizationRequest($request)) {
148 2
                return $grantType->validateAuthorizationRequest($request);
149
            }
150
        }
151
152 1
        throw OAuthServerException::unsupportedGrantType();
153
    }
154
155
    /**
156
     * Complete an authorization request
157
     *
158
     * @param AuthorizationRequest $authRequest
159
     * @param ResponseInterface    $response
160
     *
161
     * @return ResponseInterface
162
     */
163 1
    public function completeAuthorizationRequest(AuthorizationRequest $authRequest, ResponseInterface $response)
164
    {
165 1
        return $this->enabledGrantTypes[$authRequest->getGrantTypeId()]
166 1
            ->completeAuthorizationRequest($authRequest)
167 1
            ->generateHttpResponse($response);
168
    }
169
170
    /**
171
     * Return an access token response.
172
     *
173
     * @param ServerRequestInterface $request
174
     * @param ResponseInterface      $response
175
     *
176
     * @throws OAuthServerException
177
     *
178
     * @return ResponseInterface
179
     */
180 4
    public function respondToAccessTokenRequest(ServerRequestInterface $request, ResponseInterface $response)
181
    {
182 4
        foreach ($this->enabledGrantTypes as $grantType) {
183 4
            if (!$grantType->canRespondToAccessTokenRequest($request)) {
184 1
                continue;
185
            }
186 3
            $tokenResponse = $grantType->respondToAccessTokenRequest(
187 3
                $request,
188 3
                $this->getResponseType(),
189 3
                $this->grantTypeAccessTokenTTL[$grantType->getIdentifier()]
190
            );
191
192 2
            if ($tokenResponse instanceof ResponseTypeInterface) {
193 2
                return $tokenResponse->generateHttpResponse($response);
194
            }
195
        }
196
197 1
        throw OAuthServerException::unsupportedGrantType();
198
    }
199
200
    /**
201
     * Return an introspection response.
202
     *
203
     * @param ServerRequestInterface $request
204
     * @param ResponseInterface      $response
205
     *
206
     * @return ResponseInterface
207
     */
208
    public function respondToIntrospectionRequest(ServerRequestInterface $request, ResponseInterface $response)
209
    {
210
        $introspector = new Introspector($this->accessTokenRepository, $this->privateKey);
0 ignored issues
show
Bug introduced by
The call to Introspector::__construct() misses a required argument $parser.

This check looks for function calls that miss required arguments.

Loading history...
211
        $introspectionResponse = $introspector->respondToIntrospectionRequest($request);
212
213
        return $introspectionResponse->generateHttpResponse($response);
214
    }
215
216
    /**
217
     * Get the token type that grants will return in the HTTP response.
218
     *
219
     * @return ResponseTypeInterface
220
     */
221 4
    protected function getResponseType()
222
    {
223 4
        if ($this->responseType instanceof ResponseTypeInterface === false) {
224 1
            $this->responseType = new BearerTokenResponse();
225
        }
226
227 4
        if ($this->responseType instanceof AbstractResponseType === true) {
228 4
            $this->responseType->setPrivateKey($this->privateKey);
229
        }
230 4
        $this->responseType->setEncryptionKey($this->encryptionKey);
231
232 4
        return $this->responseType;
233
    }
234
235
    /**
236
     * Set the default scope for the authorization server.
237
     *
238
     * @param string $defaultScope
239
     */
240 3
    public function setDefaultScope($defaultScope)
241
    {
242 3
        $this->defaultScope = $defaultScope;
243 3
    }
244
}
245