feat: add Retry-After header if needed

This commit is contained in:
Maël Gangloff
2024-08-05 22:05:32 +02:00
parent 4e6649fc68
commit 686d39da62
3 changed files with 33 additions and 10 deletions

View File

@@ -56,12 +56,16 @@ class RegistrationController extends AbstractController
$limiter = $this->userRegisterLimiter->create($request->getClientIp());
if (false === $this->kernel->isDebug() && false === $limiter->consume()->isAccepted()) {
$this->logger->warning('IP address {ip} was rate limited by the Registration API.', [
'ip' => $request->getClientIp(),
]);
if (false === $this->kernel->isDebug()) {
$limit = $limiter->consume();
throw new TooManyRequestsHttpException();
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());
}
}
$user = $this->serializer->deserialize($request->getContent(), User::class, 'json', ['groups' => 'user:register']);