1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the O2System Framework package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
* |
8
|
|
|
* @author Steeve Andrian Salim |
9
|
|
|
* @copyright Copyright (c) Steeve Andrian Salim |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
// ------------------------------------------------------------------------ |
13
|
|
|
|
14
|
|
|
namespace O2System\Security\Authentication; |
15
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------ |
17
|
|
|
|
18
|
|
|
use O2System\Cache\Item; |
|
|
|
|
19
|
|
|
use O2System\Spl\Traits\Collectors\ConfigCollectorTrait; |
20
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class User |
24
|
|
|
* @package O2System\Security\Authentication |
25
|
|
|
*/ |
26
|
|
|
class User |
27
|
|
|
{ |
28
|
|
|
use ConfigCollectorTrait; |
29
|
|
|
|
30
|
|
|
// ------------------------------------------------------------------------ |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* User::__construct |
34
|
|
|
*/ |
35
|
|
|
public function __construct() |
36
|
|
|
{ |
37
|
|
|
$this->setConfig([ |
38
|
|
|
'password' => [ |
39
|
|
|
'algorithm' => PASSWORD_DEFAULT, |
40
|
|
|
'options' => [], |
41
|
|
|
], |
42
|
|
|
'msisdnRegex' => '/^\+[1-9]{1}[0-9]{3,14}$/', |
43
|
|
|
'maxAttempts' => 5, |
44
|
|
|
'sso' => [ |
45
|
|
|
'enable' => false, |
46
|
|
|
'server' => base_url(), |
|
|
|
|
47
|
|
|
], |
48
|
|
|
]); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
// ------------------------------------------------------------------------ |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* User::setPasswordAlgorithm |
55
|
|
|
* |
56
|
|
|
* @param $algorithm |
57
|
|
|
* |
58
|
|
|
* @return static |
59
|
|
|
*/ |
60
|
|
|
public function setPasswordAlgorithm($algorithm) |
61
|
|
|
{ |
62
|
|
|
if (in_array($algorithm, [PASSWORD_DEFAULT, PASSWORD_BCRYPT, PASSWORD_ARGON2I])) { |
63
|
|
|
$this->algorithm = $algorithm; |
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
// ------------------------------------------------------------------------ |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* User::setPasswordOptions |
73
|
|
|
* |
74
|
|
|
* @param array $options |
75
|
|
|
* |
76
|
|
|
* @return static |
77
|
|
|
*/ |
78
|
|
|
public function setPasswordOptions(array $options) |
79
|
|
|
{ |
80
|
|
|
$this->options = $options; |
|
|
|
|
81
|
|
|
|
82
|
|
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
// ------------------------------------------------------------------------ |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* User::passwordRehash |
89
|
|
|
* |
90
|
|
|
* @param string $password |
91
|
|
|
* |
92
|
|
|
* @return bool|string |
93
|
|
|
*/ |
94
|
|
|
public function passwordRehash($password) |
95
|
|
|
{ |
96
|
|
|
if (password_needs_rehash( |
97
|
|
|
$password, |
98
|
|
|
$this->config[ 'password' ][ 'algorithm' ], |
99
|
|
|
$this->config[ 'password' ][ 'options' ] |
100
|
|
|
)) { |
101
|
|
|
return $this->passwordHash($password); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $password; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
// ------------------------------------------------------------------------ |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* User::passwordHash |
111
|
|
|
* |
112
|
|
|
* @param string $password |
113
|
|
|
* |
114
|
|
|
* @return bool|string |
115
|
|
|
*/ |
116
|
|
|
public function passwordHash($password) |
117
|
|
|
{ |
118
|
|
|
return password_hash( |
119
|
|
|
$password, |
120
|
|
|
$this->config[ 'password' ][ 'algorithm' ], |
121
|
|
|
$this->config[ 'password' ][ 'options' ] |
122
|
|
|
); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
// ------------------------------------------------------------------------ |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* User::passwordVerify |
129
|
|
|
* |
130
|
|
|
* @param string $password |
131
|
|
|
* @param string $hash |
132
|
|
|
* |
133
|
|
|
* @return bool |
134
|
|
|
*/ |
135
|
|
|
public function passwordVerify($password, $hash) |
136
|
|
|
{ |
137
|
|
|
return password_verify($password, $hash); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
// ------------------------------------------------------------------------ |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* User::attempt |
144
|
|
|
*/ |
145
|
|
|
public function attempt() |
146
|
|
|
{ |
147
|
|
|
$_SESSION[ 'userAttempts' ] = $this->getAttempts() + 1; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
// ------------------------------------------------------------------------ |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* User::getAttempt |
154
|
|
|
* |
155
|
|
|
* @return int |
156
|
|
|
*/ |
157
|
|
|
public function getAttempts() |
158
|
|
|
{ |
159
|
|
|
$currentAttempts = 0; |
160
|
|
|
if (isset($_SESSION[ 'userAttempts' ])) { |
161
|
|
|
$currentAttempts = (int)$_SESSION[ 'userAttempts' ]; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return (int)$currentAttempts; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
// ------------------------------------------------------------------------ |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* User::login |
171
|
|
|
* |
172
|
|
|
* @param array $account |
173
|
|
|
*/ |
174
|
|
|
public function login(array $account) |
175
|
|
|
{ |
176
|
|
|
$_SESSION[ 'account' ] = $account; |
177
|
|
|
unset($_SESSION[ 'userAttempts' ]); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
// ------------------------------------------------------------------------ |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* User::signOn |
184
|
|
|
* |
185
|
|
|
* @param array $account |
186
|
|
|
* |
187
|
|
|
* @throws \Exception |
188
|
|
|
*/ |
189
|
|
|
public function signOn(array $account) |
190
|
|
|
{ |
191
|
|
|
$cacheItemPool = $this->getCacheItemPool(); |
192
|
|
|
$virtualUserId = md5(json_encode($account) . mt_srand() . time()); |
|
|
|
|
193
|
|
|
$cacheItemPool->save(new Item('sso-' . $virtualUserId, $account, false)); |
194
|
|
|
|
195
|
|
|
set_cookie('ssid', $virtualUserId); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
// ------------------------------------------------------------------------ |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* User::getCacheItemPool |
202
|
|
|
* |
203
|
|
|
* @return CacheItemPoolInterface |
204
|
|
|
*/ |
205
|
|
|
protected function getCacheItemPool() |
206
|
|
|
{ |
207
|
|
|
$cacheItemPool = cache()->getObject('default'); |
|
|
|
|
208
|
|
|
|
209
|
|
|
if (cache()->exists('sso')) { |
210
|
|
|
$cacheItemPool = cache()->getObject('sso'); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
return $cacheItemPool; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
// ------------------------------------------------------------------------ |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* User::loggedIn |
220
|
|
|
* |
221
|
|
|
* @return bool |
222
|
|
|
* @throws \Psr\Cache\InvalidArgumentException |
223
|
|
|
*/ |
224
|
|
|
public function loggedIn() |
225
|
|
|
{ |
226
|
|
|
if (isset($_SESSION[ 'account' ])) { |
227
|
|
|
return true; |
228
|
|
|
} elseif($this->tokenOn()) { |
229
|
|
|
return true; |
230
|
|
|
} elseif ($this->signedOn()) { |
231
|
|
|
return true; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
return false; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
// ------------------------------------------------------------------------ |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* User::tokenOn |
241
|
|
|
*/ |
242
|
|
|
public function tokenOn() |
243
|
|
|
{ |
244
|
|
|
if(false !== ($token = input()->bearerToken())) { |
|
|
|
|
245
|
|
|
$_SESSION['account'] = (new JsonWebToken())->decode($token); |
246
|
|
|
|
247
|
|
|
globals()->store('account', $_SESSION['account']); |
|
|
|
|
248
|
|
|
|
249
|
|
|
return true; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
return false; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
// ------------------------------------------------------------------------ |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* User::signedOn |
259
|
|
|
* |
260
|
|
|
* @return bool |
261
|
|
|
* @throws \Psr\Cache\InvalidArgumentException |
262
|
|
|
*/ |
263
|
|
|
public function signedOn() |
264
|
|
|
{ |
265
|
|
|
if ($virtualUserId = input()->cookie('ssid')) { |
266
|
|
|
$cacheItemPool = $this->getCacheItemPool(); |
267
|
|
|
|
268
|
|
|
if($cacheItemPool->hasItem('sso-' . $virtualUserId)) { |
269
|
|
|
|
270
|
|
|
$item = $cacheItemPool->getItem('sso-' . input()->cookie('ssid')); |
271
|
|
|
$_SESSION['account'] = $item->get(); |
272
|
|
|
|
273
|
|
|
globals()->store('account', $_SESSION['account']); |
|
|
|
|
274
|
|
|
|
275
|
|
|
return true; |
276
|
|
|
} |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
return false; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
// ------------------------------------------------------------------------ |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* User::logout |
286
|
|
|
*/ |
287
|
|
|
public function logout() |
288
|
|
|
{ |
289
|
|
|
$this->signOff(); |
290
|
|
|
|
291
|
|
|
if (isset($_SESSION[ 'account' ])) { |
292
|
|
|
unset($_SESSION[ 'account' ]); |
293
|
|
|
} |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
// ------------------------------------------------------------------------ |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* User::signOff |
300
|
|
|
* |
301
|
|
|
* @throws \Psr\Cache\InvalidArgumentException |
302
|
|
|
*/ |
303
|
|
|
public function signOff() |
304
|
|
|
{ |
305
|
|
|
if ($virtualUserId = input()->cookie('ssid')) { |
306
|
|
|
$cacheItemPool = $this->getCacheItemPool(); |
307
|
|
|
$cacheItemPool->deleteItem('sso-' . $virtualUserId); |
308
|
|
|
delete_cookie('ssid'); |
309
|
|
|
} |
310
|
|
|
} |
311
|
|
|
} |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths