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

AuthorizationServer::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 10
cts 10
cp 1
rs 9.6333
c 0
b 0
f 0
cc 2
nc 2
nop 6
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 null|IntrospectionResponse
60
     */
61
    protected $introspectionResponseType;
62
63
    /**
64
     * @var ClientRepositoryInterface
65
     */
66
    private $clientRepository;
67
68
    /**
69
     * @var AccessTokenRepositoryInterface
70
     */
71
    private $accessTokenRepository;
72
73
    /**
74
     * @var ScopeRepositoryInterface
75
     */
76
    private $scopeRepository;
77
78
    /**
79
     * @var string|Key
80
     */
81
    private $encryptionKey;
82
83
    /**
84
     * @var string
85
     */
86
    private $defaultScope = '';
87
88
    /**
89
     * New server instance.
90
     *
91
     * @param ClientRepositoryInterface      $clientRepository
92
     * @param AccessTokenRepositoryInterface $accessTokenRepository
93
     * @param ScopeRepositoryInterface       $scopeRepository
94
     * @param CryptKey|string                $privateKey
95
     * @param string|Key                     $encryptionKey
96
     * @param null|ResponseTypeInterface     $responseType
97
     */
98 9
    public function __construct(
99
        ClientRepositoryInterface $clientRepository,
100
        AccessTokenRepositoryInterface $accessTokenRepository,
101
        ScopeRepositoryInterface $scopeRepository,
102
        $privateKey,
103
        $encryptionKey,
104
        ResponseTypeInterface $responseType = null
105
    ) {
106 9
        $this->clientRepository = $clientRepository;
107 9
        $this->accessTokenRepository = $accessTokenRepository;
108 9
        $this->scopeRepository = $scopeRepository;
109
110 9
        if ($privateKey instanceof CryptKey === false) {
111 9
            $privateKey = new CryptKey($privateKey);
112
        }
113 9
        $this->privateKey = $privateKey;
114 9
        $this->encryptionKey = $encryptionKey;
115 9
        $this->responseType = $responseType;
116 9
    }
117
118
    /**
119
     * Enable a grant type on the server.
120
     *
121
     * @param GrantTypeInterface $grantType
122
     * @param null|\DateInterval $accessTokenTTL
123
     */
124 7
    public function enableGrantType(GrantTypeInterface $grantType, \DateInterval $accessTokenTTL = null)
125
    {
126 7
        if ($accessTokenTTL instanceof \DateInterval === false) {
127 4
            $accessTokenTTL = new \DateInterval('PT1H');
128
        }
129
130 7
        $grantType->setAccessTokenRepository($this->accessTokenRepository);
131 7
        $grantType->setClientRepository($this->clientRepository);
132 7
        $grantType->setScopeRepository($this->scopeRepository);
133 7
        $grantType->setDefaultScope($this->defaultScope);
134 7
        $grantType->setPrivateKey($this->privateKey);
135 7
        $grantType->setEmitter($this->getEmitter());
136 7
        $grantType->setEncryptionKey($this->encryptionKey);
137
138 7
        $this->enabledGrantTypes[$grantType->getIdentifier()] = $grantType;
139 7
        $this->grantTypeAccessTokenTTL[$grantType->getIdentifier()] = $accessTokenTTL;
140 7
    }
141
142
    /**
143
     * Validate an authorization request
144
     *
145
     * @param ServerRequestInterface $request
146
     *
147
     * @throws OAuthServerException
148
     *
149
     * @return AuthorizationRequest
150
     */
151 3
    public function validateAuthorizationRequest(ServerRequestInterface $request)
152
    {
153 3
        foreach ($this->enabledGrantTypes as $grantType) {
154 2
            if ($grantType->canRespondToAuthorizationRequest($request)) {
155 2
                return $grantType->validateAuthorizationRequest($request);
156
            }
157
        }
158
159 1
        throw OAuthServerException::unsupportedGrantType();
160
    }
