lib/composer/UserBundle/src/VisualMedia/UserBundle/EventSubscriber/SecuritySubscriber.php line 104

Open in your IDE?
  1. <?php
  2. /**
  3. * Security Subscriber
  4. *
  5. * @author Gideon Oudhuis <g.oudhuis@visualmedia.nl>
  6. */
  7. namespace VisualMedia\UserBundle\EventSubscriber;
  8. use Symfony\Component\Cache\Adapter\AdapterInterface;
  9. use Symfony\Component\HttpFoundation\RequestStack;
  10. use Symfony\Component\Security\Core\Event\AuthenticationFailureEvent;
  11. use Symfony\Component\Security\Core\AuthenticationEvents;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  14. use Symfony\Component\Security\Http\SecurityEvents;
  15. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  16. /**
  17. * Security Subscriber
  18. *
  19. * Subscriber to monitor the failed login attempts.
  20. *
  21. * @deprecated since bugfix/ssi-login-validation, system using cache login block attemps build into
  22. * BaseLoginFormAuthenticator method onAuthenticationFailure => bruteForceSecurity
  23. */
  24. class SecuritySubscriber implements EventSubscriberInterface
  25. {
  26. /**
  27. * Cache
  28. * @var AdapterInterface
  29. */
  30. protected $cache;
  31. /**
  32. * Request Stack
  33. * @var RequestStack
  34. */
  35. protected $requestStack;
  36. /**
  37. * Constructor
  38. *
  39. * @param AdapterInterface $cache
  40. * @param RequestStack $requestStack
  41. */
  42. public function __construct(AdapterInterface $cache, RequestStack $requestStack)
  43. {
  44. $this->cache = $cache;
  45. $this->requestStack = $requestStack;
  46. }
  47. /**
  48. * Get Subscribed Events
  49. *
  50. * getSubscribedEvents Event withs te subscriber listens to.
  51. */
  52. public static function getSubscribedEvents(): array
  53. {
  54. return array(
  55. AuthenticationEvents::AUTHENTICATION_FAILURE => 'onAuthenticationFailure',
  56. SecurityEvents::INTERACTIVE_LOGIN => 'onSecurityInteractiveLogin',
  57. );
  58. }
  59. /**
  60. * On Security Interactive login
  61. *
  62. * onSecurityInteractiveLogin register if login user has to many login _attemps.
  63. *
  64. * @param InteractiveLoginEvent $event
  65. *
  66. * @throws \Psr\Cache\InvalidArgumentException
  67. */
  68. public function onSecurityInteractiveLogin(InteractiveLoginEvent $event): void
  69. {
  70. $request = $this->requestStack->getCurrentRequest();
  71. $count = 0;
  72. $route = $request->get('_route');
  73. $ip = $request->getClientIp();
  74. $cacheitem = $this->cache->getItem(sprintf('%s_%s_attemps', $route, $ip));
  75. $cache_count = $cacheitem->get();
  76. if ($cache_count['count'] ?? null) {
  77. $count = $cache_count['count'];
  78. }
  79. if ($count >= 1){
  80. throw new AccessDeniedException('Login has been blocked');
  81. }
  82. }
  83. /**
  84. * On Authentication Failure
  85. *
  86. * onAuthenticationFailure event that trigger if a users authentication fails.
  87. *
  88. * @param AuthenticationFailureEvent $event
  89. *
  90. * @throws \Psr\Cache\InvalidArgumentException
  91. */
  92. public function onAuthenticationFailure(AuthenticationFailureEvent $event): void
  93. {
  94. $request = $this->requestStack->getCurrentRequest();
  95. $route = $request->get('_route');
  96. $ip = $request->getClientIp();
  97. $cacheitem = $this->cache->getItem(sprintf('%s_%s_attemps', $route, $ip));
  98. $cacheitem->expiresAfter(300);
  99. $cache_count = $cacheitem->get();
  100. $count = $cache_count[$ip] ?? null;
  101. if ($count === null) {
  102. $count = 0;
  103. }
  104. $count++;
  105. $cacheitem->set(array(
  106. $this->requestStack->getCurrentRequest()->getClientIp() => $count,
  107. ));
  108. $this->cache->save($cacheitem);
  109. }
  110. }