1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace rhertogh\Yii2Oauth2Server\base; |
4
|
|
|
|
5
|
|
|
// phpcs:disable Generic.Files.LineLength.TooLong |
6
|
|
|
use Lcobucci\JWT\Configuration; |
7
|
|
|
use Lcobucci\JWT\Signer\Key\InMemory; |
8
|
|
|
use Lcobucci\JWT\Signer\Rsa\Sha256; |
9
|
|
|
use League\OAuth2\Server\CryptKey; |
10
|
|
|
use rhertogh\Yii2Oauth2Server\components\authorization\client\Oauth2ClientAuthorizationRequest; |
11
|
|
|
use rhertogh\Yii2Oauth2Server\components\authorization\client\Oauth2ClientScopeAuthorizationRequest; |
12
|
|
|
use rhertogh\Yii2Oauth2Server\components\authorization\EndSession\Oauth2EndSessionAuthorizationRequest; |
13
|
|
|
use rhertogh\Yii2Oauth2Server\components\encryption\Oauth2Cryptographer; |
14
|
|
|
use rhertogh\Yii2Oauth2Server\components\factories\encryption\Oauth2EncryptionKeyFactory; |
15
|
|
|
use rhertogh\Yii2Oauth2Server\components\factories\grants\Oauth2AuthCodeGrantFactory; |
16
|
|
|
use rhertogh\Yii2Oauth2Server\components\factories\grants\Oauth2ClientCredentialsGrantFactory; |
17
|
|
|
use rhertogh\Yii2Oauth2Server\components\factories\grants\Oauth2ImplicitGrantFactory; |
18
|
|
|
use rhertogh\Yii2Oauth2Server\components\factories\grants\Oauth2PasswordGrantFactory; |
19
|
|
|
use rhertogh\Yii2Oauth2Server\components\factories\grants\Oauth2PersonalAccessTokenGrantFactory; |
20
|
|
|
use rhertogh\Yii2Oauth2Server\components\factories\grants\Oauth2RefreshTokenGrantFactory; |
21
|
|
|
use rhertogh\Yii2Oauth2Server\components\openidconnect\claims\Oauth2OidcClaim; |
22
|
|
|
use rhertogh\Yii2Oauth2Server\components\openidconnect\scopes\Oauth2OidcScope; |
23
|
|
|
use rhertogh\Yii2Oauth2Server\components\openidconnect\scopes\Oauth2OidcScopeCollection; |
24
|
|
|
use rhertogh\Yii2Oauth2Server\components\openidconnect\server\responses\Oauth2OidcBearerTokenResponse; |
25
|
|
|
use rhertogh\Yii2Oauth2Server\components\repositories\Oauth2AccessTokenRepository; |
26
|
|
|
use rhertogh\Yii2Oauth2Server\components\repositories\Oauth2AuthCodeRepository; |
27
|
|
|
use rhertogh\Yii2Oauth2Server\components\repositories\Oauth2ClientRepository; |
28
|
|
|
use rhertogh\Yii2Oauth2Server\components\repositories\Oauth2RefreshTokenRepository; |
29
|
|
|
use rhertogh\Yii2Oauth2Server\components\repositories\Oauth2ScopeRepository; |
30
|
|
|
use rhertogh\Yii2Oauth2Server\components\repositories\Oauth2UserRepository; |
31
|
|
|
use rhertogh\Yii2Oauth2Server\components\server\grants\Oauth2AuthCodeGrant; |
32
|
|
|
use rhertogh\Yii2Oauth2Server\components\server\grants\Oauth2ClientCredentialsGrant; |
33
|
|
|
use rhertogh\Yii2Oauth2Server\components\server\grants\Oauth2ImplicitGrant; |
34
|
|
|
use rhertogh\Yii2Oauth2Server\components\server\grants\Oauth2PasswordGrant; |
35
|
|
|
use rhertogh\Yii2Oauth2Server\components\server\grants\Oauth2PersonalAccessTokenGrant; |
36
|
|
|
use rhertogh\Yii2Oauth2Server\components\server\grants\Oauth2RefreshTokenGrant; |
37
|
|
|
use rhertogh\Yii2Oauth2Server\components\server\Oauth2AuthorizationServer; |
38
|
|
|
use rhertogh\Yii2Oauth2Server\components\server\Oauth2ResourceServer; |
39
|
|
|
use rhertogh\Yii2Oauth2Server\components\server\responses\Oauth2BearerTokenResponse; |
40
|
|
|
use rhertogh\Yii2Oauth2Server\controllers\console\client\Oauth2CreateClientAction; |
41
|
|
|
use rhertogh\Yii2Oauth2Server\controllers\console\client\Oauth2DeleteClientAction; |
42
|
|
|
use rhertogh\Yii2Oauth2Server\controllers\console\client\Oauth2ListClientsAction; |
43
|
|
|
use rhertogh\Yii2Oauth2Server\controllers\console\client\Oauth2SetClientSecretAction; |
44
|
|
|
use rhertogh\Yii2Oauth2Server\controllers\console\client\Oauth2UpdateClientAction; |
45
|
|
|
use rhertogh\Yii2Oauth2Server\controllers\console\client\Oauth2ViewClientAction; |
46
|
|
|
use rhertogh\Yii2Oauth2Server\controllers\console\debug\Oauth2DebugConfigAction; |
47
|
|
|
use rhertogh\Yii2Oauth2Server\controllers\console\encryption\Oauth2EncryptionKeyUsageAction; |
48
|
|
|
use rhertogh\Yii2Oauth2Server\controllers\console\encryption\Oauth2GenerateSecretAction; |
49
|
|
|
use rhertogh\Yii2Oauth2Server\controllers\console\encryption\Oauth2RotateEncryptionKeysAction; |
50
|
|
|
use rhertogh\Yii2Oauth2Server\controllers\console\migrations\Oauth2GenerateImportMigrationAction; |
51
|
|
|
use rhertogh\Yii2Oauth2Server\controllers\console\migrations\Oauth2GenerateMigrationsAction; |
52
|
|
|
use rhertogh\Yii2Oauth2Server\controllers\console\Oauth2ClientController; |
53
|
|
|
use rhertogh\Yii2Oauth2Server\controllers\console\Oauth2DebugController; |
54
|
|
|
use rhertogh\Yii2Oauth2Server\controllers\console\Oauth2EncryptionController; |
55
|
|
|
use rhertogh\Yii2Oauth2Server\controllers\console\Oauth2MigrationsController; |
56
|
|
|
use rhertogh\Yii2Oauth2Server\controllers\console\Oauth2PersonalAccessTokenController; |
57
|
|
|
use rhertogh\Yii2Oauth2Server\controllers\console\PersonalAccessToken\Oauth2GeneratePatAction; |
58
|
|
|
use rhertogh\Yii2Oauth2Server\controllers\web\certificates\Oauth2JwksAction; |
59
|
|
|
use rhertogh\Yii2Oauth2Server\controllers\web\consent\Oauth2AuthorizeClientAction; |
60
|
|
|
use rhertogh\Yii2Oauth2Server\controllers\web\consent\Oauth2AuthorizeEndSessionAction; |
61
|
|
|
use rhertogh\Yii2Oauth2Server\controllers\web\Oauth2CertificatesController; |
62
|
|
|
use rhertogh\Yii2Oauth2Server\controllers\web\Oauth2ConsentController; |
63
|
|
|
use rhertogh\Yii2Oauth2Server\controllers\web\Oauth2OidcController; |
64
|
|
|
use rhertogh\Yii2Oauth2Server\controllers\web\Oauth2ServerController; |
65
|
|
|
use rhertogh\Yii2Oauth2Server\controllers\web\Oauth2WellKnownController; |
66
|
|
|
use rhertogh\Yii2Oauth2Server\controllers\web\openidconnect\Oauth2OidcEndSessionAction; |
67
|
|
|
use rhertogh\Yii2Oauth2Server\controllers\web\openidconnect\Oauth2OidcUserinfoAction; |
68
|
|
|
use rhertogh\Yii2Oauth2Server\controllers\web\server\Oauth2AccessTokenAction; |
69
|
|
|
use rhertogh\Yii2Oauth2Server\controllers\web\server\Oauth2AuthorizeAction; |
70
|
|
|
use rhertogh\Yii2Oauth2Server\controllers\web\server\Oauth2RevokeAction; |
71
|
|
|
use rhertogh\Yii2Oauth2Server\controllers\web\wellknown\Oauth2OpenidConfigurationAction; |
72
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\components\authorization\client\Oauth2ClientAuthorizationRequestInterface; |
73
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\components\authorization\client\Oauth2ClientScopeAuthorizationRequestInterface; |
74
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\components\authorization\EndSession\Oauth2EndSessionAuthorizationRequestInterface; |
75
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\components\encryption\Oauth2CryptographerInterface; |
76
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\components\factories\encryption\Oauth2EncryptionKeyFactoryInterface; |
77
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\components\factories\grants\Oauth2AuthCodeGrantFactoryInterface; |
78
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\components\factories\grants\Oauth2ClientCredentialsGrantFactoryInterface; |
79
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\components\factories\grants\Oauth2ImplicitGrantFactoryInterface; |
80
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\components\factories\grants\Oauth2PasswordGrantFactoryInterface; |
81
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\components\factories\grants\Oauth2PersonalAccessTokenGrantFactoryInterface; |
82
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\components\factories\grants\Oauth2RefreshTokenGrantFactoryInterface; |
83
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\components\openidconnect\scope\Oauth2OidcClaimInterface; |
84
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\components\openidconnect\scope\Oauth2OidcScopeCollectionInterface; |
85
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\components\openidconnect\scope\Oauth2OidcScopeInterface; |
86
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\components\openidconnect\server\responses\Oauth2OidcBearerTokenResponseInterface; |
87
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\components\repositories\base\Oauth2RepositoryInterface; |
88
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\components\repositories\Oauth2AccessTokenRepositoryInterface; |
89
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\components\repositories\Oauth2AuthCodeRepositoryInterface; |
90
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\components\repositories\Oauth2ClientRepositoryInterface; |
91
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\components\repositories\Oauth2RefreshTokenRepositoryInterface; |
92
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\components\repositories\Oauth2ScopeRepositoryInterface; |
93
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\components\repositories\Oauth2UserRepositoryInterface; |
94
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\components\server\grants\Oauth2AuthCodeGrantInterface; |
95
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\components\server\grants\Oauth2ClientCredentialsGrantInterface; |
96
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\components\server\grants\Oauth2ImplicitGrantInterface; |
97
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\components\server\grants\Oauth2PasswordGrantInterface; |
98
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\components\server\grants\Oauth2PersonalAccessTokenGrantInterface; |
99
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\components\server\grants\Oauth2RefreshTokenGrantInterface; |
100
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\components\server\Oauth2AuthorizationServerInterface; |
101
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\components\server\Oauth2ResourceServerInterface; |
102
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\components\server\responses\Oauth2BearerTokenResponseInterface; |
103
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\controllers\console\client\Oauth2CreateClientActionInterface; |
104
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\controllers\console\client\Oauth2DeleteClientActionInterface; |
105
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\controllers\console\client\Oauth2ListClientsActionInterface; |
106
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\controllers\console\client\Oauth2SetClientSecretActionInterface; |
107
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\controllers\console\client\Oauth2UpdateClientActionInterface; |
108
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\controllers\console\client\Oauth2ViewClientActionInterface; |
109
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\controllers\console\debug\Oauth2DebugConfigActionInterface; |
110
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\controllers\console\encryption\Oauth2EncryptionKeyUsageActionInterface; |
111
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\controllers\console\encryption\Oauth2GenerateSecretActionInterface; |
112
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\controllers\console\encryption\Oauth2RotateEncryptionKeysActionInterface; |
113
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\controllers\console\migrations\Oauth2GenerateImportMigrationActionInterface; |
114
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\controllers\console\migrations\Oauth2GenerateMigrationsActionInterface; |
115
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\controllers\console\Oauth2ClientControllerInterface; |
116
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\controllers\console\Oauth2DebugControllerInterface; |
117
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\controllers\console\Oauth2EncryptionControllerInterface; |
118
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\controllers\console\Oauth2MigrationsControllerInterface; |
119
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\controllers\console\Oauth2PersonalAccessTokenControllerInterface; |
120
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\controllers\console\PersonalAccessToken\Oauth2GeneratePatActionInterface; |
121
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\controllers\web\certificates\Oauth2JwksActionInterface; |
122
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\controllers\web\consent\Oauth2AuthorizeClientActionInterface; |
123
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\controllers\web\consent\Oauth2AuthorizeEndSessionActionInterface; |
124
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\controllers\web\Oauth2CertificatesControllerInterface; |
125
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\controllers\web\Oauth2ConsentControllerInterface; |
126
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\controllers\web\Oauth2OidcControllerInterface; |
127
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\controllers\web\Oauth2ServerControllerInterface; |
128
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\controllers\web\Oauth2WellKnownControllerInterface; |
129
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\controllers\web\openidconnect\Oauth2OidcEndSessionActionInterface; |
130
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\controllers\web\openidconnect\Oauth2OidcUserinfoActionInterface; |
131
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\controllers\web\server\Oauth2AccessTokenActionInterface; |
132
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\controllers\web\server\Oauth2AuthorizeActionInterface; |
133
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\controllers\web\server\Oauth2RevokeActionInterface; |
134
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\controllers\web\wellknown\Oauth2OpenidConfigurationActionInterface; |
135
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\models\external\user\Oauth2OidcUserInterface; |
136
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\models\Oauth2AccessTokenInterface; |
137
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\models\Oauth2AccessTokenScopeInterface; |
138
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\models\Oauth2AuthCodeInterface; |
139
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\models\Oauth2AuthCodeScopeInterface; |
140
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\models\Oauth2ClientInterface; |
141
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\models\Oauth2ClientScopeInterface; |
142
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\models\Oauth2RefreshTokenInterface; |
143
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\models\Oauth2ScopeInterface; |
144
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\models\Oauth2UserClientInterface; |
145
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\models\Oauth2UserClientScopeInterface; |
146
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\models\queries\Oauth2AccessTokenQueryInterface; |
147
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\models\queries\Oauth2AccessTokenScopeQueryInterface; |
148
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\models\queries\Oauth2AuthCodeQueryInterface; |
149
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\models\queries\Oauth2AuthCodeScopeQueryInterface; |
150
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\models\queries\Oauth2ClientQueryInterface; |
151
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\models\queries\Oauth2ClientScopeQueryInterface; |
152
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\models\queries\Oauth2RefreshTokenQueryInterface; |
153
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\models\queries\Oauth2ScopeQueryInterface; |
154
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\models\queries\Oauth2UserClientQueryInterface; |
155
|
|
|
use rhertogh\Yii2Oauth2Server\interfaces\models\queries\Oauth2UserClientScopeQueryInterface; |
156
|
|
|
use rhertogh\Yii2Oauth2Server\models\Oauth2AccessToken; |
157
|
|
|
use rhertogh\Yii2Oauth2Server\models\Oauth2AccessTokenScope; |
158
|
|
|
use rhertogh\Yii2Oauth2Server\models\Oauth2AuthCode; |
159
|
|
|
use rhertogh\Yii2Oauth2Server\models\Oauth2AuthCodeScope; |
160
|
|
|
use rhertogh\Yii2Oauth2Server\models\Oauth2Client; |
161
|
|
|
use rhertogh\Yii2Oauth2Server\models\Oauth2ClientScope; |
162
|
|
|
use rhertogh\Yii2Oauth2Server\models\Oauth2RefreshToken; |
163
|
|
|
use rhertogh\Yii2Oauth2Server\models\Oauth2Scope; |
164
|
|
|
use rhertogh\Yii2Oauth2Server\models\Oauth2UserClient; |
165
|
|
|
use rhertogh\Yii2Oauth2Server\models\Oauth2UserClientScope; |
166
|
|
|
use rhertogh\Yii2Oauth2Server\models\queries\Oauth2AccessTokenQuery; |
167
|
|
|
use rhertogh\Yii2Oauth2Server\models\queries\Oauth2AccessTokenScopeQuery; |
168
|
|
|
use rhertogh\Yii2Oauth2Server\models\queries\Oauth2AuthCodeQuery; |
169
|
|
|
use rhertogh\Yii2Oauth2Server\models\queries\Oauth2AuthCodeScopeQuery; |
170
|
|
|
use rhertogh\Yii2Oauth2Server\models\queries\Oauth2ClientQuery; |
171
|
|
|
use rhertogh\Yii2Oauth2Server\models\queries\Oauth2ClientScopeQuery; |
172
|
|
|
use rhertogh\Yii2Oauth2Server\models\queries\Oauth2RefreshTokenQuery; |
173
|
|
|
use rhertogh\Yii2Oauth2Server\models\queries\Oauth2ScopeQuery; |
174
|
|
|
use rhertogh\Yii2Oauth2Server\models\queries\Oauth2UserClientQuery; |
175
|
|
|
use rhertogh\Yii2Oauth2Server\models\queries\Oauth2UserClientScopeQuery; |
176
|
|
|
use Yii; |
177
|
|
|
use yii\base\InvalidCallException; |
178
|
|
|
use yii\base\InvalidConfigException; |
179
|
|
|
use yii\base\Module; |
|
|
|
|
180
|
|
|
// phpcs:enable Generic.Files.LineLength.TooLong |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @property Oauth2OidcScopeCollectionInterface|array|callable|string $openIdConnectScopes; |
184
|
|
|
*/ |
185
|
|
|
abstract class Oauth2BaseModule extends Module |
186
|
|
|
{ |
187
|
|
|
# region Supported grant types. |
188
|
|
|
# Note: These should match League\OAuth2\Server\Grant\GrantTypeInterface::getIdentifier() for their respective type. |
189
|
|
|
/** |
190
|
|
|
* "authorization_code" Grant Type. |
191
|
|
|
* @since 1.0.0 |
192
|
|
|
*/ |
193
|
|
|
public const GRANT_TYPE_IDENTIFIER_AUTH_CODE = 'authorization_code'; |
194
|
|
|
/** |
195
|
|
|
* "client_credentials" Grant Type. |
196
|
|
|
* @since 1.0.0 |
197
|
|
|
*/ |
198
|
|
|
public const GRANT_TYPE_IDENTIFIER_CLIENT_CREDENTIALS = 'client_credentials'; |
199
|
|
|
/** |
200
|
|
|
* "refresh_token" Grant Type. |
201
|
|
|
* @since 1.0.0 |
202
|
|
|
*/ |
203
|
|
|
public const GRANT_TYPE_IDENTIFIER_REFRESH_TOKEN = 'refresh_token'; |
204
|
|
|
/** |
205
|
|
|
* "implicit" Grant Type. Note: This is a legacy Grant Type. |
206
|
|
|
* @since 1.0.0 |
207
|
|
|
*/ |
208
|
|
|
public const GRANT_TYPE_IDENTIFIER_IMPLICIT = 'implicit'; |
209
|
|
|
/** |
210
|
|
|
* "password" Grant Type. Note: This is a legacy Grant Type. |
211
|
|
|
* @since 1.0.0 |
212
|
|
|
*/ |
213
|
|
|
public const GRANT_TYPE_IDENTIFIER_PASSWORD = 'password'; |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* "personal_access_token" Grant Type. Note: This is a custom grant type and not part of the Oauth2 specification. |
217
|
|
|
* @since 1.0.0 |
218
|
|
|
*/ |
219
|
|
|
public const GRANT_TYPE_IDENTIFIER_PERSONAL_ACCESS_TOKEN = 'personal_access_token'; |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Supported grant type identifiers |
223
|
|
|
* @since 1.0.0 |
224
|
|
|
*/ |
225
|
|
|
public const GRANT_TYPE_IDENTIFIERS = [ |
226
|
|
|
self::GRANT_TYPE_IDENTIFIER_AUTH_CODE, |
227
|
|
|
self::GRANT_TYPE_IDENTIFIER_CLIENT_CREDENTIALS, |
228
|
|
|
self::GRANT_TYPE_IDENTIFIER_REFRESH_TOKEN, |
229
|
|
|
self::GRANT_TYPE_IDENTIFIER_IMPLICIT, |
230
|
|
|
self::GRANT_TYPE_IDENTIFIER_PASSWORD, |
231
|
|
|
self::GRANT_TYPE_IDENTIFIER_PERSONAL_ACCESS_TOKEN, |
232
|
|
|
]; |
233
|
|
|
# endregion Supported grant types |
234
|
|
|
|
235
|
|
|
# region Numeric IDs for Supported grant types |
236
|
|
|
/** |
237
|
|
|
* Numeric id for "authorization_code" Grant Type. |
238
|
|
|
* @since 1.0.0 |
239
|
|
|
*/ |
240
|
|
|
public const GRANT_TYPE_AUTH_CODE = 1; |
241
|
|
|
/** |
242
|
|
|
* Numeric id for "client_credentials" Grant Type. |
243
|
|
|
* @since 1.0.0 |
244
|
|
|
*/ |
245
|
|
|
public const GRANT_TYPE_CLIENT_CREDENTIALS = 2; |
246
|
|
|
/** |
247
|
|
|
* Numeric id for "refresh_token" Grant Type. |
248
|
|
|
* @since 1.0.0 |
249
|
|
|
*/ |
250
|
|
|
public const GRANT_TYPE_REFRESH_TOKEN = 4; |
251
|
|
|
/** |
252
|
|
|
* Numeric id for "implicit" Grant Type. Note: This is a legacy Grant Type. |
253
|
|
|
* @since 1.0.0 |
254
|
|
|
*/ |
255
|
|
|
public const GRANT_TYPE_PASSWORD = 1024; // Legacy Grant. |
256
|
|
|
/** |
257
|
|
|
* Numeric id for "password" Grant Type. Note: This is a legacy Grant Type. |
258
|
|
|
* @since 1.0.0 |
259
|
|
|
*/ |
260
|
|
|
public const GRANT_TYPE_IMPLICIT = 2048; // Legacy Grant. |
261
|
|
|
/** |
262
|
|
|
* Numeric id for "personal_access_token" Grant Type. |
263
|
|
|
* Note: This is a custom grant type and not part of the Oauth2 specification. |
264
|
|
|
* @since 1.0.0 |
265
|
|
|
*/ |
266
|
|
|
public const GRANT_TYPE_PERSONAL_ACCESS_TOKEN = 4096; |
267
|
|
|
# endregion Numeric IDs for Supported grant types |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Mapping between Grant Type identifier and its numeric id. |
271
|
|
|
* @since 1.0.0 |
272
|
|
|
*/ |
273
|
|
|
public const GRANT_TYPE_MAPPING = [ |
274
|
|
|
self::GRANT_TYPE_IDENTIFIER_AUTH_CODE => self::GRANT_TYPE_AUTH_CODE, |
275
|
|
|
self::GRANT_TYPE_IDENTIFIER_CLIENT_CREDENTIALS => self::GRANT_TYPE_CLIENT_CREDENTIALS, |
276
|
|
|
self::GRANT_TYPE_IDENTIFIER_REFRESH_TOKEN => self::GRANT_TYPE_REFRESH_TOKEN, |
277
|
|
|
self::GRANT_TYPE_IDENTIFIER_PASSWORD => self::GRANT_TYPE_PASSWORD, // Legacy Grant. |
278
|
|
|
self::GRANT_TYPE_IDENTIFIER_IMPLICIT => self::GRANT_TYPE_IMPLICIT, // Legacy Grant. |
279
|
|
|
self::GRANT_TYPE_IDENTIFIER_PERSONAL_ACCESS_TOKEN => self::GRANT_TYPE_PERSONAL_ACCESS_TOKEN, // Custom Grant. |
280
|
|
|
]; |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Events |
284
|
|
|
*/ |
285
|
|
|
public const EVENT_BEFORE_CLIENT_AUTHORIZATION = 'Oauth2Server.Client.Authorization.Before'; |
286
|
|
|
public const EVENT_BEFORE_AFTER_AUTHORIZATION = 'Oauth2Server.Client.Authorization.After'; |
287
|
|
|
public const EVENT_BEFORE_AUTH_CODE_ISSUANCE = 'Oauth2Server.Grant.AuthCode.Issuance.Before'; |
288
|
|
|
public const EVENT_AFTER_AUTH_CODE_ISSUANCE = 'Oauth2Server.Grant.AuthCode.Issuance.After'; |
289
|
|
|
public const EVENT_BEFORE_ACCESS_TOKEN_ISSUANCE = 'Oauth2Server.Grant.AccessToken.Issuance.Before'; |
290
|
|
|
public const EVENT_AFTER_ACCESS_TOKEN_ISSUANCE = 'Oauth2Server.Grant.AccessToken.Issuance.After'; |
291
|
|
|
public const EVENT_BEFORE_REFRESH_TOKEN_ISSUANCE = 'Oauth2Server.Grant.RefreshToken.Issuance.Before'; |
292
|
|
|
public const EVENT_AFTER_REFRESH_TOKEN_ISSUANCE = 'Oauth2Server.Grant.RefreshToken.Issuance.After'; |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* Never show User Account Selection for OpenID Connect. |
296
|
|
|
* @since 1.0.0 |
297
|
|
|
*/ |
298
|
|
|
public const USER_ACCOUNT_SELECTION_DISABLED = 0; |
299
|
|
|
/** |
300
|
|
|
* Show User Account Selection upon client request for OpenID Connect. |
301
|
|
|
* @since 1.0.0 |
302
|
|
|
*/ |
303
|
|
|
public const USER_ACCOUNT_SELECTION_UPON_CLIENT_REQUEST = 1; |
304
|
|
|
/** |
305
|
|
|
* Always show User Account Selection for OpenID Connect. |
306
|
|
|
* @since 1.0.0 |
307
|
|
|
*/ |
308
|
|
|
public const USER_ACCOUNT_SELECTION_ALWAYS = 2; |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* Human-readable name for user account selection options. |
312
|
|
|
* @since 1.0.0 |
313
|
|
|
*/ |
314
|
|
|
public const USER_ACCOUNT_SELECTION_NAMES = [ |
315
|
|
|
self::USER_ACCOUNT_SELECTION_DISABLED => 'disabled', |
316
|
|
|
self::USER_ACCOUNT_SELECTION_UPON_CLIENT_REQUEST => 'upon_client_request', |
317
|
|
|
self::USER_ACCOUNT_SELECTION_ALWAYS => 'always', |
318
|
|
|
]; |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* Default factory interface per grant type |
322
|
|
|
* @since 1.0.0 |
323
|
|
|
*/ |
324
|
|
|
protected const DEFAULT_GRANT_TYPE_FACTORIES = [ |
325
|
|
|
self::GRANT_TYPE_AUTH_CODE => Oauth2AuthCodeGrantFactoryInterface::class, |
326
|
|
|
self::GRANT_TYPE_CLIENT_CREDENTIALS => Oauth2ClientCredentialsGrantFactoryInterface::class, |
327
|
|
|
self::GRANT_TYPE_REFRESH_TOKEN => Oauth2RefreshTokenGrantFactoryInterface::class, |
328
|
|
|
self::GRANT_TYPE_IMPLICIT => Oauth2ImplicitGrantFactoryInterface::class, // Legacy Grant. |
329
|
|
|
self::GRANT_TYPE_PASSWORD => Oauth2PasswordGrantFactoryInterface::class, // Legacy Grant. |
330
|
|
|
self::GRANT_TYPE_PERSONAL_ACCESS_TOKEN => Oauth2PersonalAccessTokenGrantFactoryInterface::class, // Custom Grant. |
331
|
|
|
]; |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* Default mapping for interfaces |
335
|
|
|
* @since 1.0.0 |
336
|
|
|
*/ |
337
|
|
|
protected const DEFAULT_INTERFACE_IMPLEMENTATIONS = [ |
338
|
|
|
# Repositories |
339
|
|
|
Oauth2AccessTokenRepositoryInterface::class => Oauth2AccessTokenRepository::class, |
340
|
|
|
Oauth2AuthCodeRepositoryInterface::class => Oauth2AuthCodeRepository::class, |
341
|
|
|
Oauth2ClientRepositoryInterface::class => Oauth2ClientRepository::class, |
342
|
|
|
Oauth2RefreshTokenRepositoryInterface::class => Oauth2RefreshTokenRepository::class, |
343
|
|
|
Oauth2ScopeRepositoryInterface::class => Oauth2ScopeRepository::class, |
344
|
|
|
Oauth2UserRepositoryInterface::class => Oauth2UserRepository::class, |
345
|
|
|
# Models |
346
|
|
|
Oauth2AccessTokenInterface::class => Oauth2AccessToken::class, |
347
|
|
|
Oauth2AccessTokenScopeInterface::class => Oauth2AccessTokenScope::class, |
348
|
|
|
Oauth2AuthCodeInterface::class => Oauth2AuthCode::class, |
349
|
|
|
Oauth2AuthCodeScopeInterface::class => Oauth2AuthCodeScope::class, |
350
|
|
|
Oauth2ClientInterface::class => Oauth2Client::class, |
351
|
|
|
Oauth2ClientScopeInterface::class => Oauth2ClientScope::class, |
352
|
|
|
Oauth2RefreshTokenInterface::class => Oauth2RefreshToken::class, |
353
|
|
|
Oauth2ScopeInterface::class => Oauth2Scope::class, |
354
|
|
|
Oauth2UserClientInterface::class => Oauth2UserClient::class, |
355
|
|
|
Oauth2UserClientScopeInterface::class => Oauth2UserClientScope::class, |
356
|
|
|
# Queries |
357
|
|
|
Oauth2AccessTokenQueryInterface::class => Oauth2AccessTokenQuery::class, |
358
|
|
|
Oauth2AccessTokenScopeQueryInterface::class => Oauth2AccessTokenScopeQuery::class, |
359
|
|
|
Oauth2AuthCodeQueryInterface::class => Oauth2AuthCodeQuery::class, |
360
|
|
|
Oauth2AuthCodeScopeQueryInterface::class => Oauth2AuthCodeScopeQuery::class, |
361
|
|
|
Oauth2ClientQueryInterface::class => Oauth2ClientQuery::class, |
362
|
|
|
Oauth2ClientScopeQueryInterface::class => Oauth2ClientScopeQuery::class, |
363
|
|
|
Oauth2RefreshTokenQueryInterface::class => Oauth2RefreshTokenQuery::class, |
364
|
|
|
Oauth2ScopeQueryInterface::class => Oauth2ScopeQuery::class, |
365
|
|
|
Oauth2UserClientQueryInterface::class => Oauth2UserClientQuery::class, |
366
|
|
|
Oauth2UserClientScopeQueryInterface::class => Oauth2UserClientScopeQuery::class, |
367
|
|
|
# Factories |
368
|
|
|
Oauth2AuthCodeGrantFactoryInterface::class => Oauth2AuthCodeGrantFactory::class, |
369
|
|
|
Oauth2ClientCredentialsGrantFactoryInterface::class => Oauth2ClientCredentialsGrantFactory::class, |
370
|
|
|
Oauth2RefreshTokenGrantFactoryInterface::class => Oauth2RefreshTokenGrantFactory::class, |
371
|
|
|
Oauth2ImplicitGrantFactoryInterface::class => Oauth2ImplicitGrantFactory::class, |
372
|
|
|
Oauth2PasswordGrantFactoryInterface::class => Oauth2PasswordGrantFactory::class, |
373
|
|
|
Oauth2PersonalAccessTokenGrantFactoryInterface::class => Oauth2PersonalAccessTokenGrantFactory::class, |
374
|
|
|
Oauth2EncryptionKeyFactoryInterface::class => Oauth2EncryptionKeyFactory::class, |
375
|
|
|
# Controllers (web) |
376
|
|
|
Oauth2ServerControllerInterface::class => Oauth2ServerController::class, |
377
|
|
|
Oauth2ConsentControllerInterface::class => Oauth2ConsentController::class, |
378
|
|
|
Oauth2WellKnownControllerInterface::class => Oauth2WellKnownController::class, |
379
|
|
|
Oauth2CertificatesControllerInterface::class => Oauth2CertificatesController::class, |
380
|
|
|
Oauth2OidcControllerInterface::class => Oauth2OidcController::class, |
381
|
|
|
# Controllers (console) |
382
|
|
|
Oauth2PersonalAccessTokenControllerInterface::class => Oauth2PersonalAccessTokenController::class, |
383
|
|
|
Oauth2MigrationsControllerInterface::class => Oauth2MigrationsController::class, |
384
|
|
|
Oauth2EncryptionControllerInterface::class => Oauth2EncryptionController::class, |
385
|
|
|
Oauth2DebugControllerInterface::class => Oauth2DebugController::class, |
386
|
|
|
Oauth2ClientControllerInterface::class => Oauth2ClientController::class, |
387
|
|
|
# Actions (web) |
388
|
|
|
Oauth2OpenidConfigurationActionInterface::class => Oauth2OpenidConfigurationAction::class, |
389
|
|
|
Oauth2RevokeActionInterface::class => Oauth2RevokeAction::class, |
390
|
|
|
Oauth2AuthorizeActionInterface::class => Oauth2AuthorizeAction::class, |
391
|
|
|
Oauth2AccessTokenActionInterface::class => Oauth2AccessTokenAction::class, |
392
|
|
|
Oauth2OidcUserinfoActionInterface::class => Oauth2OidcUserinfoAction::class, |
393
|
|
|
Oauth2OidcEndSessionActionInterface::class => Oauth2OidcEndSessionAction::class, |
394
|
|
|
Oauth2AuthorizeClientActionInterface::class => Oauth2AuthorizeClientAction::class, |
395
|
|
|
Oauth2AuthorizeEndSessionActionInterface::class => Oauth2AuthorizeEndSessionAction::class, |
396
|
|
|
Oauth2JwksActionInterface::class => Oauth2JwksAction::class, |
397
|
|
|
# Actions (console) |
398
|
|
|
Oauth2GeneratePatActionInterface::class => Oauth2GeneratePatAction::class, |
399
|
|
|
Oauth2GenerateMigrationsActionInterface::class => Oauth2GenerateMigrationsAction::class, |
400
|
|
|
Oauth2GenerateImportMigrationActionInterface::class => Oauth2GenerateImportMigrationAction::class, |
401
|
|
|
Oauth2RotateEncryptionKeysActionInterface::class => Oauth2RotateEncryptionKeysAction::class, |
402
|
|
|
Oauth2GenerateSecretActionInterface::class => Oauth2GenerateSecretAction::class, |
403
|
|
|
Oauth2EncryptionKeyUsageActionInterface::class => Oauth2EncryptionKeyUsageAction::class, |
404
|
|
|
Oauth2DebugConfigActionInterface::class => Oauth2DebugConfigAction::class, |
405
|
|
|
Oauth2ViewClientActionInterface::class => Oauth2ViewClientAction::class, |
406
|
|
|
Oauth2UpdateClientActionInterface::class => Oauth2UpdateClientAction::class, |
407
|
|
|
Oauth2SetClientSecretActionInterface::class => Oauth2SetClientSecretAction::class, |
408
|
|
|
Oauth2ListClientsActionInterface::class => Oauth2ListClientsAction::class, |
409
|
|
|
Oauth2DeleteClientActionInterface::class => Oauth2DeleteClientAction::class, |
410
|
|
|
Oauth2CreateClientActionInterface::class => Oauth2CreateClientAction::class, |
411
|
|
|
# Components (Server) |
412
|
|
|
Oauth2AuthorizationServerInterface::class => Oauth2AuthorizationServer::class, |
413
|
|
|
Oauth2ResourceServerInterface::class => Oauth2ResourceServer::class, |
414
|
|
|
# Components (Server Grants) |
415
|
|
|
Oauth2AuthCodeGrantInterface::class => Oauth2AuthCodeGrant::class, |
416
|
|
|
Oauth2ClientCredentialsGrantInterface::class => Oauth2ClientCredentialsGrant::class, |
417
|
|
|
Oauth2ImplicitGrantInterface::class => Oauth2ImplicitGrant::class, |
418
|
|
|
Oauth2PasswordGrantInterface::class => Oauth2PasswordGrant::class, |
419
|
|
|
Oauth2RefreshTokenGrantInterface::class => Oauth2RefreshTokenGrant::class, |
420
|
|
|
Oauth2PersonalAccessTokenGrantInterface::class => Oauth2PersonalAccessTokenGrant::class, |
421
|
|
|
# Components (Responses) |
422
|
|
|
Oauth2BearerTokenResponseInterface::class => Oauth2BearerTokenResponse::class, |
423
|
|
|
# Components (OpenID Connect) |
424
|
|
|
Oauth2OidcScopeCollectionInterface::class => Oauth2OidcScopeCollection::class, |
425
|
|
|
Oauth2OidcScopeInterface::class => Oauth2OidcScope::class, |
426
|
|
|
Oauth2OidcClaimInterface::class => Oauth2OidcClaim::class, |
427
|
|
|
Oauth2OidcBearerTokenResponseInterface::class => Oauth2OidcBearerTokenResponse::class, |
428
|
|
|
# Authorization |
429
|
|
|
Oauth2ClientAuthorizationRequestInterface::class => Oauth2ClientAuthorizationRequest::class, |
430
|
|
|
Oauth2ClientScopeAuthorizationRequestInterface::class => Oauth2ClientScopeAuthorizationRequest::class, |
431
|
|
|
Oauth2EndSessionAuthorizationRequestInterface::class => Oauth2EndSessionAuthorizationRequest::class, |
432
|
|
|
# Components (Misc) |
433
|
|
|
Oauth2CryptographerInterface::class => Oauth2Cryptographer::class, |
434
|
|
|
]; |
435
|
|
|
|
436
|
|
|
/** |
437
|
|
|
* Cache for the Repositories |
438
|
|
|
* @var Oauth2RepositoryInterface[] |
439
|
|
|
* @since 1.0.0 |
440
|
|
|
*/ |
441
|
|
|
protected $_repositories; |
442
|
|
|
|
443
|
|
|
/** |
444
|
|
|
* Claims for the current request |
445
|
|
|
* @var mixed[] |
446
|
|
|
* @since 1.0.0 |
447
|
|
|
*/ |
448
|
|
|
protected $_oauthClaims; |
449
|
|
|
|
450
|
|
|
/** |
451
|
|
|
* Configuration for the enabled OpenID Connect scopes. |
452
|
|
|
* @var Oauth2OidcScopeCollectionInterface|array|callable|string |
453
|
|
|
* @since 1.0.0 |
454
|
|
|
*/ |
455
|
|
|
protected $_openIdConnectScopes = Oauth2OidcScopeCollectionInterface::OPENID_CONNECT_DEFAULT_SCOPES; |
456
|
|
|
|
457
|
|
|
/** |
458
|
|
|
* Cache for the OpenID Connect scope collection. |
459
|
|
|
* @var Oauth2OidcScopeCollectionInterface|null |
460
|
|
|
* @since 1.0.0 |
461
|
|
|
*/ |
462
|
|
|
protected $_oidcScopeCollection = null; |
463
|
|
|
|
464
|
|
|
////////////////////////// |
465
|
|
|
/// Abstract Functions /// |
466
|
|
|
////////////////////////// |
467
|
|
|
|
468
|
|
|
/** |
469
|
|
|
* @return Oauth2OidcScopeCollectionInterface The supported scopes for OpenID Connect |
470
|
|
|
* @since 1.0.0 |
471
|
|
|
*/ |
472
|
|
|
abstract public function getOidcScopeCollection(); |
473
|
|
|
|
474
|
|
|
/** |
475
|
|
|
* Get a specific claim from an authorized Request |
476
|
|
|
* @param string $attribute |
477
|
|
|
* @param mixed|null $default |
478
|
|
|
* @return mixed|null The value of the claim or the $default value if not set. |
479
|
|
|
* @throws InvalidCallException |
480
|
|
|
* @since 1.0.0 |
481
|
|
|
*/ |
482
|
|
|
abstract protected function getRequestOauthClaim($attribute, $default = null); |
483
|
|
|
|
484
|
|
|
|
485
|
|
|
//////////////////////// |
486
|
|
|
/// Static Functions /// |
487
|
|
|
//////////////////////// |
488
|
|
|
|
489
|
|
|
/** |
490
|
|
|
* Convert a grant type identifier to its numeric id |
491
|
|
|
* @param string $grantTypeIdentifier |
492
|
|
|
* @return int|null |
493
|
|
|
* @since 1.0.0 |
494
|
|
|
*/ |
495
|
1 |
|
public static function getGrantTypeId($grantTypeIdentifier) |
496
|
|
|
{ |
497
|
1 |
|
return static::GRANT_TYPE_MAPPING[$grantTypeIdentifier] ?? null; |
498
|
|
|
} |
499
|
|
|
|
500
|
|
|
/** |
501
|
|
|
* Convert a numeric grant type id to its string identifier |
502
|
|
|
* @param int $grantTypeId |
503
|
|
|
* @return int|null |
504
|
|
|
* @since 1.0.0 |
505
|
|
|
*/ |
506
|
1 |
|
public static function getGrantTypeIdentifier($grantTypeId) |
507
|
|
|
{ |
508
|
1 |
|
return array_flip(static::GRANT_TYPE_MAPPING)[$grantTypeId] ?? null; |
509
|
|
|
} |
510
|
|
|
|
511
|
|
|
/** |
512
|
|
|
* Convert Grant Type IDs to an array of their identifiers |
513
|
|
|
* @param int $grantTypeIDs |
514
|
|
|
* @return array |
515
|
|
|
*/ |
516
|
|
|
public static function getGrantTypeIdentifiers($grantTypeIDs) |
517
|
|
|
{ |
518
|
|
|
$identifiers = []; |
519
|
|
|
foreach (static::GRANT_TYPE_MAPPING as $identifier => $id) { |
520
|
|
|
if ($grantTypeIDs & $id) { |
521
|
|
|
$identifiers[] = $identifier; |
522
|
|
|
} |
523
|
|
|
} |
524
|
|
|
return $identifiers; |
525
|
|
|
} |
526
|
|
|
|
527
|
|
|
///////////////////////// |
528
|
|
|
/// Getters & Setters /// |
529
|
|
|
///////////////////////// |
530
|
|
|
|
531
|
|
|
/** |
532
|
|
|
* @return Oauth2AccessTokenRepositoryInterface The Access Token Repository |
533
|
|
|
* @since 1.0.0 |
534
|
|
|
*/ |
535
|
7 |
|
public function getAccessTokenRepository(): Oauth2AccessTokenRepositoryInterface |
536
|
|
|
{ |
537
|
7 |
|
return $this->getRepository(Oauth2AccessTokenRepositoryInterface::class); |
538
|
|
|
} |
539
|
|
|
|
540
|
|
|
/** |
541
|
|
|
* @return $this |
542
|
|
|
* @since 1.0.0 |
543
|
|
|
*/ |
544
|
1 |
|
public function setAccessTokenRepository(Oauth2AccessTokenRepositoryInterface $repository) |
545
|
|
|
{ |
546
|
1 |
|
$this->setRepository(Oauth2AccessTokenRepositoryInterface::class, $repository); |
547
|
1 |
|
return $this; |
548
|
|
|
} |
549
|
|
|
|
550
|
|
|
/** |
551
|
|
|
* @return Oauth2AuthCodeRepositoryInterface The Auth Code Repository |
552
|
|
|
* @since 1.0.0 |
553
|
|
|
*/ |
554
|
7 |
|
public function getAuthCodeRepository(): Oauth2AuthCodeRepositoryInterface |
555
|
|
|
{ |
556
|
7 |
|
return $this->getRepository(Oauth2AuthCodeRepositoryInterface::class); |
557
|
|
|
} |
558
|
|
|
|
559
|
|
|
/** |
560
|
|
|
* @return $this |
561
|
|
|
* @since 1.0.0 |
562
|
|
|
*/ |
563
|
1 |
|
public function setAuthCodeRepository(Oauth2AuthCodeRepositoryInterface $repository) |
564
|
|
|
{ |
565
|
1 |
|
$this->setRepository(Oauth2AuthCodeRepositoryInterface::class, $repository); |
566
|
1 |
|
return $this; |
567
|
|
|
} |
568
|
|
|
|
569
|
|
|
/** |
570
|
|
|
* @return Oauth2ClientRepositoryInterface The Client Repository |
571
|
|
|
* @since 1.0.0 |
572
|
|
|
*/ |
573
|
6 |
|
public function getClientRepository(): Oauth2ClientRepositoryInterface |
574
|
|
|
{ |
575
|
6 |
|
return $this->getRepository(Oauth2ClientRepositoryInterface::class); |
576
|
|
|
} |
577
|
|
|
|
578
|
|
|
/** |
579
|
|
|
* @return $this |
580
|
|
|
* @since 1.0.0 |
581
|
|
|
*/ |
582
|
1 |
|
public function setClientRepository(Oauth2ClientRepositoryInterface $repository) |
583
|
|
|
{ |
584
|
1 |
|
$this->setRepository(Oauth2ClientRepositoryInterface::class, $repository); |
585
|
1 |
|
return $this; |
586
|
|
|
} |
587
|
|
|
|
588
|
|
|
/** |
589
|
|
|
* @return Oauth2RefreshTokenRepositoryInterface The Refresh Token Repository |
590
|
|
|
* @since 1.0.0 |
591
|
|
|
*/ |
592
|
9 |
|
public function getRefreshTokenRepository(): Oauth2RefreshTokenRepositoryInterface |
593
|
|
|
{ |
594
|
9 |
|
return $this->getRepository(Oauth2RefreshTokenRepositoryInterface::class); |
595
|
|
|
} |
596
|
|
|
|
597
|
|
|
/** |
598
|
|
|
* @return $this |
599
|
|
|
* @since 1.0.0 |
600
|
|
|
*/ |
601
|
1 |
|
public function setRefreshTokenRepository(Oauth2RefreshTokenRepositoryInterface $repository) |
602
|
|
|
{ |
603
|
1 |
|
$this->setRepository(Oauth2RefreshTokenRepositoryInterface::class, $repository); |
604
|
1 |
|
return $this; |
605
|
|
|
} |
606
|
|
|
|
607
|
|
|
/** |
608
|
|
|
* @return Oauth2ScopeRepositoryInterface The Scope Repository |
609
|
|
|
* @since 1.0.0 |
610
|
|
|
*/ |
611
|
6 |
|
public function getScopeRepository(): Oauth2ScopeRepositoryInterface |
612
|
|
|
{ |
613
|
6 |
|
return $this->getRepository(Oauth2ScopeRepositoryInterface::class); |
614
|
|
|
} |
615
|
|
|
|
616
|
|
|
/** |
617
|
|
|
* @return $this |
618
|
|
|
* @since 1.0.0 |
619
|
|
|
*/ |
620
|
1 |
|
public function setScopeRepository(Oauth2ScopeRepositoryInterface $repository) |
621
|
|
|
{ |
622
|
1 |
|
$this->setRepository(Oauth2ScopeRepositoryInterface::class, $repository); |
623
|
1 |
|
return $this; |
624
|
|
|
} |
625
|
|
|
|
626
|
|
|
/** |
627
|
|
|
* @return Oauth2UserRepositoryInterface The User Repository |
628
|
|
|
* @since 1.0.0 |
629
|
|
|
*/ |
630
|
8 |
|
public function getUserRepository(): Oauth2UserRepositoryInterface |
631
|
|
|
{ |
632
|
8 |
|
return $this->getRepository(Oauth2UserRepositoryInterface::class); |
633
|
|
|
} |
634
|
|
|
|
635
|
|
|
/** |
636
|
|
|
* @return $this |
637
|
|
|
* @since 1.0.0 |
638
|
|
|
*/ |
639
|
1 |
|
public function setUserRepository(Oauth2UserRepositoryInterface $repository) |
640
|
|
|
{ |
641
|
1 |
|
$this->setRepository(Oauth2UserRepositoryInterface::class, $repository); |
642
|
1 |
|
return $this; |
643
|
|
|
} |
644
|
|
|
|
645
|
|
|
/** |
646
|
|
|
* Get a repository by class. |
647
|
|
|
* @template T of Oauth2RepositoryInterface |
648
|
|
|
* @param class-string<T> $class |
|
|
|
|
649
|
|
|
* @return T |
650
|
|
|
* @throws \yii\base\InvalidConfigException |
651
|
|
|
* @since 1.0.0 |
652
|
|
|
*/ |
653
|
20 |
|
protected function getRepository($class) |
654
|
|
|
{ |
655
|
20 |
|
if (empty($this->_repositories[$class])) { |
656
|
14 |
|
$this->setRepository($class, Yii::createObject($class)); |
657
|
|
|
} |
658
|
|
|
|
659
|
20 |
|
return $this->_repositories[$class]; |
660
|
|
|
} |
661
|
|
|
|
662
|
|
|
/** |
663
|
|
|
* @param class-string<Oauth2RepositoryInterface> $class |
|
|
|
|
664
|
|
|
* @return $this |
665
|
|
|
* @throws InvalidConfigException |
666
|
|
|
*/ |
667
|
20 |
|
protected function setRepository($class, $repository) |
668
|
|
|
{ |
669
|
20 |
|
$repository->setModule($this); |
670
|
20 |
|
$this->_repositories[$class] = $repository; |
671
|
|
|
|
672
|
20 |
|
return $this; |
673
|
|
|
} |
674
|
|
|
|
675
|
|
|
/** |
676
|
|
|
* Get the Oauth 'access_token_id' claim. |
677
|
|
|
* @return string|null |
678
|
|
|
* @see validateAuthenticatedRequest() |
679
|
|
|
* @since 1.0.0 |
680
|
|
|
*/ |
681
|
2 |
|
public function getRequestOauthAccessTokenIdentifier() |
682
|
|
|
{ |
683
|
2 |
|
return $this->getRequestOauthClaim('oauth_access_token_id'); |
684
|
|
|
} |
685
|
|
|
|
686
|
|
|
/** |
687
|
|
|
* Get the Oauth 'client_id' claim. |
688
|
|
|
* @return string |
689
|
|
|
* @see validateAuthenticatedRequest() |
690
|
|
|
* @since 1.0.0 |
691
|
|
|
*/ |
692
|
1 |
|
public function getRequestOauthClientIdentifier() |
693
|
|
|
{ |
694
|
1 |
|
return $this->getRequestOauthClaim('oauth_client_id'); |
695
|
|
|
} |
696
|
|
|
|
697
|
|
|
/** |
698
|
|
|
* Get the Oauth 'user_id' claim. |
699
|
|
|
* @return mixed|null |
700
|
|
|
* @see validateAuthenticatedRequest() |
701
|
|
|
* @since 1.0.0 |
702
|
|
|
*/ |
703
|
1 |
|
public function getRequestOauthUserId() |
704
|
|
|
{ |
705
|
1 |
|
return $this->getRequestOauthClaim('oauth_user_id'); |
706
|
|
|
} |
707
|
|
|
|
708
|
|
|
/** |
709
|
|
|
* Get the Oauth 'scopes' claim. |
710
|
|
|
* @return string[] |
711
|
|
|
* @see validateAuthenticatedRequest() |
712
|
|
|
* @since 1.0.0 |
713
|
|
|
*/ |
714
|
2 |
|
public function getRequestOauthScopeIdentifiers() |
715
|
|
|
{ |
716
|
2 |
|
return $this->getRequestOauthClaim('oauth_scopes', []); |
717
|
|
|
} |
718
|
|
|
|
719
|
|
|
/** |
720
|
|
|
* Check if the Request has the specified scope. |
721
|
|
|
* @param string $scopeIdentifier |
722
|
|
|
* @param bool $strict If strict is `false` and the user is not authenticated via Oauth, return true. |
723
|
|
|
* @return bool |
724
|
|
|
* @see validateAuthenticatedRequest() |
725
|
|
|
* @since 1.0.0 |
726
|
|
|
*/ |
727
|
1 |
|
public function requestHasScope($scopeIdentifier, $strict = true) |
728
|
|
|
{ |
729
|
1 |
|
if (!$strict && ($this->getRequestOauthAccessTokenIdentifier() === null)) { |
730
|
|
|
//If not strict and the user is not authenticated via Oauth, allow the scope. |
731
|
1 |
|
return true; |
732
|
|
|
} |
733
|
1 |
|
return in_array($scopeIdentifier, $this->getRequestOauthScopeIdentifiers()); |
734
|
|
|
} |
735
|
|
|
|
736
|
|
|
/** |
737
|
|
|
* Get the configuration for the enabled OpenID Connect scopes. |
738
|
|
|
* @return Oauth2OidcScopeCollectionInterface|array|callable|string |
739
|
|
|
* @see getOidcScopeCollection() |
740
|
|
|
* @since 1.0.0 |
741
|
|
|
*/ |
742
|
3 |
|
public function getOpenIdConnectScopes() |
743
|
|
|
{ |
744
|
3 |
|
return $this->_openIdConnectScopes; |
745
|
|
|
} |
746
|
|
|
|
747
|
|
|
/** |
748
|
|
|
* Set the configuration for the enabled OpenID Connect scopes. |
749
|
|
|
* @return $this |
750
|
|
|
* @see getOidcScopeCollection() |
751
|
|
|
* @since 1.0.0 |
752
|
|
|
*/ |
753
|
2 |
|
public function setOpenIdConnectScopes($openIdConnectScopes) |
754
|
|
|
{ |
755
|
2 |
|
$this->_openIdConnectScopes = $openIdConnectScopes; |
756
|
2 |
|
$this->_oidcScopeCollection = null; |
757
|
2 |
|
return $this; |
758
|
|
|
} |
759
|
|
|
|
760
|
|
|
//////////////////////// |
761
|
|
|
/// Public Functions /// |
762
|
|
|
//////////////////////// |
763
|
|
|
|
764
|
|
|
/** |
765
|
|
|
* Generates a JWT 'id_token' for OpenID Connect |
766
|
|
|
* @param Oauth2OidcUserInterface $user |
767
|
|
|
* @param string $clientIdentifier |
768
|
|
|
* @param CryptKey $privateKey |
769
|
|
|
* @param string[] $scopeIdentifiers |
770
|
|
|
* @param string|null $nonce |
771
|
|
|
* @param \DateTimeImmutable|null $expiryDateTime |
772
|
|
|
* @return \Lcobucci\JWT\Token\Plain |
773
|
|
|
* @throws InvalidConfigException |
774
|
|
|
* @see getOidcScopeCollection() |
775
|
|
|
*/ |
776
|
2 |
|
public function generateOpenIdConnectUserClaimsToken( |
777
|
|
|
$user, |
778
|
|
|
$clientIdentifier, |
779
|
|
|
$privateKey, |
780
|
|
|
$scopeIdentifiers, |
781
|
|
|
$nonce = null, |
782
|
|
|
$expiryDateTime = null |
783
|
|
|
) { |
784
|
2 |
|
if (!($user instanceof Oauth2OidcUserInterface)) { |
|
|
|
|
785
|
1 |
|
throw new InvalidConfigException('In order to support OpenID Connect ' |
786
|
1 |
|
. get_class($user) . ' must implement ' . Oauth2OidcUserInterface::class); |
787
|
|
|
} |
788
|
|
|
|
789
|
1 |
|
$jwtConfiguration = Configuration::forAsymmetricSigner( |
790
|
1 |
|
new Sha256(), |
791
|
1 |
|
InMemory::plainText($privateKey->getKeyContents(), $privateKey->getPassPhrase() ?? ''), |
792
|
1 |
|
InMemory::empty(), |
|
|
|
|
793
|
1 |
|
); |
794
|
|
|
|
795
|
1 |
|
$builder = $jwtConfiguration->builder() |
796
|
1 |
|
->permittedFor($clientIdentifier) |
797
|
1 |
|
->issuedBy(Yii::$app->request->hostInfo) |
|
|
|
|
798
|
1 |
|
->issuedAt(new \DateTimeImmutable()) |
799
|
1 |
|
->relatedTo((string)$user->getIdentifier()) |
800
|
1 |
|
->withClaim( |
801
|
1 |
|
Oauth2OidcClaimInterface::OPENID_CONNECT_CLAIM_AUTH_TIME, |
802
|
1 |
|
$user->getLatestAuthenticatedAt()->getTimestamp() |
803
|
1 |
|
); |
804
|
|
|
|
805
|
1 |
|
if ($nonce) { |
806
|
1 |
|
$builder->withClaim(Oauth2OidcClaimInterface::OPENID_CONNECT_CLAIM_NONCE, $nonce); |
807
|
|
|
} |
808
|
|
|
|
809
|
1 |
|
if ($expiryDateTime) { |
810
|
1 |
|
$builder->expiresAt($expiryDateTime); |
811
|
|
|
} |
812
|
|
|
|
813
|
1 |
|
$oidcScopeCollection = $this->getOidcScopeCollection(); |
814
|
|
|
|
815
|
1 |
|
$claims = $oidcScopeCollection->getFilteredClaims($scopeIdentifiers); |
816
|
|
|
|
817
|
1 |
|
foreach ($claims as $claim) { |
818
|
|
|
if ( |
819
|
1 |
|
in_array( |
820
|
1 |
|
$claim->getIdentifier(), |
821
|
1 |
|
Oauth2OidcScopeInterface::OPENID_CONNECT_DEFAULT_SCOPE_CLAIMS[ |
822
|
1 |
|
Oauth2OidcScopeInterface::OPENID_CONNECT_SCOPE_OPENID |
823
|
1 |
|
] |
824
|
1 |
|
) |
825
|
|
|
) { |
826
|
|
|
// Skip default claims for OpenID (already set above). |
827
|
1 |
|
continue; |
828
|
|
|
} |
829
|
1 |
|
$claimValue = $user->getOpenIdConnectClaimValue($claim, $this); |
830
|
1 |
|
$builder->withClaim($claim->getIdentifier(), $claimValue); |
831
|
|
|
} |
832
|
|
|
|
833
|
1 |
|
return $builder->getToken($jwtConfiguration->signer(), $jwtConfiguration->signingKey()); |
834
|
|
|
} |
835
|
|
|
} |
836
|
|
|
|