8000 add Request type json check in json_login · symfony/symfony@772ef1d · GitHub
[go: up one dir, main page]

Skip to content

Commit 772ef1d

Browse files
committed
add Request type json check in json_login
1 parent 6c7bced commit 772ef1d

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

src/Symfony/Component/Security/Http/Firewall/UsernamePasswordJsonAuthenticationListener.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ public function __construct(TokenStorageInterface $tokenStorage, AuthenticationM
7373
public function handle(GetResponseEvent $event)
7474
{
7575
$request = $event->getRequest();
76+
if (false !== strpos($request->getRequestFormat(), 'json')
77+
|| false !== strpos($request->getContentType(), 'json')
78+
) {
79+
return;
80+
}
7681

7782
if (isset($this->options['check_path']) && !$this->httpUtils->checkRequestPath($request, $this->options['check_path'])) {
7883
return;

src/Symfony/Component/Security/Http/Tests/Firewall/UsernamePasswordJsonAuthenticationListenerTest.php

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ private function createListener(array $options = array(), $success = true, $matc
6666
public function testHandleSuccess()
6767
{
6868
$this->createListener();
69-
$request = new Request(array(), array(), array(), array(), array(), array(), '{"username": "dunglas", "password": "foo"}');
69+
$request = new Request(array(), array(), array(), array(), array(), array('HTTP_CONTENT_TYPE' => 'application/json'), '{"username": "dunglas", "password": "foo"}');
7070
$event = new GetResponseEvent($this->getMockBuilder(KernelInterface::class)->getMock(), $request, KernelInterface::MASTER_REQUEST);
7171

7272
$this->listener->handle($event);
@@ -76,7 +76,7 @@ public function testHandleSuccess()
7676
public function testHandleFailure()
7777
{
7878
$this->createListener(array(), false);
79-
$request = new Request(array(), array(), array(), array(), array(), array(), '{"username": "dunglas", "password": "foo"}');
79+
$request = new Request(array(), array(), array(), array(), array(), array('HTTP_CONTENT_TYPE' => 'application/json'), '{"username": "dunglas", "password": "foo"}');
8080
$event = new GetResponseEvent($this->getMockBuilder(KernelInterface::class)->getMock(), $request, KernelInterface::MASTER_REQUEST);
8181

8282
$this->listener->handle($event);
@@ -86,7 +86,7 @@ public function testHandleFailure()
8686
public function testUsePath()
8787
{
8888
$this->createListener(array('username_path' => 'user.login', 'password_path' => 'user.pwd'));
89-
$request = new Request(array(), array(), array(), array(), array(), array(), '{"user": {"login": "dunglas", "pwd": "foo"}}');
89+
$request = new Request(array(), array(), array(), array(), array(), array('HTTP_CONTENT_TYPE' => 'application/json'), '{"user": {"login": "dunglas", "pwd": "foo"}}');
9090
$event = new GetResponseEvent($this->getMockBuilder(KernelInterface::class)->getMock(), $request, KernelInterface::MASTER_REQUEST);
9191

9292
$this->listener->handle($event);
@@ -96,7 +96,7 @@ public function testUsePath()
9696
public function testAttemptAuthenticationNoUsername()
9797
{
9898
$this->createListener();
99-
$request = new Request(array(), array(), array(), array(), array(), array(), '{"usr": "dunglas", "password": "foo"}');
99+
$request = new Request(array(), array(), array(), array(), array(), array('HTTP_CONTENT_TYPE' => 'application/json'), '{"usr": "dunglas", "password": "foo"}');
100100
$event = new GetResponseEvent($this->getMockBuilder(KernelInterface::class)->getMock(), $request, KernelInterface::MASTER_REQUEST);
101101

102102
$this->listener->handle($event);
@@ -106,7 +106,7 @@ public function testAttemptAuthenticationNoUsername()
106106
public function testAttemptAuthenticationNoPassword()
107107
{
108108
$this->createListener();
109-
$request = new Request(array(), array(), array(), array(), array(), array(), '{"username": "dunglas", "pass": "foo"}');
109+
$request = new Request(array(), array(), array(), array(), array(), array('HTTP_CONTENT_TYPE' => 'application/json'), '{"username": "dunglas", "pass": "foo"}');
110110
$event = new GetResponseEvent($this->getMockBuilder(KernelInterface::class)->getMock(), $request, KernelInterface::MASTER_REQUEST);
111111

112112
$this->listener->handle($event);
@@ -116,7 +116,7 @@ public function testAttemptAuthenticationNoPassword()
116116
public function testAttemptAuthenticationUsernameNotAString()
117117
{
118118
$this->createListener();
119-
$request = new Request(array(), array(), array(), array(), array(), array(), '{"username": 1, "password": "foo"}');
119+
$request = new Request(array(), array(), array(), array(), array(), array('HTTP_CONTENT_TYPE' => 'application/json'), '{"username": 1, "password": "foo"}');
120120
$event = new GetResponseEvent($this->getMockBuilder(KernelInterface::class)->getMock(), $request, KernelInterface::MASTER_REQUEST);
121121

122122
$this->listener->handle($event);
@@ -126,7 +126,7 @@ public function testAttemptAuthenticationUsernameNotAString()
126126
public function testAttemptAuthenticationPasswordNotAString()
127127
{
128128
$this->createListener();
129-
$request = new Request(array(), array(), array(), array(), array(), array(), '{"username": "dunglas", "password": 1}');
129+
$request = new Request(array(), array(), array(), array(), array(), array('HTTP_CONTENT_TYPE' => 'application/json'), '{"username": "dunglas", "password": 1}');
130130
$event = new GetResponseEvent($this->getMockBuilder(KernelInterface::class)->getMock(), $request, KernelInterface::MASTER_REQUEST);
131131

132132
$this->listener->handle($event);
@@ -137,7 +137,7 @@ public function testAttemptAuthenticationUsernameTooLong()
137137
{
138138
$this->createListener();
139139
$username = str_repeat('x', Security::MAX_USERNAME_LENGTH + 1);
140-
$request = new Request(array(), array(), array(), array(), array(), array(), sprintf('{"username": "%s", "password": 1}', $username));
140+
$request = new Request(array(), array(), array(), array(), array(), array('HTTP_CONTENT_TYPE' => 'application/json'), sprintf('{"username": "%s", "password": 1}', $username));
141141
$event = new GetResponseEvent($this->getMockBuilder(KernelInterface::class)->getMock(), $request, KernelInterface::MASTER_REQUEST);
142142

143143
$this->listener->handle($event);
@@ -147,7 +147,18 @@ public function testAttemptAuthenticationUsernameTooLong()
147147
public function testDoesNotAttemptAuthenticationIfRequestPathDoesNotMatchCheckPath()
148148
{
149149
$this->createListener(array('check_path' => '/'), true, false);
150-
$request = new Request();
150+
$request = new Request(array(), array(), array(), array(), array(), array('HTTP_CONTENT_TYPE' => 'application/json'));
151+
$event = new GetResponseEvent($this->getMockBuilder(KernelInterface::class)->getMock(), $request, KernelInterface::MASTER_REQUEST);
152+
$event->setResponse(new Response('original'));
153+
154+
$this->listener->handle($event);
155+
$this->assertSame('original', $event->getResponse()->getContent());
156+
}
157+
158+
public function testDoesNotAttemptAuthenticationIfRequestContentTypeIsNotJson()
159+
{
160+
$this->createListener();
161+
$request = new Request(array(), array(), array(), array(), array(), array(), '{"username": "dunglas", "password": "foo"}');
151162
$event = new GetResponseEvent($this->getMockBuilder(KernelInterface::class)->getMock(), $request, KernelInterface::MASTER_REQUEST);
152163
$event->setResponse(new Response('original'));
153164

@@ -158,7 +169,7 @@ public function testDoesNotAttemptAuthenticationIfRequestPathDoesNotMatchCheckPa
158169
public function testAttemptAuthenticationIfRequestPathMatchesCheckPath()
159170
{
160171
$this->createListener(array('check_path' => '/'));
161-
$request = new Request(array(), array(), array(), array(), array(), array(), '{"username": "dunglas", "password": "foo"}');
172+
$request = new Request(array(), array(), array(), array(), array(), array('HTTP_CONTENT_TYPE' => 'application/json'), '{"username": "dunglas", "password": "foo"}');
162173
$event = new GetResponseEvent($this->getMockBuilder(KernelInterface::class)->getMock(), $request, KernelInterface::MASTER_REQUEST);
163174

164175
$this->listener->handle($event);

0 commit comments

Comments
 (0)
0