Skip to content

Commit 7b37c75

Browse files
abunchchalasr
authored andcommitted
Prevent FormLoginAuthenticator from responding to requests that should be handled by JsonLoginAuthenticator
1 parent 9a2cabe commit 7b37c75

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

Authenticator/FormLoginAuthenticator.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public function __construct(HttpUtils $httpUtils, UserProviderInterface $userPro
6060
'password_parameter' => '_password',
6161
'check_path' => '/login_check',
6262
'post_only' => true,
63+
'form_only' => false,
6364
'enable_csrf' => false,
6465
'csrf_parameter' => '_csrf_token',
6566
'csrf_token_id' => 'authenticate',
@@ -74,7 +75,8 @@ protected function getLoginUrl(Request $request): string
7475
public function supports(Request $request): bool
7576
{
7677
return ($this->options['post_only'] ? $request->isMethod('POST') : true)
77-
&& $this->httpUtils->checkRequestPath($request, $this->options['check_path']);
78+
&& $this->httpUtils->checkRequestPath($request, $this->options['check_path'])
79+
&& ($this->options['form_only'] ? 'form' === $request->getContentType() : true);
7880
}
7981

8082
public function authenticate(Request $request): Passport

Tests/Authenticator/FormLoginAuthenticatorTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,27 @@ public function testUpgradePassword()
156156
$this->assertEquals('s$cr$t', $badge->getAndErasePlaintextPassword());
157157
}
158158

159+
/**
160+
* @dataProvider provideContentTypes()
161+
*/
162+
public function testSupportsFormOnly(string $contentType, bool $shouldSupport)
163+
{
164+
$request = new Request();
165+
$request->headers->set('CONTENT_TYPE', $contentType);
166+
$request->server->set('REQUEST_URI', '/login_check');
167+
$request->setMethod('POST');
168+
169+
$this->setUpAuthenticator(['form_only' => true]);
170+
171+
$this->assertSame($shouldSupport, $this->authenticator->supports($request));
172+
}
173+
174+
public function provideContentTypes()
175+
{
176+
yield ['application/json', false];
177+
yield ['application/x-www-form-urlencoded', true];
178+
}
179+
159180
private function setUpAuthenticator(array $options = [])
160181
{
161182
$this->authenticator = new FormLoginAuthenticator(new HttpUtils(), $this->userProvider, $this->successHandler, $this->failureHandler, $options);

0 commit comments

Comments
 (0)