1 | <?php namespace Limoncello\Application\Packages\Csrf; |
||
26 | class CsrfTokenStorage implements CsrfTokenStorageInterface |
||
27 | { |
||
28 | /** |
||
29 | * Number of random bytes in a token. |
||
30 | */ |
||
31 | protected const TOKEN_BYTE_LENGTH = 16; |
||
32 | |||
33 | /** |
||
34 | * @var ArrayAccess |
||
35 | */ |
||
36 | private $sessionStorage; |
||
37 | |||
38 | /** |
||
39 | * @var string |
||
40 | */ |
||
41 | private $tokenStorageKey; |
||
42 | |||
43 | /** |
||
44 | * @var null|int |
||
45 | */ |
||
46 | private $maxTokens = null; |
||
47 | |||
48 | /** |
||
49 | * @var int |
||
50 | */ |
||
51 | private $maxTokensGcThreshold; |
||
52 | |||
53 | /** |
||
54 | * @param ArrayAccess $sessionStorage |
||
55 | * @param string $tokenStorageKey |
||
56 | * @param int|null $maxTokens |
||
57 | * @param int $maxTokensGcThreshold |
||
58 | */ |
||
59 | 3 | public function __construct( |
|
70 | |||
71 | /** |
||
72 | * @inheritdoc |
||
73 | * |
||
74 | * @throws Exception |
||
75 | */ |
||
76 | 2 | public function create(): string |
|
99 | |||
100 | /** |
||
101 | * @inheritdoc |
||
102 | */ |
||
103 | 1 | public function check(string $token): bool |
|
115 | |||
116 | /** |
||
117 | * @return ArrayAccess |
||
118 | */ |
||
119 | 2 | protected function getSessionStorage(): ArrayAccess |
|
123 | |||
124 | /** |
||
125 | * @param ArrayAccess $sessionStorage |
||
126 | * |
||
127 | * @return self |
||
128 | */ |
||
129 | 3 | protected function setSessionStorage(ArrayAccess $sessionStorage): self |
|
135 | |||
136 | /** |
||
137 | * @return string |
||
138 | */ |
||
139 | 2 | protected function getTokenStorageKey(): string |
|
143 | |||
144 | /** |
||
145 | * @param string $tokenStorageKey |
||
146 | * |
||
147 | * @return self |
||
148 | */ |
||
149 | 3 | protected function setTokenStorageKey(string $tokenStorageKey): self |
|
157 | |||
158 | /** |
||
159 | * @return int|null |
||
160 | */ |
||
161 | 2 | protected function getMaxTokens(): ?int |
|
165 | |||
166 | /** |
||
167 | * @param int|null $maxTokens |
||
168 | * |
||
169 | * @return self |
||
170 | */ |
||
171 | 3 | protected function setMaxTokens(?int $maxTokens): self |
|
179 | |||
180 | /** |
||
181 | * @return int |
||
182 | */ |
||
183 | 2 | protected function getMaxTokensGcThreshold(): int |
|
187 | |||
188 | /** |
||
189 | * @param int $maxTokensGcThreshold |
||
190 | * |
||
191 | * @return self |
||
192 | */ |
||
193 | 3 | protected function setMaxTokensGcThreshold(int $maxTokensGcThreshold): self |
|
201 | |||
202 | /** |
||
203 | * @return string |
||
204 | * |
||
205 | * @throws Exception |
||
206 | */ |
||
207 | 2 | protected function createTokenValue(): string |
|
213 | |||
214 | /** |
||
215 | * Additional information that would be stored with a token. For example, could be creation timestamp. |
||
216 | * |
||
217 | * @return int |
||
218 | */ |
||
219 | 2 | protected function createTokenTimestamp(): int |
|
223 | |||
224 | /** |
||
225 | * @return array |
||
226 | */ |
||
227 | 2 | protected function getTokenStorage(): array |
|
237 | |||
238 | /** |
||
239 | * Replace whole token storage. |
||
240 | * |
||
241 | * @param array $tokenStorage |
||
242 | * |
||
243 | * @return self |
||
244 | */ |
||
245 | 2 | protected function setTokenStorage(array $tokenStorage): self |
|
251 | } |
||
252 |