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

Skip to content

Commit 13cdedb

Browse files
committed
[Security] Replace message data in JSON security error response
1 parent 8fc46dc commit 13cdedb

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
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/Firewall/UsernamePasswordJsonAuthenticationListener.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,10 @@ private function onFailure(Request $request, AuthenticationException $failed): R
188188
}
189189

190190
if (!$this->failureHandler) {
191-
$errorMessage = $failed->getMessageKey();
192-
193191
if (null !== $this->translator) {
194192
$errorMessage = $this->translator->trans($failed->getMessageKey(), $failed->getMessageData(), 'security');
193+
} else {
194+
$errorMessage = strtr($failed->getMessageKey(), $failed->getMessageData());
195195
}
196196

197197
return new JsonResponse(['error' => $errorMessage], 401);

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
1717
use Symfony\Component\Security\Core\Exception\AuthenticationException;
1818
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
19+
use Symfony\Component\Security\Core\Exception\TooManyLoginAttemptsAuthenticationException;
1920
use Symfony\Component\Security\Core\Security;
2021
use Symfony\Component\Security\Core\User\UserProviderInterface;
2122
use Symfony\Component\Security\Http\Authenticator\JsonLoginAuthenticator;
@@ -147,8 +148,29 @@ public function testAuthenticationFailureWithTranslator()
147148
$this->assertSame(['error' => 'foo'], json_decode($response->getContent(), true));
148149
}
149150

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

0 commit comments

Comments
 (0)