Skip to content

Commit a8b5ba8

Browse files
committed
Reset limiters on successful login
1 parent 3aebf0e commit a8b5ba8

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

EventListener/LoginThrottlingListener.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\Security\Core\Security;
1919
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
2020
use Symfony\Component\Security\Http\Event\CheckPassportEvent;
21+
use Symfony\Component\Security\Http\Event\LoginSuccessEvent;
2122

2223
/**
2324
* @author Wouter de Jong <[email protected]>
@@ -51,10 +52,16 @@ public function checkPassport(CheckPassportEvent $event): void
5152
}
5253
}
5354

55+
public function onSuccessfulLogin(LoginSuccessEvent $event): void
56+
{
57+
$this->limiter->reset($event->getRequest());
58+
}
59+
5460
public static function getSubscribedEvents(): array
5561
{
5662
return [
5763
CheckPassportEvent::class => ['checkPassport', 2080],
64+
LoginSuccessEvent::class => 'onSuccessfulLogin',
5865
];
5966
}
6067
}

Tests/EventListener/LoginThrottlingListenerTest.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ public function testPreventsLoginWhenOverLocalThreshold()
6363
$this->listener->checkPassport($this->createCheckPassportEvent($passport));
6464
}
6565

66+
$this->listener->onSuccessfulLogin($this->createLoginSuccessfulEvent($passport));
67+
68+
for ($i = 0; $i < 3; ++$i) {
69+
$this->listener->checkPassport($this->createCheckPassportEvent($passport));
70+
}
71+
6672
$this->expectException(TooManyLoginAttemptsAuthenticationException::class);
6773
$this->listener->checkPassport($this->createCheckPassportEvent($passport));
6874
}
@@ -87,12 +93,9 @@ private function createPassport($username)
8793
return new SelfValidatingPassport(new UserBadge($username));
8894
}
8995

90-
private function createLoginSuccessfulEvent($passport, $username = 'wouter')
96+
private function createLoginSuccessfulEvent($passport)
9197
{
92-
$token = $this->createMock(TokenInterface::class);
93-
$token->expects($this->any())->method('getUsername')->willReturn($username);
94-
95-
return new LoginSuccessEvent($this->createMock(AuthenticatorInterface::class), $passport, $token, $this->requestStack->getCurrentRequest(), null, 'main');
98+
return new LoginSuccessEvent($this->createMock(AuthenticatorInterface::class), $passport, $this->createMock(TokenInterface::class), $this->requestStack->getCurrentRequest(), null, 'main');
9699
}
97100

98101
private function createCheckPassportEvent($passport)

0 commit comments

Comments
 (0)