You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DatabaseBackend.php 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. ->andWhere(
  77. $qb->expr()->gte('delete_after', $qb->createNamedParameter($currentTime, IQueryBuilder::PARAM_DATE))
  78. );
  79. $cursor = $qb->executeQuery();
  80. $row = $cursor->fetchOne();
  81. $cursor->closeCursor();
  82. return (int)$row;
  83. }
  84. /**
  85. * {@inheritDoc}
  86. */
  87. public function getAttempts(string $methodIdentifier,
  88. string $userIdentifier): int {
  89. $identifier = $this->hash($methodIdentifier, $userIdentifier);
  90. return $this->getExistingAttemptCount($identifier);
  91. }
  92. /**
  93. * {@inheritDoc}
  94. */
  95. public function registerAttempt(string $methodIdentifier,
  96. string $userIdentifier,
  97. int $period) {
  98. $identifier = $this->hash($methodIdentifier, $userIdentifier);
  99. $deleteAfter = $this->timeFactory->getDateTime()->add(new \DateInterval("PT{$period}S"));
  100. $qb = $this->dbConnection->getQueryBuilder();
  101. $qb->insert(self::TABLE_NAME)
  102. ->values([
  103. 'hash' => $qb->createNamedParameter($identifier, IQueryBuilder::PARAM_STR),
  104. 'delete_after' => $qb->createNamedParameter($deleteAfter, IQueryBuilder::PARAM_DATE),
  105. ])
  106. ->executeStatement();
  107. }
  108. }