1 | <?php |
||
17 | class Csrf |
||
18 | { |
||
19 | use Utils\FormTrait; |
||
20 | |||
21 | const KEY = 'CSRF'; |
||
22 | const KEY_GENERATOR = 'CSRF_GENERATOR'; |
||
23 | |||
24 | /** |
||
25 | * @var int Max number of CSRF tokens |
||
26 | */ |
||
27 | private $maxTokens = 100; |
||
28 | |||
29 | /** |
||
30 | * @var string field name with the CSRF index |
||
31 | */ |
||
32 | private $formIndex = '_CSRF_INDEX'; |
||
33 | |||
34 | /** |
||
35 | * @var string field name with the CSRF token |
||
36 | */ |
||
37 | private $formToken = '_CSRF_TOKEN'; |
||
38 | |||
39 | /** |
||
40 | * Returns a callable to generate the inputs. |
||
41 | * |
||
42 | * @param ServerRequestInterface $request |
||
43 | * |
||
44 | * @return callable|null |
||
45 | */ |
||
46 | public static function getGenerator(ServerRequestInterface $request) |
||
50 | |||
51 | /** |
||
52 | * Execute the middleware. |
||
53 | * |
||
54 | * @param ServerRequestInterface $request |
||
55 | * @param ResponseInterface $response |
||
56 | * @param callable $next |
||
57 | * |
||
58 | * @return ResponseInterface |
||
59 | */ |
||
60 | public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) |
||
103 | |||
104 | /** |
||
105 | * Generate and retrieve the tokens. |
||
106 | * |
||
107 | * @param ServerRequestInterface $request |
||
108 | * @param string $lockTo |
||
109 | * @param array $tokens |
||
110 | * |
||
111 | * @return string |
||
112 | */ |
||
113 | private function generateTokens(ServerRequestInterface $request, $lockTo, array &$tokens) |
||
132 | |||
133 | /** |
||
134 | * Validate the request. |
||
135 | * |
||
136 | * @param ServerRequestInterface $request |
||
137 | * @param array &$tokens |
||
138 | * |
||
139 | * @return bool |
||
140 | */ |
||
141 | private function validateRequest(ServerRequestInterface $request, array &$tokens) |
||
169 | |||
170 | /** |
||
171 | * Enforce an upper limit on the number of tokens stored in session state |
||
172 | * by removing the oldest tokens first. |
||
173 | * |
||
174 | * @param array &$tokens |
||
175 | */ |
||
176 | private function recycleTokens(array &$tokens) |
||
190 | |||
191 | /** |
||
192 | * Encode string with base64, but strip padding. |
||
193 | * PHP base64_decode does not croak on that. |
||
194 | * |
||
195 | * @param string $value |
||
196 | * |
||
197 | * @return string |
||
198 | */ |
||
199 | private static function encode($value) |
||
203 | } |
||
204 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.