DatabaseBackend.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2021 Lukas Reschke <lukas@statuscode.ch>
  5. *
  6. * @author Lukas Reschke <lukas@statuscode.ch>
  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\Security\RateLimiting\Backend;
  25. use OCP\AppFramework\Utility\ITimeFactory;
  26. use OCP\DB\QueryBuilder\IQueryBuilder;
  27. use OCP\IDBConnection;
  28. class DatabaseBackend implements IBackend {
  29. private const TABLE_NAME = 'ratelimit_entries';
  30. /** @var IDBConnection */
  31. private $dbConnection;
  32. /** @var ITimeFactory */
  33. private $timeFactory;
  34. /**
  35. * @param IDBConnection $dbConnection
  36. * @param ITimeFactory $timeFactory
  37. */
  38. public function __construct(
  39. IDBConnection $dbConnection,
  40. ITimeFactory $timeFactory
  41. ) {
  42. $this->dbConnection = $dbConnection;
  43. $this->timeFactory = $timeFactory;
  44. }
  45. /**
  46. * @param string $methodIdentifier
  47. * @param string $userIdentifier
  48. * @return string
  49. */
  50. private function hash(string $methodIdentifier,
  51. string $userIdentifier): string {
  52. return hash('sha512', $methodIdentifier . $userIdentifier);
  53. }
  54. /**
  55. * @param string $identifier
  56. * @param int $seconds
  57. * @return int
  58. * @throws \OCP\DB\Exception
  59. */
  60. private function getExistingAttemptCount(
  61. string $identifier
  62. ): int {
  63. $currentTime = $this->timeFactory->getDateTime();
  64. $qb = $this->dbConnection->getQueryBuilder();
  65. $qb->delete(self::TABLE_NAME)
  66. ->where(
  67. $qb->expr()->lte('delete_after', $qb->createNamedParameter($currentTime, IQueryBuilder::PARAM_DATE))
  68. )
  69. ->executeStatement();
  70. $qb = $this->dbConnection->getQueryBuilder();
  71. $qb->select($qb->func()->count())
  72. ->from(self::TABLE_NAME)
  73. ->where(
  74. $qb->expr()->eq('hash', $qb->createNamedParameter($identifier, IQueryBuilder::PARAM_STR))
  75. );
  76. $cursor = $qb->executeQuery();
  77. $row = $cursor->fetchOne();
  78. $cursor->closeCursor();
  79. return (int)$row;
  80. }
  81. /**
  82. * {@inheritDoc}
  83. */
  84. public function getAttempts(string $methodIdentifier,
  85. string $userIdentifier): int {
  86. $identifier = $this->hash($methodIdentifier, $userIdentifier);
  87. return $this->getExistingAttemptCount($identifier);
  88. }
  89. /**
  90. * {@inheritDoc}
  91. */
  92. public function registerAttempt(string $methodIdentifier,
  93. string $userIdentifier,
  94. int $period) {
  95. $identifier = $this->hash($methodIdentifier, $userIdentifier);
  96. $deleteAfter = $this->timeFactory->getDateTime()->add(new \DateInterval("PT{$period}S"));
  97. $qb = $this->dbConnection->getQueryBuilder();
  98. $qb->insert(self::TABLE_NAME)
  99. ->values([
  100. 'hash' => $qb->createNamedParameter($identifier, IQueryBuilder::PARAM_STR),
  101. 'delete_after' => $qb->createNamedParameter($deleteAfter, IQueryBuilder::PARAM_DATE),
  102. ])
  103. ->executeStatement();
  104. }
  105. }