Failed Conditions
Push — master ( bbfade...32fb37 )
by Florent
02:44
created

PublicKeyCredentialCreationOptions::getTimeout()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
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) 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
class PublicKeyCredentialCreationOptions implements \JsonSerializable
17
{
18
    public const ATTESTATION_CONVEYANCE_PREFERENCE_NONE = 'none';
19
    public const ATTESTATION_CONVEYANCE_PREFERENCE_INDIRECT = 'indirect';
20
    public const ATTESTATION_CONVEYANCE_PREFERENCE_DIRECT = 'direct';
21
22
    /**
23
     * @var PublicKeyCredentialRpEntity
24
     */
25
    private $rp;
26
27
    /**
28
     * @var PublicKeyCredentialUserEntity
29
     */
30
    private $user;
31
32
    /**
33
     * @var string
34
     */
35
    private $challenge;
36
37
    /**
38
     * @var PublicKeyCredentialParameters[]
39
     */
40
    private $pubKeyCredParams;
41
42
    /**
43
     * @var int|null
44
     */
45
    private $timeout;
46
47
    /**
48
     * @var PublicKeyCredentialDescriptor[]
49
     */
50
    private $excludeCredentials;
51
52
    /**
53
     * @var AuthenticatorSelectionCriteria
54
     */
55
    private $authenticatorSelection;
56
57
    /**
58
     * @var string
59
     */
60
    private $attestation;
61
62
    /**
63
     * @var AuthenticationExtensionsClientInputs
64
     */
65
    private $extensions;
66
67
    /**
68
     * PublicKeyCredentialCreationOptions constructor.
69
     *
70
     * @param PublicKeyCredentialRpEntity          $rp
71
     * @param PublicKeyCredentialUserEntity        $user
72
     * @param string                               $challenge
73
     * @param PublicKeyCredentialParameters[]      $pubKeyCredParams
74
     * @param null|int                             $timeout
75
     * @param PublicKeyCredentialDescriptor[]      $excludeCredentials
76
     * @param AuthenticatorSelectionCriteria       $authenticatorSelection
77
     * @param string                               $attestation
78
     * @param AuthenticationExtensionsClientInputs $extensions
79
     */
80
    public function __construct(PublicKeyCredentialRpEntity $rp, PublicKeyCredentialUserEntity $user, string $challenge, array $pubKeyCredParams, ?int $timeout, array $excludeCredentials, AuthenticatorSelectionCriteria $authenticatorSelection, string $attestation, AuthenticationExtensionsClientInputs $extensions)
81
    {
82
        $this->rp = $rp;
83
        $this->user = $user;
84
        $this->challenge = $challenge;
85
        $this->pubKeyCredParams = $pubKeyCredParams;
86
        $this->timeout = $timeout;
87
        $this->excludeCredentials = $excludeCredentials;
88
        $this->authenticatorSelection = $authenticatorSelection;
89
        $this->attestation = $attestation;
90
        $this->extensions = $extensions;
91
    }
92
93
    /**
94
     * @return PublicKeyCredentialRpEntity
95
     */
96
    public function getRp(): PublicKeyCredentialRpEntity
97
    {
98
        return $this->rp;
99
    }
100
101
    /**
102
     * @return PublicKeyCredentialUserEntity
103
     */
104
    public function getUser(): PublicKeyCredentialUserEntity
105
    {
106
        return $this->user;
107
    }
108
109
    /**
110
     * @return string
111
     */
112
    public function getChallenge(): string
113
    {
114
        return $this->challenge;
115
    }
116
117
    /**
118
     * @return PublicKeyCredentialParameters[]
119
     */
120
    public function getPubKeyCredParams(): array
121
    {
122
        return $this->pubKeyCredParams;
123
    }
124
125
    /**
126
     * @return null|int
127
     */
128
    public function getTimeout(): ?int
129
    {
130
        return $this->timeout;
131
    }
132
133
    /**
134
     * @return PublicKeyCredentialDescriptor[]
135
     */
136
    public function getExcludeCredentials(): array
137
    {
138
        return $this->excludeCredentials;
139
    }
140
141
    /**
142
     * @return AuthenticatorSelectionCriteria
143
     */
144
    public function getAuthenticatorSelection(): AuthenticatorSelectionCriteria
145
    {
146
        return $this->authenticatorSelection;
147
    }
148
149
    /**
150
     * @return string
151
     */
152
    public function getAttestation(): string
153
    {
154
        return $this->attestation;
155
    }
156
157
    /**
158
     * @return AuthenticationExtensionsClientInputs
159
     */
160
    public function getExtensions(): AuthenticationExtensionsClientInputs
161
    {
162
        return $this->extensions;
163
    }
164
165
    /**
166
     * {@inheritdoc}
167
     */
168
    public function jsonSerialize(): array
169
    {
170
        $json = [
171
            'rp'                     => $this->rp,
172
            'pubKeyCredParams'       => $this->pubKeyCredParams,
173
            'challenge'              => $this->splitChallenge(),
174
            'attestation'            => $this->attestation,
175
            'user'                   => $this->user,
176
            'authenticatorSelection' => $this->authenticatorSelection,
177
        ];
178
179
        if (!empty($this->excludeCredentials)) {
180
            $json['excludeCredentials'] = $this->excludeCredentials;
181
        }
182
183
        if (!empty($this->extensions)) {
184
            $json['extensions'] = $this->extensions;
185
        }
186
187
        if (!is_null($this->timeout)) {
188
            $json['timeout'] = $this->timeout;
189
        }
190
191
        return $json;
192
    }
193
194
    /**
195
     * @return int[]
196
     */
197
    private function splitChallenge(): array
198
    {
199
        $result = [];
200
        $split = str_split($this->challenge);
201
        foreach ($split as $char) {
202
            $result[] = ord($char);
203
        }
204
205
        return $result;
206
    }
207
}
208