AttestedCredentialData::getLength()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 * Platine Webauth
5
 *
6
 * Platine Webauthn is the implementation of webauthn specifications
7
 *
8
 * This content is released under the MIT License (MIT)
9
 *
10
 * Copyright (c) 2020 Platine Webauth
11
 * Copyright (c) Jakob Bennemann <[email protected]>
12
 *
13
 * Permission is hereby granted, free of charge, to any person obtaining a copy
14
 * of this software and associated documentation files (the "Software"), to deal
15
 * in the Software without restriction, including without limitation the rights
16
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17
 * copies of the Software, and to permit persons to whom the Software is
18
 * furnished to do so, subject to the following conditions:
19
 *
20
 * The above copyright notice and this permission notice shall be included in all
21
 * copies or substantial portions of the Software.
22
 *
23
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29
 * SOFTWARE.
30
 */
31
32
declare(strict_types=1);
33
34
namespace Platine\Webauthn\Entity;
35
36
use JsonSerializable;
37
use Platine\Webauthn\Exception\WebauthnException;
38
39
/**
40
 * @class AttestedCredentialData
41
 * @package Platine\Webauthn\Entity
42
 */
43
class AttestedCredentialData implements JsonSerializable
44
{
45
    /**
46
     * The AAGUID of the authenticator
47
     * @var string
48
     */
49
    protected string $aaguid;
50
51
    /**
52
     * The credential id. Byte length L of Credential ID,
53
     * 16-bit unsigned big-endian integer.
54
     * @var string
55
     */
56
    protected string $credentialId;
57
58
    /**
59
     * The credential public key
60
     * @var CredentialPublicKey
61
     */
62
    protected CredentialPublicKey $credentialPublicKey;
63
64
    /**
65
     * The length
66
     * @var int
67
     */
68
    protected int $length;
69
70
    /**
71
     * Create new instance
72
     * @param string $binaryData
73
     */
74
    public function __construct(string $binaryData)
75
    {
76
        if (strlen($binaryData) <= 55) {
77
            throw new WebauthnException('Attested credential data should be present but is missing');
78
        }
79
80
        $this->aaguid = substr($binaryData, 37, 16);
81
82
        $lengthData = unpack('nlength', substr($binaryData, 53, 2));
83
        if ($lengthData === false) {
84
            throw new WebauthnException('Can not unpack[nlength] data');
85
        }
86
        $length = $lengthData['length'];
87
88
        $this->credentialId = substr($binaryData, 55, $length);
89
        $this->length = $length;
90
    }
91
92
    /**
93
     *
94
     * @return int
95
     */
96
    public function getLength(): int
97
    {
98
        return $this->length;
99
    }
100
101
102
    /**
103
     * Set credential Public key
104
     * @param CredentialPublicKey $credentialPublicKey
105
     * @return $this
106
     */
107
    public function setCredentialPublicKey(CredentialPublicKey $credentialPublicKey): self
108
    {
109
        $this->credentialPublicKey = $credentialPublicKey;
110
        return $this;
111
    }
112
113
114
    /**
115
     *
116
     * @return string
117
     */
118
    public function getAaguid(): string
119
    {
120
        return $this->aaguid;
121
    }
122
123
    /**
124
     *
125
     * @return string
126
     */
127
    public function getCredentialId(): string
128
    {
129
        return $this->credentialId;
130
    }
131
132
    /**
133
     *
134
     * @return CredentialPublicKey
135
     */
136
    public function getCredentialPublicKey(): CredentialPublicKey
137
    {
138
        return $this->credentialPublicKey;
139
    }
140
141
    /**
142
    * {@inheritdoc}
143
    * @return mixed
144
    */
145
    public function jsonSerialize(): mixed
146
    {
147
        return get_object_vars($this);
148
    }
149
}
150