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

Skip to content

Commit f62c7be

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

File tree

3 files changed

+39
-12
lines changed

3 files changed

+39
-12
lines changed

src/Symfony/Bundle/SecurityBundle/Tests/Functional/JsonLoginTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ class JsonLoginTest extends WebTestCase
1919
public function testJsonLoginSuccess()
2020
{
2121
$client = $this->createClient(array('test_case' => 'JsonLogin', 'root_config' => 'config.yml'));
22-
$client->request('POST', '/chk', array(), array(), array(), '{"user": {"login": "dunglas", "password": "foo"}}');
22+
$client->request('POST', '/chk', array(), array(), array('CONTENT_TYPE' => 'application/json'), '{"user": {"login": "dunglas", "password": "foo"}}');
2323
$this->assertEquals('http://localhost/', $client->getResponse()->headers->get('location'));
2424
}
2525

2626
public function testJsonLoginFailure()
2727
{
2828
$client = $this->createClient(array('test_case' => 'JsonLogin', 'root_config' => 'config.yml'));
29-
$client->request('POST', '/chk', array(), array(), array(), '{"user": {"login": "dunglas", "password": "bad"}}');
29+
$client->request('POST', '/chk', array(), array(), array('CONTENT_TYPE' => 'application/json'), '{"user": {"login": "dunglas", "password": "bad"}}');
3030
$this->assertEquals('http://localhost/login', $client->getResponse()->headers->get('location'));
3131
}
3232
}

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: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,21 @@ private function createListener(array $options = array(), $success = true, $matc
6363
$this->listener = new UsernamePasswordJsonAuthenticationListener($tokenStorage, $authenticationManager, $httpUtils, 'providerKey', $authenticationSuccessHandler, $authenticationFailureHandler, $options);
6464
}
6565

