|
1
|
|
|
<?php namespace Limoncello\Passport\Package; |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright 2015-2017 [email protected] |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
7
|
|
|
* you may not use this file except in compliance with the License. |
|
8
|
|
|
* You may obtain a copy of the License at |
|
9
|
|
|
* |
|
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
11
|
|
|
* |
|
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15
|
|
|
* See the License for the specific language governing permissions and |
|
16
|
|
|
* limitations under the License. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
use Limoncello\Contracts\Application\ApplicationConfigurationInterface as A; |
|
20
|
|
|
use Limoncello\Contracts\Settings\SettingsInterface; |
|
21
|
|
|
use Limoncello\Core\Reflection\CheckCallableTrait; |
|
22
|
|
|
use Limoncello\Passport\Contracts\Entities\TokenInterface; |
|
23
|
|
|
use Psr\Container\ContainerInterface; |
|
24
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
25
|
|
|
use ReflectionParameter; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @package Limoncello\Passport |
|
29
|
|
|
*/ |
|
30
|
|
|
class PassportSettings implements SettingsInterface |
|
31
|
|
|
{ |
|
32
|
|
|
use CheckCallableTrait; |
|
33
|
|
|
|
|
34
|
|
|
/** Config key */ |
|
35
|
|
|
const KEY_IS_LOG_ENABLED = 0; |
|
36
|
|
|
|
|
37
|
|
|
/** Config key */ |
|
38
|
|
|
const KEY_APPROVAL_URI_STRING = self::KEY_IS_LOG_ENABLED + 1; |
|
39
|
|
|
|
|
40
|
|
|
/** Config key */ |
|
41
|
|
|
const KEY_ERROR_URI_STRING = self::KEY_APPROVAL_URI_STRING + 1; |
|
42
|
|
|
|
|
43
|
|
|
/** Config key */ |
|
44
|
|
|
const KEY_DEFAULT_CLIENT_ID = self::KEY_ERROR_URI_STRING + 1; |
|
45
|
|
|
|
|
46
|
|
|
/** Config key */ |
|
47
|
|
|
const KEY_CODE_EXPIRATION_TIME_IN_SECONDS = self::KEY_DEFAULT_CLIENT_ID + 1; |
|
48
|
|
|
|
|
49
|
|
|
/** Config key */ |
|
50
|
|
|
const KEY_TOKEN_EXPIRATION_TIME_IN_SECONDS = self::KEY_CODE_EXPIRATION_TIME_IN_SECONDS + 1; |
|
51
|
|
|
|
|
52
|
|
|
/** Config key */ |
|
53
|
|
|
const KEY_RENEW_REFRESH_VALUE_ON_TOKEN_REFRESH = self::KEY_TOKEN_EXPIRATION_TIME_IN_SECONDS + 1; |
|
54
|
|
|
|
|
55
|
|
|
/** Config key */ |
|
56
|
|
|
const KEY_USER_TABLE_NAME = self::KEY_RENEW_REFRESH_VALUE_ON_TOKEN_REFRESH + 1; |
|
57
|
|
|
|
|
58
|
|
|
/** Config key */ |
|
59
|
|
|
const KEY_USER_PRIMARY_KEY_NAME = self::KEY_USER_TABLE_NAME + 1; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Config key |
|
63
|
|
|
* |
|
64
|
|
|
* Value should be a static callable for user credentials validator (login and password). |
|
65
|
|
|
* |
|
66
|
|
|
* Examples ['SomeNamespace\ClassName', 'staticMethodName'] or 'SomeNamespace\ClassName::staticMethodName' |
|
67
|
|
|
* |
|
68
|
|
|
* Method signature |
|
69
|
|
|
* |
|
70
|
|
|
* public static function validateUser(ContainerInterface $container, string $userName, string $password) |
|
71
|
|
|
* |
|
72
|
|
|
* which returns either user ID (int|string) or null if user not found/invalid credentials. |
|
73
|
|
|
*/ |
|
74
|
|
|
const KEY_USER_CREDENTIALS_VALIDATOR = self::KEY_USER_PRIMARY_KEY_NAME + 1; |
|
75
|
|
|
|
|
76
|
|
|
/** Config key */ |
|
77
|
|
|
const KEY_FAILED_CUSTOM_UNAUTHENTICATED_FACTORY = self::KEY_USER_CREDENTIALS_VALIDATOR + 1; |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Config key |
|
81
|
|
|
* |
|
82
|
|
|
* Value should be a static callable for user scope validator (allowed scope identities). |
|
83
|
|
|
* |
|
84
|
|
|
* Examples ['SomeNamespace\ClassName', 'staticMethodName'] or 'SomeNamespace\ClassName::staticMethodName' |
|
85
|
|
|
* |
|
86
|
|
|
* Method signature |
|
87
|
|
|
* |
|
88
|
|
|
* public static function validateScope(ContainerInterface $container, int $userId, array $scopeIds = null): ?array |
|
89
|
|
|
* |
|
90
|
|
|
* which returns either changed allowed scope IDs or null if scope was not changed or throws auth exception. |
|
91
|
|
|
*/ |
|
92
|
|
|
const KEY_USER_SCOPE_VALIDATOR = self::KEY_FAILED_CUSTOM_UNAUTHENTICATED_FACTORY + 1; |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Config key |
|
96
|
|
|
* |
|
97
|
|
|
* A custom properties provider for auth token. All the values returned from the provider |
|
98
|
|
|
* will be added to the token. |
|
99
|
|
|
* |
|
100
|
|
|
* Value should be a static callable. |
|
101
|
|
|
* |
|
102
|
|
|
* Examples ['SomeNamespace\ClassName', 'staticMethodName'] or 'SomeNamespace\ClassName::staticMethodName' |
|
103
|
|
|
* |
|
104
|
|
|
* Method signature |
|
105
|
|
|
* |
|
106
|
|
|
* public static function getExtraProps(ContainerInterface $container, TokenInterface $token): array |
|
107
|
|
|
*/ |
|
108
|
|
|
const KEY_TOKEN_CUSTOM_PROPERTIES_PROVIDER = self::KEY_USER_SCOPE_VALIDATOR + 1; |
|
109
|
|
|
|
|
110
|
|
|
/** Config key */ |
|
111
|
|
|
protected const KEY_LAST = self::KEY_TOKEN_CUSTOM_PROPERTIES_PROVIDER; |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @var array |
|
115
|
|
|
*/ |
|
116
|
|
|
private $appConfig; |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @inheritdoc |
|
120
|
|
|
*/ |
|
121
|
1 |
|
final public function get(array $appConfig): array |
|
122
|
|
|
{ |
|
123
|
1 |
|
$this->appConfig = $appConfig; |
|
124
|
|
|
|
|
125
|
1 |
|
$defaults = $this->getSettings(); |
|
126
|
|
|
|
|
127
|
1 |
|
$credentialsValidator = $defaults[static::KEY_USER_CREDENTIALS_VALIDATOR]; |
|
128
|
1 |
|
assert( |
|
129
|
1 |
|
$this->checkPublicStaticCallable( |
|
130
|
1 |
|
$credentialsValidator, |
|
131
|
1 |
|
[ContainerInterface::class, 'string', 'string'] |
|
132
|
|
|
), |
|
133
|
1 |
|
"Invalid credentials validator." |
|
134
|
|
|
); |
|
135
|
|
|
|
|
136
|
1 |
|
$scopeValidator = $defaults[static::KEY_USER_SCOPE_VALIDATOR] ?? null; |
|
137
|
1 |
|
assert( |
|
138
|
1 |
|
$this->checkPublicStaticCallable( |
|
139
|
1 |
|
$scopeValidator, |
|
140
|
|
|
[ |
|
141
|
1 |
|
ContainerInterface::class, |
|
142
|
1 |
|
'int', |
|
143
|
1 |
|
function (ReflectionParameter $parameter) { |
|
144
|
1 |
|
return $parameter->allowsNull() === true && $parameter->isArray() === true; |
|
145
|
1 |
|
}, |
|
146
|
|
|
] |
|
147
|
|
|
), |
|
148
|
1 |
|
"Invalid scope validator." |
|
149
|
|
|
); |
|
150
|
|
|
|
|
151
|
1 |
|
$customPropsProvider = $defaults[static::KEY_TOKEN_CUSTOM_PROPERTIES_PROVIDER] ?? null; |
|
152
|
1 |
|
assert( |
|
153
|
1 |
|
$customPropsProvider === null || |
|
154
|
1 |
|
$this->checkPublicStaticCallable( |
|
155
|
1 |
|
$customPropsProvider, |
|
156
|
1 |
|
[ContainerInterface::class, TokenInterface::class], |
|
157
|
1 |
|
'array' |
|
158
|
|
|
), |
|
159
|
1 |
|
"Invalid token custom properties provider." |
|
160
|
|
|
); |
|
161
|
|
|
|
|
162
|
1 |
|
$customUnAuthFactory = $defaults[static::KEY_FAILED_CUSTOM_UNAUTHENTICATED_FACTORY] ?? null; |
|
163
|
1 |
|
assert( |
|
164
|
1 |
|
$customUnAuthFactory === null || |
|
165
|
1 |
|
$this->checkPublicStaticCallable( |
|
166
|
1 |
|
$customUnAuthFactory, |
|
167
|
1 |
|
[], |
|
168
|
1 |
|
ResponseInterface::class |
|
169
|
|
|
), |
|
170
|
1 |
|
"Invalid custom factory." |
|
171
|
|
|
); |
|
172
|
|
|
|
|
173
|
1 |
|
$approvalUri = $defaults[static::KEY_APPROVAL_URI_STRING]; |
|
174
|
1 |
|
assert(empty($approvalUri) === false, "Invalid Approval URI `$approvalUri`."); |
|
175
|
|
|
|
|
176
|
1 |
|
$errorUri = $defaults[static::KEY_ERROR_URI_STRING]; |
|
177
|
1 |
|
assert(empty($errorUri) === false, "Invalid Error URI `$errorUri`."); |
|
178
|
|
|
|
|
179
|
1 |
|
$defaultClientId = $defaults[static::KEY_DEFAULT_CLIENT_ID]; |
|
180
|
1 |
|
assert(empty($defaultClientId) === false, "Invalid Default Client ID `$defaultClientId`."); |
|
181
|
|
|
|
|
182
|
1 |
|
$userTable = $defaults[static::KEY_USER_TABLE_NAME]; |
|
183
|
1 |
|
assert(empty($userTable) === false, "Invalid User Table Name `$userTable`."); |
|
184
|
|
|
|
|
185
|
1 |
|
$userPk = $defaults[static::KEY_USER_TABLE_NAME]; |
|
186
|
1 |
|
assert(empty($userPk) === false, "Invalid User Primary Key Name `$userPk`."); |
|
187
|
|
|
|
|
188
|
|
|
return $defaults + [ |
|
189
|
1 |
|
static::KEY_APPROVAL_URI_STRING => $approvalUri, |
|
190
|
1 |
|
static::KEY_ERROR_URI_STRING => $errorUri, |
|
191
|
1 |
|
static::KEY_DEFAULT_CLIENT_ID => $defaultClientId, |
|
192
|
1 |
|
static::KEY_USER_TABLE_NAME => $userTable, |
|
193
|
1 |
|
static::KEY_USER_PRIMARY_KEY_NAME => $userPk, |
|
194
|
1 |
|
static::KEY_USER_CREDENTIALS_VALIDATOR => $credentialsValidator, |
|
195
|
1 |
|
static::KEY_USER_SCOPE_VALIDATOR => $scopeValidator, |
|
196
|
1 |
|
static::KEY_TOKEN_CUSTOM_PROPERTIES_PROVIDER => $customPropsProvider, |
|
197
|
|
|
]; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* @return array |
|
202
|
|
|
*/ |
|
203
|
1 |
|
protected function getSettings(): array |
|
204
|
|
|
{ |
|
205
|
1 |
|
$appConfig = $this->getAppConfig(); |
|
206
|
|
|
|
|
207
|
|
|
return [ |
|
208
|
1 |
|
static::KEY_IS_LOG_ENABLED => (bool)($appConfig[A::KEY_IS_LOG_ENABLED] ?? false), |
|
209
|
1 |
|
static::KEY_CODE_EXPIRATION_TIME_IN_SECONDS => 10 * 60, |
|
210
|
1 |
|
static::KEY_TOKEN_EXPIRATION_TIME_IN_SECONDS => 60 * 60, |
|
211
|
1 |
|
static::KEY_RENEW_REFRESH_VALUE_ON_TOKEN_REFRESH => true, |
|
212
|
|
|
]; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* @return mixed |
|
217
|
|
|
*/ |
|
218
|
1 |
|
protected function getAppConfig() |
|
219
|
|
|
{ |
|
220
|
1 |
|
return $this->appConfig; |
|
221
|
|
|
} |
|
222
|
|
|
} |
|
223
|
|
|
|