|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace MadWizard\WebAuthn\Policy; |
|
4
|
|
|
|
|
5
|
|
|
use MadWizard\WebAuthn\Crypto\CoseAlgorithm; |
|
6
|
|
|
use MadWizard\WebAuthn\Exception\ConfigurationException; |
|
7
|
|
|
|
|
8
|
|
|
final class Policy implements PolicyInterface |
|
9
|
|
|
{ |
|
10
|
|
|
public const DEFAULT_CHALLENGE_LENGTH = 64; |
|
11
|
|
|
|
|
12
|
|
|
private const MIN_CHALLENGE_LENGTH = 32; |
|
13
|
|
|
|
|
14
|
|
|
public const SUPPORTED_ALGORITHMS = [ |
|
15
|
|
|
CoseAlgorithm::EDDSA, |
|
16
|
|
|
CoseAlgorithm::ES512, |
|
17
|
|
|
CoseAlgorithm::ES384, |
|
18
|
|
|
CoseAlgorithm::ES256, |
|
19
|
|
|
CoseAlgorithm::RS512, |
|
20
|
|
|
CoseAlgorithm::RS384, |
|
21
|
|
|
CoseAlgorithm::RS256, |
|
22
|
|
|
CoseAlgorithm::RS1, |
|
23
|
|
|
]; |
|
24
|
|
|
|
|
25
|
|
|
public const DEFAULT_ALGORITHMS = [ |
|
26
|
|
|
CoseAlgorithm::EDDSA, |
|
27
|
|
|
CoseAlgorithm::ES512, |
|
28
|
|
|
CoseAlgorithm::ES384, |
|
29
|
|
|
CoseAlgorithm::ES256, |
|
30
|
|
|
CoseAlgorithm::RS512, |
|
31
|
|
|
CoseAlgorithm::RS384, |
|
32
|
|
|
CoseAlgorithm::RS256, |
|
33
|
|
|
]; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var bool |
|
37
|
|
|
*/ |
|
38
|
|
|
private $userPresenceRequired = true; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var int |
|
42
|
|
|
*/ |
|
43
|
|
|
private $challengeLength = self::DEFAULT_CHALLENGE_LENGTH; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var int[] |
|
47
|
|
|
*/ |
|
48
|
|
|
private $algorithms = self::DEFAULT_ALGORITHMS; |
|
49
|
|
|
|
|
50
|
24 |
|
public function __construct() |
|
51
|
|
|
{ |
|
52
|
24 |
|
} |
|
53
|
|
|
|
|
54
|
2 |
|
public function isUserPresenceRequired(): bool |
|
55
|
|
|
{ |
|
56
|
2 |
|
return $this->userPresenceRequired; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Set to false to allow silent authenticators (User Preset bit not set in authenticator data) |
|
61
|
|
|
* NOTE: setting this to false violates the WebAuthn specs but this option is needed to pass FIDO2 conformance, which |
|
62
|
|
|
* includes silent operations. |
|
63
|
|
|
*/ |
|
64
|
|
|
public function setUserPresenceRequired(bool $required): void |
|
65
|
|
|
{ |
|
66
|
|
|
$this->userPresenceRequired = $required; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
3 |
|
public function getChallengeLength(): int |
|
70
|
|
|
{ |
|
71
|
3 |
|
return $this->challengeLength; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
2 |
|
public function setChallengeLength(int $challengeLength): void |
|
75
|
|
|
{ |
|
76
|
2 |
|
if ($challengeLength < self::MIN_CHALLENGE_LENGTH) { |
|
77
|
1 |
|
throw new ConfigurationException(sprintf('Challenge should be at least of length %d.', self::MIN_CHALLENGE_LENGTH)); |
|
78
|
|
|
} |
|
79
|
1 |
|
$this->challengeLength = $challengeLength; |
|
80
|
1 |
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Sets which algorithms are allowed for the credentials that are created. Array of constants from the COSEAlgorithm |
|
84
|
|
|
* enumeration (e.g. COSEAlgorithm::ES256). |
|
85
|
|
|
* |
|
86
|
|
|
* @param int[] $algorithms |
|
87
|
|
|
* |
|
88
|
|
|
* @throws ConfigurationException |
|
89
|
|
|
* |
|
90
|
|
|
* @see CoseAlgorithm |
|
91
|
|
|
*/ |
|
92
|
3 |
|
public function setAllowedAlgorithms(array $algorithms): void |
|
93
|
|
|
{ |
|
94
|
3 |
|
$validList = []; |
|
95
|
3 |
|
foreach ($algorithms as $algorithm) { |
|
96
|
3 |
|
if (!\is_int($algorithm)) { |
|
97
|
1 |
|
throw new ConfigurationException('Algorithms should be integer constants from the COSEAlgorithm enumeratons.'); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
3 |
|
if (!\in_array($algorithm, self::SUPPORTED_ALGORITHMS, true)) { |
|
101
|
1 |
|
throw new ConfigurationException(sprintf('Unsupported algorithm "%d".', $algorithm)); |
|
102
|
|
|
} |
|
103
|
2 |
|
$validList[] = $algorithm; |
|
104
|
|
|
} |
|
105
|
1 |
|
$this->algorithms = $validList; |
|
106
|
1 |
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @return int[] |
|
110
|
|
|
*/ |
|
111
|
3 |
|
public function getAllowedAlgorithms(): array |
|
112
|
|
|
{ |
|
113
|
3 |
|
return $this->algorithms; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|