PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 6.2.2
Defender Security – Malware Scanner, Login Security & Firewall v6.2.2
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / lib / packages / Webauthn / PublicKeyCredentialRequestOptions.php

PublicKeyCredentialRequestOptions.php in Defender Security – Malware Scanner, Login Security & Firewall 6.2.2, at lib/packages/Webauthn/PublicKeyCredentialRequestOptions.php

195 lines 5.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 /*
6 * The MIT License (MIT)
7 *
8 * Copyright (c) 2014-2021 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 WP_DEFENDER_VENDOR\Webauthn;
15
16 use WP_DEFENDER_VENDOR\Assert\Assertion;
17 use WP_DEFENDER_VENDOR\Base64Url\Base64Url;
18 use function count;
19 use function WP_DEFENDER_VENDOR\Safe\json_decode;
20 use WP_DEFENDER_VENDOR\Webauthn\AuthenticationExtensions\AuthenticationExtensionsClientInputs;
21
22 class PublicKeyCredentialRequestOptions extends PublicKeyCredentialOptions
23 {
24 public const USER_VERIFICATION_REQUIREMENT_REQUIRED = 'required';
25 public const USER_VERIFICATION_REQUIREMENT_PREFERRED = 'preferred';
26 public const USER_VERIFICATION_REQUIREMENT_DISCOURAGED = 'discouraged';
27
28 /**
29 * @var string|null
30 */
31 private $rpId;
32
33 /**
34 * @var PublicKeyCredentialDescriptor[]
35 */
36 private $allowCredentials = [];
37
38 /**
39 * @var string|null
40 */
41 private $userVerification;
42
43 /**
44 * @param PublicKeyCredentialDescriptor[] $allowCredentials
45 */
46 public function __construct(string $challenge, ?int $timeout = null, ?string $rpId = null, array $allowCredentials = [], ?string $userVerification = null, ?AuthenticationExtensionsClientInputs $extensions = null)
47 {
48 if (0 !== count($allowCredentials)) {
49 @trigger_error('The argument "allowCredentials" is deprecated since version 3.3 and will be removed in 4.0. Please use the method "addAllowedCredentials" or "addAllowedCredential".', E_USER_DEPRECATED);
50 }
51 if (null !== $rpId) {
52 @trigger_error('The argument "rpId" is deprecated since version 3.3 and will be removed in 4.0. Please use the method "setRpId".', E_USER_DEPRECATED);
53 }
54 if (null !== $userVerification) {
55 @trigger_error('The argument "userVerification" is deprecated since version 3.3 and will be removed in 4.0. Please use the method "setUserVerification".', E_USER_DEPRECATED);
56 }
57 parent::__construct($challenge, $timeout, $extensions);
58 $this
59 ->setRpId($rpId)
60 ->allowCredentials($allowCredentials)
61 ->setUserVerification($userVerification)
62 ;
63 }
64
65 public static function create(string $challenge): self
66 {
67 return new self($challenge);
68 }
69
70 public function setRpId(?string $rpId): self
71 {
72 $this->rpId = $rpId;
73
74 return $this;
75 }
76
77 public function allowCredential(PublicKeyCredentialDescriptor $allowCredential): self
78 {
79 $this->allowCredentials[] = $allowCredential;
80
81 return $this;
82 }
83
84 /**
85 * @param PublicKeyCredentialDescriptor[] $allowCredentials
86 */
87 public function allowCredentials(array $allowCredentials): self
88 {
89 foreach ($allowCredentials as $allowCredential) {
90 $this->allowCredential($allowCredential);
91 }
92
93 return $this;
94 }
95
96 public function setUserVerification(?string $userVerification): self
97 {
98 if (null === $userVerification) {
99 $this->rpId = null;
100
101 return $this;
102 }
103 Assertion::inArray($userVerification, [
104 self::USER_VERIFICATION_REQUIREMENT_REQUIRED,
105 self::USER_VERIFICATION_REQUIREMENT_PREFERRED,
106 self::USER_VERIFICATION_REQUIREMENT_DISCOURAGED,
107 ], 'Invalid user verification requirement');
108 $this->userVerification = $userVerification;
109
110 return $this;
111 }
112
113 public function getRpId(): ?string
114 {
115 return $this->rpId;
116 }
117
118 /**
119 * @return PublicKeyCredentialDescriptor[]
120 */
121 public function getAllowCredentials(): array
122 {
123 return $this->allowCredentials;
124 }
125
126 public function getUserVerification(): ?string
127 {
128 return $this->userVerification;
129 }
130
131 public static function createFromString(string $data): PublicKeyCredentialOptions
132 {
133 $data = json_decode($data, true);
134 Assertion::isArray($data, 'Invalid data');
135
136 return self::createFromArray($data);
137 }
138
139 /**
140 * @param mixed[] $json
141 */
142 public static function createFromArray(array $json): PublicKeyCredentialOptions
143 {
144 Assertion::keyExists($json, 'challenge', 'Invalid input. "challenge" is missing.');
145
146 $allowCredentials = [];
147 $allowCredentialList = $json['allowCredentials'] ?? [];
148 foreach ($allowCredentialList as $allowCredential) {
149 $allowCredentials[] = PublicKeyCredentialDescriptor::createFromArray($allowCredential);
150 }
151
152 return self::create(Base64Url::decode($json['challenge']))
153 ->setRpId($json['rpId'] ?? null)
154 ->allowCredentials($allowCredentials)
155 ->setUserVerification($json['userVerification'] ?? null)
156 ->setTimeout($json['timeout'] ?? null)
157 ->setExtensions(isset($json['extensions']) ? AuthenticationExtensionsClientInputs::createFromArray($json['extensions']) : new AuthenticationExtensionsClientInputs())
158 ;
159 }
160
161 /**
162 * @return mixed[]
163 */
164 public function jsonSerialize(): array
165 {
166 $json = [
167 'challenge' => Base64Url::encode($this->challenge),
168 ];
169
170 if (null !== $this->rpId) {
171 $json['rpId'] = $this->rpId;
172 }
173
174 if (null !== $this->userVerification) {
175 $json['userVerification'] = $this->userVerification;
176 }
177
178 if (0 !== count($this->allowCredentials)) {
179 $json['allowCredentials'] = array_map(static function (PublicKeyCredentialDescriptor $object): array {
180 return $object->jsonSerialize();
181 }, $this->allowCredentials);
182 }
183
184 if (0 !== $this->extensions->count()) {
185 $json['extensions'] = $this->extensions->jsonSerialize();
186 }
187
188 if (null !== $this->timeout) {
189 $json['timeout'] = $this->timeout;
190 }
191
192 return $json;
193 }
194 }
195