PublicKeyCredentialMapper.php 1.9 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 OCP\AppFramework\Db\DoesNotExistException;
  9. use OCP\AppFramework\Db\QBMapper;
  10. use OCP\IDBConnection;
  11. /**
  12. * @template-extends QBMapper<PublicKeyCredentialEntity>
  13. */
  14. class PublicKeyCredentialMapper extends QBMapper {
  15. public function __construct(IDBConnection $db) {
  16. parent::__construct($db, 'webauthn', PublicKeyCredentialEntity::class);
  17. }
  18. public function findOneByCredentialId(string $publicKeyCredentialId): PublicKeyCredentialEntity {
  19. $qb = $this->db->getQueryBuilder();
  20. $qb->select('*')
  21. ->from($this->getTableName())
  22. ->where(
  23. $qb->expr()->eq('public_key_credential_id', $qb->createNamedParameter(base64_encode($publicKeyCredentialId)))
  24. );
  25. return $this->findEntity($qb);
  26. }
  27. /**
  28. * @return PublicKeyCredentialEntity[]
  29. */
  30. public function findAllForUid(string $uid): array {
  31. $qb = $this->db->getQueryBuilder();
  32. $qb->select('*')
  33. ->from($this->getTableName())
  34. ->where(
  35. $qb->expr()->eq('uid', $qb->createNamedParameter($uid))
  36. );
  37. return $this->findEntities($qb);
  38. }
  39. /**
  40. * @param string $uid
  41. * @param int $id
  42. *
  43. * @return PublicKeyCredentialEntity
  44. * @throws DoesNotExistException
  45. */
  46. public function findById(string $uid, int $id): PublicKeyCredentialEntity {
  47. $qb = $this->db->getQueryBuilder();
  48. $qb->select('*')
  49. ->from($this->getTableName())
  50. ->where($qb->expr()->andX(
  51. $qb->expr()->eq('id', $qb->createNamedParameter($id)),
  52. $qb->expr()->eq('uid', $qb->createNamedParameter($uid))
  53. ));
  54. return $this->findEntity($qb);
  55. }
  56. /**
  57. * @throws \OCP\DB\Exception
  58. */
  59. public function deleteByUid(string $uid) {
  60. $qb = $this->db->getQueryBuilder();
  61. $qb->delete($this->getTableName())
  62. ->where(
  63. $qb->expr()->eq('uid', $qb->createNamedParameter($uid))
  64. );
  65. $qb->executeStatement();
  66. }
  67. }