setRpId($rpId) ->allowCredentials($allowCredentials) ->setUserVerification($userVerification) ; } public static function create(string $challenge): self { return new self($challenge); } public function setRpId(?string $rpId): self { $this->rpId = $rpId; return $this; } public function allowCredential(PublicKeyCredentialDescriptor $allowCredential): self { $this->allowCredentials[] = $allowCredential; return $this; } /** * @param PublicKeyCredentialDescriptor[] $allowCredentials */ public function allowCredentials(array $allowCredentials): self { foreach ($allowCredentials as $allowCredential) { $this->allowCredential($allowCredential); } return $this; } public function setUserVerification(?string $userVerification): self { if (null === $userVerification) { $this->rpId = null; return $this; } Assertion::inArray($userVerification, [ self::USER_VERIFICATION_REQUIREMENT_REQUIRED, self::USER_VERIFICATION_REQUIREMENT_PREFERRED, self::USER_VERIFICATION_REQUIREMENT_DISCOURAGED, ], 'Invalid user verification requirement'); $this->userVerification = $userVerification; return $this; } public function getRpId(): ?string { return $this->rpId; } /** * @return PublicKeyCredentialDescriptor[] */ public function getAllowCredentials(): array { return $this->allowCredentials; } public function getUserVerification(): ?string { return $this->userVerification; } public static function createFromString(string $data): PublicKeyCredentialOptions { $data = json_decode($data, true); Assertion::isArray($data, 'Invalid data'); return self::createFromArray($data); } /** * @param mixed[] $json */ public static function createFromArray(array $json): PublicKeyCredentialOptions { Assertion::keyExists($json, 'challenge', 'Invalid input. "challenge" is missing.'); $allowCredentials = []; $allowCredentialList = $json['allowCredentials'] ?? []; foreach ($allowCredentialList as $allowCredential) { $allowCredentials[] = PublicKeyCredentialDescriptor::createFromArray($allowCredential); } return self::create(Base64Url::decode($json['challenge'])) ->setRpId($json['rpId'] ?? null) ->allowCredentials($allowCredentials) ->setUserVerification($json['userVerification'] ?? null) ->setTimeout($json['timeout'] ?? null) ->setExtensions(isset($json['extensions']) ? AuthenticationExtensionsClientInputs::createFromArray($json['extensions']) : new AuthenticationExtensionsClientInputs()) ; } /** * @return mixed[] */ public function jsonSerialize(): array { $json = [ 'challenge' => Base64Url::encode($this->challenge), ]; if (null !== $this->rpId) { $json['rpId'] = $this->rpId; } if (null !== $this->userVerification) { $json['userVerification'] = $this->userVerification; } if (0 !== count($this->allowCredentials)) { $json['allowCredentials'] = array_map(static function (PublicKeyCredentialDescriptor $object): array { return $object->jsonSerialize(); }, $this->allowCredentials); } if (0 !== $this->extensions->count()) { $json['extensions'] = $this->extensions->jsonSerialize(); } if (null !== $this->timeout) { $json['timeout'] = $this->timeout; } return $json; } }