Completed
Push — master ( a34f5d...c87be9 )
by Andrew
11s
created

AuthorizationServer::newResponseType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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 ResponseTypeInterface
53
     */
54
    protected $responseTypePrototype;
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     $responseTypePrototype
90
     */
91 10
    public function __construct(
92
        ClientRepositoryInterface $clientRepository,
93
        AccessTokenRepositoryInterface $accessTokenRepository,
94
        ScopeRepositoryInterface $scopeRepository,
95
        $privateKey,
96
        $encryptionKey,
97
        ResponseTypeInterface $responseTypePrototype = null
98
    ) {
99 10
        $this->clientRepository = $clientRepository;
100 10
        $this->accessTokenRepository = $accessTokenRepository;
101 10
        $this->scopeRepository = $scopeRepository;
102
103 10
        if ($privateKey instanceof CryptKey === false) {
104 10
            $privateKey = new CryptKey($privateKey);
105
        }
106 10
        $this->privateKey = $privateKey;
107 10
        $this->encryptionKey = $encryptionKey;
108
109 10
        if ($responseTypePrototype === null) {
110 5
            $responseTypePrototype = new BearerTokenResponse();
111
        } else {
112 5
            $responseTypePrototype = clone $responseTypePrototype;
113
        }
114 10
        if ($responseTypePrototype instanceof AbstractResponseType) {
115 10
            $responseTypePrototype->setPrivateKey($this->privateKey);
116
        }
117 10
        $responseTypePrototype->setEncryptionKey($this->encryptionKey);
118 10
        $this->responseTypePrototype = $responseTypePrototype;
119 10
    }
120
121
    /**
122
     * Enable a grant type on the server.
123
     *
124
     * @param GrantTypeInterface $grantType
125
     * @param null|\DateInterval $accessTokenTTL
126
     */
127 7
    public function enableGrantType(GrantTypeInterface $grantType, \DateInterval $accessTokenTTL = null)
128
    {
129 7
        if ($accessTokenTTL instanceof \DateInterval === false) {
130 4
            $accessTokenTTL = new \DateInterval('PT1H');
131
        }
132
133 7
        $grantType->setAccessTokenRepository($this->accessTokenRepository);
134 7
        $grantType->setClientRepository($this->clientRepository);
135 7
        $grantType->setScopeRepository($this->scopeRepository);
136 7
        $grantType->setDefaultScope($this->defaultScope);
137 7
        $grantType->setPrivateKey($this->privateKey);
138 7
        $grantType->setEmitter($this->getEmitter());
139 7
        $grantType->setEncryptionKey($this->encryptionKey);
140
141 7
        $this->enabledGrantTypes[$grantType->getIdentifier()] = $grantType;
142 7
        $this->grantTypeAccessTokenTTL[$grantType->getIdentifier()] = $accessTokenTTL;
143 7
    }
144
145
    /**
146
     * Validate an authorization request
147
     *
148
     * @param ServerRequestInterface $request
149
     *
150
     * @throws OAuthServerException
151
     *
152
     * @return AuthorizationRequest
153
     */
154 3
    public function validateAuthorizationRequest(ServerRequestInterface $request)
155
    {
156 3
        foreach ($this->enabledGrantTypes as $grantType) {
157 2
            if ($grantType->canRespondToAuthorizationRequest($request)) {
158 2
                return $grantType->validateAuthorizationRequest($request);
159
            }
160
        }
161
162 1
        throw OAuthServerException::unsupportedGrantType();
163
    }
164
165
    /**
166
     * Complete an authorization request
167
     *
168
     * @param AuthorizationRequest $authRequest
169
     * @param ResponseInterface    $response
170
     *
171
     * @return ResponseInterface
172
     */
173 1
    public function completeAuthorizationRequest(AuthorizationRequest $authRequest, ResponseInterface $response)
174
    {
175 1
        return $this->enabledGrantTypes[$authRequest->getGrantTypeId()]
176 1
            ->completeAuthorizationRequest($authRequest)
177 1
            ->generateHttpResponse($response);
178
    }
179
180
    /**
181
     * Return an access token response.
182
     *
183
     * @param ServerRequestInterface $request
184
     * @param ResponseInterface      $response
185
     *
186
     * @throws OAuthServerException
187
     *
188
     * @return ResponseInterface
189
     */
190 4
    public function respondToAccessTokenRequest(ServerRequestInterface $request, ResponseInterface $response)
191
    {
192 4
        foreach ($this->enabledGrantTypes as $grantType) {
193 4
            if (!$grantType->canRespondToAccessTokenRequest($request)) {
194 1
                continue;
195
            }
196 3
            $tokenResponse = $grantType->respondToAccessTokenRequest(
197 3
                $request,
198 3
                $this->newResponseType(),
199 3
                $this->grantTypeAccessTokenTTL[$grantType->getIdentifier()]
200
            );
201
202 2
            if ($tokenResponse instanceof ResponseTypeInterface) {
203 2
                return $tokenResponse->generateHttpResponse($response);
204
            }
205
        }
206
207 1
        throw OAuthServerException::unsupportedGrantType();
208
    }
209
210
    /**
211
     * Get the token type that grants will return in the HTTP response.
212
     *
213
     * @return ResponseTypeInterface
214
     */
215 5
    protected function newResponseType()
216
    {
217 5
        return clone $this->responseTypePrototype;
218
    }
219
220
    /**
221
     * Set the default scope for the authorization server.
222
     *
223
     * @param string $defaultScope
224
     */
225 3
    public function setDefaultScope($defaultScope)
226
    {
227 3
        $this->defaultScope = $defaultScope;
228 3
    }
229
}
230