|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\User\CurrentIdentity\Storage; |
|
6
|
|
|
|
|
7
|
|
|
use Yiisoft\Session\SessionInterface; |
|
8
|
|
|
|
|
9
|
|
|
final class SessionCurrentIdentityStorage implements CurrentIdentityStorageInterface |
|
10
|
|
|
{ |
|
11
|
|
|
private const SESSION_AUTH_ID = '__auth_id'; |
|
12
|
|
|
private const SESSION_AUTH_EXPIRE = '__auth_expire'; |
|
13
|
|
|
private const SESSION_AUTH_ABSOLUTE_EXPIRE = '__auth_absolute_expire'; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var int|null the number of seconds in which the user will be logged out automatically in case of |
|
17
|
|
|
* remaining inactive. If this property is not set, the user will be logged out after |
|
18
|
|
|
* the current session expires. |
|
19
|
|
|
*/ |
|
20
|
|
|
private ?int $authTimeout = null; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var int|null the number of seconds in which the user will be logged out automatically |
|
24
|
|
|
* regardless of activity. |
|
25
|
|
|
*/ |
|
26
|
|
|
private ?int $absoluteAuthTimeout = null; |
|
27
|
|
|
|
|
28
|
|
|
private SessionInterface $session; |
|
29
|
|
|
|
|
30
|
|
|
public function __construct(SessionInterface $session) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->session = $session; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function setAuthTimeout(int $timeout = null): self |
|
36
|
|
|
{ |
|
37
|
|
|
$this->authTimeout = $timeout; |
|
38
|
|
|
return $this; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function setAbsoluteAuthTimeout(int $timeout = null): self |
|
42
|
|
|
{ |
|
43
|
|
|
$this->absoluteAuthTimeout = $timeout; |
|
44
|
|
|
return $this; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function get(): ?string |
|
48
|
|
|
{ |
|
49
|
|
|
/** @var mixed $id */ |
|
50
|
|
|
$id = $this->session->get(self::SESSION_AUTH_ID); |
|
51
|
|
|
|
|
52
|
|
|
if ( |
|
53
|
|
|
$id !== null && |
|
54
|
|
|
($this->authTimeout !== null || $this->absoluteAuthTimeout !== null) |
|
55
|
|
|
) { |
|
56
|
|
|
$expire = $this->getExpire(); |
|
57
|
|
|
$expireAbsolute = $this->getExpireAbsoulte(); |
|
58
|
|
|
|
|
59
|
|
|
if ( |
|
60
|
|
|
($expire !== null && $expire < time()) || |
|
61
|
|
|
($expireAbsolute !== null && $expireAbsolute < time()) |
|
62
|
|
|
) { |
|
63
|
|
|
$this->clear(); |
|
64
|
|
|
return null; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
if ($this->authTimeout !== null) { |
|
68
|
|
|
$this->session->set(self::SESSION_AUTH_EXPIRE, time() + $this->authTimeout); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return $id === null ? null : (string)$id; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
private function getExpire(): ?int |
|
76
|
|
|
{ |
|
77
|
|
|
/** @var mixed $expire */ |
|
78
|
|
|
$expire = $this->authTimeout !== null |
|
79
|
|
|
? $this->session->get(self::SESSION_AUTH_EXPIRE) |
|
80
|
|
|
: null; |
|
81
|
|
|
return $expire !== null ? (int)$expire : null; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
private function getExpireAbsoulte(): ?int |
|
85
|
|
|
{ |
|
86
|
|
|
/** @var mixed $expire */ |
|
87
|
|
|
$expire = $this->absoluteAuthTimeout !== null |
|
88
|
|
|
? $this->session->get(self::SESSION_AUTH_ABSOLUTE_EXPIRE) |
|
89
|
|
|
: null; |
|
90
|
|
|
return $expire !== null ? (int)$expire : null; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function set(string $id): void |
|
94
|
|
|
{ |
|
95
|
|
|
$this->switchId($id); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function clear(): void |
|
99
|
|
|
{ |
|
100
|
|
|
$this->switchId(null); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
private function switchId(?string $id): void |
|
104
|
|
|
{ |
|
105
|
|
|
$this->session->regenerateID(); |
|
106
|
|
|
|
|
107
|
|
|
$this->session->remove(self::SESSION_AUTH_ID); |
|
108
|
|
|
$this->session->remove(self::SESSION_AUTH_EXPIRE); |
|
109
|
|
|
|
|
110
|
|
|
if ($id === null) { |
|
111
|
|
|
return; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
$this->session->set(self::SESSION_AUTH_ID, $id); |
|
115
|
|
|
if ($this->authTimeout !== null) { |
|
116
|
|
|
$this->session->set(self::SESSION_AUTH_EXPIRE, time() + $this->authTimeout); |
|
117
|
|
|
} |
|
118
|
|
|
if ($this->absoluteAuthTimeout !== null) { |
|
119
|
|
|
$this->session->set(self::SESSION_AUTH_ABSOLUTE_EXPIRE, time() + $this->absoluteAuthTimeout); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|