Passed
Push — master ( f215e2...ea45dc )
by Florent
02:22
created

PublicKeyCredentialRequestOptions   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 144
rs 10
c 0
b 0
f 0
wmc 14

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getAllowCredentials() 0 3 1
A getChallenge() 0 3 1
A getTimeout() 0 3 1
A splitChallenge() 0 9 2
B jsonSerialize() 0 24 5
A getRpId() 0 3 1
A __construct() 0 8 1
A getExtensions() 0 3 1
A getUserVerification() 0 3 1
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 PublicKeyCredentialRequestOptions implements \JsonSerializable
17
{
18
    public const USER_VERIFICATION_REQUIREMENT_REQUIRED = 'required';
19
    public const USER_VERIFICATION_REQUIREMENT_PREFERRED = 'preferred';
20
    public const USER_VERIFICATION_REQUIREMENT_DISCOURAGED = 'discouraged';
21
    /**
22
     * @var string
23
     */
24
    private $challenge;
25
26
    /**
27
     * @var int|null
28
     */
29
    private $timeout;
30
31
    /**
32
     * @var string|null
33
     */
34
    private $rpId;
35
36
    /**
37
     * @var PublicKeyCredentialDescriptor[]
38
     */
39
    private $allowCredentials;
40
41
    /**
42
     * @var string|null
43
     */
44
    private $userVerification;
45
46
    /**
47
     * @var AuthenticationExtensionsClientInputs
48
     */
49
    private $extensions;
50
51
    /**
52
     * PublicKeyCredentialRequestOptions constructor.
53
     *
54
     * @param string                               $challenge
55
     * @param int|null                             $timeout
56
     * @param string|null                          $rpId
57
     * @param PublicKeyCredentialDescriptor[]      $allowCredentials
58
     * @param string|null                          $userVerification
59
     * @param AuthenticationExtensionsClientInputs $extensions
60
     */
61
    public function __construct(string $challenge, ?int $timeout = null, ?string $rpId = null, array $allowCredentials = [], ?string $userVerification = null, AuthenticationExtensionsClientInputs $extensions)
62
    {
63
        $this->challenge = $challenge;
64
        $this->timeout = $timeout;
65
        $this->rpId = $rpId;
66
        $this->allowCredentials = $allowCredentials;
67
        $this->userVerification = $userVerification;
68
        $this->extensions = $extensions;
69
    }
70
71
    /**
72
     * @return string
73
     */
74
    public function getChallenge(): string
75
    {
76
        return $this->challenge;
77
    }
78
79
    /**
80
     * @return int|null
81
     */
82
    public function getTimeout(): ?int
83
    {
84
        return $this->timeout;
85
    }
86
87
    /**
88
     * @return string|null
89
     */
90
    public function getRpId(): ?string
91
    {
92
        return $this->rpId;
93
    }
94
95
    /**
96
     * @return PublicKeyCredentialDescriptor[]
97
     */
98
    public function getAllowCredentials(): array
99
    {
100
        return $this->allowCredentials;
101
    }
102
103
    /**
104
     * @return string|null
105
     */
106
    public function getUserVerification(): ?string
107
    {
108
        return $this->userVerification;
109
    }
110
111
    /**
112
     * @return AuthenticationExtensionsClientInputs
113
     */
114
    public function getExtensions(): AuthenticationExtensionsClientInputs
115
    {
116
        return $this->extensions;
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122
    public function jsonSerialize(): array
123
    {
124
        $json = [
125
            'rpId'      => $this->rpId,
126
            'challenge' => $this->splitChallenge(),
127
        ];
128
129
        if ($this->userVerification) {
130
            $json['userVerification'] = $this->userVerification;
131
        }
132
133
        if (!empty($this->allowCredentials)) {
134
            $json['allowCredentials'] = $this->allowCredentials;
135
        }
136
137
        if (!empty($this->extensions)) {
138
            $json['extensions'] = $this->extensions;
139
        }
140
141
        if (!is_null($this->timeout)) {
142
            $json['timeout'] = $this->timeout;
143
        }
144
145
        return $json;
146
    }
147
148
    /**
149
     * @return int[]
150
     */
151
    private function splitChallenge(): array
152
    {
153
        $result = [];
154
        $split = str_split($this->challenge);
155
        foreach ($split as $char) {
156
            $result[] = ord($char);
157
        }
158
159
        return $result;
160
    }
161
}
162