vendor/anyx/login-gate-bundle/Security/AuthenticationHandler.php line 54

Open in your IDE?
  1. <?php
  2. namespace Anyx\LoginGateBundle\Security;
  3. use Anyx\LoginGateBundle\Service\UsernameResolverInterface;
  4. use Anyx\LoginGateBundle\Storage\StorageInterface;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\RequestStack;
  8. use Symfony\Component\Security\Core\AuthenticationEvents;
  9. use Symfony\Component\Security\Core\Event\AuthenticationFailureEvent;
  10. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  11. use Symfony\Component\Security\Http\SecurityEvents;
  12. class AuthenticationHandler implements EventSubscriberInterface
  13. {
  14.     /**
  15.      * @var \Symfony\Component\HttpFoundation\RequestStack
  16.      */
  17.     private $requestStack;
  18.     /**
  19.      * @var \Anyx\LoginGateBundle\Storage\StorageInterface
  20.      */
  21.     private $storage;
  22.     /**
  23.      * @var UsernameResolverInterface|null
  24.      */
  25.     private $usernameProvider;
  26.     public function __construct(RequestStack $requestStackStorageInterface $storageUsernameResolverInterface $usernameProvider null)
  27.     {
  28.         $this->requestStack $requestStack;
  29.         $this->storage $storage;
  30.         $this->usernameProvider $usernameProvider;
  31.     }
  32.     public static function getSubscribedEvents()
  33.     {
  34.         return [
  35.             AuthenticationEvents::AUTHENTICATION_FAILURE => 'onAuthenticationFailure',
  36.             SecurityEvents::INTERACTIVE_LOGIN => 'onInteractiveLogin',
  37.         ];
  38.     }
  39.     public function onAuthenticationFailure(AuthenticationFailureEvent $event)
  40.     {
  41.         $request $this->getRequestStack()->getCurrentRequest();
  42.         $this->getStorage()->incrementCountAttempts($request$this->getUsername($request), $event->getAuthenticationException());
  43.     }
  44.     public function onInteractiveLogin(InteractiveLoginEvent $event)
  45.     {
  46.         $request $this->getRequestStack()->getCurrentRequest();
  47.         $this->getStorage()->clearCountAttempts($request$this->getUsername($request));
  48.     }
  49.     /**
  50.      * @return \Symfony\Component\HttpFoundation\RequestStack
  51.      */
  52.     public function getRequestStack()
  53.     {
  54.         return $this->requestStack;
  55.     }
  56.     /**
  57.      * @return \Anyx\LoginGateBundle\Storage\StorageInterface
  58.      */
  59.     public function getStorage()
  60.     {
  61.         return $this->storage;
  62.     }
  63.     protected function getUsername(Request $request)
  64.     {
  65.         if (!$this->usernameProvider) {
  66.             return null;
  67.         }
  68.         return $this->usernameProvider->resolve($request);
  69.     }
  70. }