feat: add rate limit : a user can update 25 domains per day

This commit is contained in:
Maël Gangloff
2024-07-21 14:56:10 +02:00
parent b45bbe63f5
commit 43c4c9a33d
8 changed files with 200 additions and 18 deletions

View File

@@ -8,12 +8,15 @@ use App\Service\RDAPService;
use DateTimeImmutable;
use Exception;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException;
use Symfony\Component\RateLimiter\RateLimiterFactory;
class DomainRefreshController extends AbstractController
{
public function __construct(private readonly DomainRepository $domainRepository,
private readonly RDAPService $RDAPService)
public function __construct(private readonly DomainRepository $domainRepository,
private readonly RDAPService $RDAPService,
private readonly RateLimiterFactory $authenticatedApiLimiter)
{
}
@@ -27,7 +30,11 @@ class DomainRefreshController extends AbstractController
if ($domain === null ||
$domain->getUpdatedAt()->diff(new DateTimeImmutable('now'))->days >= 7) {
//TODO : Domain search rate limit here, before the RDAP request
$limiter = $this->authenticatedApiLimiter->create($this->getUser()->getUserIdentifier());
if (false === $limiter->consume()->isAccepted()) {
throw new TooManyRequestsHttpException();
}
$domain = $this->RDAPService->registerDomain($ldhName);
}
return $domain;