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\Settings\SettingsInterface; |
20
|
|
|
use Limoncello\Core\Reflection\CheckCallableTrait; |
21
|
|
|
use Limoncello\Passport\Contracts\Entities\TokenInterface; |
22
|
|
|
use Psr\Container\ContainerInterface; |
23
|
|
|
use Psr\Http\Message\ResponseInterface; |
24
|
|
|
use ReflectionParameter; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @package Limoncello\Passport |
28
|
|
|
*/ |
29
|
|
|
class PassportSettings implements SettingsInterface |
30
|
|
|
{ |
31
|
|
|
use CheckCallableTrait; |
32
|
|
|
|
33
|
|
|
/** Config key */ |
34
|
|
|
const KEY_IS_LOG_ENABLED = 0; |
35
|
|
|
|
36
|
|
|
/** Config key */ |
37
|
|
|
const KEY_APPROVAL_URI_STRING = self::KEY_IS_LOG_ENABLED + 1; |
38
|
|
|
|
39
|
|
|
/** Config key */ |
40
|
|
|
const KEY_ERROR_URI_STRING = self::KEY_APPROVAL_URI_STRING + 1; |
41
|
|
|
|
42
|
|
|
/** Config key */ |
43
|
|
|
const KEY_DEFAULT_CLIENT_ID = self::KEY_ERROR_URI_STRING + 1; |
44
|
|
|
|
45
|
|
|
/** Config key */ |
46
|
|
|
const KEY_CODE_EXPIRATION_TIME_IN_SECONDS = self::KEY_DEFAULT_CLIENT_ID + 1; |
47
|
|
|
|
48
|
|
|
/** Config key */ |
49
|
|
|
const KEY_TOKEN_EXPIRATION_TIME_IN_SECONDS = self::KEY_CODE_EXPIRATION_TIME_IN_SECONDS + 1; |
50
|
|
|
|
51
|
|
|
/** Config key */ |
52
|
|
|
const KEY_RENEW_REFRESH_VALUE_ON_TOKEN_REFRESH = self::KEY_TOKEN_EXPIRATION_TIME_IN_SECONDS + 1; |
53
|
|
|
|
54
|
|
|
/** Config key */ |
55
|
|
|
const KEY_USER_TABLE_NAME = self::KEY_RENEW_REFRESH_VALUE_ON_TOKEN_REFRESH + 1; |
56
|
|
|
|
57
|
|
|
/** Config key */ |
58
|
|
|
const KEY_USER_PRIMARY_KEY_NAME = self::KEY_USER_TABLE_NAME + 1; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Config key |
62
|
|
|
* |
63
|
|
|
* Value should be a static callable for user credentials validator (login and password). |
64
|
|
|
* |
65
|
|
|
* Examples ['SomeNamespace\ClassName', 'staticMethodName'] or 'SomeNamespace\ClassName::staticMethodName' |
66
|
|
|
* |
67
|
|
|
* Method signature |
68
|
|
|
* |
69
|
|
|
* public static function validateUser(ContainerInterface $container, string $userName, string $password) |
70
|
|
|
* |
71
|
|
|
* which returns either user ID (int|string) or null if user not found/invalid credentials. |
72
|
|
|
*/ |
73
|
|
|
const KEY_USER_CREDENTIALS_VALIDATOR = self::KEY_USER_PRIMARY_KEY_NAME + 1; |
74
|
|
|
|
75
|
|
|
/** Config key */ |
76
|
|
|
const KEY_FAILED_CUSTOM_UNAUTHENTICATED_FACTORY = self::KEY_USER_CREDENTIALS_VALIDATOR + 1; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Config key |
80
|
|
|
* |
81
|
|
|
* Value should be a static callable for user scope validator (allowed scope identities). |
82
|
|
|
* |
83
|
|
|
* Examples ['SomeNamespace\ClassName', 'staticMethodName'] or 'SomeNamespace\ClassName::staticMethodName' |
84
|
|
|
* |
85
|
|
|
* Method signature |
86
|
|
|
* |
87
|
|
|
* public static function validateScope(ContainerInterface $container, int $userId, array $scopeIds = null): ?array |
88
|
|
|
* |
89
|
|
|
* which returns either changed allowed scope IDs or null if scope was not changed or throws auth exception. |
90
|
|
|
*/ |
91
|
|
|
const KEY_USER_SCOPE_VALIDATOR = self::KEY_FAILED_CUSTOM_UNAUTHENTICATED_FACTORY + 1; |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Config key |
95
|
|
|
* |
96
|
|
|
* A custom properties provider for auth token. All the values returned from the provider |
97
|
|
|
* will be added to the token. |
98
|
|
|
* |
99
|
|
|
* Value should be a static callable. |
100
|
|
|
* |
101
|
|
|
* Examples ['SomeNamespace\ClassName', 'staticMethodName'] or 'SomeNamespace\ClassName::staticMethodName' |
102
|
|
|
* |
103
|
|
|
* Method signature |
104
|
|
|
* |
105
|
|
|
* public static function getExtraProps(ContainerInterface $container, TokenInterface $token): array |
106
|
|
|
*/ |
107
|
|
|
const KEY_TOKEN_CUSTOM_PROPERTIES_PROVIDER = self::KEY_USER_SCOPE_VALIDATOR + 1; |
108
|
|
|
|
109
|
|
|
/** Config key */ |
110
|
|
|
const KEY_LAST = self::KEY_TOKEN_CUSTOM_PROPERTIES_PROVIDER; |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @inheritdoc |
114
|
|
|
*/ |
115
|
1 |
|
final public function get(): array |
116
|
|
|
{ |
117
|
1 |
|
$defaults = $this->getSettings(); |
118
|
|
|
|
119
|
1 |
|
$credentialsValidator = $defaults[static::KEY_USER_CREDENTIALS_VALIDATOR]; |
120
|
1 |
|
assert( |
121
|
1 |
|
$this->checkPublicStaticCallable( |
122
|
1 |
|
$credentialsValidator, |
123
|
1 |
|
[ContainerInterface::class, 'string', 'string'] |
124
|
|
|
), |
125
|
1 |
|
"Invalid credentials validator." |
126
|
|
|
); |
127
|
|
|
|
128
|
1 |
|
$scopeValidator = $defaults[static::KEY_USER_SCOPE_VALIDATOR] ?? null; |
129
|
1 |
|
assert( |
130
|
1 |
|
$this->checkPublicStaticCallable( |
131
|
1 |
|
$scopeValidator, |
132
|
|
|
[ |
133
|
1 |
|
ContainerInterface::class, |
134
|
1 |
|
'int', |
135
|
1 |
|
function (ReflectionParameter $parameter) { |
136
|
1 |
|
return $parameter->allowsNull() === true && $parameter->isArray() === true; |
137
|
1 |
|
}, |
138
|
|
|
] |
139
|
|
|
), |
140
|
1 |
|
"Invalid scope validator." |
141
|
|
|
); |
142
|
|
|
|
143
|
1 |
|
$customPropsProvider = $defaults[static::KEY_TOKEN_CUSTOM_PROPERTIES_PROVIDER] ?? null; |
144
|
1 |
|
assert( |
145
|
1 |
|
$customPropsProvider === null || |
146
|
1 |
|
$this->checkPublicStaticCallable( |
147
|
1 |
|
$customPropsProvider, |
148
|
1 |
|
[ContainerInterface::class, TokenInterface::class], |
149
|
1 |
|
'array' |
150
|
|
|
), |
151
|
1 |
|
"Invalid token custom properties provider." |
152
|
|
|
); |
153
|
|
|
|
154
|
1 |
|
$customUnAuthFactory = $defaults[static::KEY_FAILED_CUSTOM_UNAUTHENTICATED_FACTORY] ?? null; |
155
|
1 |
|
assert( |
156
|
1 |
|
$customUnAuthFactory === null || |
157
|
1 |
|
$this->checkPublicStaticCallable( |
158
|
1 |
|
$customUnAuthFactory, |
159
|
1 |
|
[], |
160
|
1 |
|
ResponseInterface::class |
161
|
|
|
), |
162
|
1 |
|
"Invalid custom factory." |
163
|
|
|
); |
164
|
|
|
|
165
|
1 |
|
$approvalUri = $defaults[static::KEY_APPROVAL_URI_STRING]; |
166
|
1 |
|
assert(empty($approvalUri) === false, "Invalid Approval URI `$approvalUri`."); |
167
|
|
|
|
168
|
1 |
|
$errorUri = $defaults[static::KEY_ERROR_URI_STRING]; |
169
|
1 |
|
assert(empty($errorUri) === false, "Invalid Error URI `$errorUri`."); |
170
|
|
|
|
171
|
1 |
|
$defaultClientId = $defaults[static::KEY_DEFAULT_CLIENT_ID]; |
172
|
1 |
|
assert(empty($defaultClientId) === false, "Invalid Default Client ID `$defaultClientId`."); |
173
|
|
|
|
174
|
1 |
|
$userTable = $defaults[static::KEY_USER_TABLE_NAME]; |
175
|
1 |
|
assert(empty($userTable) === false, "Invalid User Table Name `$userTable`."); |
176
|
|
|
|
177
|
1 |
|
$userPk = $defaults[static::KEY_USER_TABLE_NAME]; |
178
|
1 |
|
assert(empty($userPk) === false, "Invalid User Primary Key Name `$userPk`."); |
179
|
|
|
|
180
|
|
|
return $defaults + [ |
181
|
1 |
|
static::KEY_APPROVAL_URI_STRING => $approvalUri, |
182
|
1 |
|
static::KEY_ERROR_URI_STRING => $errorUri, |
183
|
1 |
|
static::KEY_DEFAULT_CLIENT_ID => $defaultClientId, |
184
|
1 |
|
static::KEY_USER_TABLE_NAME => $userTable, |
185
|
1 |
|
static::KEY_USER_PRIMARY_KEY_NAME => $userPk, |
186
|
1 |
|
static::KEY_USER_CREDENTIALS_VALIDATOR => $credentialsValidator, |
187
|
1 |
|
static::KEY_USER_SCOPE_VALIDATOR => $scopeValidator, |
188
|
1 |
|
static::KEY_TOKEN_CUSTOM_PROPERTIES_PROVIDER => $customPropsProvider, |
189
|
|
|
]; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @return array |
194
|
|
|
*/ |
195
|
1 |
|
protected function getSettings(): array |
196
|
|
|
{ |
197
|
|
|
return [ |
198
|
1 |
|
static::KEY_IS_LOG_ENABLED => false, |
199
|
1 |
|
static::KEY_CODE_EXPIRATION_TIME_IN_SECONDS => 10 * 60, |
200
|
1 |
|
static::KEY_TOKEN_EXPIRATION_TIME_IN_SECONDS => 60 * 60, |
201
|
1 |
|
static::KEY_RENEW_REFRESH_VALUE_ON_TOKEN_REFRESH => true, |
202
|
|
|
]; |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|