PublicKeyCredentialEntity.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Authentication\WebAuthn\Db;
  8. use JsonSerializable;
  9. use OCP\AppFramework\Db\Entity;
  10. use Webauthn\PublicKeyCredentialSource;
  11. /**
  12. * @since 19.0.0
  13. *
  14. * @method string getUid();
  15. * @method void setUid(string $uid)
  16. * @method string getName();
  17. * @method void setName(string $name);
  18. * @method string getPublicKeyCredentialId();
  19. * @method void setPublicKeyCredentialId(string $id);
  20. * @method string getData();
  21. * @method void setData(string $data);
  22. *
  23. * @since 30.0.0 Add userVerification attribute
  24. * @method bool|null getUserVerification();
  25. * @method void setUserVerification(bool $userVerification);
  26. */
  27. class PublicKeyCredentialEntity extends Entity implements JsonSerializable {
  28. /** @var string */
  29. protected $name;
  30. /** @var string */
  31. protected $uid;
  32. /** @var string */
  33. protected $publicKeyCredentialId;
  34. /** @var string */
  35. protected $data;
  36. /** @var bool|null */
  37. protected $userVerification;
  38. public function __construct() {
  39. $this->addType('name', 'string');
  40. $this->addType('uid', 'string');
  41. $this->addType('publicKeyCredentialId', 'string');
  42. $this->addType('data', 'string');
  43. $this->addType('userVerification', 'boolean');
  44. }
  45. public static function fromPublicKeyCrendentialSource(string $name, PublicKeyCredentialSource $publicKeyCredentialSource, bool $userVerification): PublicKeyCredentialEntity {
  46. $publicKeyCredentialEntity = new self();
  47. $publicKeyCredentialEntity->setName($name);
  48. $publicKeyCredentialEntity->setUid($publicKeyCredentialSource->getUserHandle());
  49. $publicKeyCredentialEntity->setPublicKeyCredentialId(base64_encode($publicKeyCredentialSource->getPublicKeyCredentialId()));
  50. $publicKeyCredentialEntity->setData(json_encode($publicKeyCredentialSource));
  51. $publicKeyCredentialEntity->setUserVerification($userVerification);
  52. return $publicKeyCredentialEntity;
  53. }
  54. public function toPublicKeyCredentialSource(): PublicKeyCredentialSource {
  55. return PublicKeyCredentialSource::createFromArray(
  56. json_decode($this->getData(), true)
  57. );
  58. }
  59. /**
  60. * @inheritDoc
  61. */
  62. public function jsonSerialize(): array {
  63. return [
  64. 'id' => $this->getId(),
  65. 'name' => $this->getName(),
  66. ];
  67. }
  68. }