vendor/symfony/security-http/EventListener/LoginThrottlingListener.php line 49

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\Security\Http\EventListener;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\HttpFoundation\RateLimiter\RequestRateLimiterInterface;
  13. use Symfony\Component\HttpFoundation\RequestStack;
  14. use Symfony\Component\Security\Core\Exception\TooManyLoginAttemptsAuthenticationException;
  15. use Symfony\Component\Security\Core\Security;
  16. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
  17. use Symfony\Component\Security\Http\Event\CheckPassportEvent;
  18. use Symfony\Component\Security\Http\Event\LoginSuccessEvent;
  19. /**
  20.  * @author Wouter de Jong <wouter@wouterj.nl>
  21.  */
  22. final class LoginThrottlingListener implements EventSubscriberInterface
  23. {
  24.     private RequestStack $requestStack;
  25.     private RequestRateLimiterInterface $limiter;
  26.     public function __construct(RequestStack $requestStackRequestRateLimiterInterface $limiter)
  27.     {
  28.         $this->requestStack $requestStack;
  29.         $this->limiter $limiter;
  30.     }
  31.     public function checkPassport(CheckPassportEvent $event): void
  32.     {
  33.         $passport $event->getPassport();
  34.         if (!$passport->hasBadge(UserBadge::class)) {
  35.             return;
  36.         }
  37.         $request $this->requestStack->getMainRequest();
  38.         $request->attributes->set(Security::LAST_USERNAME$passport->getBadge(UserBadge::class)->getUserIdentifier());
  39.         $limit $this->limiter->consume($request);
  40.         if (!$limit->isAccepted()) {
  41.             throw new TooManyLoginAttemptsAuthenticationException(ceil(($limit->getRetryAfter()->getTimestamp() - time()) / 60));
  42.         }
  43.     }
  44.     public function onSuccessfulLogin(LoginSuccessEvent $event): void
  45.     {
  46.         $this->limiter->reset($event->getRequest());
  47.     }
  48.     public static function getSubscribedEvents(): array
  49.     {
  50.         return [
  51.             CheckPassportEvent::class => ['checkPassport'2080],
  52.             LoginSuccessEvent::class => 'onSuccessfulLogin',
  53.         ];
  54.     }
  55. }