1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* sysPass |
4
|
|
|
* |
5
|
|
|
* @author nuxsmin |
6
|
|
|
* @link https://syspass.org |
7
|
|
|
* @copyright 2012-2019, Rubén Domínguez nuxsmin@$syspass.org |
8
|
|
|
* |
9
|
|
|
* This file is part of sysPass. |
10
|
|
|
* |
11
|
|
|
* sysPass is free software: you can redistribute it and/or modify |
12
|
|
|
* it under the terms of the GNU General Public License as published by |
13
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
14
|
|
|
* (at your option) any later version. |
15
|
|
|
* |
16
|
|
|
* sysPass is distributed in the hope that it will be useful, |
17
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
18
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19
|
|
|
* GNU General Public License for more details. |
20
|
|
|
* |
21
|
|
|
* You should have received a copy of the GNU General Public License |
22
|
|
|
* along with sysPass. If not, see <http://www.gnu.org/licenses/>. |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace SP\Core\Context; |
26
|
|
|
|
27
|
|
|
use SP\Core\Crypt\Vault; |
28
|
|
|
use SP\DataModel\Dto\AccountCache; |
29
|
|
|
use SP\DataModel\ProfileData; |
30
|
|
|
use SP\Services\Account\AccountSearchFilter; |
31
|
|
|
use SP\Services\User\UserLoginResponse; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Class Session |
35
|
|
|
* |
36
|
|
|
* @package SP\Core\Session |
37
|
|
|
*/ |
38
|
|
|
final class SessionContext extends ContextBase |
39
|
|
|
{ |
40
|
|
|
const MAX_SID_TIME = 120; |
41
|
|
|
|
42
|
|
|
private static $isReset = false; |
43
|
|
|
private static $isLocked = false; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return bool |
47
|
|
|
*/ |
48
|
|
|
public static function isLocked() |
49
|
|
|
{ |
50
|
|
|
return self::$isLocked; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Closes session |
55
|
|
|
*/ |
56
|
|
|
public static function close() |
57
|
|
|
{ |
58
|
|
|
if (!self::$isLocked) { |
59
|
|
|
logger('Session closed'); |
60
|
|
|
|
61
|
|
|
session_write_close(); |
62
|
|
|
|
63
|
|
|
self::$isLocked = true; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Destruir la sesión y reiniciar |
69
|
|
|
*/ |
70
|
|
|
public static function restart() |
71
|
|
|
{ |
72
|
|
|
self::$isReset = true; |
73
|
|
|
|
74
|
|
|
session_unset(); |
75
|
|
|
session_destroy(); |
76
|
|
|
session_start(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Devuelve el tema visual utilizado en sysPass |
81
|
|
|
* |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
|
|
public function getTheme() |
85
|
|
|
{ |
86
|
|
|
return $this->getContextKey('theme'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Devolver una variable de sesión |
91
|
|
|
* |
92
|
|
|
* @param string $key |
93
|
|
|
* @param mixed $default |
94
|
|
|
* |
95
|
|
|
* @return mixed |
96
|
|
|
*/ |
97
|
|
|
protected function getContextKey(string $key, $default = null) |
98
|
|
|
{ |
99
|
|
|
try { |
100
|
|
|
return parent::getContextKey($key, $default); |
101
|
|
|
} catch (ContextException $e) { |
102
|
|
|
processException($e); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $default; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Establece el tema visual utilizado en sysPass |
110
|
|
|
* |
111
|
|
|
* @param $theme string El tema visual a utilizar |
112
|
|
|
*/ |
113
|
|
|
public function setTheme($theme) |
114
|
|
|
{ |
115
|
|
|
$this->setContextKey('theme', $theme); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Establecer una variable de sesión |
120
|
|
|
* |
121
|
|
|
* @param string $key El nombre de la variable |
122
|
|
|
* @param mixed $value El valor de la variable |
123
|
|
|
* |
124
|
|
|
* @return mixed |
125
|
|
|
*/ |
126
|
|
|
protected function setContextKey(string $key, $value) |
127
|
|
|
{ |
128
|
|
|
try { |
129
|
|
|
if (self::$isLocked) { |
130
|
|
|
logger('Session locked; key=' . $key); |
131
|
|
|
} else { |
132
|
|
|
parent::setContextKey($key, $value); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return $value; |
136
|
|
|
} catch (ContextException $e) { |
137
|
|
|
processException($e); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return null; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Establecer la hora de carga de la configuración |
145
|
|
|
* |
146
|
|
|
* @param int $time |
147
|
|
|
*/ |
148
|
|
|
public function setConfigTime($time) |
149
|
|
|
{ |
150
|
|
|
$this->setContextKey('configTime', (int)$time); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Devolver la hora de carga de la configuración |
155
|
|
|
* |
156
|
|
|
* @return int |
157
|
|
|
*/ |
158
|
|
|
public function getConfigTime() |
159
|
|
|
{ |
160
|
|
|
return $this->getContextKey('configTime'); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Establece los datos del usuario en la sesión. |
165
|
|
|
* |
166
|
|
|
* @param UserLoginResponse $userLoginResponse |
167
|
|
|
*/ |
168
|
|
|
public function setUserData(UserLoginResponse $userLoginResponse = null) |
169
|
|
|
{ |
170
|
|
|
$this->setContextKey('userData', $userLoginResponse); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Obtiene el objeto de perfil de usuario de la sesión. |
175
|
|
|
* |
176
|
|
|
* @return ProfileData |
177
|
|
|
*/ |
178
|
|
|
public function getUserProfile() |
179
|
|
|
{ |
180
|
|
|
return $this->getContextKey('userProfile'); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Establece el objeto de perfil de usuario en la sesión. |
185
|
|
|
* |
186
|
|
|
* @param ProfileData $ProfileData |
187
|
|
|
*/ |
188
|
|
|
public function setUserProfile(ProfileData $ProfileData) |
189
|
|
|
{ |
190
|
|
|
$this->setContextKey('userProfile', $ProfileData); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @return AccountSearchFilter |
195
|
|
|
*/ |
196
|
|
|
public function getSearchFilters() |
197
|
|
|
{ |
198
|
|
|
return $this->getContextKey('searchFilters', null); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @param AccountSearchFilter $searchFilters |
203
|
|
|
*/ |
204
|
|
|
public function setSearchFilters(AccountSearchFilter $searchFilters) |
205
|
|
|
{ |
206
|
|
|
$this->setContextKey('searchFilters', $searchFilters); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
public function resetAccountAcl() |
210
|
|
|
{ |
211
|
|
|
$this->setContextKey('accountAcl', null); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Returns if user is logged in |
216
|
|
|
* |
217
|
|
|
* @return bool |
218
|
|
|
*/ |
219
|
|
|
public function isLoggedIn() |
220
|
|
|
{ |
221
|
|
|
return self::$isReset === false && $this->getUserData()->getLogin() |
222
|
|
|
&& is_object($this->getUserData()->getPreferences()); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Devuelve los datos del usuario en la sesión. |
227
|
|
|
* |
228
|
|
|
* @return UserLoginResponse |
229
|
|
|
*/ |
230
|
|
|
public function getUserData() |
231
|
|
|
{ |
232
|
|
|
return $this->getContextKey('userData', new UserLoginResponse()); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Establecer si el usuario está completamente autorizado |
237
|
|
|
* |
238
|
|
|
* @param $bool |
239
|
|
|
*/ |
240
|
|
|
public function setAuthCompleted($bool) |
241
|
|
|
{ |
242
|
|
|
$this->setContextKey('authCompleted', (bool)$bool); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Devolver si el usuario está completamente logeado |
247
|
|
|
*/ |
248
|
|
|
public function getAuthCompleted() |
249
|
|
|
{ |
250
|
|
|
return $this->getContextKey('authCompleted', false); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Devolver la clave maestra temporal |
255
|
|
|
* |
256
|
|
|
* @return string |
257
|
|
|
*/ |
258
|
|
|
public function getTemporaryMasterPass() |
259
|
|
|
{ |
260
|
|
|
return $this->getContextKey('tempmasterpass'); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Sets a temporary master password |
265
|
|
|
* |
266
|
|
|
* @param string $password |
267
|
|
|
*/ |
268
|
|
|
public function setTemporaryMasterPass(string $password) |
269
|
|
|
{ |
270
|
|
|
$this->setContextKey('tempmasterpass', $password); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* @return mixed |
275
|
|
|
*/ |
276
|
|
|
public function getSecurityKey() |
277
|
|
|
{ |
278
|
|
|
return $this->getContextKey('sk'); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* @param string $salt |
283
|
|
|
* |
284
|
|
|
* @return string |
285
|
|
|
*/ |
286
|
|
|
public function generateSecurityKey(string $salt) |
287
|
|
|
{ |
288
|
|
|
return $this->setSecurityKey(sha1(time() . $salt)); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* @param $sk |
293
|
|
|
* |
294
|
|
|
* @return mixed |
295
|
|
|
*/ |
296
|
|
|
public function setSecurityKey($sk) |
297
|
|
|
{ |
298
|
|
|
return $this->setContextKey('sk', $sk); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* Devolver la clave pública |
303
|
|
|
* |
304
|
|
|
* @return mixed |
305
|
|
|
*/ |
306
|
|
|
public function getPublicKey() |
307
|
|
|
{ |
308
|
|
|
return $this->getContextKey('pubkey'); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* Establecer la clave pública |
313
|
|
|
* |
314
|
|
|
* @param $key |
315
|
|
|
*/ |
316
|
|
|
public function setPublicKey($key) |
317
|
|
|
{ |
318
|
|
|
$this->setContextKey('pubkey', $key); |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* Devuelve el timeout de la sesión |
323
|
|
|
* |
324
|
|
|
* @return int|null El valor en segundos |
325
|
|
|
*/ |
326
|
|
|
public function getSessionTimeout() |
327
|
|
|
{ |
328
|
|
|
return $this->getContextKey('sessionTimeout'); |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* Establecer el timeout de la sesión |
333
|
|
|
* |
334
|
|
|
* @param int $timeout El valor en segundos |
335
|
|
|
* |
336
|
|
|
* @return int |
337
|
|
|
*/ |
338
|
|
|
public function setSessionTimeout($timeout) |
339
|
|
|
{ |
340
|
|
|
$this->setContextKey('sessionTimeout', $timeout); |
341
|
|
|
|
342
|
|
|
return $timeout; |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
/** |
346
|
|
|
* Devuelve la hora de la última actividad |
347
|
|
|
* |
348
|
|
|
* @return int |
349
|
|
|
*/ |
350
|
|
|
public function getLastActivity() |
351
|
|
|
{ |
352
|
|
|
return $this->getContextKey('lastActivity', 0); |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* Establece la hora de la última actividad |
357
|
|
|
* |
358
|
|
|
* @param $time int La marca de hora |
359
|
|
|
*/ |
360
|
|
|
public function setLastActivity($time) |
361
|
|
|
{ |
362
|
|
|
$this->setContextKey('lastActivity', $time); |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
/** |
366
|
|
|
* Devuelve la hora de inicio de actividad. |
367
|
|
|
* |
368
|
|
|
* @return int |
369
|
|
|
*/ |
370
|
|
|
public function getStartActivity() |
371
|
|
|
{ |
372
|
|
|
return $this->getContextKey('startActivity', 0); |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
/** |
376
|
|
|
* Establecer el lenguaje de la sesión |
377
|
|
|
* |
378
|
|
|
* @param $locale |
379
|
|
|
*/ |
380
|
|
|
public function setLocale($locale) |
381
|
|
|
{ |
382
|
|
|
$this->setContextKey('locale', $locale); |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
/** |
386
|
|
|
* Devuelve el lenguaje de la sesión |
387
|
|
|
* |
388
|
|
|
* @return string |
389
|
|
|
*/ |
390
|
|
|
public function getLocale() |
391
|
|
|
{ |
392
|
|
|
return $this->getContextKey('locale'); |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
/** |
396
|
|
|
* Devolver el color asociado a una cuenta |
397
|
|
|
* |
398
|
|
|
* @return string |
399
|
|
|
*/ |
400
|
|
|
public function getAccountColor() |
401
|
|
|
{ |
402
|
|
|
return $this->getContextKey('accountcolor'); |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
/** |
406
|
|
|
* Establece el color asociado a una cuenta |
407
|
|
|
* |
408
|
|
|
* @param array $color |
409
|
|
|
*/ |
410
|
|
|
public function setAccountColor(array $color) |
411
|
|
|
{ |
412
|
|
|
$this->setContextKey('accountcolor', $color); |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
/** |
416
|
|
|
* Devuelve el estado de la aplicación |
417
|
|
|
* |
418
|
|
|
* @return bool |
419
|
|
|
*/ |
420
|
|
|
public function getAppStatus() |
421
|
|
|
{ |
422
|
|
|
return $this->getContextKey('status'); |
423
|
|
|
} |
424
|
|
|
|
425
|
|
|
/** |
426
|
|
|
* Establecer el estado de la aplicación |
427
|
|
|
* |
428
|
|
|
* @param string $status |
429
|
|
|
*/ |
430
|
|
|
public function setAppStatus($status) |
431
|
|
|
{ |
432
|
|
|
$this->setContextKey('status', $status); |
433
|
|
|
} |
434
|
|
|
|
435
|
|
|
/** |
436
|
|
|
* Reset del estado de la aplicación |
437
|
|
|
* |
438
|
|
|
* @return bool |
439
|
|
|
*/ |
440
|
|
|
public function resetAppStatus() |
441
|
|
|
{ |
442
|
|
|
return $this->setContextKey('status', null); |
|
|
|
|
443
|
|
|
} |
444
|
|
|
|
445
|
|
|
/** |
446
|
|
|
* Devuelve la clave maestra encriptada |
447
|
|
|
* |
448
|
|
|
* @return Vault |
449
|
|
|
*/ |
450
|
|
|
public function getVault() |
451
|
|
|
{ |
452
|
|
|
return $this->getContextKey('vault'); |
453
|
|
|
} |
454
|
|
|
|
455
|
|
|
/** |
456
|
|
|
* Establecer la clave maestra encriptada |
457
|
|
|
* |
458
|
|
|
* @param Vault $vault |
459
|
|
|
*/ |
460
|
|
|
public function setVault(Vault $vault) |
461
|
|
|
{ |
462
|
|
|
$this->setContextKey('vault', $vault); |
463
|
|
|
} |
464
|
|
|
|
465
|
|
|
/** |
466
|
|
|
* Establece la cache de cuentas |
467
|
|
|
* |
468
|
|
|
* @param array $accountsCache |
469
|
|
|
*/ |
470
|
|
|
public function setAccountsCache(array $accountsCache) |
471
|
|
|
{ |
472
|
|
|
$this->setContextKey('accountsCache', $accountsCache); |
473
|
|
|
} |
474
|
|
|
|
475
|
|
|
/** |
476
|
|
|
* Devuelve la cache de cuentas |
477
|
|
|
* |
478
|
|
|
* @return AccountCache[] |
479
|
|
|
*/ |
480
|
|
|
public function getAccountsCache() |
481
|
|
|
{ |
482
|
|
|
return $this->getContextKey('accountsCache'); |
483
|
|
|
} |
484
|
|
|
|
485
|
|
|
/** |
486
|
|
|
* @throws ContextException |
487
|
|
|
*/ |
488
|
|
|
public function initialize() |
489
|
|
|
{ |
490
|
|
|
// Si la sesión no puede ser iniciada, devolver un error 500 |
491
|
|
|
if (session_start() === false) { |
492
|
|
|
throw new ContextException(__u('Session cannot be initialized')); |
493
|
|
|
} |
494
|
|
|
|
495
|
|
|
$this->setContextReference($_SESSION); |
496
|
|
|
|
497
|
|
|
if ($this->getSidStartTime() === 0) { |
498
|
|
|
$this->setSidStartTime(time()); |
499
|
|
|
$this->setStartActivity(time()); |
500
|
|
|
} |
501
|
|
|
} |
502
|
|
|
|
503
|
|
|
/** |
504
|
|
|
* Devuelve la hora en la que el SID de sesión fue creado |
505
|
|
|
* |
506
|
|
|
* @return int |
507
|
|
|
*/ |
508
|
|
|
public function getSidStartTime() |
509
|
|
|
{ |
510
|
|
|
return $this->getContextKey('sidStartTime', 0); |
511
|
|
|
} |
512
|
|
|
|
513
|
|
|
/** |
514
|
|
|
* Establece la hora de creación del SID |
515
|
|
|
* |
516
|
|
|
* @param $time int La marca de hora |
517
|
|
|
* |
518
|
|
|
* @return int |
519
|
|
|
*/ |
520
|
|
|
public function setSidStartTime($time) |
521
|
|
|
{ |
522
|
|
|
$this->setContextKey('sidStartTime', $time); |
523
|
|
|
|
524
|
|
|
return $time; |
525
|
|
|
} |
526
|
|
|
|
527
|
|
|
/** |
528
|
|
|
* Establece la hora de inicio de actividad |
529
|
|
|
* |
530
|
|
|
* @param $time int La marca de hora |
531
|
|
|
* |
532
|
|
|
* @return int |
533
|
|
|
*/ |
534
|
|
|
public function setStartActivity($time) |
535
|
|
|
{ |
536
|
|
|
$this->setContextKey('startActivity', $time); |
537
|
|
|
|
538
|
|
|
return $time; |
539
|
|
|
} |
540
|
|
|
|
541
|
|
|
/** |
542
|
|
|
* @param string $ctxKeyName |
543
|
|
|
* @param string $key |
544
|
|
|
* @param mixed $value |
545
|
|
|
* |
546
|
|
|
* @return mixed |
547
|
|
|
*/ |
548
|
|
|
public function setPluginKey(string $ctxKeyName, string $key, $value) |
549
|
|
|
{ |
550
|
|
|
/** @var ContextCollection $ctxKey */ |
551
|
|
|
$ctxKey = $this->getContextKey($ctxKeyName, new ContextCollection()); |
552
|
|
|
|
553
|
|
|
$this->setContextKey($ctxKeyName, $ctxKey->set($key, $value)); |
554
|
|
|
|
555
|
|
|
return $value; |
556
|
|
|
} |
557
|
|
|
|
558
|
|
|
/** |
559
|
|
|
* @param string $ctxKeyName |
560
|
|
|
* @param string $key |
561
|
|
|
* |
562
|
|
|
* @return mixed |
563
|
|
|
*/ |
564
|
|
|
public function getPluginKey(string $ctxKeyName, string $key) |
565
|
|
|
{ |
566
|
|
|
/** @var ContextCollection $ctxKey */ |
567
|
|
|
$ctxKey = $this->getContextKey($ctxKeyName); |
568
|
|
|
|
569
|
|
|
if ($ctxKey !== null) { |
570
|
|
|
return $ctxKey->get($key); |
571
|
|
|
} |
572
|
|
|
|
573
|
|
|
return null; |
574
|
|
|
} |
575
|
|
|
} |
576
|
|
|
|
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.