Skip to content

Commit

Permalink
Reset limiters on successful login
Browse files Browse the repository at this point in the history
  • Loading branch information
MatTheCat committed May 8, 2021
1 parent 3aebf0e commit a8b5ba8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
7 changes: 7 additions & 0 deletions EventListener/LoginThrottlingListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
use Symfony\Component\Security\Http\Event\CheckPassportEvent;
use Symfony\Component\Security\Http\Event\LoginSuccessEvent;

/**
* @author Wouter de Jong <wouter@wouterj.nl>
Expand Down Expand Up @@ -51,10 +52,16 @@ public function checkPassport(CheckPassportEvent $event): void
}
}

public function onSuccessfulLogin(LoginSuccessEvent $event): void
{
$this->limiter->reset($event->getRequest());
}

public static function getSubscribedEvents(): array
{
return [
CheckPassportEvent::class => ['checkPassport', 2080],
LoginSuccessEvent::class => 'onSuccessfulLogin',
];
}
}
13 changes: 8 additions & 5 deletions Tests/EventListener/LoginThrottlingListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ public function testPreventsLoginWhenOverLocalThreshold()
$this->listener->checkPassport($this->createCheckPassportEvent($passport));
}

$this->listener->onSuccessfulLogin($this->createLoginSuccessfulEvent($passport));

for ($i = 0; $i < 3; ++$i) {
$this->listener->checkPassport($this->createCheckPassportEvent($passport));
}

$this->expectException(TooManyLoginAttemptsAuthenticationException::class);
$this->listener->checkPassport($this->createCheckPassportEvent($passport));
}
Expand All @@ -87,12 +93,9 @@ private function createPassport($username)
return new SelfValidatingPassport(new UserBadge($username));
}

private function createLoginSuccessfulEvent($passport, $username = 'wouter')
private function createLoginSuccessfulEvent($passport)
{
$token = $this->createMock(TokenInterface::class);
$token->expects($this->any())->method('getUsername')->willReturn($username);

return new LoginSuccessEvent($this->createMock(AuthenticatorInterface::class), $passport, $token, $this->requestStack->getCurrentRequest(), null, 'main');
return new LoginSuccessEvent($this->createMock(AuthenticatorInterface::class), $passport, $this->createMock(TokenInterface::class), $this->requestStack->getCurrentRequest(), null, 'main');
}

private function createCheckPassportEvent($passport)
Expand Down

0 comments on commit a8b5ba8

Please sign in to comment.