fix: correct rate limiter

This commit is contained in:
Maël Gangloff
2024-08-05 22:26:18 +02:00
parent 686d39da62
commit 5663b1a3b4
2 changed files with 11 additions and 16 deletions

View File

@@ -18,6 +18,7 @@ use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
use Symfony\Component\Mime\Address;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\RateLimiter\Exception\RateLimitExceededException;
use Symfony\Component\RateLimiter\RateLimiterFactory;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Serializer\SerializerInterface;
@@ -54,17 +55,13 @@ class RegistrationController extends AbstractController
throw new UnauthorizedHttpException('', 'Registration is disabled on this instance');
}
$limiter = $this->userRegisterLimiter->create($request->getClientIp());
if (false === $this->kernel->isDebug()) {
$limit = $limiter->consume();
$limiter = $this->userRegisterLimiter->create($request->getClientIp());
if (false === $limit->isAccepted()) {
$this->logger->warning('IP address {ip} was rate limited by the Registration API.', [
'ip' => $request->getClientIp(),
]);
throw new TooManyRequestsHttpException($limit->getRetryAfter()->getTimestamp() - time());
try {
$limiter->consume()->ensureAccepted();
} catch (RateLimitExceededException $e) {
throw new TooManyRequestsHttpException($e->getRetryAfter()->getTimestamp() - time(), $e->getMessage());
}
}