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.

MemoryCacheBackend.php 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2023 Joas Schilling <coding@schilljs.com>
  5. * @copyright Copyright (c) 2017 Lukas Reschke <lukas@statuscode.ch>
  6. *
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Lukas Reschke <lukas@statuscode.ch>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. * @author Roeland Jago Douma <roeland@famdouma.nl>
  12. *
  13. * @license GNU AGPL version 3 or any later version
  14. *
  15. * This program is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License as
  17. * published by the Free Software Foundation, either version 3 of the
  18. * License, or (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  27. *
  28. */
  29. namespace OC\Security\RateLimiting\Backend;
  30. use OCP\AppFramework\Utility\ITimeFactory;
  31. use OCP\ICache;
  32. use OCP\ICacheFactory;
  33. use OCP\IConfig;
  34. /**
  35. * Class MemoryCacheBackend uses the configured distributed memory cache for storing
  36. * rate limiting data.
  37. *
  38. * @package OC\Security\RateLimiting\Backend
  39. */
  40. class MemoryCacheBackend implements IBackend {
  41. private ICache $cache;
  42. public function __construct(
  43. private IConfig $config,
  44. ICacheFactory $cacheFactory,
  45. private ITimeFactory $timeFactory,
  46. ) {
  47. $this->cache = $cacheFactory->createDistributed(__CLASS__);
  48. }
  49. private function hash(
  50. string $methodIdentifier,
  51. string $userIdentifier,
  52. ): string {
  53. return hash('sha512', $methodIdentifier . $userIdentifier);
  54. }
  55. private function getExistingAttempts(string $identifier): array {
  56. $cachedAttempts = $this->cache->get($identifier);
  57. if ($cachedAttempts === null) {
  58. return [];
  59. }
  60. $cachedAttempts = json_decode($cachedAttempts, true);
  61. if (\is_array($cachedAttempts)) {
  62. return $cachedAttempts;
  63. }
  64. return [];
  65. }
  66. /**
  67. * {@inheritDoc}
  68. */
  69. public function getAttempts(
  70. string $methodIdentifier,
  71. string $userIdentifier,
  72. ): int {
  73. $identifier = $this->hash($methodIdentifier, $userIdentifier);
  74. $existingAttempts = $this->getExistingAttempts($identifier);
  75. $count = 0;
  76. $currentTime = $this->timeFactory->getTime();
  77. foreach ($existingAttempts as $expirationTime) {
  78. if ($expirationTime > $currentTime) {
  79. $count++;
  80. }
  81. }
  82. return $count;
  83. }
  84. /**
  85. * {@inheritDoc}
  86. */
  87. public function registerAttempt(
  88. string $methodIdentifier,
  89. string $userIdentifier,
  90. int $period,
  91. ): void {
  92. $identifier = $this->hash($methodIdentifier, $userIdentifier);
  93. $existingAttempts = $this->getExistingAttempts($identifier);
  94. $currentTime = $this->timeFactory->getTime();
  95. // Unset all attempts that are already expired
  96. foreach ($existingAttempts as $key => $expirationTime) {
  97. if ($expirationTime < $currentTime) {
  98. unset($existingAttempts[$key]);
  99. }
  100. }
  101. $existingAttempts = array_values($existingAttempts);
  102. // Store the new attempt
  103. $existingAttempts[] = (string)($currentTime + $period);
  104. if (!$this->config->getSystemValueBool('ratelimit.protection.enabled', true)) {
  105. return;
  106. }
  107. $this->cache->set($identifier, json_encode($existingAttempts));
  108. }
  109. }