forked from oat-sa/tao-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTokenStoreKeyValue.php
214 lines (173 loc) · 6.14 KB
/
TokenStoreKeyValue.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php
/**
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; under version 2
* of the License (non-upgradable).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2018-2023 (original work) Open Assessment Technologies SA.
*/
declare(strict_types=1);
namespace oat\tao\model\security\xsrf;
use common_exception_Error;
use common_persistence_PhpRedisDriver;
use Psr\Container\ContainerInterface;
use oat\oatbox\session\SessionService;
use oat\oatbox\service\ConfigurableService;
use common_persistence_AdvKeyValuePersistence;
use oat\generis\persistence\PersistenceManager;
/**
* Class to store tokens in a key value storage
*
* @author Martijn Swinkels <[email protected]>
*/
class TokenStoreKeyValue extends ConfigurableService implements TokenStore
{
public const OPTION_PERSISTENCE = 'persistence';
public const OPTION_TTL = 'ttl';
public const TOKENS_STORAGE_KEY = 'tao_tokens';
private common_persistence_AdvKeyValuePersistence $persistence;
private string $keyPrefix;
public function getToken(string $tokenId): ?Token
{
$tokenData = $this->getPersistence()->hGet($this->getKey(), $tokenId);
return is_string($tokenData) ? new Token(json_decode($tokenData, true)) : null;
}
public function setToken(string $tokenId, Token $token): void
{
$this->getPersistence()->hSet($this->getKey(), $tokenId, json_encode($token));
}
public function hasToken(string $tokenId): bool
{
return $this->getPersistence()->hExists($this->getKey(), $tokenId);
}
public function removeToken(string $tokenId): bool
{
return $this->getPersistence()->hDel($this->getKey(), $tokenId);
}
public function clear(): void
{
$this->getPersistence()->del($this->getKey());
}
/**
* @inheritDoc
*/
public function getAll(): array
{
$tokensData = $this->getPersistence()->hGetAll($this->getKey());
if (!is_array($tokensData)) {
return [];
}
$tokens = [];
foreach ($tokensData as $tokenData) {
if (is_string($tokenData)) {
$tokens[] = new Token(json_decode($tokenData, true));
}
}
return $tokens;
}
/**
* @throws common_exception_Error
*/
protected function getPersistence(): common_persistence_AdvKeyValuePersistence
{
if (!isset($this->persistence)) {
$persistence = $this->getPersistenceManager()->getPersistenceById(
$this->getOption(self::OPTION_PERSISTENCE)
);
if (!$persistence instanceof common_persistence_AdvKeyValuePersistence) {
throw new common_exception_Error(
'TokenStoreKeyValue expects advanced key value persistence implementation.'
);
}
$this->persistence = $persistence;
}
return $this->persistence;
}
/**
* @throws common_exception_Error
*/
protected function getKey(): string
{
if (!isset($this->keyPrefix)) {
$this->keyPrefix = sprintf(
'%s_%s',
$this->getSessionService()->getCurrentUser()->getIdentifier(),
self::TOKENS_STORAGE_KEY
);
}
return $this->keyPrefix;
}
/**
* @param int $uSleepInterval Microseconds interval between scan/keys to perform deletion
* @param int $timeLimit Expiration time for tokens, 0 means, no expiration
* @return int - The total deleted records
*/
public function clearAll(int $uSleepInterval, int $timeLimit = 0): int
{
$persistence = $this->getPersistence();
$driver = $persistence->getDriver();
$pattern = sprintf('*%s*', self::TOKENS_STORAGE_KEY);
$countDeleted = 0;
if ($driver instanceof common_persistence_PhpRedisDriver) {
$iterator = null;
while ($iterator !== 0) {
foreach ($driver->scan($iterator, $pattern) as $key) {
$countDeleted += $this->deleteAllExpiredByKey($key, $timeLimit);
}
usleep($uSleepInterval);
}
return $countDeleted;
}
$keys = $persistence->keys($pattern);
$keys = is_array($keys) ? $keys : [];
foreach ($keys as $key) {
$countDeleted += $this->deleteAllExpiredByKey($key, $timeLimit);
}
return $countDeleted;
}
private function deleteAllExpiredByKey(string $key, int $timeLimit): int
{
$persistence = $this->getPersistence();
$tokensData = $persistence->hGetAll($key);
$countDeleted = 0;
if (empty($tokensData)) {
if ($persistence->del($key)) {
return 1;
}
}
foreach ($tokensData as $tokenData) {
if (is_string($tokenData)) {
$token = new Token(json_decode($tokenData, true));
if (!$token->isExpired($timeLimit)) {
continue;
}
if ($persistence->hDel($key, $token->getValue())) {
$countDeleted++;
}
}
}
return $countDeleted;
}
private function getPersistenceManager(): PersistenceManager
{
return $this->getContainer()->get(PersistenceManager::SERVICE_ID);
}
private function getSessionService(): SessionService
{
return $this->getContainer()->get(SessionService::SERVICE_ID);
}
private function getContainer(): ContainerInterface
{
return $this->getServiceManager()->getContainer();
}
}