Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
NonceGenerator.php
Go to the documentation of this file.
1<?php
3
5
7
8 private array $nonces = [];
9
10 private $onNonce;
11
12 public function __construct(?callable $onNonce) {
13 $this->onNonce = $onNonce;
14 }
15
16 protected function _generateNonce(string $name, ?int $value = null): string {
17 $bytes = \random_bytes((int) ($value ?? 32));
18 $nonce = \base64_encode($bytes);
19 if (isset($this->onNonce) && ! URequest::isAjax()) {
21 $onNonce($name, $nonce, 'nonce');
22 }
23 return $nonce;
24 }
25
33 public function getNonce(string $name, int $size = 32): string {
34 return $this->nonces[$name] ??= self::_generateNonce($name, $size);
35 }
36
42 public function hasNonce(string $name): bool {
43 return isset($this->nonces[$name]);
44 }
45
50 public function __toString() {
51 return \count($this->nonces);
52 }
53}
_generateNonce(string $name, ?int $value=null)
getNonce(string $name, int $size=32)
Returns a new or an existing nonce value.
Http Request utilities, wrapper for accessing to $_GET, $_POST and php://input.
Definition URequest.php:18