PublicKeyCredentialMapper.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 OCP\AppFramework\Db\DoesNotExistException;
  27. use OCP\AppFramework\Db\QBMapper;
  28. use OCP\IDBConnection;
  29. /**
  30. * @template-extends QBMapper<PublicKeyCredentialEntity>
  31. */
  32. class PublicKeyCredentialMapper extends QBMapper {
  33. public function __construct(IDBConnection $db) {
  34. parent::__construct($db, 'webauthn', PublicKeyCredentialEntity::class);
  35. }
  36. public function findOneByCredentialId(string $publicKeyCredentialId): PublicKeyCredentialEntity {
  37. $qb = $this->db->getQueryBuilder();
  38. $qb->select('*')
  39. ->from($this->getTableName())
  40. ->where(
  41. $qb->expr()->eq('public_key_credential_id', $qb->createNamedParameter(base64_encode($publicKeyCredentialId)))
  42. );
  43. return $this->findEntity($qb);
  44. }
  45. /**
  46. * @return PublicKeyCredentialEntity[]
  47. */
  48. public function findAllForUid(string $uid): array {
  49. $qb = $this->db->getQueryBuilder();
  50. $qb->select('*')
  51. ->from($this->getTableName())
  52. ->where(
  53. $qb->expr()->eq('uid', $qb->createNamedParameter($uid))
  54. );
  55. return $this->findEntities($qb);
  56. }
  57. /**
  58. * @param string $uid
  59. * @param int $id
  60. *
  61. * @return PublicKeyCredentialEntity
  62. * @throws DoesNotExistException
  63. */
  64. public function findById(string $uid, int $id): PublicKeyCredentialEntity {
  65. $qb = $this->db->getQueryBuilder();
  66. $qb->select('*')
  67. ->from($this->getTableName())
  68. ->where($qb->expr()->andX(
  69. $qb->expr()->eq('id', $qb->createNamedParameter($id)),
  70. $qb->expr()->eq('uid', $qb->createNamedParameter($uid))
  71. ));
  72. return $this->findEntity($qb);
  73. }
  74. /**
  75. * @throws \OCP\DB\Exception
  76. */
  77. public function deleteByUid(string $uid) {
  78. $qb = $this->db->getQueryBuilder();
  79. $qb->delete($this->getTableName())
  80. ->where(
  81. $qb->expr()->eq('uid', $qb->createNamedParameter($uid))
  82. );
  83. $qb->executeStatement();
  84. }
  85. }