PublicKeyCredentialEntity.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2020, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OC\Authentication\WebAuthn\Db;
  26. use JsonSerializable;
  27. use OCP\AppFramework\Db\Entity;
  28. use Webauthn\PublicKeyCredentialSource;
  29. /**
  30. * @since 19.0.0
  31. *
  32. * @method string getUid();
  33. * @method void setUid(string $uid)
  34. * @method string getName();
  35. * @method void setName(string $name);
  36. * @method string getPublicKeyCredentialId();
  37. * @method void setPublicKeyCredentialId(string $id);
  38. * @method string getData();
  39. * @method void setData(string $data);
  40. */
  41. class PublicKeyCredentialEntity extends Entity implements JsonSerializable {
  42. /** @var string */
  43. protected $name;
  44. /** @var string */
  45. protected $uid;
  46. /** @var string */
  47. protected $publicKeyCredentialId;
  48. /** @var string */
  49. protected $data;
  50. public function __construct() {
  51. $this->addType('name', 'string');
  52. $this->addType('uid', 'string');
  53. $this->addType('publicKeyCredentialId', 'string');
  54. $this->addType('data', 'string');
  55. }
  56. public static function fromPublicKeyCrendentialSource(string $name, PublicKeyCredentialSource $publicKeyCredentialSource): PublicKeyCredentialEntity {
  57. $publicKeyCredentialEntity = new self();
  58. $publicKeyCredentialEntity->setName($name);
  59. $publicKeyCredentialEntity->setUid($publicKeyCredentialSource->getUserHandle());
  60. $publicKeyCredentialEntity->setPublicKeyCredentialId(base64_encode($publicKeyCredentialSource->getPublicKeyCredentialId()));
  61. $publicKeyCredentialEntity->setData(json_encode($publicKeyCredentialSource));
  62. return $publicKeyCredentialEntity;
  63. }
  64. public function toPublicKeyCredentialSource(): PublicKeyCredentialSource {
  65. return PublicKeyCredentialSource::createFromArray(
  66. json_decode($this->getData(), true)
  67. );
  68. }
  69. /**
  70. * @inheritDoc
  71. */
  72. public function jsonSerialize(): array {
  73. return [
  74. 'id' => $this->getId(),
  75. 'name' => $this->getName(),
  76. ];
  77. }
  78. }