CredentialRepository.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2020, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\Authentication\WebAuthn;
  25. use OC\Authentication\WebAuthn\Db\PublicKeyCredentialEntity;
  26. use OC\Authentication\WebAuthn\Db\PublicKeyCredentialMapper;
  27. use OCP\AppFramework\Db\IMapperException;
  28. use Webauthn\PublicKeyCredentialSource;
  29. use Webauthn\PublicKeyCredentialSourceRepository;
  30. use Webauthn\PublicKeyCredentialUserEntity;
  31. class CredentialRepository implements PublicKeyCredentialSourceRepository {
  32. /** @var PublicKeyCredentialMapper */
  33. private $credentialMapper;
  34. public function __construct(PublicKeyCredentialMapper $credentialMapper) {
  35. $this->credentialMapper = $credentialMapper;
  36. }
  37. public function findOneByCredentialId(string $publicKeyCredentialId): ?PublicKeyCredentialSource {
  38. try {
  39. $entity = $this->credentialMapper->findOneByCredentialId($publicKeyCredentialId);
  40. return $entity->toPublicKeyCredentialSource();
  41. } catch (IMapperException $e) {
  42. return null;
  43. }
  44. }
  45. /**
  46. * @return PublicKeyCredentialSource[]
  47. */
  48. public function findAllForUserEntity(PublicKeyCredentialUserEntity $publicKeyCredentialUserEntity): array {
  49. $uid = $publicKeyCredentialUserEntity->getId();
  50. $entities = $this->credentialMapper->findAllForUid($uid);
  51. return array_map(function (PublicKeyCredentialEntity $entity) {
  52. return $entity->toPublicKeyCredentialSource();
  53. }, $entities);
  54. }
  55. public function saveAndReturnCredentialSource(PublicKeyCredentialSource $publicKeyCredentialSource, string $name = null): PublicKeyCredentialEntity {
  56. $oldEntity = null;
  57. try {
  58. $oldEntity = $this->credentialMapper->findOneByCredentialId($publicKeyCredentialSource->getPublicKeyCredentialId());
  59. } catch (IMapperException $e) {
  60. }
  61. $defaultName = false;
  62. if ($name === null) {
  63. $defaultName = true;
  64. $name = 'default';
  65. }
  66. $entity = PublicKeyCredentialEntity::fromPublicKeyCrendentialSource($name, $publicKeyCredentialSource);
  67. if ($oldEntity) {
  68. $entity->setId($oldEntity->getId());
  69. if ($defaultName) {
  70. $entity->setName($oldEntity->getName());
  71. }
  72. }
  73. return $this->credentialMapper->insertOrUpdate($entity);
  74. }
  75. public function saveCredentialSource(PublicKeyCredentialSource $publicKeyCredentialSource, string $name = null): void {
  76. $this->saveAndReturnCredentialSource($publicKeyCredentialSource, $name);
  77. }
  78. }