|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace MadWizard\WebAuthn\Dom; |
|
4
|
|
|
|
|
5
|
|
|
use MadWizard\WebAuthn\Exception\ParseException; |
|
6
|
|
|
use MadWizard\WebAuthn\Exception\WebAuthnException; |
|
7
|
|
|
use function json_last_error; |
|
8
|
|
|
|
|
9
|
|
|
abstract class AbstractAuthenticatorResponse implements AuthenticatorResponseInterface |
|
10
|
|
|
{ |
|
11
|
|
|
public const UTF8_BOM = "\xEF\xBB\xBF"; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @var string |
|
15
|
|
|
*/ |
|
16
|
|
|
private $clientDataJson; |
|
17
|
|
|
|
|
18
|
|
|
private $parsedJson; |
|
19
|
|
|
|
|
20
|
25 |
|
public function __construct(string $clientDataJson) |
|
21
|
|
|
{ |
|
22
|
25 |
|
$this->clientDataJson = $clientDataJson; |
|
23
|
|
|
|
|
24
|
|
|
// Specification says to remove the UTF-8 byte order mark, if any |
|
25
|
25 |
|
if (\substr($clientDataJson, 0, 3) === self::UTF8_BOM) { |
|
26
|
1 |
|
$clientDataJson = substr($clientDataJson, 3); |
|
27
|
|
|
} |
|
28
|
25 |
|
$data = \json_decode($clientDataJson, true, 10); |
|
29
|
25 |
|
if ($data === null && json_last_error() !== JSON_ERROR_NONE) { |
|
30
|
3 |
|
throw new ParseException('Unparseable client data JSON'); |
|
31
|
|
|
} |
|
32
|
22 |
|
if (!\is_array($data)) { |
|
33
|
|
|
throw new ParseException('Expected object for client data'); |
|
34
|
|
|
} |
|
35
|
22 |
|
$this->parsedJson = $data; |
|
36
|
22 |
|
} |
|
37
|
|
|
|
|
38
|
11 |
|
public function getClientDataJson(): string |
|
39
|
|
|
{ |
|
40
|
11 |
|
return $this->clientDataJson; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
13 |
|
public function getParsedClientData(): array |
|
44
|
|
|
{ |
|
45
|
13 |
|
return $this->parsedJson; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function asAttestationResponse(): AuthenticatorAttestationResponseInterface |
|
49
|
|
|
{ |
|
50
|
|
|
throw new WebAuthnException('Response is not an attestation response.'); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function asAssertionResponse(): AuthenticatorAssertionResponseInterface |
|
54
|
|
|
{ |
|
55
|
|
|
throw new WebAuthnException('Response is not an assertion response.'); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|