Passed
Push — master ( 8d86ce...bd9937 )
by Thomas
07:31
created

AbstractAuthenticatorResponse::asAssertionResponse()   A

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
    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