Ubiquity
2.5.2
php rapid development framework
Loading...
Searching...
No Matches
NonceGenerator.php
Go to the documentation of this file.
1
<?php
2
namespace
Ubiquity\security\csp
;
3
4
use
Ubiquity\utils\http\URequest
;
5
6
class
NonceGenerator
{
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()) {
20
$onNonce
=
$this->onNonce
;
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
}
Ubiquity\security\csp\NonceGenerator
Definition
NonceGenerator.php:6
Ubiquity\security\csp\NonceGenerator\$nonces
array $nonces
Definition
NonceGenerator.php:8
Ubiquity\security\csp\NonceGenerator\_generateNonce
_generateNonce(string $name, ?int $value=null)
Definition
NonceGenerator.php:16
Ubiquity\security\csp\NonceGenerator\hasNonce
hasNonce(string $name)
Definition
NonceGenerator.php:42
Ubiquity\security\csp\NonceGenerator\__toString
__toString()
Definition
NonceGenerator.php:50
Ubiquity\security\csp\NonceGenerator\__construct
__construct(?callable $onNonce)
Definition
NonceGenerator.php:12
Ubiquity\security\csp\NonceGenerator\getNonce
getNonce(string $name, int $size=32)
Returns a new or an existing nonce value.
Definition
NonceGenerator.php:33
Ubiquity\security\csp\NonceGenerator\$onNonce
$onNonce
Definition
NonceGenerator.php:10
Ubiquity\utils\http\URequest
Http Request utilities, wrapper for accessing to $_GET, $_POST and php://input.
Definition
URequest.php:18
Ubiquity\security\csp
Definition
ContentSecurity.php:2
C:
Users
myadd
Documents
GitHub
ubiquity-doc-api
vendor
phpmv
ubiquity-security
src
Ubiquity
security
csp
NonceGenerator.php
Generated on Sun Dec 10 2023 15:04:10 for
Ubiquity
by
doxygen
1.9.8