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.

MemoryCache.php 3.5KB

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