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