Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5ba4925

Browse files
committed
bug #39859 [Security] Replace message data in JSON security error response (wouterj)
This PR was merged into the 5.2 branch. Discussion ---------- [Security] Replace message data in JSON security error response | Q | A | ------------- | --- | Branch? | 5.2 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #39663 | License | MIT | Doc PR | n/a Commits ------- 5e5795a [Security] Replace message data in JSON security error response
2 parents 8fc46dc + 5e5795a commit 5ba4925

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/Symfony/Component/Security/Http/Authenticator/JsonLoginAuthenticator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,10 @@ public function onAuthenticationSuccess(Request $request, TokenInterface $token,
126126
public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response
127127
{
128128
if (null === $this->failureHandler) {
129-
$errorMessage = $exception->getMessageKey();
130-
131129
if (null !== $this->translator) {
132130
$errorMessage = $this->translator->trans($exception->getMessageKey(), $exception->getMessageData(), 'security');
131+
} else {
132+
$errorMessage = strtr($exception->getMessageKey(), $exception->getMessageData());
133133
}
134134

135135
return new JsonResponse(['error' => $errorMessage], JsonResponse::HTTP_UNAUTHORIZED);

src/Symfony/Component/Security/Http/Tests/Authenticator/JsonLoginAuthenticatorTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,25 @@ public function testAuthenticationFailureWithTranslator()
147147
$this->assertSame(['error' => 'foo'], json_decode($response->getContent(), true));
148148
}
149149

150+
public function testOnFailureReplacesMessageDataWithoutTranslator()
151+
{
152+
$this->setUpAuthenticator();
153+
154+
$response = $this->authenticator->onAuthenticationFailure(new Request(), new class() extends AuthenticationException {
155+
public function getMessageData(): array
156+
{
157+
return ['%failed_attempts%' => 3];
158+
}
159+
160+
public function getMessageKey(): string
161+
{
162+
return 'Session locked after %failed_attempts% failed attempts.';
163+
}
164+
});
165+
166+
$this->assertSame(['error' => 'Session locked after 3 failed attempts.'], json_decode($response->getContent(), true));
167+
}
168+
150169
private function setUpAuthenticator(array $options = [])
151170
{
152171
$this->authenticator = new JsonLoginAuthenticator(new HttpUtils(), $this->userProvider, null, null, $options);

0 commit comments

Comments
 (0)