asAttestationResponse()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
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
    /**
19
     * @var CollectedClientData
20
     */
21
    private $clientData;
22
23 26
    public function __construct(string $clientDataJson)
24
    {
25 26
        $this->clientDataJson = $clientDataJson;
26
27
        // Specification says to remove the UTF-8 byte order mark, if any
28 26
        if (\substr($clientDataJson, 0, 3) === self::UTF8_BOM) {
29 1
            $clientDataJson = substr($clientDataJson, 3);
30
        }
31 26
        $data = \json_decode($clientDataJson, true, 10);
32 26
        if ($data === null && json_last_error() !== JSON_ERROR_NONE) {
33 3
            throw new ParseException('Unparseable client data JSON');
34
        }
35 23
        if (!\is_array($data)) {
36
            throw new ParseException('Expected object for client data');
37
        }
38 23
        $this->clientData = CollectedClientData::fromJson($data);
39 20
    }
40
41 11
    public function getClientDataJson(): string
42
    {
43 11
        return $this->clientDataJson;
44
    }
45
46 11
    public function getParsedClientData(): CollectedClientData
47
    {
48 11
        return $this->clientData;
49
    }
50
51
    public function asAttestationResponse(): AuthenticatorAttestationResponseInterface
52
    {
53
        throw new WebAuthnException('Response is not an attestation response.');
54
    }
55
56
    public function asAssertionResponse(): AuthenticatorAssertionResponseInterface
57
    {
58
        throw new WebAuthnException('Response is not an assertion response.');
59
    }
60
}
61