Completed
Push — master ( cf2c3c...c63c7f )
by Florent
02:05
created

AttestedCredentialData::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2018 Spomky-Labs
9
 *
10
 * This software may be modified and distributed under the terms
11
 * of the MIT license.  See the LICENSE file for details.
12
 */
13
14
namespace U2FAuthentication\Fido2;
15
16
use Assert\Assertion;
17
18
/**
19
 * @see https://www.w3.org/TR/webauthn/#sec-attested-credential-data
20
 */
21
class AttestedCredentialData implements \JsonSerializable
22
{
23
    private $aaguid;
24
25
    private $credentialId;
26
27
    private $credentialPublicKey;
28
29
    public function __construct(string $aaguid, string $credentialId, ?string $credentialPublicKey)
30
    {
31
        $this->aaguid = $aaguid;
32
        $this->credentialId = $credentialId;
33
        $this->credentialPublicKey = $credentialPublicKey;
34
    }
35
36
    public function getAaguid(): string
37
    {
38
        return $this->aaguid;
39
    }
40
41
    public function getCredentialId(): string
42
    {
43
        return $this->credentialId;
44
    }
45
46
    public function getCredentialPublicKey(): ?string
47
    {
48
        return $this->credentialPublicKey;
49
    }
50
51
    public static function createFromJson(string $json): self
52
    {
53
        $data = \Safe\json_decode($json, true);
54
        Assertion::isArray($data, 'Invalid input.');
55
        Assertion::keyExists($data, 'aaguid', 'Invalid input.');
56
        Assertion::keyExists($data, 'credentialId', 'Invalid input.');
57
58
        return new self(
59
            \Safe\base64_decode($data['aaguid'], true),
60
            \Safe\base64_decode($data['credentialId'], true),
61
            $data['credentialPublicKey'] ? \Safe\base64_decode($data['credentialPublicKey'], true) : null
62
        );
63
    }
64
65
    public function jsonSerialize()
66
    {
67
        $result = [
68
            'aaguid' => base64_encode($this->aaguid),
69
            'credentialId' => base64_encode($this->credentialId),
70
        ];
71
        if (null !== $this->credentialPublicKey) {
72
            $result['credentialPublicKey'] = base64_encode($this->credentialPublicKey);
73
        }
74
75
        return $result;
76
    }
77
}
78