SignatoryMapper.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Security\Signature\Db;
  8. use NCU\Security\Signature\Exceptions\SignatoryNotFoundException;
  9. use NCU\Security\Signature\Model\Signatory;
  10. use OCP\AppFramework\Db\DoesNotExistException;
  11. use OCP\AppFramework\Db\QBMapper;
  12. use OCP\DB\Exception;
  13. use OCP\IDBConnection;
  14. /**
  15. * @template-extends QBMapper<Signatory>
  16. */
  17. class SignatoryMapper extends QBMapper {
  18. public const TABLE = 'sec_signatory';
  19. public function __construct(
  20. IDBConnection $db,
  21. ) {
  22. parent::__construct($db, self::TABLE, Signatory::class);
  23. }
  24. /**
  25. *
  26. */
  27. public function getByHost(string $host, string $account = ''): Signatory {
  28. $qb = $this->db->getQueryBuilder();
  29. $qb->select('*')
  30. ->from($this->getTableName())
  31. ->where($qb->expr()->eq('host', $qb->createNamedParameter($host)))
  32. ->andWhere($qb->expr()->eq('account', $qb->createNamedParameter($account)));
  33. try {
  34. return $this->findEntity($qb);
  35. } catch (DoesNotExistException) {
  36. throw new SignatoryNotFoundException('no signatory found');
  37. }
  38. }
  39. /**
  40. */
  41. public function getByKeyId(string $keyId): Signatory {
  42. $qb = $this->db->getQueryBuilder();
  43. $qb->select('*')
  44. ->from($this->getTableName())
  45. ->where($qb->expr()->eq('key_id_sum', $qb->createNamedParameter($this->hashKeyId($keyId))));
  46. try {
  47. return $this->findEntity($qb);
  48. } catch (DoesNotExistException) {
  49. throw new SignatoryNotFoundException('no signatory found');
  50. }
  51. }
  52. /**
  53. * @param string $keyId
  54. *
  55. * @return int
  56. * @throws Exception
  57. */
  58. public function deleteByKeyId(string $keyId): int {
  59. $qb = $this->db->getQueryBuilder();
  60. $qb->delete($this->getTableName())
  61. ->where($qb->expr()->eq('key_id_sum', $qb->createNamedParameter($this->hashKeyId($keyId))));
  62. return $qb->executeStatement();
  63. }
  64. /**
  65. * @param Signatory $signatory
  66. *
  67. * @return int
  68. */
  69. public function updateMetadata(Signatory $signatory): int {
  70. $qb = $this->db->getQueryBuilder();
  71. $qb->update($this->getTableName())
  72. ->set('metadata', $qb->createNamedParameter(json_encode($signatory->getMetadata())))
  73. ->set('last_updated', $qb->createNamedParameter(time()));
  74. $qb->where($qb->expr()->eq('key_id_sum', $qb->createNamedParameter($this->hashKeyId($signatory->getKeyId()))));
  75. return $qb->executeStatement();
  76. }
  77. /**
  78. * @param Signatory $signator
  79. */
  80. public function updatePublicKey(Signatory $signatory): int {
  81. $qb = $this->db->getQueryBuilder();
  82. $qb->update($this->getTableName())
  83. ->set('signatory', $qb->createNamedParameter($signatory->getPublicKey()))
  84. ->set('last_updated', $qb->createNamedParameter(time()));
  85. $qb->where($qb->expr()->eq('key_id_sum', $qb->createNamedParameter($this->hashKeyId($signatory->getKeyId()))));
  86. return $qb->executeStatement();
  87. }
  88. /**
  89. * returns a hash version for keyId for better index in the database
  90. *
  91. * @param string $keyId
  92. *
  93. * @return string
  94. */
  95. private function hashKeyId(string $keyId): string {
  96. return hash('sha256', $keyId);
  97. }
  98. }