Completed
Push — master ( 06424f...aac467 )
by Alex
86:38 queued 51:34
created

AuthorizationServer::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
rs 9.4285
cc 2
eloc 15
nc 2
nop 6
1
<?php
2
/**
3
 * @author      Alex Bilbie <[email protected]>
4
 * @copyright   Copyright (c) Alex Bilbie
5
 * @license     http://mit-license.org/
6
 * @link        https://github.com/thephpleague/oauth2-server
7
 */
8
9
namespace League\OAuth2\Server;
10
11
use League\Event\EmitterAwareInterface;
12
use League\Event\EmitterAwareTrait;
13
use League\OAuth2\Server\Exception\OAuthServerException;
14
use League\OAuth2\Server\Grant\GrantTypeInterface;
15
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
16
use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
17
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
18
use League\OAuth2\Server\RequestTypes\AuthorizationRequest;
19
use League\OAuth2\Server\ResponseTypes\BearerTokenResponse;
20
use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface;
21
use Psr\Http\Message\ResponseInterface;
22
use Psr\Http\Message\ServerRequestInterface;
23
24
class AuthorizationServer implements EmitterAwareInterface
25
{
26
    use EmitterAwareTrait;
27
28
    /**
29
     * @var GrantTypeInterface[]
30
     */
31
    protected $enabledGrantTypes = [];
32
33
    /**
34
     * @var \DateInterval[]
35
     */
36
    protected $grantTypeAccessTokenTTL = [];
37
38
    /**
39
     * @var CryptKey
40
     */
41
    protected $privateKey;
42
43
    /**
44
     * @var CryptKey
45
     */
46
    protected $publicKey;
47
48
    /**
49
     * @var null|ResponseTypeInterface
50
     */
51
    protected $responseType;
52
53
    /**
54
     * @var ClientRepositoryInterface
55
     */
56
    private $clientRepository;
57
58
    /**
59
     * @var AccessTokenRepositoryInterface
60
     */
61
    private $accessTokenRepository;
62
63
    /**
64
     * @var ScopeRepositoryInterface
65
     */
66
    private $scopeRepository;
67
68
    /**
69
     * @var string
70
     */
71
    private $encryptionKey;
72
73
    /**
74
     * New server instance.
75
     *
76
     * @param ClientRepositoryInterface      $clientRepository
77
     * @param AccessTokenRepositoryInterface $accessTokenRepository
78
     * @param ScopeRepositoryInterface       $scopeRepository
79
     * @param CryptKey|string                $privateKey
80
     * @param string                         $encryptionKey
81
     * @param null|ResponseTypeInterface     $responseType
82
     */
83
    public function __construct(
84
        ClientRepositoryInterface $clientRepository,
85
        AccessTokenRepositoryInterface $accessTokenRepository,
86
        ScopeRepositoryInterface $scopeRepository,
87
        $privateKey,
88
        $encryptionKey,
89
        ResponseTypeInterface $responseType = null
90
    ) {
91
        $this->clientRepository = $clientRepository;
92
        $this->accessTokenRepository = $accessTokenRepository;
93
        $this->scopeRepository = $scopeRepository;
94
95
        if ($privateKey instanceof CryptKey === false) {
96
            $privateKey = new CryptKey($privateKey);
97
        }
98
        $this->privateKey = $privateKey;
99
100
        $this->encryptionKey = $encryptionKey;
101
        $this->responseType = $responseType;
102
    }
103
104
    /**
105
     * Enable a grant type on the server.
106
     *
107
     * @param GrantTypeInterface $grantType
108
     * @param null|\DateInterval $accessTokenTTL
109
     */
110
    public function enableGrantType(GrantTypeInterface $grantType, \DateInterval $accessTokenTTL = null)
111
    {
112
        if ($accessTokenTTL instanceof \DateInterval === false) {
113
            $accessTokenTTL = new \DateInterval('PT1H');
114
        }
115
116
        $grantType->setAccessTokenRepository($this->accessTokenRepository);
117
        $grantType->setClientRepository($this->clientRepository);
118
        $grantType->setScopeRepository($this->scopeRepository);
119
        $grantType->setPrivateKey($this->privateKey);
120
        $grantType->setPublicKey($this->publicKey);
0 ignored issues
show
Bug introduced by
The method setPublicKey() does not seem to exist on object<League\OAuth2\Ser...ant\GrantTypeInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
121
        $grantType->setEmitter($this->getEmitter());
122
        $grantType->setEncryptionKey($this->encryptionKey);
123
124
        $this->enabledGrantTypes[$grantType->getIdentifier()] = $grantType;
125
        $this->grantTypeAccessTokenTTL[$grantType->getIdentifier()] = $accessTokenTTL;
126
    }
127
128
    /**
129
     * Validate an authorization request
130
     *
131
     * @param ServerRequestInterface $request
132
     *
133
     * @throws OAuthServerException
134
     *
135
     * @return AuthorizationRequest
136
     */
137
    public function validateAuthorizationRequest(ServerRequestInterface $request)
138
    {
139
        foreach ($this->enabledGrantTypes as $grantType) {
140
            if ($grantType->canRespondToAuthorizationRequest($request)) {
141
                return $grantType->validateAuthorizationRequest($request);
142
            }
143
        }
144
145
        throw OAuthServerException::unsupportedGrantType();
146
    }
147
148
    /**
149
     * Complete an authorization request
150
     *
151
     * @param AuthorizationRequest $authRequest
152
     * @param ResponseInterface    $response
153
     *
154
     * @return ResponseInterface
155
     */
156
    public function completeAuthorizationRequest(AuthorizationRequest $authRequest, ResponseInterface $response)
157
    {
158
        return $this->enabledGrantTypes[$authRequest->getGrantTypeId()]
159
            ->completeAuthorizationRequest($authRequest)
160
            ->generateHttpResponse($response);
161
    }
162
163
    /**
164
     * Return an access token response.
165
     *
166
     * @param ServerRequestInterface $request
167
     * @param ResponseInterface      $response
168
     *
169
     * @throws OAuthServerException
170
     *
171
     * @return ResponseInterface
172
     */
173
    public function respondToAccessTokenRequest(ServerRequestInterface $request, ResponseInterface $response)
174
    {
175
        foreach ($this->enabledGrantTypes as $grantType) {
176
            if ($grantType->canRespondToAccessTokenRequest($request)) {
177
                $tokenResponse = $grantType->respondToAccessTokenRequest(
178
                    $request,
179
                    $this->getResponseType(),
180
                    $this->grantTypeAccessTokenTTL[$grantType->getIdentifier()]
181
                );
182
183
                if ($tokenResponse instanceof ResponseTypeInterface) {
184
                    return $tokenResponse->generateHttpResponse($response);
185
                }
186
            }
187
        }
188
189
        throw OAuthServerException::unsupportedGrantType();
190
    }
191
192
    /**
193
     * Get the token type that grants will return in the HTTP response.
194
     *
195
     * @return ResponseTypeInterface
196
     */
197
    protected function getResponseType()
198
    {
199
        if ($this->responseType instanceof ResponseTypeInterface === false) {
200
            $this->responseType = new BearerTokenResponse();
201
        }
202
203
        $this->responseType->setPrivateKey($this->privateKey);
204
        $this->responseType->setEncryptionKey($this->encryptionKey);
205
206
        return $this->responseType;
207
    }
208
}
209