1 | <?php |
||
17 | class CsrfGuard |
||
18 | { |
||
19 | /** |
||
20 | * @var Session The session class. |
||
21 | */ |
||
22 | private $session; |
||
23 | |||
24 | /** |
||
25 | * @var int Max number of tokens stored in session. |
||
26 | */ |
||
27 | private $maxStorage; |
||
28 | |||
29 | /** |
||
30 | * @var int Rapresent the lenght of the token in bytes. |
||
31 | */ |
||
32 | private $tokenStrength; |
||
33 | |||
34 | /** |
||
35 | * Constructor. |
||
36 | * |
||
37 | * @param int $maxStorage Max number of tokens stored in session, work as |
||
38 | * FIFO data structure, when maximun capacity is |
||
39 | * reached, oldest token be dequeued from storage. |
||
40 | * @param int $tokenStrength Rapresent the lenght of the token in bytes. |
||
41 | */ |
||
42 | 18 | public function __construct(int $maxStorage, int $tokenStrength) |
|
53 | |||
54 | /** |
||
55 | * Limit number of token stored in session. |
||
56 | */ |
||
57 | 17 | private function dequeue(array &$array) |
|
66 | |||
67 | /** |
||
68 | * Return csrf token as array. |
||
69 | * |
||
70 | * @return array |
||
71 | */ |
||
72 | 17 | public function getToken() : array |
|
87 | |||
88 | /** |
||
89 | * Return csrf token as hidden input form. |
||
90 | * |
||
91 | * @return string |
||
92 | */ |
||
93 | 1 | public function getHiddenInput() : string |
|
99 | |||
100 | /** |
||
101 | * Validate a csrf token. |
||
102 | * |
||
103 | * @param array $requestData From request or from superglobal variables $_POST, |
||
104 | * $_GET, $_REQUEST and $_COOKIE. |
||
105 | * |
||
106 | * @return bool |
||
107 | */ |
||
108 | 1 | public function validate(array $requestData) : bool |
|
120 | } |
||
121 |