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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 MemoryCacheBackend uses the configured distributed memory cache for storing
  33. * rate limiting data.
  34. *
  35. * @package OC\Security\RateLimiting\Backend
  36. */
  37. class MemoryCacheBackend 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): int {
  80. $identifier = $this->hash($methodIdentifier, $userIdentifier);
  81. $existingAttempts = $this->getExistingAttempts($identifier);
  82. $count = 0;
  83. $currentTime = $this->timeFactory->getTime();
  84. foreach ($existingAttempts as $expirationTime) {
  85. if ($expirationTime > $currentTime) {
  86. $count++;
  87. }
  88. }
  89. return $count;
  90. }
  91. /**
  92. * {@inheritDoc}
  93. */
  94. public function registerAttempt(string $methodIdentifier,
  95. string $userIdentifier,
  96. int $period) {
  97. $identifier = $this->hash($methodIdentifier, $userIdentifier);
  98. $existingAttempts = $this->getExistingAttempts($identifier);
  99. $currentTime = $this->timeFactory->getTime();
  100. // Unset all attempts that are already expired
  101. foreach ($existingAttempts as $key => $expirationTime) {
  102. if ($expirationTime < $currentTime) {
  103. unset($existingAttempts[$key]);
  104. }
  105. }
  106. $existingAttempts = array_values($existingAttempts);
  107. // Store the new attempt
  108. $existingAttempts[] = (string)($currentTime + $period);
  109. $this->cache->set($identifier, json_encode($existingAttempts));
  110. }
  111. }