Skip to content

Commit 120fc17

Browse files
Merge branch '5.2' into 5.x
* 5.2: [Inflector][String] wrong plural form of words ending by "pectus" [HttpClient] Don't prepare the request in ScopingHttpClient [Console] Fixes for PHP 8.1 deprecations Make LoginRateLimiter case insentive Fix/Rewrite .gitignore regex builder Reset limiters on successful login Provide count argument for TooManyLoginAttemptsAuthenticationException to be able to translate in plural way [security] NullToken signature
2 parents 1c477c6 + eb86bcf commit 120fc17

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
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]>
@@ -49,10 +50,16 @@ public function checkPassport(CheckPassportEvent $event): void
4950
}
5051
}
5152

53+
public function onSuccessfulLogin(LoginSuccessEvent $event): void
54+
{
55+
$this->limiter->reset($event->getRequest());
56+
}
57+
5258
public static function getSubscribedEvents(): array
5359
{
5460
return [
5561
CheckPassportEvent::class => ['checkPassport', 2080],
62+
LoginSuccessEvent::class => 'onSuccessfulLogin',
5663
];
5764
}
5865
}

RateLimiter/DefaultLoginRateLimiter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ protected function getLimiters(Request $request): array
3939
{
4040
return [
4141
$this->globalFactory->create($request->getClientIp()),
42-
$this->localFactory->create($request->attributes->get(Security::LAST_USERNAME).'-'.$request->getClientIp()),
42+
$this->localFactory->create(strtolower($request->attributes->get(Security::LAST_USERNAME)).'-'.$request->getClientIp()),
4343
];
4444
}
4545
}

Tests/EventListener/LoginThrottlingListenerTest.php

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,31 @@ 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
}
6975

76+
public function testPreventsLoginWithMultipleCase()
77+
{
78+
$request = $this->createRequest();
79+
$passports = [$this->createPassport('wouter'), $this->createPassport('Wouter'), $this->createPassport('wOuter')];
80+
81+
$this->requestStack->push($request);
82+
83+
for ($i = 0; $i < 3; ++$i) {
84+
$this->listener->checkPassport($this->createCheckPassportEvent($passports[$i % 3]));
85+
}
86+
87+
$this->expectException(TooManyLoginAttemptsAuthenticationException::class);
88+
$this->listener->checkPassport($this->createCheckPassportEvent($passports[0]));
89+
}
90+
7091
public function testPreventsLoginWhenOverGlobalThreshold()
7192
{
7293
$request = $this->createRequest();
@@ -87,12 +108,9 @@ private function createPassport($username)
87108
return new SelfValidatingPassport(new UserBadge($username));
88109
}
89110

90-
private function createLoginSuccessfulEvent($passport, $username = 'wouter')
111+
private function createLoginSuccessfulEvent($passport)
91112
{
92-
$token = $this->createMock(TokenInterface::class);
93-
$token->expects($this->any())->method('getUserIdentifier')->willReturn($username);
94-
95-
return new LoginSuccessEvent($this->createMock(AuthenticatorInterface::class), $passport, $token, $this->requestStack->getCurrentRequest(), null, 'main');
113+
return new LoginSuccessEvent($this->createMock(AuthenticatorInterface::class), $passport, $this->createMock(TokenInterface::class), $this->requestStack->getCurrentRequest(), null, 'main');
96114
}
97115

98116
private function createCheckPassportEvent($passport)

0 commit comments

Comments
 (0)