Limiter.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. use OCP\Security\RateLimiting\ILimiter;
  31. class Limiter implements ILimiter {
  32. public function __construct(
  33. private IBackend $backend,
  34. ) {
  35. }
  36. /**
  37. * @param int $period in seconds
  38. * @throws RateLimitExceededException
  39. */
  40. private function register(
  41. string $methodIdentifier,
  42. string $userIdentifier,
  43. int $period,
  44. int $limit,
  45. ): void {
  46. $existingAttempts = $this->backend->getAttempts($methodIdentifier, $userIdentifier);
  47. if ($existingAttempts >= $limit) {
  48. throw new RateLimitExceededException();
  49. }
  50. $this->backend->registerAttempt($methodIdentifier, $userIdentifier, $period);
  51. }
  52. /**
  53. * Registers attempt for an anonymous request
  54. *
  55. * @param int $anonPeriod in seconds
  56. * @throws RateLimitExceededException
  57. */
  58. public function registerAnonRequest(
  59. string $identifier,
  60. int $anonLimit,
  61. int $anonPeriod,
  62. string $ip,
  63. ): void {
  64. $ipSubnet = (new IpAddress($ip))->getSubnet();
  65. $anonHashIdentifier = hash('sha512', 'anon::' . $identifier . $ipSubnet);
  66. $this->register($identifier, $anonHashIdentifier, $anonPeriod, $anonLimit);
  67. }
  68. /**
  69. * Registers attempt for an authenticated request
  70. *
  71. * @param int $userPeriod in seconds
  72. * @throws RateLimitExceededException
  73. */
  74. public function registerUserRequest(
  75. string $identifier,
  76. int $userLimit,
  77. int $userPeriod,
  78. IUser $user,
  79. ): void {
  80. $userHashIdentifier = hash('sha512', 'user::' . $identifier . $user->getUID());
  81. $this->register($identifier, $userHashIdentifier, $userPeriod, $userLimit);
  82. }
  83. }