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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. /**
  29. * Class DatabaseBackend uses the database for storing rate limiting data.
  30. *
  31. * @package OC\Security\RateLimiting\Backend
  32. */
  33. class DatabaseBackend implements IBackend {
  34. private const TABLE_NAME = 'ratelimit_entries';
  35. /** @var IDBConnection */
  36. private $dbConnection;
  37. /** @var ITimeFactory */
  38. private $timeFactory;
  39. /**
  40. * @param IDBConnection $dbConnection
  41. * @param ITimeFactory $timeFactory
  42. */
  43. public function __construct(
  44. IDBConnection $dbConnection,
  45. ITimeFactory $timeFactory
  46. ) {
  47. $this->dbConnection = $dbConnection;
  48. $this->timeFactory = $timeFactory;
  49. }
  50. /**
  51. * @param string $methodIdentifier
  52. * @param string $userIdentifier
  53. * @return string
  54. */
  55. private function hash(string $methodIdentifier,
  56. string $userIdentifier): string {
  57. return hash('sha512', $methodIdentifier . $userIdentifier);
  58. }
  59. /**
  60. * @param string $identifier
  61. * @param int $seconds
  62. * @return int
  63. * @throws \OCP\DB\Exception
  64. */
  65. private function getExistingAttemptCount(
  66. string $identifier,
  67. int $seconds
  68. ): int {
  69. $qb = $this->dbConnection->getQueryBuilder();
  70. $notOlderThan = $this->timeFactory->getDateTime()->sub(new \DateInterval("PT{$seconds}S"));
  71. $qb->selectAlias($qb->createFunction('COUNT(*)'), '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('timestamp', $qb->createParameter('notOlderThan'))
  78. )
  79. ->setParameter('notOlderThan', $notOlderThan, 'datetime');
  80. $cursor = $qb->executeQuery();
  81. $row = $cursor->fetch();
  82. $cursor->closeCursor();
  83. return (int)$row['count'];
  84. }
  85. /**
  86. * {@inheritDoc}
  87. */
  88. public function getAttempts(string $methodIdentifier,
  89. string $userIdentifier,
  90. int $seconds): int {
  91. $identifier = $this->hash($methodIdentifier, $userIdentifier);
  92. return $this->getExistingAttemptCount($identifier, $seconds);
  93. }
  94. /**
  95. * {@inheritDoc}
  96. */
  97. public function registerAttempt(string $methodIdentifier,
  98. string $userIdentifier,
  99. int $period) {
  100. $identifier = $this->hash($methodIdentifier, $userIdentifier);
  101. $currentTime = $this->timeFactory->getDateTime();
  102. $notOlderThan = $this->timeFactory->getDateTime('@' . $period);
  103. $qb = $this->dbConnection->getQueryBuilder();
  104. $qb->delete(self::TABLE_NAME)
  105. ->where(
  106. $qb->expr()->eq('hash', $qb->createNamedParameter($identifier, IQueryBuilder::PARAM_STR))
  107. )
  108. ->andWhere(
  109. $qb->expr()->lt('timestamp', $qb->createParameter('notOlderThan'))
  110. )
  111. ->setParameter('notOlderThan', $notOlderThan, 'datetime')
  112. ->executeStatement();
  113. $qb->insert(self::TABLE_NAME)
  114. ->values([
  115. 'hash' => $qb->createNamedParameter($identifier, IQueryBuilder::PARAM_STR),
  116. 'timestamp' => $qb->createNamedParameter($currentTime, IQueryBuilder::PARAM_DATE),
  117. ])
  118. ->executeStatement();
  119. }
  120. }