Issues (66)

src/AuthorizationServer.php (1 issue)

Labels
Severity
1
<?php
2
3
/**
4
 * @author      Alex Bilbie <[email protected]>
5
 * @copyright   Copyright (c) Alex Bilbie
6
 * @license     http://mit-license.org/
7
 *
8
 * @link        https://github.com/thephpleague/oauth2-server
9
 */
10
11
declare(strict_types=1);
12
13
namespace League\OAuth2\Server;
14
15
use DateInterval;
16
use Defuse\Crypto\Key;
17
use League\OAuth2\Server\EventEmitting\EmitterAwareInterface;
18
use League\OAuth2\Server\EventEmitting\EmitterAwarePolyfill;
19
use League\OAuth2\Server\Exception\OAuthServerException;
20
use League\OAuth2\Server\Grant\GrantTypeInterface;
21
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
22
use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
23
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
24
use League\OAuth2\Server\RequestTypes\AuthorizationRequestInterface;
25
use League\OAuth2\Server\ResponseTypes\AbstractResponseType;
26
use League\OAuth2\Server\ResponseTypes\BearerTokenResponse;
27
use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface;
28
use Psr\Http\Message\ResponseInterface;
29
use Psr\Http\Message\ServerRequestInterface;
30
use SensitiveParameter;
0 ignored issues
show
The type SensitiveParameter was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
31
32
class AuthorizationServer implements EmitterAwareInterface
33
{
34
    use EmitterAwarePolyfill;
35
36
    /**
37
     * @var GrantTypeInterface[]
38
     */
39
    protected array $enabledGrantTypes = [];
40
41
    /**
42
     * @var DateInterval[]
43
     */
44
    protected array $grantTypeAccessTokenTTL = [];
45
46
    protected CryptKeyInterface $privateKey;
47
48
    protected CryptKeyInterface $publicKey;
49
50
    protected ResponseTypeInterface $responseType;
51
52
    private string|Key $encryptionKey;
53
54
    private string $defaultScope = '';
55
56
    private bool $revokeRefreshTokens = true;
57
58
    /**
59
     * New server instance
60
     */
61 12
    public function __construct(
62
        private ClientRepositoryInterface $clientRepository,
63
        private AccessTokenRepositoryInterface $accessTokenRepository,
64
        private ScopeRepositoryInterface $scopeRepository,
65
        #[SensitiveParameter]
66
        CryptKeyInterface|string $privateKey,
67
        #[SensitiveParameter]
68
        Key|string $encryptionKey,
69
        ResponseTypeInterface|null $responseType = null
70
    ) {
71 12
        if ($privateKey instanceof CryptKeyInterface === false) {
72 12
            $privateKey = new CryptKey($privateKey);
73
        }
74
75 12
        $this->privateKey = $privateKey;
76 12
        $this->encryptionKey = $encryptionKey;
77
78 12
        if ($responseType === null) {
79 5
            $responseType = new BearerTokenResponse();
80
        } else {
81 7
            $responseType = clone $responseType;
82
        }
83
84 12
        $this->responseType = $responseType;
85
    }
86
87
    /**
88
     * Enable a grant type on the server
89
     */
90 8
    public function enableGrantType(GrantTypeInterface $grantType, DateInterval|null $accessTokenTTL = null): void
91
    {
92 8
        if ($accessTokenTTL === null) {
93 4
            $accessTokenTTL = new DateInterval('PT1H');
94
        }
95
96 8
        $grantType->setAccessTokenRepository($this->accessTokenRepository);
97 8
        $grantType->setClientRepository($this->clientRepository);
98 8
        $grantType->setScopeRepository($this->scopeRepository);
99 8
        $grantType->setDefaultScope($this->defaultScope);
100 8
        $grantType->setPrivateKey($this->privateKey);
101 8
        $grantType->setEmitter($this->getEmitter());
102 8
        $grantType->setEncryptionKey($this->encryptionKey);
103 8
        $grantType->revokeRefreshTokens($this->revokeRefreshTokens);
104
105 8
        $this->enabledGrantTypes[$grantType->getIdentifier()] = $grantType;
106 8
        $this->grantTypeAccessTokenTTL[$grantType->getIdentifier()] = $accessTokenTTL;
107
    }
108
109
    /**
110
     * Validate an authorization request
111
     *
112
     * @throws OAuthServerException
113
     */
114 3
    public function validateAuthorizationRequest(ServerRequestInterface $request): AuthorizationRequestInterface
115
    {
116 3
        foreach ($this->enabledGrantTypes as $grantType) {
117 2
            if ($grantType->canRespondToAuthorizationRequest($request)) {
118 2
                return $grantType->validateAuthorizationRequest($request);
119
            }
120
        }
121
122 1
        throw OAuthServerException::unsupportedGrantType();
123
    }
124
125
    /**
126
     * Complete an authorization request
127
     */
128 1
    public function completeAuthorizationRequest(
129
        AuthorizationRequestInterface $authRequest,
130
        ResponseInterface $response
131
    ): ResponseInterface {
132 1
        return $this->enabledGrantTypes[$authRequest->getGrantTypeId()]
133 1
            ->completeAuthorizationRequest($authRequest)
134 1
            ->generateHttpResponse($response);
135
    }
136
137
    /**
138
     * Respond to device authorization request
139
     *
140
     * @throws OAuthServerException
141
     */
142 1
    public function respondToDeviceAuthorizationRequest(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
143
    {
144 1
        foreach ($this->enabledGrantTypes as $grantType) {
145 1
            if ($grantType->canRespondToDeviceAuthorizationRequest($request)) {
146 1
                return $grantType
147 1
                    ->respondToDeviceAuthorizationRequest($request)
148 1
                    ->generateHttpResponse($response);
149
            }
150
        }
151
152
        throw OAuthServerException::unsupportedGrantType();
153
    }
154
155
    /**
156
     * Complete a device authorization request
157
     */
158
    public function completeDeviceAuthorizationRequest(string $deviceCode, string $userId, bool $userApproved): void
159
    {
160
        $this->enabledGrantTypes['urn:ietf:params:oauth:grant-type:device_code']
161
          ->completeDeviceAuthorizationRequest($deviceCode, $userId, $userApproved);
162
    }
163
164
    /**
165
     * Return an access token response.
166
     *
167
     * @throws OAuthServerException
168
     */
169 4
    public function respondToAccessTokenRequest(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
170
    {
171 4
        foreach ($this->enabledGrantTypes as $grantType) {
172 4
            if (!$grantType->canRespondToAccessTokenRequest($request)) {
173 1
                continue;
174
            }
175
176 3
            $tokenResponse = $grantType->respondToAccessTokenRequest(
177 3
                $request,
178 3
                $this->getResponseType(),
179 3
                $this->grantTypeAccessTokenTTL[$grantType->getIdentifier()]
180 3
            );
181
182 2
            return $tokenResponse->generateHttpResponse($response);
183
        }
184
185 1
        throw OAuthServerException::unsupportedGrantType();
186
    }
187
188
    /**
189
     * Get the token type that grants will return in the HTTP response.
190
     */
191 6
    protected function getResponseType(): ResponseTypeInterface
192
    {
193 6
        $responseType = clone $this->responseType;
194
195 6
        if ($responseType instanceof AbstractResponseType) {
196 6
            $responseType->setPrivateKey($this->privateKey);
197
        }
198
199 6
        $responseType->setEncryptionKey($this->encryptionKey);
200
201 6
        return $responseType;
202
    }
203
204
    /**
205
     * Set the default scope for the authorization server.
206
     */
207 4
    public function setDefaultScope(string $defaultScope): void
208
    {
209 4
        $this->defaultScope = $defaultScope;
210
    }
211
212
    /**
213
     * Sets whether to revoke refresh tokens or not (for all grant types).
214
     */
215
    public function revokeRefreshTokens(bool $revokeRefreshTokens): void
216
    {
217
        $this->revokeRefreshTokens = $revokeRefreshTokens;
218
    }
219
}
220