1 | <?php |
||
19 | class CsrfGuard |
||
20 | { |
||
21 | /** |
||
22 | * @var array Php session data from superglobal. |
||
23 | */ |
||
24 | private $session; |
||
25 | |||
26 | /** |
||
27 | * @var int Max number of tokens stored in session. |
||
28 | */ |
||
29 | private $maxStorage; |
||
30 | |||
31 | /** |
||
32 | * @var int Rapresent the lenght of the token in bytes. |
||
33 | */ |
||
34 | private $tokenStrength; |
||
35 | |||
36 | /** |
||
37 | * Constructor. |
||
38 | * |
||
39 | * @param int $maxStorage Max number of tokens stored in session, work as |
||
40 | * FIFO data structure, when maximun capacity is |
||
41 | * reached, oldest token be dequeued from storage. |
||
42 | * @param int $tokenStrength Rapresent the lenght of the token in bytes. |
||
43 | */ |
||
44 | 20 | public function __construct(int $maxStorage, int $tokenStrength) |
|
59 | |||
60 | /** |
||
61 | * Limit number of token stored in session. |
||
62 | */ |
||
63 | 18 | private function dequeue(array &$array) |
|
72 | |||
73 | /** |
||
74 | * Return csrf token as array. |
||
75 | * |
||
76 | * @return array |
||
77 | */ |
||
78 | 17 | public function getToken() : array |
|
94 | |||
95 | /** |
||
96 | * Return timed csrf token as array. |
||
97 | * |
||
98 | * @param int $ttl Time to live for the token. |
||
99 | * |
||
100 | * @return array |
||
101 | */ |
||
102 | 1 | public function getTimedToken(int $ttl) : array |
|
115 | |||
116 | /** |
||
117 | * Generate a random token. |
||
118 | * |
||
119 | * @return array |
||
120 | */ |
||
121 | 18 | private function generateToken() : array |
|
128 | |||
129 | /** |
||
130 | * Return csrf token as hidden input form. |
||
131 | * |
||
132 | * @return string |
||
133 | * |
||
134 | * @deprecated since version 1.1.0 |
||
135 | */ |
||
136 | 1 | public function getHiddenInput() : string |
|
142 | |||
143 | /** |
||
144 | * Validate a csrf token. |
||
145 | * |
||
146 | * @param array $requestData From request or from superglobal variables $_POST, |
||
147 | * $_GET, $_REQUEST and $_COOKIE. |
||
148 | * |
||
149 | * @return bool |
||
150 | */ |
||
151 | 1 | public function validate(array $requestData) : bool |
|
159 | |||
160 | /** |
||
161 | * Tests for valid token. |
||
162 | * |
||
163 | * @param string $value |
||
164 | * @param string $key |
||
165 | * |
||
166 | * @return bool |
||
167 | */ |
||
168 | 1 | private function matchToken(string $value, string $key) : bool |
|
189 | } |
||
190 |