Limiter.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2017 Lukas Reschke <lukas@statuscode.ch>
  5. *
  6. * @author Lukas Reschke <lukas@statuscode.ch>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OC\Security\RateLimiting;
  26. use OC\Security\Normalizer\IpAddress;
  27. use OC\Security\RateLimiting\Backend\IBackend;
  28. use OC\Security\RateLimiting\Exception\RateLimitExceededException;
  29. use OCP\IUser;
  30. class Limiter {
  31. public function __construct(
  32. private IBackend $backend,
  33. ) {
  34. }
  35. /**
  36. * @param int $period in seconds
  37. * @throws RateLimitExceededException
  38. */
  39. private function register(
  40. string $methodIdentifier,
  41. string $userIdentifier,
  42. int $period,
  43. int $limit,
  44. ): void {
  45. $existingAttempts = $this->backend->getAttempts($methodIdentifier, $userIdentifier);
  46. if ($existingAttempts >= $limit) {
  47. throw new RateLimitExceededException();
  48. }
  49. $this->backend->registerAttempt($methodIdentifier, $userIdentifier, $period);
  50. }
  51. /**
  52. * Registers attempt for an anonymous request
  53. *
  54. * @param int $anonPeriod in seconds
  55. * @throws RateLimitExceededException
  56. */
  57. public function registerAnonRequest(
  58. string $identifier,
  59. int $anonLimit,
  60. int $anonPeriod,
  61. string $ip,
  62. ): void {
  63. $ipSubnet = (new IpAddress($ip))->getSubnet();
  64. $anonHashIdentifier = hash('sha512', 'anon::' . $identifier . $ipSubnet);
  65. $this->register($identifier, $anonHashIdentifier, $anonPeriod, $anonLimit);
  66. }
  67. /**
  68. * Registers attempt for an authenticated request
  69. *
  70. * @param int $userPeriod in seconds
  71. * @throws RateLimitExceededException
  72. */
  73. public function registerUserRequest(
  74. string $identifier,
  75. int $userLimit,
  76. int $userPeriod,
  77. IUser $user,
  78. ): void {
  79. $userHashIdentifier = hash('sha512', 'user::' . $identifier . $user->getUID());
  80. $this->register($identifier, $userHashIdentifier, $userPeriod, $userLimit);
  81. }
  82. }