1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @link http://www.yiiframework.com/ |
5
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
6
|
|
|
* @license http://www.yiiframework.com/license/ |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace rhertogh\Yii2Oauth2Server; |
10
|
|
|
|
11
|
|
|
use Defuse\Crypto\Exception\BadFormatException; |
12
|
|
|
use Defuse\Crypto\Exception\EnvironmentIsBrokenException; |
13
|
|
|
use GuzzleHttp\Psr7\Response as Psr7Response; |
14
|
|
|
use GuzzleHttp\Psr7\ServerRequest as Psr7ServerRequest; |
15
|
|
|
use League\OAuth2\Server\CryptKey; |
16
|
|
|
use League\OAuth2\Server\Grant\GrantTypeInterface; |
17
|
|
|
use rhertogh\Yii2Oauth2Server\base\Oauth2BaseModule; |
18
|
|
|
use rhertogh\Yii2Oauth2Server\components\server\tokens\Oauth2AccessTokenData; |
19
|
|
|
use rhertogh\Yii2Oauth2Server\controllers\console\Oauth2ClientController; |
20
|
|
|
use rhertogh\Yii2Oauth2Server\controllers\console\Oauth2DebugController; |
21
|
|
|
use rhertogh\Yii2Oauth2Server\controllers\console\Oauth2EncryptionController; |
22
|
|
|
use rhertogh\Yii2Oauth2Server\controllers\console\Oauth2MigrationsController; |
23
|
|
|
use rhertogh\Yii2Oauth2Server\controllers\console\Oauth2PersonalAccessTokenController; |
24
|
|
|
use rhertogh\Yii2Oauth2Server\exceptions\Oauth2ServerException; |
25
|
|
|
use rhertogh\Yii2Oauth2Server\helpers\DiHelper; |
26
|
|
|
use rhertogh\Yii2Oauth2Server\helpers\Psr7Helper; |
27
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\components\authorization\Oauth2ClientAuthorizationRequestInterface; |
28
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\components\common\DefaultAccessTokenTtlInterface; |
29
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\components\encryption\Oauth2CryptographerInterface; |
30
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\components\factories\encryption\Oauth2EncryptionKeyFactoryInterface; |
31
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\components\factories\grants\base\Oauth2GrantTypeFactoryInterface; |
32
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\components\openidconnect\scope\Oauth2OidcScopeCollectionInterface; |
33
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\components\openidconnect\server\Oauth2OidcBearerTokenResponseInterface; |
34
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\components\server\Oauth2AuthorizationServerInterface; |
35
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\components\server\Oauth2ResourceServerInterface; |
36
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\controllers\web\Oauth2CertificatesControllerInterface; |
37
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\controllers\web\Oauth2ConsentControllerInterface; |
38
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\controllers\web\Oauth2OidcControllerInterface; |
39
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\controllers\web\Oauth2ServerControllerInterface; |
40
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\controllers\web\Oauth2WellKnownControllerInterface; |
41
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\filters\auth\Oauth2HttpBearerAuthInterface; |
42
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\models\base\Oauth2EncryptedStorageInterface; |
43
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\models\external\user\Oauth2OidcUserInterface; |
44
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\models\external\user\Oauth2UserInterface; |
45
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\models\Oauth2ClientInterface; |
46
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\models\Oauth2ScopeInterface; |
47
|
|
|
use rhertogh\Yii2Oauth2Server\traits\DefaultAccessTokenTtlTrait; |
48
|
|
|
use Yii; |
49
|
|
|
use yii\base\BootstrapInterface; |
50
|
|
|
use yii\base\InvalidArgumentException; |
51
|
|
|
use yii\base\InvalidCallException; |
52
|
|
|
use yii\base\InvalidConfigException; |
53
|
|
|
use yii\console\Application as ConsoleApplication; |
54
|
|
|
use yii\helpers\ArrayHelper; |
55
|
|
|
use yii\helpers\Json; |
56
|
|
|
use yii\helpers\StringHelper; |
57
|
|
|
use yii\helpers\VarDumper; |
58
|
|
|
use yii\i18n\PhpMessageSource; |
59
|
|
|
use yii\validators\IpValidator; |
60
|
|
|
use yii\web\Application as WebApplication; |
61
|
|
|
use yii\web\GroupUrlRule; |
62
|
|
|
use yii\web\IdentityInterface; |
63
|
|
|
use yii\web\Response; |
64
|
|
|
use yii\web\UrlRule; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* This is the main module class for the Yii2 Oauth2 Server module. |
68
|
|
|
* To use it, include it as a module in the application configuration like the following: |
69
|
|
|
* |
70
|
|
|
* ~~~ |
71
|
|
|
* return [ |
72
|
|
|
* 'bootstrap' => ['oauth2'], |
73
|
|
|
* 'modules' => [ |
74
|
|
|
* 'oauth2' => [ |
75
|
|
|
* 'class' => 'rhertogh\Yii2Oauth2Server\Oauth2Module', |
76
|
|
|
* // ... Please check docs/guide/start-installation.md further details |
77
|
|
|
* ], |
78
|
|
|
* ], |
79
|
|
|
* ] |
80
|
|
|
* ~~~ |
81
|
|
|
* |
82
|
|
|
* @property \DateInterval|string|null $defaultAccessTokenTTL |
83
|
|
|
* @since 1.0.0 |
84
|
|
|
*/ |
85
|
|
|
class Oauth2Module extends Oauth2BaseModule implements BootstrapInterface, DefaultAccessTokenTtlInterface |
86
|
|
|
{ |
87
|
|
|
use DefaultAccessTokenTtlTrait; |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Application type "web": http response. |
91
|
|
|
* @since 1.0.0 |
92
|
|
|
*/ |
93
|
|
|
public const APPLICATION_TYPE_WEB = 'web'; |
94
|
|
|
/** |
95
|
|
|
* Application type "console": cli response. |
96
|
|
|
* @since 1.0.0 |
97
|
|
|
*/ |
98
|
|
|
public const APPLICATION_TYPE_CONSOLE = 'console'; |
99
|
|
|
/** |
100
|
|
|
* Supported Application types. |
101
|
|
|
* @since 1.0.0 |
102
|
|
|
*/ |
103
|
|
|
public const APPLICATION_TYPES = [ |
104
|
|
|
self::APPLICATION_TYPE_WEB, |
105
|
|
|
self::APPLICATION_TYPE_CONSOLE, |
106
|
|
|
]; |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* "Authorization Server" Role, please see guide for details. |
110
|
|
|
* @since 1.0.0 |
111
|
|
|
*/ |
112
|
|
|
public const SERVER_ROLE_AUTHORIZATION_SERVER = 1; |
113
|
|
|
/** |
114
|
|
|
* "Resource Server" Role, please see guide for details. |
115
|
|
|
* @since 1.0.0 |
116
|
|
|
*/ |
117
|
|
|
public const SERVER_ROLE_RESOURCE_SERVER = 2; |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Required settings when the server role includes Authorization Server |
121
|
|
|
* @since 1.0.0 |
122
|
|
|
*/ |
123
|
|
|
protected const REQUIRED_SETTINGS_AUTHORIZATION_SERVER = [ |
124
|
|
|
'codesEncryptionKey', |
125
|
|
|
'storageEncryptionKeys', |
126
|
|
|
'defaultStorageEncryptionKey', |
127
|
|
|
'privateKey', |
128
|
|
|
'publicKey', |
129
|
|
|
]; |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Encrypted Models |
133
|
|
|
* |
134
|
|
|
* @since 1.0.0 |
135
|
|
|
*/ |
136
|
|
|
protected const ENCRYPTED_MODELS = [ |
137
|
|
|
Oauth2ClientInterface::class, |
138
|
|
|
]; |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Required settings when the server role includes Resource Server |
142
|
|
|
* @since 1.0.0 |
143
|
|
|
*/ |
144
|
|
|
protected const REQUIRED_SETTINGS_RESOURCE_SERVER = [ |
145
|
|
|
'publicKey', |
146
|
|
|
]; |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Prefix used in session storage of Client Authorization Requests |
150
|
|
|
* @since 1.0.0 |
151
|
|
|
*/ |
152
|
|
|
protected const CLIENT_AUTHORIZATION_REQUEST_SESSION_PREFIX = 'OATH2_CLIENT_AUTHORIZATION_REQUEST_'; |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Controller mapping for the module. Will be parsed on `init()`. |
156
|
|
|
* @since 1.0.0 |
157
|
|
|
*/ |
158
|
|
|
protected const CONTROLLER_MAP = [ |
159
|
|
|
self::APPLICATION_TYPE_WEB => [ |
160
|
|
|
Oauth2ServerControllerInterface::CONTROLLER_NAME => [ |
161
|
|
|
'controller' => Oauth2ServerControllerInterface::class, |
162
|
|
|
'serverRole' => self::SERVER_ROLE_AUTHORIZATION_SERVER, |
163
|
|
|
], |
164
|
|
|
Oauth2ConsentControllerInterface::CONTROLLER_NAME => [ |
165
|
|
|
'controller' => Oauth2ConsentControllerInterface::class, |
166
|
|
|
'serverRole' => self::SERVER_ROLE_AUTHORIZATION_SERVER, |
167
|
|
|
], |
168
|
|
|
Oauth2WellKnownControllerInterface::CONTROLLER_NAME => [ |
169
|
|
|
'controller' => Oauth2WellKnownControllerInterface::class, |
170
|
|
|
'serverRole' => self::SERVER_ROLE_AUTHORIZATION_SERVER, |
171
|
|
|
], |
172
|
|
|
Oauth2CertificatesControllerInterface::CONTROLLER_NAME => [ |
173
|
|
|
'controller' => Oauth2CertificatesControllerInterface::class, |
174
|
|
|
'serverRole' => self::SERVER_ROLE_AUTHORIZATION_SERVER, |
175
|
|
|
], |
176
|
|
|
Oauth2OidcControllerInterface::CONTROLLER_NAME => [ |
177
|
|
|
'controller' => Oauth2OidcControllerInterface::class, |
178
|
|
|
'serverRole' => self::SERVER_ROLE_AUTHORIZATION_SERVER, |
179
|
|
|
], |
180
|
|
|
], |
181
|
|
|
self::APPLICATION_TYPE_CONSOLE => [ |
182
|
|
|
'migrations' => [ |
183
|
|
|
'controller' => Oauth2MigrationsController::class, |
184
|
|
|
'serverRole' => self::SERVER_ROLE_AUTHORIZATION_SERVER | self::SERVER_ROLE_RESOURCE_SERVER, |
185
|
|
|
], |
186
|
|
|
'client' => [ |
187
|
|
|
'controller' => Oauth2ClientController::class, |
188
|
|
|
'serverRole' => self::SERVER_ROLE_AUTHORIZATION_SERVER, |
189
|
|
|
], |
190
|
|
|
'encryption' => [ |
191
|
|
|
'controller' => Oauth2EncryptionController::class, |
192
|
|
|
'serverRole' => self::SERVER_ROLE_AUTHORIZATION_SERVER, |
193
|
|
|
], |
194
|
|
|
'debug' => [ |
195
|
|
|
'controller' => Oauth2DebugController::class, |
196
|
|
|
'serverRole' => self::SERVER_ROLE_AUTHORIZATION_SERVER | self::SERVER_ROLE_RESOURCE_SERVER, |
197
|
|
|
], |
198
|
|
|
'pat' => [ |
199
|
|
|
'controller' => Oauth2PersonalAccessTokenController::class, |
200
|
|
|
'serverRole' => self::SERVER_ROLE_AUTHORIZATION_SERVER, |
201
|
|
|
] |
202
|
|
|
] |
203
|
|
|
]; |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* @inheritdoc |
207
|
|
|
*/ |
208
|
|
|
public $controllerNamespace = __NAMESPACE__ . '\-'; // Set explicitly via $controllerMap in `init()`. |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @var string|null The application type. If `null` the type will be automatically detected. |
212
|
|
|
* @see APPLICATION_TYPES |
213
|
|
|
*/ |
214
|
|
|
public $appType = null; |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @var int The Oauth 2.0 Server Roles the module will perform. |
218
|
|
|
* @since 1.0.0 |
219
|
|
|
*/ |
220
|
|
|
public $serverRole = self::SERVER_ROLE_AUTHORIZATION_SERVER | self::SERVER_ROLE_RESOURCE_SERVER; |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @var string|null The private key for the server. Can be a string containing the key itself or point to a file. |
224
|
|
|
* When pointing to a file it's recommended to use an absolute path prefixed with 'file://' or start with |
225
|
|
|
* '@' to use a Yii path alias. |
226
|
|
|
* @see $privateKeyPassphrase For setting a passphrase for the private key. |
227
|
|
|
* @since 1.0.0 |
228
|
|
|
*/ |
229
|
|
|
public $privateKey = null; |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* @var string|null The passphrase for the private key. |
233
|
|
|
* @since 1.0.0 |
234
|
|
|
*/ |
235
|
|
|
public $privateKeyPassphrase = null; |
236
|
|
|
/** |
237
|
|
|
* @var string|null The public key for the server. Can be a string containing the key itself or point to a file. |
238
|
|
|
* When pointing to a file it's recommended to use an absolute path prefixed with 'file://' or start with |
239
|
|
|
* '@' to use a Yii path alias. |
240
|
|
|
* @since 1.0.0 |
241
|
|
|
*/ |
242
|
|
|
public $publicKey = null; |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* @var string|null The encryption key for authorization and refresh codes. |
246
|
|
|
* @since 1.0.0 |
247
|
|
|
*/ |
248
|
|
|
public $codesEncryptionKey = null; |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* @var string[]|string|null The encryption keys for storage like client secrets. |
252
|
|
|
* Where the array key is the name of the key, and the value the key itself. E.g. |
253
|
|
|
* `['2022-01-01' => 'def00000cb36fd6ed6641e0ad70805b28d....']` |
254
|
|
|
* If a string (instead of an array of strings) is specified it will be JSON decoded |
255
|
|
|
* it should contain an object where each property name is the name of the key, its value the key itself. E.g. |
256
|
|
|
* `{"2022-01-01": "def00000cb36fd6ed6641e0ad70805b28d...."}` |
257
|
|
|
* |
258
|
|
|
* @since 1.0.0 |
259
|
|
|
*/ |
260
|
|
|
public $storageEncryptionKeys = null; |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* @var string|null The index of the default key in storageEncryptionKeys. E.g. 'myKey'. |
264
|
|
|
* @since 1.0.0 |
265
|
|
|
*/ |
266
|
|
|
public $defaultStorageEncryptionKey = null; |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* @var string|string[]|null IP addresses, CIDR ranges, or range aliases that are allowed to connect over a |
270
|
|
|
* non-TLS connection. If `null` or an empty array LTS is always required. |
271
|
|
|
* |
272
|
|
|
* Warning: Although you can use '*' or 'any' to allow a non-TLS connection from any ip address, |
273
|
|
|
* doing so would most likely introduce a security risk and should be done for debugging purposes only! |
274
|
|
|
* |
275
|
|
|
* @see \yii\validators\IpValidator::$networks for a list of available alliasses. |
276
|
|
|
*/ |
277
|
|
|
public $nonTlsAllowedRanges = 'localhost'; |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* @var class-string<Oauth2UserInterface>|null The Identity Class of your application, |
|
|
|
|
281
|
|
|
* most likely the same as the 'identityClass' of your application's User Component. |
282
|
|
|
* @since 1.0.0 |
283
|
|
|
*/ |
284
|
|
|
public $identityClass = null; |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* @var null|string Prefix used for url rules. When `null` the module's uniqueId will be used. |
288
|
|
|
* @since 1.0.0 |
289
|
|
|
*/ |
290
|
|
|
public $urlRulesPrefix = null; |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* @var string URL path for the access token endpoint (will be prefixed with $urlRulesPrefix). |
294
|
|
|
* @since 1.0.0 |
295
|
|
|
*/ |
296
|
|
|
public $authorizePath = 'authorize'; |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* @var string URL path for the access token endpoint (will be prefixed with $urlRulesPrefix). |
300
|
|
|
* @since 1.0.0 |
301
|
|
|
*/ |
302
|
|
|
public $accessTokenPath = 'access-token'; |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* @var string URL path for the certificates jwks endpoint (will be prefixed with $urlRulesPrefix). |
306
|
|
|
* @since 1.0.0 |
307
|
|
|
*/ |
308
|
|
|
public $jwksPath = 'certs'; |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* The URL to the page where the user can perform the client/scope authorization |
312
|
|
|
* (if `null` the build in page will be used). |
313
|
|
|
* @return string |
314
|
|
|
* @since 1.0.0 |
315
|
|
|
*/ |
316
|
|
|
public $clientAuthorizationUrl = null; |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* @var string The URL path to the build in page where the user can authorize the client for the requested scopes |
320
|
|
|
* (will be prefixed with $urlRulesPrefix). |
321
|
|
|
* Note: This setting will only be used if $clientAuthorizationUrl is `null`. |
322
|
|
|
* @since 1.0.0 |
323
|
|
|
*/ |
324
|
|
|
public $clientAuthorizationPath = 'authorize-client'; |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* @var string The view to use in the "client authorization action" for the page where the user can |
328
|
|
|
* authorize the client for the requested scopes. |
329
|
|
|
* Note: This setting will only be used if $clientAuthorizationUrl is `null`. |
330
|
|
|
* @since 1.0.0 |
331
|
|
|
*/ |
332
|
|
|
public $clientAuthorizationView = 'authorize-client'; |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* @var bool Will the server throw an exception when a Client requests an unknown or unauthorized scope |
336
|
|
|
* (would be silently ignored otherwise). |
337
|
|
|
* Note: this setting can be overwritten per client. |
338
|
|
|
*/ |
339
|
|
|
public $exceptionOnInvalidScope = false; |
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* Configuration for `Oauth2Client::getRedirectUrisEnvVarConfig()` fallback (the |
343
|
|
|
* Oauth2Client::$envVarConfig['redirectUris'] has precedence). |
344
|
|
|
* When configured, environment variables specified in the `Oauth2Client` redirect URI(s) will be substituted with |
345
|
|
|
* their values. Please see `EnvironmentHelper::parseEnvVars()` for more details. |
346
|
|
|
* |
347
|
|
|
* Warning: This setting applies to all clients, for security it's recommended to specify this configuration at the |
348
|
|
|
* individual client level via its `envVarConfig` setting. |
349
|
|
|
* |
350
|
|
|
* @var array{ |
|
|
|
|
351
|
|
|
* allowList: array, |
352
|
|
|
* denyList: array|null, |
353
|
|
|
* parseNested: bool, |
354
|
|
|
* exceptionWhenNotSet: bool, |
355
|
|
|
* exceptionWhenNotAllowed: bool, |
356
|
|
|
* }|null |
357
|
|
|
* @see Oauth2ClientInterface::setEnvVarConfig() |
358
|
|
|
* @see Oauth2ClientInterface::getRedirectUrisEnvVarConfig() |
359
|
|
|
* @see \rhertogh\Yii2Oauth2Server\helpers\EnvironmentHelper::parseEnvVars() |
360
|
|
|
*/ |
361
|
|
|
public $clientRedirectUrisEnvVarConfig = null; |
362
|
|
|
|
363
|
|
|
/** |
364
|
|
|
* @var string|null The URL path to the OpenID Connect Provider Configuration Information Action. |
365
|
|
|
* If set to `null` the endpoint will be disabled. |
366
|
|
|
* Note: This path is defined in the |
367
|
|
|
* [OpenID Connect Discovery](https://openid.net/specs/openid-connect-discovery-1_0.html#rfc.section.4) |
368
|
|
|
* specification and should normally not be changed. |
369
|
|
|
* @since 1.0.0 |
370
|
|
|
*/ |
371
|
|
|
public $openIdConnectProviderConfigurationInformationPath = '.well-known/openid-configuration'; |
372
|
|
|
|
373
|
|
|
/** |
374
|
|
|
* @var string The URL path to the OpenID Connect Userinfo Action (will be prefixed with $urlRulesPrefix). |
375
|
|
|
* Note: This setting will only be used if $enableOpenIdConnect and $openIdConnectUserinfoEndpoint are `true`. |
376
|
|
|
* @since 1.0.0 |
377
|
|
|
*/ |
378
|
|
|
public $openIdConnectUserinfoPath = 'oidc/userinfo'; |
379
|
|
|
|
380
|
|
|
/** |
381
|
|
|
* @var Oauth2GrantTypeFactoryInterface[]|GrantTypeInterface[]|string[]|Oauth2GrantTypeFactoryInterface|GrantTypeInterface|string|callable |
382
|
|
|
* The Oauth 2.0 Grant Types that the module will serve. |
383
|
|
|
* @since 1.0.0 |
384
|
|
|
*/ |
385
|
|
|
public $grantTypes = []; |
386
|
|
|
|
387
|
|
|
/** |
388
|
|
|
* @var bool Should the resource server check for revocation of the access token. |
389
|
|
|
* @since 1.0.0 |
390
|
|
|
*/ |
391
|
|
|
public $resourceServerAccessTokenRevocationValidation = true; |
392
|
|
|
|
393
|
|
|
/** |
394
|
|
|
* @var bool Enable support for OpenIdvConnect. |
395
|
|
|
* @since 1.0.0 |
396
|
|
|
*/ |
397
|
|
|
public $enableOpenIdConnect = false; |
398
|
|
|
|
399
|
|
|
/** |
400
|
|
|
* @var bool Enable the .well-known/openid-configuration discovery endpoint. |
401
|
|
|
* @since 1.0.0 |
402
|
|
|
*/ |
403
|
|
|
public $enableOpenIdConnectDiscovery = true; |
404
|
|
|
|
405
|
|
|
/** |
406
|
|
|
* @var bool include `grant_types_supported` in the OpenIdConnect Discovery. |
407
|
|
|
* Note: Since grant types can be specified per client not all clients might support all enabled grant types. |
408
|
|
|
* @since 1.0.0 |
409
|
|
|
*/ |
410
|
|
|
public $openIdConnectDiscoveryIncludeSupportedGrantTypes = true; |
411
|
|
|
|
412
|
|
|
/** |
413
|
|
|
* @var string URL to include in the OpenID Connect Discovery Service of a page containing |
414
|
|
|
* human-readable information that developers might want or need to know when using the OpenID Provider. |
415
|
|
|
* @see 'service_documentation' in https://openid.net/specs/openid-connect-discovery-1_0.html#rfc.section.3 |
416
|
|
|
* @since 1.0.0 |
417
|
|
|
*/ |
418
|
|
|
public $openIdConnectDiscoveryServiceDocumentationUrl = null; |
419
|
|
|
|
420
|
|
|
/** |
421
|
|
|
* @var string|bool A string to a custom userinfo endpoint or `true` to enable the build in endpoint. |
422
|
|
|
* @since 1.0.0 |
423
|
|
|
*/ |
424
|
|
|
public $openIdConnectUserinfoEndpoint = true; |
425
|
|
|
|
426
|
|
|
/** |
427
|
|
|
* Warning! Enabling this setting might introduce privacy concerns since the client could poll for the |
428
|
|
|
* online status of a user. |
429
|
|
|
* |
430
|
|
|
* @var bool If this setting is disabled in case of OpenID Connect Context the Access Token won't include a |
431
|
|
|
* Refresh Token when the 'offline_access' scope is not included in the authorization request. |
432
|
|
|
* In some cases it might be needed to always include a Refresh Token, in that case enable this setting and |
433
|
|
|
* implement the `Oauth2OidcUserSessionStatusInterface` on the User Identity model. |
434
|
|
|
* @since 1.0.0 |
435
|
|
|
*/ |
436
|
|
|
public $openIdConnectIssueRefreshTokenWithoutOfflineAccessScope = false; |
437
|
|
|
|
438
|
|
|
/** |
439
|
|
|
* @var int The default option for "User Account Selection' when not specified for a client. |
440
|
|
|
* @since 1.0.0 |
441
|
|
|
*/ |
442
|
|
|
public $defaultUserAccountSelection = self::USER_ACCOUNT_SELECTION_DISABLED; |
443
|
|
|
|
444
|
|
|
/** |
445
|
|
|
* @var bool|null Display exception messages that might leak server details. This could be useful for debugging. |
446
|
|
|
* In case of `null` (default) the YII_DEBUG constant will be used. |
447
|
|
|
* Warning: Should NOT be enabled in production! |
448
|
|
|
* @since 1.0.0 |
449
|
|
|
*/ |
450
|
|
|
public $displayConfidentialExceptionMessages = null; |
451
|
|
|
|
452
|
|
|
/** |
453
|
|
|
* @var string|null The namespace with which migrations will be created (and by which they will be located). |
454
|
|
|
* Note: The specified namespace must be defined as a Yii alias (e.g. '@app'). |
455
|
|
|
* @since 1.0.0 |
456
|
|
|
*/ |
457
|
|
|
public $migrationsNamespace = null; |
458
|
|
|
/** |
459
|
|
|
* @var string|null Optional prefix used in the name of generated migrations |
460
|
|
|
* @since 1.0.0 |
461
|
|
|
*/ |
462
|
|
|
public $migrationsPrefix = null; |
463
|
|
|
/** |
464
|
|
|
* @var string|array|int|null Sets the file ownership of generated migrations |
465
|
|
|
* @see \yii\helpers\BaseFileHelper::changeOwnership() |
466
|
|
|
* @since 1.0.0 |
467
|
|
|
*/ |
468
|
|
|
public $migrationsFileOwnership = null; |
469
|
|
|
/** |
470
|
|
|
* @var int|null Sets the file mode of generated migrations |
471
|
|
|
* @see \yii\helpers\BaseFileHelper::changeOwnership() |
472
|
|
|
* @since 1.0.0 |
473
|
|
|
*/ |
474
|
|
|
public $migrationsFileMode = null; |
475
|
|
|
|
476
|
|
|
/** |
477
|
|
|
* @var Oauth2AuthorizationServerInterface|null Cache for the authorization server |
478
|
|
|
* @since 1.0.0 |
479
|
|
|
*/ |
480
|
|
|
protected $_authorizationServer = null; |
481
|
|
|
|
482
|
|
|
/** |
483
|
|
|
* @var Oauth2ResourceServerInterface|null Cache for the resource server |
484
|
|
|
* @since 1.0.0 |
485
|
|
|
*/ |
486
|
|
|
protected $_resourceServer = null; |
487
|
|
|
|
488
|
|
|
/** |
489
|
|
|
* @var Oauth2CryptographerInterface|null Cache for the Oauth2Cryptographer |
490
|
|
|
* @since 1.0.0 |
491
|
|
|
*/ |
492
|
|
|
protected $_cryptographer = null; |
493
|
|
|
|
494
|
|
|
/** |
495
|
|
|
* @var string|null The authorization header used when the authorization request was validated. |
496
|
|
|
* @since 1.0.0 |
497
|
|
|
*/ |
498
|
|
|
protected $_oauthClaimsAuthorizationHeader = null; |
499
|
|
|
|
500
|
|
|
/** |
501
|
|
|
* @inheritDoc |
502
|
|
|
* @throws InvalidConfigException |
503
|
|
|
*/ |
504
|
130 |
|
public function init() |
505
|
|
|
{ |
506
|
130 |
|
parent::init(); |
507
|
|
|
|
508
|
130 |
|
$app = Yii::$app; |
509
|
|
|
|
510
|
130 |
|
if ($app instanceof WebApplication || $this->appType == static::APPLICATION_TYPE_WEB) { |
511
|
29 |
|
$controllerMap = static::CONTROLLER_MAP[static::APPLICATION_TYPE_WEB]; |
512
|
130 |
|
} elseif ($app instanceof ConsoleApplication || $this->appType == static::APPLICATION_TYPE_CONSOLE) { |
|
|
|
|
513
|
130 |
|
$controllerMap = static::CONTROLLER_MAP[static::APPLICATION_TYPE_CONSOLE]; |
514
|
|
|
} else { |
515
|
1 |
|
throw new InvalidConfigException( |
516
|
1 |
|
'Unable to detect application type, configure it manually by setting `$appType`.' |
517
|
1 |
|
); |
518
|
|
|
} |
519
|
130 |
|
$controllerMap = array_filter( |
520
|
130 |
|
$controllerMap, |
521
|
130 |
|
fn($controllerSettings) => $controllerSettings['serverRole'] & $this->serverRole |
522
|
130 |
|
); |
523
|
130 |
|
$this->controllerMap = ArrayHelper::getColumn($controllerMap, 'controller'); |
524
|
|
|
|
525
|
130 |
|
if (empty($this->identityClass)) { |
526
|
1 |
|
throw new InvalidConfigException('$identityClass must be set.'); |
527
|
130 |
|
} elseif (!is_a($this->identityClass, Oauth2UserInterface::class, true)) { |
528
|
1 |
|
throw new InvalidConfigException( |
529
|
1 |
|
$this->identityClass . ' must implement ' . Oauth2UserInterface::class |
530
|
1 |
|
); |
531
|
|
|
} |
532
|
|
|
|
533
|
130 |
|
foreach (static::DEFAULT_INTERFACE_IMPLEMENTATIONS as $interface => $implementation) { |
534
|
130 |
|
if (!Yii::$container->has($interface)) { |
535
|
130 |
|
Yii::$container->set($interface, $implementation); |
536
|
|
|
} |
537
|
|
|
} |
538
|
|
|
|
539
|
130 |
|
if (empty($this->urlRulesPrefix)) { |
540
|
130 |
|
$this->urlRulesPrefix = $this->uniqueId; |
541
|
|
|
} |
542
|
|
|
|
543
|
130 |
|
$this->registerTranslations(); |
544
|
|
|
} |
545
|
|
|
|
546
|
|
|
/** |
547
|
|
|
* @inheritdoc |
548
|
|
|
* @throws InvalidConfigException |
549
|
|
|
*/ |
550
|
130 |
|
public function bootstrap($app) |
551
|
|
|
{ |
552
|
|
|
if ( |
553
|
130 |
|
$app instanceof WebApplication |
554
|
130 |
|
&& $this->serverRole & static::SERVER_ROLE_AUTHORIZATION_SERVER |
555
|
|
|
) { |
556
|
29 |
|
$rules = [ |
557
|
29 |
|
$this->accessTokenPath => Oauth2ServerControllerInterface::CONTROLLER_NAME |
558
|
29 |
|
. '/' . Oauth2ServerControllerInterface::ACTION_NAME_ACCESS_TOKEN, |
559
|
29 |
|
$this->authorizePath => Oauth2ServerControllerInterface::CONTROLLER_NAME |
560
|
29 |
|
. '/' . Oauth2ServerControllerInterface::ACTION_NAME_AUTHORIZE, |
561
|
29 |
|
$this->jwksPath => Oauth2CertificatesControllerInterface::CONTROLLER_NAME |
562
|
29 |
|
. '/' . Oauth2CertificatesControllerInterface::ACTION_NAME_JWKS, |
563
|
29 |
|
]; |
564
|
|
|
|
565
|
29 |
|
if (empty($this->clientAuthorizationUrl)) { |
566
|
28 |
|
$rules[$this->clientAuthorizationPath] = Oauth2ConsentControllerInterface::CONTROLLER_NAME |
567
|
28 |
|
. '/' . Oauth2ConsentControllerInterface::ACTION_NAME_AUTHORIZE_CLIENT; |
568
|
|
|
} |
569
|
|
|
|
570
|
29 |
|
if ($this->enableOpenIdConnect && $this->openIdConnectUserinfoEndpoint === true) { |
571
|
29 |
|
$rules[$this->openIdConnectUserinfoPath] = |
572
|
29 |
|
Oauth2OidcControllerInterface::CONTROLLER_NAME |
573
|
29 |
|
. '/' . Oauth2OidcControllerInterface::ACTION_NAME_USERINFO; |
574
|
|
|
} |
575
|
|
|
|
576
|
29 |
|
$urlManager = $app->getUrlManager(); |
577
|
29 |
|
$urlManager->addRules([ |
578
|
29 |
|
Yii::createObject([ |
579
|
29 |
|
'class' => GroupUrlRule::class, |
580
|
29 |
|
'prefix' => $this->urlRulesPrefix, |
581
|
29 |
|
'routePrefix' => $this->id, |
582
|
29 |
|
'rules' => $rules, |
583
|
29 |
|
]), |
584
|
29 |
|
]); |
585
|
|
|
|
586
|
|
|
if ( |
587
|
29 |
|
$this->enableOpenIdConnect |
588
|
29 |
|
&& $this->enableOpenIdConnectDiscovery |
589
|
29 |
|
&& $this->openIdConnectProviderConfigurationInformationPath |
590
|
|
|
) { |
591
|
29 |
|
$urlManager->addRules([ |
592
|
29 |
|
Yii::createObject([ |
593
|
29 |
|
'class' => UrlRule::class, |
594
|
29 |
|
'pattern' => $this->openIdConnectProviderConfigurationInformationPath, |
595
|
29 |
|
'route' => $this->id |
596
|
29 |
|
. '/' . Oauth2WellKnownControllerInterface::CONTROLLER_NAME |
597
|
29 |
|
. '/' . Oauth2WellKnownControllerInterface::ACTION_NAME_OPENID_CONFIGURATION, |
598
|
29 |
|
]), |
599
|
29 |
|
]); |
600
|
|
|
} |
601
|
|
|
} |
602
|
|
|
} |
603
|
|
|
|
604
|
|
|
/** |
605
|
|
|
* Registers the translations for the module |
606
|
|
|
* @param bool $force Force the setting of the translations (even if they are already defined). |
607
|
|
|
* @since 1.0.0 |
608
|
|
|
*/ |
609
|
130 |
|
public function registerTranslations($force = false) |
610
|
|
|
{ |
611
|
130 |
|
if ($force || !array_key_exists('oauth2', Yii::$app->i18n->translations)) { |
612
|
130 |
|
Yii::$app->i18n->translations['oauth2'] = [ |
613
|
130 |
|
'class' => PhpMessageSource::class, |
614
|
130 |
|
'sourceLanguage' => 'en-US', |
615
|
130 |
|
'basePath' => __DIR__ . DIRECTORY_SEPARATOR . 'messages', |
616
|
130 |
|
'fileMap' => [ |
617
|
130 |
|
'oauth2' => 'oauth2.php', |
618
|
130 |
|
], |
619
|
130 |
|
]; |
620
|
|
|
} |
621
|
|
|
} |
622
|
|
|
|
623
|
|
|
/** |
624
|
|
|
* @param string $identifier The client identifier |
625
|
|
|
* @param string $name The (user-friendly) name of the client |
626
|
|
|
* @param int $grantTypes The grant types enabled for this client. |
627
|
|
|
* Use bitwise `OR` to combine multiple types, |
628
|
|
|
* e.g. `Oauth2Module::GRANT_TYPE_AUTH_CODE | Oauth2Module::GRANT_TYPE_REFRESH_TOKEN` |
629
|
|
|
* @param string|string[] $redirectURIs One or multiple redirect URIs for the client |
630
|
|
|
* @param int $type The client type (e.g. Confidential or Public) |
631
|
|
|
* See `\rhertogh\Yii2Oauth2Server\interfaces\models\Oauth2ClientInterface::TYPES` for possible values |
632
|
|
|
* @param string|null $secret The client secret in case the client `type` is `confidential`. |
633
|
|
|
* @param string|string[]|array[]|Oauth2ScopeInterface[]|null $scopes |
634
|
|
|
* @param int|null $userId |
635
|
|
|
* @return Oauth2ClientInterface |
636
|
|
|
* @throws InvalidConfigException |
637
|
|
|
* @throws \yii\db\Exception |
638
|
|
|
*/ |
639
|
5 |
|
public function createClient( |
640
|
|
|
$identifier, |
641
|
|
|
$name, |
642
|
|
|
$grantTypes, |
643
|
|
|
$redirectURIs, |
644
|
|
|
$type, |
645
|
|
|
$secret = null, |
646
|
|
|
$scopes = null, |
647
|
|
|
$userId = null, |
648
|
|
|
$endUsersMayAuthorizeClient = null, |
649
|
|
|
$skipAuthorizationIfScopeIsAllowed = null |
650
|
|
|
) { |
651
|
5 |
|
if (!($this->serverRole & static::SERVER_ROLE_AUTHORIZATION_SERVER)) { |
652
|
1 |
|
throw new InvalidCallException('Oauth2 server role does not include authorization server.'); |
653
|
|
|
} |
654
|
|
|
|
655
|
|
|
/** @var Oauth2ClientInterface $client */ |
656
|
4 |
|
$client = Yii::createObject([ |
657
|
4 |
|
'class' => Oauth2ClientInterface::class, |
658
|
4 |
|
'identifier' => $identifier, |
659
|
4 |
|
'type' => $type, |
660
|
4 |
|
'name' => $name, |
661
|
4 |
|
'redirectUri' => $redirectURIs, |
662
|
4 |
|
'grantTypes' => $grantTypes, |
663
|
4 |
|
'endUsersMayAuthorizeClient' => $endUsersMayAuthorizeClient, |
664
|
4 |
|
'skip_authorization_if_scope_is_allowed' => $skipAuthorizationIfScopeIsAllowed, |
665
|
4 |
|
'clientCredentialsGrantUserId' => $userId |
666
|
4 |
|
]); |
667
|
|
|
|
668
|
4 |
|
$transaction = $client::getDb()->beginTransaction(); |
669
|
|
|
|
670
|
|
|
try { |
671
|
4 |
|
if ($type == Oauth2ClientInterface::TYPE_CONFIDENTIAL) { |
672
|
4 |
|
$client->setSecret($secret, $this->getCryptographer()); |
673
|
|
|
} |
674
|
|
|
|
675
|
3 |
|
$client |
676
|
3 |
|
->persist() |
677
|
3 |
|
->syncClientScopes($scopes, $this->getScopeRepository()); |
678
|
|
|
|
679
|
3 |
|
$transaction->commit(); |
680
|
1 |
|
} catch (\Exception $e) { |
681
|
1 |
|
$transaction->rollBack(); |
682
|
1 |
|
throw $e; |
683
|
|
|
} |
684
|
|
|
|
685
|
3 |
|
return $client; |
686
|
|
|
} |
687
|
|
|
|
688
|
|
|
/** |
689
|
|
|
* @return CryptKey The private key of the server. |
690
|
|
|
* @throws InvalidConfigException |
691
|
|
|
* @since 1.0.0 |
692
|
|
|
*/ |
693
|
22 |
|
public function getPrivateKey() |
694
|
|
|
{ |
695
|
22 |
|
$privateKey = $this->privateKey; |
696
|
22 |
|
if (StringHelper::startsWith($privateKey, '@')) { |
697
|
19 |
|
$privateKey = 'file://' . Yii::getAlias($privateKey); |
|
|
|
|
698
|
|
|
} |
699
|
22 |
|
return Yii::createObject(CryptKey::class, [$privateKey, $this->privateKeyPassphrase]); |
700
|
|
|
} |
701
|
|
|
|
702
|
|
|
/** |
703
|
|
|
* @return CryptKey The public key of the server. |
704
|
|
|
* @throws InvalidConfigException |
705
|
|
|
* @since 1.0.0 |
706
|
|
|
*/ |
707
|
9 |
|
public function getPublicKey() |
708
|
|
|
{ |
709
|
9 |
|
$publicKey = $this->publicKey; |
710
|
9 |
|
if (StringHelper::startsWith($publicKey, '@')) { |
711
|
6 |
|
$publicKey = 'file://' . Yii::getAlias($publicKey); |
|
|
|
|
712
|
|
|
} |
713
|
9 |
|
return Yii::createObject(CryptKey::class, [$publicKey]); |
714
|
|
|
} |
715
|
|
|
|
716
|
|
|
/** |
717
|
|
|
* @return Oauth2AuthorizationServerInterface The authorization server. |
718
|
|
|
* @throws InvalidConfigException |
719
|
|
|
* @since 1.0.0 |
720
|
|
|
*/ |
721
|
27 |
|
public function getAuthorizationServer() |
722
|
|
|
{ |
723
|
27 |
|
if (!($this->serverRole & static::SERVER_ROLE_AUTHORIZATION_SERVER)) { |
724
|
1 |
|
throw new InvalidCallException('Oauth2 server role does not include authorization server.'); |
725
|
|
|
} |
726
|
|
|
|
727
|
26 |
|
if (!$this->_authorizationServer) { |
728
|
26 |
|
$this->ensureProperties(static::REQUIRED_SETTINGS_AUTHORIZATION_SERVER); |
729
|
|
|
|
730
|
21 |
|
if (!$this->getCryptographer()->hasKey($this->defaultStorageEncryptionKey)) { |
731
|
1 |
|
throw new InvalidConfigException( |
732
|
1 |
|
'Key "' . $this->defaultStorageEncryptionKey . '" is not set in $storageEncryptionKeys' |
733
|
1 |
|
); |
734
|
|
|
} |
735
|
|
|
|
736
|
|
|
/** @var Oauth2EncryptionKeyFactoryInterface $keyFactory */ |
737
|
19 |
|
$keyFactory = Yii::createObject(Oauth2EncryptionKeyFactoryInterface::class); |
738
|
|
|
try { |
739
|
19 |
|
$codesEncryptionKey = $keyFactory->createFromAsciiSafeString($this->codesEncryptionKey); |
740
|
1 |
|
} catch (BadFormatException $e) { |
741
|
1 |
|
throw new InvalidConfigException( |
742
|
1 |
|
'$codesEncryptionKey is malformed: ' . $e->getMessage(), |
743
|
1 |
|
0, |
744
|
1 |
|
$e |
745
|
1 |
|
); |
746
|
|
|
} catch (EnvironmentIsBrokenException $e) { |
747
|
|
|
throw new InvalidConfigException( |
748
|
|
|
'Could not instantiate $codesEncryptionKey: ' . $e->getMessage(), |
749
|
|
|
0, |
750
|
|
|
$e |
751
|
|
|
); |
752
|
|
|
} |
753
|
|
|
|
754
|
18 |
|
$responseType = null; |
755
|
18 |
|
if ($this->enableOpenIdConnect) { |
756
|
18 |
|
$responseType = Yii::createObject(Oauth2OidcBearerTokenResponseInterface::class, [ |
757
|
18 |
|
$this, |
758
|
18 |
|
]); |
759
|
|
|
} |
760
|
|
|
|
761
|
18 |
|
$this->_authorizationServer = Yii::createObject(Oauth2AuthorizationServerInterface::class, [ |
762
|
18 |
|
$this->getClientRepository(), |
763
|
18 |
|
$this->getAccessTokenRepository(), |
764
|
18 |
|
$this->getScopeRepository(), |
765
|
18 |
|
$this->getPrivateKey(), |
766
|
18 |
|
$codesEncryptionKey, |
767
|
18 |
|
$responseType |
768
|
18 |
|
]); |
769
|
|
|
|
770
|
18 |
|
if (!empty($this->grantTypes)) { |
771
|
18 |
|
$grantTypes = $this->grantTypes; |
772
|
|
|
|
773
|
18 |
|
if (is_callable($grantTypes)) { |
774
|
1 |
|
call_user_func($grantTypes, $this->_authorizationServer, $this); |
|
|
|
|
775
|
|
|
} else { |
776
|
17 |
|
if (!is_array($grantTypes)) { |
777
|
2 |
|
$grantTypes = [$grantTypes]; |
778
|
|
|
} |
779
|
|
|
|
780
|
17 |
|
foreach ($grantTypes as $grantTypeDefinition) { |
781
|
17 |
|
if ($grantTypeDefinition instanceof GrantTypeInterface) { |
782
|
1 |
|
$accessTokenTTL = $this->getDefaultAccessTokenTTL(); |
783
|
1 |
|
$this->_authorizationServer->enableGrantType($grantTypeDefinition, $accessTokenTTL); |
784
|
|
|
} elseif ( |
785
|
|
|
( |
786
|
16 |
|
is_numeric($grantTypeDefinition) |
787
|
16 |
|
&& array_key_exists($grantTypeDefinition, static::DEFAULT_GRANT_TYPE_FACTORIES) |
788
|
|
|
) |
789
|
16 |
|
|| is_a($grantTypeDefinition, Oauth2GrantTypeFactoryInterface::class, true) |
790
|
|
|
) { |
791
|
|
|
if ( |
792
|
15 |
|
is_numeric($grantTypeDefinition) |
793
|
15 |
|
&& array_key_exists($grantTypeDefinition, static::DEFAULT_GRANT_TYPE_FACTORIES) |
794
|
|
|
) { |
795
|
15 |
|
$grantTypeDefinition = static::DEFAULT_GRANT_TYPE_FACTORIES[$grantTypeDefinition]; |
796
|
|
|
} |
797
|
|
|
|
798
|
|
|
/** @var Oauth2GrantTypeFactoryInterface $factory */ |
799
|
15 |
|
$factory = Yii::createObject([ |
800
|
15 |
|
'class' => $grantTypeDefinition, |
801
|
15 |
|
'module' => $this, |
802
|
15 |
|
]); |
803
|
15 |
|
$accessTokenTTL = $factory->getDefaultAccessTokenTTL() |
804
|
15 |
|
?? $this->getDefaultAccessTokenTTL(); |
805
|
15 |
|
$this->_authorizationServer->enableGrantType($factory->getGrantType(), $accessTokenTTL); |
806
|
|
|
} else { |
807
|
1 |
|
throw new InvalidConfigException( |
808
|
1 |
|
'Unknown grantType ' |
809
|
1 |
|
. ( |
810
|
1 |
|
is_scalar($grantTypeDefinition) |
811
|
1 |
|
? '"' . $grantTypeDefinition . '".' |
812
|
1 |
|
: 'with data type ' . gettype($grantTypeDefinition) |
813
|
1 |
|
) |
814
|
1 |
|
); |
815
|
|
|
} |
816
|
|
|
} |
817
|
|
|
} |
818
|
|
|
} |
819
|
|
|
} |
820
|
|
|
|
821
|
17 |
|
return $this->_authorizationServer; |
822
|
|
|
} |
823
|
|
|
|
824
|
|
|
/** |
825
|
|
|
* @inheritDoc |
826
|
|
|
* @throws InvalidConfigException |
827
|
|
|
*/ |
828
|
6 |
|
public function getOidcScopeCollection() |
829
|
|
|
{ |
830
|
6 |
|
if ($this->_oidcScopeCollection === null) { |
831
|
6 |
|
$openIdConnectScopes = $this->getOpenIdConnectScopes(); |
832
|
6 |
|
if ($openIdConnectScopes instanceof Oauth2OidcScopeCollectionInterface) { |
833
|
1 |
|
$this->_oidcScopeCollection = $openIdConnectScopes; |
834
|
5 |
|
} elseif (is_callable($openIdConnectScopes)) { |
835
|
1 |
|
$this->_oidcScopeCollection = call_user_func($openIdConnectScopes, $this); |
836
|
1 |
|
if (!($this->_oidcScopeCollection instanceof Oauth2OidcScopeCollectionInterface)) { |
837
|
1 |
|
throw new InvalidConfigException( |
838
|
1 |
|
'$openIdConnectScopes must return an instance of ' |
839
|
1 |
|
. Oauth2OidcScopeCollectionInterface::class |
840
|
1 |
|
); |
841
|
|
|
} |
842
|
4 |
|
} elseif (is_array($openIdConnectScopes) || is_string($openIdConnectScopes)) { |
843
|
3 |
|
$this->_oidcScopeCollection = Yii::createObject([ |
844
|
3 |
|
'class' => Oauth2OidcScopeCollectionInterface::class, |
845
|
3 |
|
'oidcScopes' => (array)$openIdConnectScopes, |
846
|
3 |
|
]); |
847
|
|
|
} else { |
848
|
1 |
|
throw new InvalidConfigException( |
849
|
1 |
|
'$openIdConnectScopes must be a callable, array, string or ' |
850
|
1 |
|
. Oauth2OidcScopeCollectionInterface::class |
851
|
1 |
|
); |
852
|
|
|
} |
853
|
|
|
} |
854
|
|
|
|
855
|
5 |
|
return $this->_oidcScopeCollection; |
856
|
|
|
} |
857
|
|
|
|
858
|
|
|
/** |
859
|
|
|
* @return Oauth2ResourceServerInterface The resource server. |
860
|
|
|
* @throws InvalidConfigException |
861
|
|
|
* @since 1.0.0 |
862
|
|
|
*/ |
863
|
7 |
|
public function getResourceServer() |
864
|
|
|
{ |
865
|
7 |
|
if (!($this->serverRole & static::SERVER_ROLE_RESOURCE_SERVER)) { |
866
|
1 |
|
throw new InvalidCallException('Oauth2 server role does not include resource server.'); |
867
|
|
|
} |
868
|
|
|
|
869
|
6 |
|
if (!$this->_resourceServer) { |
870
|
6 |
|
$this->ensureProperties(static::REQUIRED_SETTINGS_RESOURCE_SERVER); |
871
|
|
|
|
872
|
5 |
|
$accessTokenRepository = $this->getAccessTokenRepository() |
873
|
5 |
|
->setRevocationValidation($this->resourceServerAccessTokenRevocationValidation); |
874
|
|
|
|
875
|
5 |
|
$this->_resourceServer = Yii::createObject(Oauth2ResourceServerInterface::class, [ |
876
|
5 |
|
$accessTokenRepository, |
877
|
5 |
|
$this->getPublicKey(), |
878
|
5 |
|
]); |
879
|
|
|
} |
880
|
|
|
|
881
|
5 |
|
return $this->_resourceServer; |
882
|
|
|
} |
883
|
|
|
|
884
|
|
|
/** |
885
|
|
|
* @return Oauth2CryptographerInterface The data cryptographer for the module. |
886
|
|
|
* @throws InvalidConfigException |
887
|
|
|
* @since 1.0.0 |
888
|
|
|
*/ |
889
|
27 |
|
public function getCryptographer() |
890
|
|
|
{ |
891
|
27 |
|
if (!$this->_cryptographer) { |
892
|
27 |
|
$this->_cryptographer = Yii::createObject([ |
893
|
27 |
|
'class' => Oauth2CryptographerInterface::class, |
894
|
27 |
|
'keys' => $this->storageEncryptionKeys, |
895
|
27 |
|
'defaultKeyName' => $this->defaultStorageEncryptionKey, |
896
|
27 |
|
]); |
897
|
|
|
} |
898
|
|
|
|
899
|
26 |
|
return $this->_cryptographer; |
900
|
|
|
} |
901
|
|
|
|
902
|
|
|
/** |
903
|
|
|
* @param string|null $newKeyName |
904
|
|
|
* @return array |
905
|
|
|
* @throws InvalidConfigException |
906
|
|
|
*/ |
907
|
1 |
|
public function rotateStorageEncryptionKeys($newKeyName = null) |
908
|
|
|
{ |
909
|
1 |
|
$cryptographer = $this->getCryptographer(); |
910
|
|
|
|
911
|
1 |
|
$result = []; |
912
|
1 |
|
foreach (static::ENCRYPTED_MODELS as $modelInterface) { |
913
|
1 |
|
$modelClass = DiHelper::getValidatedClassName($modelInterface); |
914
|
1 |
|
if (!is_a($modelClass, Oauth2EncryptedStorageInterface::class, true)) { |
915
|
|
|
throw new InvalidConfigException($modelInterface . ' must implement ' |
916
|
|
|
. Oauth2EncryptedStorageInterface::class); |
917
|
|
|
} |
918
|
1 |
|
$result[$modelClass] = $modelClass::rotateStorageEncryptionKeys($cryptographer, $newKeyName); |
919
|
|
|
} |
920
|
|
|
|
921
|
1 |
|
return $result; |
922
|
|
|
} |
923
|
|
|
|
924
|
|
|
/** |
925
|
|
|
* Checks if the connection is using TLS or if the remote IP address is allowed to connect without TLS. |
926
|
|
|
* @return bool |
927
|
|
|
*/ |
928
|
12 |
|
public function validateTlsConnection() |
929
|
|
|
{ |
930
|
12 |
|
if (Yii::$app->request->getIsSecureConnection()) { |
931
|
1 |
|
return true; |
932
|
|
|
} |
933
|
|
|
|
934
|
|
|
if ( |
935
|
11 |
|
!empty($this->nonTlsAllowedRanges) |
936
|
11 |
|
&& (new IpValidator(['ranges' => $this->nonTlsAllowedRanges]))->validate(Yii::$app->request->getRemoteIP()) |
937
|
|
|
) { |
938
|
7 |
|
return true; |
939
|
|
|
} |
940
|
|
|
|
941
|
4 |
|
return false; |
942
|
|
|
} |
943
|
|
|
|
944
|
|
|
/** |
945
|
|
|
* @return array |
946
|
|
|
* @throws InvalidConfigException |
947
|
|
|
*/ |
948
|
|
|
public function getStorageEncryptionKeyUsage() |
949
|
|
|
{ |
950
|
|
|
$cryptographer = $this->getCryptographer(); |
951
|
|
|
|
952
|
|
|
$result = []; |
953
|
|
|
foreach (static::ENCRYPTED_MODELS as $modelInterface) { |
954
|
|
|
$modelClass = DiHelper::getValidatedClassName($modelInterface); |
955
|
|
|
if (!is_a($modelClass, Oauth2EncryptedStorageInterface::class, true)) { |
956
|
|
|
throw new InvalidConfigException($modelInterface . ' must implement ' |
957
|
|
|
. Oauth2EncryptedStorageInterface::class); |
958
|
|
|
} |
959
|
|
|
|
960
|
|
|
$result[$modelClass] = $modelClass::getUsedStorageEncryptionKeys($cryptographer); |
961
|
|
|
} |
962
|
|
|
|
963
|
|
|
return $result; |
964
|
|
|
} |
965
|
|
|
|
966
|
|
|
/** |
967
|
|
|
* @param Oauth2ClientInterface $client |
968
|
|
|
* @param string[] $requestedScopeIdentifiers |
969
|
|
|
* @throws Oauth2ServerException |
970
|
|
|
*/ |
971
|
6 |
|
public function validateAuthRequestScopes($client, $requestedScopeIdentifiers, $redirectUri = null) |
972
|
|
|
{ |
973
|
6 |
|
if (!$client->validateAuthRequestScopes($requestedScopeIdentifiers, $unknownScopes, $unauthorizedScopes)) { |
974
|
|
|
Yii::info('Invalid scope for client "' . $client->getIdentifier() . '": ' |
975
|
|
|
. VarDumper::export(['unauthorizedScopes' => $unauthorizedScopes, 'unknownScopes' => $unknownScopes])); |
976
|
|
|
|
977
|
|
|
if ( |
978
|
|
|
$client->getExceptionOnInvalidScope() === true |
979
|
|
|
|| ( |
980
|
|
|
$client->getExceptionOnInvalidScope() === null |
981
|
|
|
&& $this->exceptionOnInvalidScope === true |
982
|
|
|
) |
983
|
|
|
) { |
984
|
|
|
if ($unknownScopes) { |
985
|
|
|
throw Oauth2ServerException::unknownScope(array_shift($unknownScopes), $redirectUri); |
986
|
|
|
} |
987
|
|
|
throw Oauth2ServerException::scopeNotAllowedForClient(array_shift($unauthorizedScopes), $redirectUri); |
988
|
|
|
} |
989
|
|
|
} |
990
|
|
|
} |
991
|
|
|
|
992
|
|
|
/** |
993
|
|
|
* Generates a redirect Response to the client authorization page where the user is prompted to authorize the |
994
|
|
|
* client and requested scope. |
995
|
|
|
* @param Oauth2ClientAuthorizationRequestInterface $clientAuthorizationRequest |
996
|
|
|
* @return Response |
997
|
|
|
* @since 1.0.0 |
998
|
|
|
*/ |
999
|
5 |
|
public function generateClientAuthReqRedirectResponse($clientAuthorizationRequest) |
1000
|
|
|
{ |
1001
|
5 |
|
$this->setClientAuthReqSession($clientAuthorizationRequest); |
1002
|
5 |
|
if (!empty($this->clientAuthorizationUrl)) { |
1003
|
1 |
|
$url = $this->clientAuthorizationUrl; |
1004
|
|
|
} else { |
1005
|
4 |
|
$url = $this->uniqueId |
1006
|
4 |
|
. '/' . Oauth2ConsentControllerInterface::CONTROLLER_NAME |
1007
|
4 |
|
. '/' . Oauth2ConsentControllerInterface::ACTION_NAME_AUTHORIZE_CLIENT; |
1008
|
|
|
} |
1009
|
5 |
|
return Yii::$app->response->redirect([ |
1010
|
5 |
|
$url, |
1011
|
5 |
|
'clientAuthorizationRequestId' => $clientAuthorizationRequest->getRequestId(), |
1012
|
5 |
|
]); |
1013
|
|
|
} |
1014
|
|
|
|
1015
|
|
|
/** |
1016
|
|
|
* Get a previously stored Client Authorization Request from the session. |
1017
|
|
|
* @param string $requestId |
1018
|
|
|
* @return Oauth2ClientAuthorizationRequestInterface|null |
1019
|
|
|
* @since 1.0.0 |
1020
|
|
|
*/ |
1021
|
5 |
|
public function getClientAuthReqSession($requestId) |
1022
|
|
|
{ |
1023
|
5 |
|
if (empty($requestId)) { |
1024
|
|
|
return null; |
1025
|
|
|
} |
1026
|
5 |
|
$key = static::CLIENT_AUTHORIZATION_REQUEST_SESSION_PREFIX . $requestId; |
1027
|
5 |
|
$clientAuthorizationRequest = Yii::$app->session->get($key); |
|
|
|
|
1028
|
5 |
|
if (!($clientAuthorizationRequest instanceof Oauth2ClientAuthorizationRequestInterface)) { |
1029
|
2 |
|
if (!empty($clientAuthorizationRequest)) { |
1030
|
1 |
|
Yii::warning( |
1031
|
1 |
|
'Found a ClientAuthorizationRequestSession with key "' . $key |
1032
|
1 |
|
. '", but it\'s not a ' . Oauth2ClientAuthorizationRequestInterface::class |
1033
|
1 |
|
); |
1034
|
|
|
} |
1035
|
2 |
|
return null; |
1036
|
|
|
} |
1037
|
5 |
|
if ($clientAuthorizationRequest->getRequestId() !== $requestId) { |
1038
|
1 |
|
Yii::warning( |
1039
|
1 |
|
'Found a ClientAuthorizationRequestSession with key "' . $key |
1040
|
1 |
|
. '", but its request id does not match "' . $requestId . '".' |
1041
|
1 |
|
); |
1042
|
1 |
|
return null; |
1043
|
|
|
} |
1044
|
5 |
|
$clientAuthorizationRequest->setModule($this); |
1045
|
|
|
|
1046
|
5 |
|
return $clientAuthorizationRequest; |
1047
|
|
|
} |
1048
|
|
|
|
1049
|
|
|
/** |
1050
|
|
|
* Stores the Client Authorization Request in the session. |
1051
|
|
|
* @param Oauth2ClientAuthorizationRequestInterface $clientAuthorizationRequest |
1052
|
|
|
* @since 1.0.0 |
1053
|
|
|
*/ |
1054
|
8 |
|
public function setClientAuthReqSession($clientAuthorizationRequest) |
1055
|
|
|
{ |
1056
|
8 |
|
$requestId = $clientAuthorizationRequest->getRequestId(); |
1057
|
8 |
|
if (empty($requestId)) { |
1058
|
1 |
|
throw new InvalidArgumentException('$scopeAuthorization must return a request id.'); |
1059
|
|
|
} |
1060
|
7 |
|
$key = static::CLIENT_AUTHORIZATION_REQUEST_SESSION_PREFIX . $requestId; |
1061
|
7 |
|
Yii::$app->session->set($key, $clientAuthorizationRequest); |
1062
|
|
|
} |
1063
|
|
|
|
1064
|
|
|
/** |
1065
|
|
|
* Stores whether the user was authenticated during the completion of the Client Authorization Request. |
1066
|
|
|
* @param string $clientAuthorizationRequestId |
1067
|
|
|
* @param bool $authenticatedDuringRequest |
1068
|
|
|
* @since 1.0.0 |
1069
|
|
|
*/ |
1070
|
|
|
public function setUserAuthenticatedDuringClientAuthRequest( |
1071
|
|
|
$clientAuthorizationRequestId, |
1072
|
|
|
$authenticatedDuringRequest |
1073
|
|
|
) { |
1074
|
|
|
$clientAuthorizationRequest = $this->getClientAuthReqSession($clientAuthorizationRequestId); |
1075
|
|
|
if ($clientAuthorizationRequest) { |
1076
|
|
|
$clientAuthorizationRequest->setUserAuthenticatedDuringRequest($authenticatedDuringRequest); |
1077
|
|
|
$this->setClientAuthReqSession($clientAuthorizationRequest); |
1078
|
|
|
} |
1079
|
|
|
} |
1080
|
|
|
|
1081
|
|
|
/** |
1082
|
|
|
* Stores the user identity selected during the completion of the Client Authorization Request. |
1083
|
|
|
* @param string $clientAuthorizationRequestId |
1084
|
|
|
* @param Oauth2UserInterface $userIdentity |
1085
|
|
|
* @since 1.0.0 |
1086
|
|
|
*/ |
1087
|
|
|
public function setClientAuthRequestUserIdentity($clientAuthorizationRequestId, $userIdentity) |
1088
|
|
|
{ |
1089
|
|
|
$clientAuthorizationRequest = $this->getClientAuthReqSession($clientAuthorizationRequestId); |
1090
|
|
|
if ($clientAuthorizationRequest) { |
1091
|
|
|
$clientAuthorizationRequest->setUserIdentity($userIdentity); |
1092
|
|
|
$this->setClientAuthReqSession($clientAuthorizationRequest); |
1093
|
|
|
} |
1094
|
|
|
} |
1095
|
|
|
|
1096
|
|
|
/** |
1097
|
|
|
* Clears a Client Authorization Request from the session storage. |
1098
|
|
|
* @param string $requestId |
1099
|
|
|
* @since 1.0.0 |
1100
|
|
|
*/ |
1101
|
2 |
|
public function removeClientAuthReqSession($requestId) |
1102
|
|
|
{ |
1103
|
2 |
|
if (empty($requestId)) { |
1104
|
1 |
|
throw new InvalidArgumentException('$requestId can not be empty.'); |
1105
|
|
|
} |
1106
|
1 |
|
$key = static::CLIENT_AUTHORIZATION_REQUEST_SESSION_PREFIX . $requestId; |
1107
|
1 |
|
Yii::$app->session->remove($key); |
1108
|
|
|
} |
1109
|
|
|
|
1110
|
|
|
/** |
1111
|
|
|
* Generates a redirect Response when the Client Authorization Request is completed. |
1112
|
|
|
* @param Oauth2ClientAuthorizationRequestInterface $clientAuthorizationRequest |
1113
|
|
|
* @return Response |
1114
|
|
|
* @since 1.0.0 |
1115
|
|
|
*/ |
1116
|
1 |
|
public function generateClientAuthReqCompledRedirectResponse($clientAuthorizationRequest) |
1117
|
|
|
{ |
1118
|
1 |
|
$clientAuthorizationRequest->processAuthorization(); |
1119
|
1 |
|
$this->setClientAuthReqSession($clientAuthorizationRequest); |
1120
|
1 |
|
return Yii::$app->response->redirect($clientAuthorizationRequest->getAuthorizationRequestUrl()); |
1121
|
|
|
} |
1122
|
|
|
|
1123
|
|
|
/** |
1124
|
|
|
* @return IdentityInterface|Oauth2UserInterface|Oauth2OidcUserInterface|null |
1125
|
|
|
* @throws InvalidConfigException |
1126
|
|
|
* @since 1.0.0 |
1127
|
|
|
*/ |
1128
|
5 |
|
public function getUserIdentity() |
1129
|
|
|
{ |
1130
|
5 |
|
$user = Yii::$app->user->identity; |
1131
|
5 |
|
if (!empty($user) && !($user instanceof Oauth2UserInterface)) { |
1132
|
1 |
|
throw new InvalidConfigException( |
1133
|
1 |
|
'Yii::$app->user->identity (currently ' . get_class($user) |
1134
|
1 |
|
. ') must implement ' . Oauth2UserInterface::class |
1135
|
1 |
|
); |
1136
|
|
|
} |
1137
|
4 |
|
return $user; |
1138
|
|
|
} |
1139
|
|
|
|
1140
|
|
|
/** |
1141
|
|
|
* Validates a bearer token authenticated request. Note: this method does not return a result but will throw |
1142
|
|
|
* an exception when the authentication fails. |
1143
|
|
|
* @throws InvalidConfigException |
1144
|
|
|
* @throws Oauth2ServerException |
1145
|
|
|
* @since 1.0.0 |
1146
|
|
|
*/ |
1147
|
3 |
|
public function validateAuthenticatedRequest() |
1148
|
|
|
{ |
1149
|
3 |
|
$psr7Request = Psr7Helper::yiiToPsr7Request(Yii::$app->request); |
|
|
|
|
1150
|
|
|
|
1151
|
3 |
|
$psr7Request = $this->getResourceServer()->validateAuthenticatedRequest($psr7Request); |
1152
|
|
|
|
1153
|
3 |
|
$this->_oauthClaims = $psr7Request->getAttributes(); |
1154
|
3 |
|
$this->_oauthClaimsAuthorizationHeader = Yii::$app->request->getHeaders()->get('Authorization'); |
|
|
|
|
1155
|
|
|
} |
1156
|
|
|
|
1157
|
|
|
/** |
1158
|
|
|
* Find a user identity bases on an access token. |
1159
|
|
|
* Note: validateAuthenticatedRequest() must be called before this method is called. |
1160
|
|
|
* @param string $token |
1161
|
|
|
* @param string $type |
1162
|
|
|
* @return Oauth2UserInterface|null |
1163
|
|
|
* @throws InvalidConfigException |
1164
|
|
|
* @throws Oauth2ServerException |
1165
|
|
|
* @see validateAuthenticatedRequest() |
1166
|
|
|
* @since 1.0.0 |
1167
|
|
|
*/ |
1168
|
4 |
|
public function findIdentityByAccessToken($token, $type) |
1169
|
|
|
{ |
1170
|
4 |
|
if (!is_a($type, Oauth2HttpBearerAuthInterface::class, true)) { |
1171
|
1 |
|
throw new InvalidCallException($type . ' must implement ' . Oauth2HttpBearerAuthInterface::class); |
1172
|
|
|
} |
1173
|
|
|
|
1174
|
|
|
if ( |
1175
|
3 |
|
!preg_match('/^Bearer\s+(.*?)$/', $this->_oauthClaimsAuthorizationHeader, $matches) |
|
|
|
|
1176
|
3 |
|
|| !Yii::$app->security->compareString($matches[1], $token) |
1177
|
|
|
) { |
1178
|
1 |
|
throw new InvalidCallException( |
1179
|
1 |
|
'validateAuthenticatedRequest() must be called before findIdentityByAccessToken().' |
1180
|
1 |
|
); |
1181
|
|
|
} |
1182
|
|
|
|
1183
|
2 |
|
$userId = $this->getRequestOauthUserId(); |
1184
|
2 |
|
if (empty($userId)) { |
1185
|
1 |
|
return null; |
1186
|
|
|
} |
1187
|
|
|
|
1188
|
1 |
|
return $this->identityClass::findIdentity($userId); |
1189
|
|
|
} |
1190
|
|
|
|
1191
|
|
|
/** |
1192
|
|
|
* Generate a "Personal Access Token" (PAT) which can be used as an alternative to using passwords |
1193
|
|
|
* for authentication (e.g. when using an API or command line). |
1194
|
|
|
* |
1195
|
|
|
* Note: Personal Access Tokens are intended to access resources on behalf users themselves. |
1196
|
|
|
* To grant access to resources on behalf of an organization, or for long-lived integrations, |
1197
|
|
|
* you most likely want to define an Oauth2 Client with the "Client Credentials" grant |
1198
|
|
|
* (https://oauth.net/2/grant-types/client-credentials). |
1199
|
|
|
* |
1200
|
|
|
* @param string $clientIdentifier The Oauth2 client identifier for which the PAT should be generated. |
1201
|
|
|
* @param int|string $userIdentifier The identifier (primary key) of the user for which the PAT should be generated. |
1202
|
|
|
* @param Oauth2ScopeInterface[]|string[]|string|null $scope The Access Token scope. |
1203
|
|
|
* @param string|true|null $clientSecret If the client is a "confidential" client the secret is required. |
1204
|
|
|
* If the boolean value `true` is passed, the client secret is automatically injected. |
1205
|
|
|
* @return Oauth2AccessTokenData |
1206
|
|
|
*/ |
1207
|
3 |
|
public function generatePersonalAccessToken($clientIdentifier, $userIdentifier, $scope = null, $clientSecret = null) |
1208
|
|
|
{ |
1209
|
3 |
|
if (is_array($scope)) { |
1210
|
2 |
|
$scopeIdentifiers = []; |
1211
|
2 |
|
foreach ($scope as $scopeItem) { |
1212
|
2 |
|
if (is_string($scopeItem)) { |
1213
|
1 |
|
$scopeIdentifiers[] = $scopeItem; |
1214
|
1 |
|
} elseif ($scopeItem instanceof Oauth2ScopeInterface) { |
1215
|
1 |
|
$scopeIdentifiers[] = $scopeItem->getIdentifier(); |
1216
|
|
|
} else { |
1217
|
|
|
throw new InvalidArgumentException('If $scope is an array its elements must be either' |
1218
|
|
|
. ' a string or an instance of ' . Oauth2ScopeInterface::class); |
1219
|
|
|
} |
1220
|
|
|
} |
1221
|
2 |
|
$scope = implode(' ', $scopeIdentifiers); |
1222
|
|
|
} |
1223
|
|
|
|
1224
|
3 |
|
if ($clientSecret === true) { |
1225
|
|
|
/** @var Oauth2ClientInterface $client */ |
1226
|
3 |
|
$client = $this->getClientRepository()->findModelByIdentifier($clientIdentifier); |
1227
|
3 |
|
if ($client && $client->isConfidential()) { |
1228
|
3 |
|
$clientSecret = $client->getDecryptedSecret($this->getCryptographer()); |
1229
|
|
|
} else { |
1230
|
|
|
$clientSecret = null; |
1231
|
|
|
} |
1232
|
|
|
} |
1233
|
|
|
|
1234
|
3 |
|
$request = (new Psr7ServerRequest('POST', ''))->withParsedBody([ |
1235
|
3 |
|
'grant_type' => static::GRANT_TYPE_IDENTIFIER_PERSONAL_ACCESS_TOKEN, |
1236
|
3 |
|
'client_id' => $clientIdentifier, |
1237
|
3 |
|
'client_secret' => $clientSecret, |
1238
|
3 |
|
'user_id' => $userIdentifier, |
1239
|
3 |
|
'scope' => $scope, |
1240
|
3 |
|
]); |
1241
|
|
|
|
1242
|
3 |
|
return new Oauth2AccessTokenData(Json::decode( |
|
|
|
|
1243
|
3 |
|
$this->getAuthorizationServer() |
1244
|
3 |
|
->respondToAccessTokenRequest( |
1245
|
3 |
|
$request, |
1246
|
3 |
|
new Psr7Response() |
1247
|
3 |
|
) |
1248
|
3 |
|
->getBody() |
1249
|
3 |
|
->__toString() |
1250
|
3 |
|
)); |
1251
|
|
|
} |
1252
|
|
|
|
1253
|
|
|
/** |
1254
|
|
|
* @inheritDoc |
1255
|
|
|
*/ |
1256
|
5 |
|
protected function getRequestOauthClaim($attribute, $default = null) |
1257
|
|
|
{ |
1258
|
5 |
|
if (empty($this->_oauthClaimsAuthorizationHeader)) { |
1259
|
|
|
// User authorization was not processed by Oauth2Module. |
1260
|
1 |
|
return $default; |
1261
|
|
|
} |
1262
|
4 |
|
if (Yii::$app->request->getHeaders()->get('Authorization') !== $this->_oauthClaimsAuthorizationHeader) { |
1263
|
1 |
|
throw new InvalidCallException( |
1264
|
1 |
|
'App Request Authorization header does not match the processed Oauth header.' |
1265
|
1 |
|
); |
1266
|
|
|
} |
1267
|
3 |
|
return $this->_oauthClaims[$attribute] ?? $default; |
1268
|
|
|
} |
1269
|
|
|
|
1270
|
|
|
/** |
1271
|
|
|
* Helper function to ensure the required properties are configured for the module. |
1272
|
|
|
* @param string[] $properties |
1273
|
|
|
* @throws InvalidConfigException |
1274
|
|
|
* @since 1.0.0 |
1275
|
|
|
*/ |
1276
|
32 |
|
protected function ensureProperties($properties) |
1277
|
|
|
{ |
1278
|
32 |
|
foreach ($properties as $property) { |
1279
|
32 |
|
if (empty($this->$property)) { |
1280
|
6 |
|
throw new InvalidConfigException(__CLASS__ . '::$' . $property . ' must be set.'); |
1281
|
|
|
} |
1282
|
|
|
} |
1283
|
|
|
} |
1284
|
|
|
} |
1285
|
|
|
|