66-
public function testHandleSuccess()
66+
public function testHandleSuccessIfRequestContentTypeIsJson()
67+
{
68+
$this->createListener();
69+
$request = new Request(array(), array(), array(), array(), array(), array('HTTP_CONTENT_TYPE' => 'application/json'), '{"username": "dunglas", "password": "foo"}');
70+
$event = new GetResponseEvent($this->getMockBuilder(KernelInterface::class)->getMock(), $request, KernelInterface::MASTER_REQUEST);
71+
72+
$this->listener->handle($event);
73+
$this->assertEquals('ok', $event->getResponse()->getContent());
74+
}
75+
76+
public function testSuccessIfRequestFormatIsJsonLD()
6777
{
6878
$this->createListener();
6979
$request = new Request(array(), array(), array(), array(), array(), array(), '{"username": "dunglas", "password": "foo"}');
80+
$request->setRequestFormat('json-ld');
7081
$event = new GetResponseEvent($this->getMockBuilder(KernelInterface::class)->getMock(), $request, KernelInterface::MASTER_REQUEST);
7182

7283
$this->listener->handle($event);
@@ -76,7 +87,7 @@ public function testHandleSuccess()
7687
public function testHandleFailure()
7788
{
7889
$this->createListener(array(), false);
79-
$request = new Request(array(), array(), array(), array(), array(), array(), '{"username": "dunglas", "password": "foo"}');
90+
$request = new Request(array(), array(), array(), array(), array(), array('HTTP_CONTENT_TYPE' => 'application/json'), '{"username": "dunglas", "password": "foo"}');
8091
$event = new GetResponseEvent($this->getMockBuilder(KernelInterface::class)->getMock(), $request, KernelInterface::MASTER_REQUEST);
8192

8293
$this->listener->handle($event);
@@ -86,7 +97,7 @@ public function testHandleFailure()
8697
public function testUsePath()
8798
{
8899
$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"}}');
100+
$request = new Request(array(), array(), array(), array(), array(), array('HTTP_CONTENT_TYPE' => 'application/json'), '{"user": {"login": "dunglas", "pwd": "foo"}}');
90101
$event = new GetResponseEvent($this->getMockBuilder(KernelInterface::class)->getMock(), $request, KernelInterface::MASTER_REQUEST);
91102

92103
$this->listener->handle($event);
@@ -96,7 +107,7 @@ public function testUsePath()
96107
public function testAttemptAuthenticationNoUsername()
97108
{
98109
$this->createListener();
99-
$request = new Request(array(), array(), array(), array(), array(), array(), '{"usr": "dunglas", "password": "foo"}');
110+
$request = new Request(array(), array(), array(), array(), array(), array('HTTP_CONTENT_TYPE' => 'application/json'), '{"usr": "dunglas", "password": "foo"}');
100111
$event = new GetResponseEvent($this->getMockBuilder(KernelInterface::class)->getMock(), $request, KernelInterface::MASTER_REQUEST);
101112

102113
$this->listener->handle($event);
@@ -106,7 +117,7 @@ public function testAttemptAuthenticationNoUsername()
106117
public function testAttemptAuthenticationNoPassword()
107118
{
108119
$this->createListener();
109-
$request = new Request(array(), array(), array(), array(), array(), array(), '{"username": "dunglas", "pass": "foo"}');
120+
$request = new Request(array(), array(), array(), array(), array(), array('HTTP_CONTENT_TYPE' => 'application/json'), '{"username": "dunglas", "pass": "foo"}');
110121
$event = new GetResponseEvent($this->getMockBuilder(KernelInterface::class)->getMock(), $request, KernelInterface::MASTER_REQUEST);
111122

112123
$this->listener->handle($event);
@@ -116,7 +127,7 @@ public function testAttemptAuthenticationNoPassword()
116127
public function testAttemptAuthenticationUsernameNotAString()
117128
{
118129
$this->createListener();
119-
$request = new Request(array(), array(), array(), array(), array(), array(), '{"username": 1, "password": "foo"}');
130+
$request = new Request(array(), array(), array(), array(), array(), array('HTTP_CONTENT_TYPE' => 'application/json'), '{"username": 1, "password": "foo"}');
120131
$event = new GetResponseEvent($this->getMockBuilder(KernelInterface::class)->getMock(), $request, KernelInterface::MASTER_REQUEST);
121132

122133
$this->listener->handle($event);
@@ -126,7 +137,7 @@ public function testAttemptAuthenticationUsernameNotAString()
126137
public function testAttemptAuthenticationPasswordNotAString()
127138
{
128139
$this->createListener();
129-
$request = new Request(array(), array(), array(), array(), array(), array(), '{"username": "dunglas", "password": 1}');
140+
$request = new Request(array(), array(), array(), array(), array(), array('HTTP_CONTENT_TYPE' => 'application/json'), '{"username": "dunglas", "password": 1}');
130141
$event = new GetResponseEvent($this->getMockBuilder(KernelInterface::class)->getMock(), $request, KernelInterface::MASTER_REQUEST);
131142

132143
$this->listener->handle($event);
@@ -137,7 +148,7 @@ public function testAttemptAuthenticationUsernameTooLong()
137148
{
138149
$this->createListener();
139150
$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));
151+
$request = new Request(array(), array(), array(), array(), array(), array('HTTP_CONTENT_TYPE' => 'application/json'), sprintf('{"username": "%s", "password": 1}', $username));
141152
$event = new GetResponseEvent($this->getMockBuilder(KernelInterface::class)->getMock(), $request, KernelInterface::MASTER_REQUEST);
142153

143154
$this->listener->handle($event);
@@ -147,7 +158,18 @@ public function testAttemptAuthenticationUsernameTooLong()
147158
public function testDoesNotAttemptAuthenticationIfRequestPathDoesNotMatchCheckPath()
148159
{
149160
$this->createListener(array('check_path' => '/'), true, false);
150-
$request = new Request();
161+
$request = new Request(array(), array(), array(), array(), array(), array('HTTP_CONTENT_TYPE' => 'application/json'));
162+
$event = new GetResponseEvent($this->getMockBuilder(KernelInterface::class)->getMock(), $request, KernelInterface::MASTER_REQUEST);
163+
$event->setResponse(new Response('original'));
164+
165+
$this->listener->handle($event);
166+
$this->assertSame('original', $event->getResponse()->getContent());
167+
}
168+
169+
public function testDoesNotAttemptAuthenticationIfRequestContentTypeIsNotJson()
170+
{
171+
$this->createListener();
172+
$request = new Request(array(), array(), array(), array(), array(), array(), '{"username": "dunglas", "password": "foo"}');
151173
$event = new GetResponseEvent($this->getMockBuilder(KernelInterface::class)->getMock(), $request, KernelInterface::MASTER_REQUEST);
152174
$event->setResponse(new Response('original'));
153175

@@ -158,7 +180,7 @@ public function testDoesNotAttemptAuthenticationIfRequestPathDoesNotMatchCheckPa
158180
public function testAttemptAuthenticationIfRequestPathMatchesCheckPath()
159181
{
160182
$this->createListener(array('check_path' => '/'));
161-
$request = new Request(array(), array(), array(), array(), array(), array(), '{"username": "dunglas", "password": "foo"}');
183+
$request = new Request(array(), array(), array(), array(), array(), array('HTTP_CONTENT_TYPE' => 'application/json'), '{"username": "dunglas", "password": "foo"}');
162184
$event = new GetResponseEvent($this->getMockBuilder(KernelInterface::class)->getMock(), $request, KernelInterface::MASTER_REQUEST);
163185

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

0 commit comments

Comments
 (0)
0