Completed
Pull Request — master (#925)
by
unknown
02:52
created

respondToIntrospectionRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
204
     */
205
    public function setIntrospectionReponseType(IntrospectionResponse $reponseType)
206
    {
207
        $this->introspectionResponseType = $reponseType;
208
    }
209
210
    /**
211
     * Get the introspection response
212
     *
213
     * @return ResponseTypeInterface
214
     */
215
    protected function getIntrospectionResponseType()
216
    {
217
        if ($this->introspectionResponseType instanceof IntrospectionResponse === false) {
218
            $this->introspectionResponseType = new IntrospectionResponse;
219
        }
220
221
        return $this->introspectionResponseType;
222
    }
223
224
    /**
225
     * Return an introspection response.
226
     *
227
     * @param ServerRequestInterface $request
228
     * @param ResponseInterface      $response
229
     *
230
     * @return ResponseInterface
231
     */
232
    public function respondToIntrospectionRequest(ServerRequestInterface $request, ResponseInterface $response)
233
    {
234
        $introspector = new Introspector($this->accessTokenRepository, $this->privateKey, new Parser);
235
        $introspectionResponse = $introspector->respondToIntrospectionRequest(
236
            $request,
237
            $this->getIntrospectionResponseType()
0 ignored issues
show
Compatibility introduced by
$this->getIntrospectionResponseType() of type object<League\OAuth2\Ser...\ResponseTypeInterface> is not a sub-type of object<League\OAuth2\Ser...\IntrospectionResponse>. It seems like you assume a concrete implementation of the interface League\OAuth2\Server\Res...s\ResponseTypeInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
238
        );
239
240
        return $introspectionResponse->generateHttpResponse($response);
241
    }
242
243
    /**
244
     * Get the token type that grants will return in the HTTP response.
245
     *
246
     * @return ResponseTypeInterface
247
     */
248 4
    protected function getResponseType()
249
    {
250 4
        if ($this->responseType instanceof ResponseTypeInterface === false) {
251 1
            $this->responseType = new BearerTokenResponse();
252
        }
253
254 4
        if ($this->responseType instanceof AbstractResponseType === true) {
255 4
            $this->responseType->setPrivateKey($this->privateKey);
256
        }
257 4
        $this->responseType->setEncryptionKey($this->encryptionKey);
258
259 4
        return $this->responseType;
260
    }
261
262
    /**
263
     * Set the default scope for the authorization server.
264
     *
265
     * @param string $defaultScope
266
     */
267 3
    public function setDefaultScope($defaultScope)
268
    {
269 3
        $this->defaultScope = $defaultScope;
270 3
    }
271
}
272