161
162
    /**
163
     * Complete an authorization request
164
     *
165
     * @param AuthorizationRequest $authRequest
166
     * @param ResponseInterface    $response
167
     *
168
     * @return ResponseInterface
169
     */
170 1
    public function completeAuthorizationRequest(AuthorizationRequest $authRequest, ResponseInterface $response)
171
    {
172 1
        return $this->enabledGrantTypes[$authRequest->getGrantTypeId()]
173 1
            ->completeAuthorizationRequest($authRequest)
174 1
            ->generateHttpResponse($response);
175
    }
176
177
    /**
178
     * Return an access token response.
179
     *
180
     * @param ServerRequestInterface $request
181
     * @param ResponseInterface      $response
182
     *
183
     * @throws OAuthServerException
184
     *
185
     * @return ResponseInterface
186
     */
187 4
    public function respondToAccessTokenRequest(ServerRequestInterface $request, ResponseInterface $response)
188
    {
189 4
        foreach ($this->enabledGrantTypes as $grantType) {
190 4
            if (!$grantType->canRespondToAccessTokenRequest($request)) {
191 1
                continue;
192
            }
193 3
            $tokenResponse = $grantType->respondToAccessTokenRequest(
194 3
                $request,
195 3
                $this->getResponseType(),
196 3
                $this->grantTypeAccessTokenTTL[$grantType->getIdentifier()]
197
            );
198
199 2
            if ($tokenResponse instanceof ResponseTypeInterface) {
200 2
                return $tokenResponse->generateHttpResponse($response);
201
            }
202
        }
203
204 1
        throw OAuthServerException::unsupportedGrantType();
205
    }
206
207
    /**
208
     * @param IntrospectionResponse $reponseType
209
     */
210
    public function setIntrospectionReponseType(IntrospectionResponse $reponseType)
211
    {
212
        $this->introspectionResponseType = $reponseType;
213
    }
214
215
    /**
216
     * Get the introspection response
217
     *
218
     * @return IntrospectionResponse
219
     */
220
    protected function getIntrospectionResponseType()
221
    {
222
        if ($this->introspectionResponseType instanceof IntrospectionResponse === false) {
223
            $this->introspectionResponseType = new IntrospectionResponse;
224
        }
225
226
        return $this->introspectionResponseType;
227
    }
228
229
    /**
230
     * Return an introspection response.
231
     *
232
     * @param ServerRequestInterface $request
233
     * @param ResponseInterface      $response
234
     *
235
     * @return ResponseInterface
236
     */
237
    public function respondToIntrospectionRequest(ServerRequestInterface $request, ResponseInterface $response)
238
    {
239
        $introspector = new Introspector($this->accessTokenRepository, $this->privateKey, new Parser);
240
        $introspectionResponse = $introspector->respondToIntrospectionRequest(
241
            $request,
242
            $this->getIntrospectionResponseType()
243
        );
244
245
        return $introspectionResponse->generateHttpResponse($response);
246
    }
247
248
    /**
249
     * Get the token type that grants will return in the HTTP response.
250
     *
251
     * @return ResponseTypeInterface
252
     */
253 4
    protected function getResponseType()
254
    {
255 4
        if ($this->responseType instanceof ResponseTypeInterface === false) {
256 1
            $this->responseType = new BearerTokenResponse();
257
        }
258
259 4
        if ($this->responseType instanceof AbstractResponseType === true) {
260 4
            $this->responseType->setPrivateKey($this->privateKey);
261
        }
262 4
        $this->responseType->setEncryptionKey($this->encryptionKey);
263
264 4
        return $this->responseType;
265
    }
266
267
    /**
268
     * Set the default scope for the authorization server.
269
     *
270
     * @param string $defaultScope
271
     */
272 3
    public function setDefaultScope($defaultScope)
273
    {
274 3
        $this->defaultScope = $defaultScope;
275 3
    }
276
}
277