1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MadWizard\WebAuthn\Attestation\Registry; |
4
|
|
|
|
5
|
|
|
use MadWizard\WebAuthn\Attestation\AttestationObject; |
6
|
|
|
use MadWizard\WebAuthn\Attestation\Statement\AttestationStatementInterface; |
7
|
|
|
use MadWizard\WebAuthn\Attestation\Statement\UnsupportedAttestationStatement; |
8
|
|
|
use MadWizard\WebAuthn\Attestation\Verifier\AttestationVerifierInterface; |
9
|
|
|
use MadWizard\WebAuthn\Attestation\Verifier\UnsupportedAttestationVerifier; |
10
|
|
|
use MadWizard\WebAuthn\Exception\FormatNotSupportedException; |
11
|
|
|
|
12
|
|
|
final class AttestationFormatRegistry implements AttestationFormatRegistryInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var AttestationFormatInterface[] |
16
|
|
|
*/ |
17
|
|
|
private $formats = []; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var bool |
21
|
|
|
*/ |
22
|
|
|
private $strictSupportedFormats = true; |
23
|
|
|
|
24
|
28 |
|
public function __construct() |
25
|
|
|
{ |
26
|
28 |
|
} |
27
|
|
|
|
28
|
22 |
|
public function addFormat(AttestationFormatInterface $format): void |
29
|
|
|
{ |
30
|
22 |
|
$this->formats[$format->getFormatId()] = $format; |
31
|
22 |
|
} |
32
|
|
|
|
33
|
3 |
|
public function createStatement(AttestationObject $attestationObject): AttestationStatementInterface |
34
|
|
|
{ |
35
|
3 |
|
$formatId = $attestationObject->getFormat(); |
36
|
3 |
|
$format = $this->formats[$formatId] ?? null; |
37
|
3 |
|
if ($format === null) { |
38
|
1 |
|
if ($this->strictSupportedFormats) { |
39
|
1 |
|
throw new FormatNotSupportedException(sprintf('Format "%s" is not supported.', $formatId)); |
40
|
|
|
} |
41
|
|
|
return new UnsupportedAttestationStatement($attestationObject); |
42
|
|
|
} |
43
|
2 |
|
return $format->createStatement($attestationObject); |
44
|
|
|
} |
45
|
|
|
|
46
|
3 |
|
public function getVerifier(string $formatId): AttestationVerifierInterface |
47
|
|
|
{ |
48
|
3 |
|
$format = $this->formats[$formatId] ?? null; |
49
|
3 |
|
if ($format === null) { |
50
|
1 |
|
if ($this->strictSupportedFormats) { |
51
|
1 |
|
throw new FormatNotSupportedException(sprintf('Format "%s" is not supported.', $formatId)); |
52
|
|
|
} |
53
|
|
|
return new UnsupportedAttestationVerifier(); |
54
|
|
|
} |
55
|
2 |
|
return $format->getVerifier(); |
56
|
|
|
} |
57
|
|
|
|
58
|
18 |
|
public function strictSupportedFormats(bool $strict): void |
59
|
|
|
{ |
60
|
18 |
|
$this->strictSupportedFormats = $strict; |
61
|
18 |
|
} |
62
|
|
|
} |
63
|
|
|
|