File size: 7,956 Bytes
cb29db0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
diff --git a/config/packages/security.yaml b/config/packages/security.yaml
index be39e46f5cc..ae669245ccd 100644
--- a/config/packages/security.yaml
+++ b/config/packages/security.yaml
@@ -20,14 +20,13 @@ security:
         - { path: ^/admin/reset, roles: PUBLIC_ACCESS }
         - { path: ^/admin/security/reset, roles: PUBLIC_ACCESS }
         - { path: ^/admin/login$, roles: PUBLIC_ACCESS }
+        - { path: ^/admin/2fa, roles: PUBLIC_ACCESS }
         - { path: ^/admin/_wdt, roles: PUBLIC_ACCESS }
+        - { path: ^/admin/_profiler, roles: PUBLIC_ACCESS }
         - { path: ^/admin/translations, roles: PUBLIC_ACCESS }
         - { path: ^/admin$, roles: PUBLIC_ACCESS }
         - { path: ^/admin/$, roles: PUBLIC_ACCESS }
         - { path: ^/admin/p/, roles: PUBLIC_ACCESS }
-        - { path: ^/admin/logout, role: PUBLIC_ACCESS }
-        - { path: ^/admin/_profiler, role: PUBLIC_ACCESS }
-        - { path: ^/admin/2fa, role: PUBLIC_ACCESS }
         - { path: ^/admin, roles: ROLE_USER }
 
     firewalls:
diff --git a/src/Sulu/Bundle/SecurityBundle/Security/AuthenticationHandler.php b/src/Sulu/Bundle/SecurityBundle/Security/AuthenticationHandler.php
index 6054a2a4881..8d4efb8f70c 100644
--- a/src/Sulu/Bundle/SecurityBundle/Security/AuthenticationHandler.php
+++ b/src/Sulu/Bundle/SecurityBundle/Security/AuthenticationHandler.php
@@ -104,7 +104,7 @@ public function onAuthenticationFailure(Request $request, AuthenticationExceptio
     {
         if ($request->isXmlHttpRequest()) {
             // if AJAX login
-            $array = ['message' => $exception->getMessage()];
+            $array = ['message' => $exception->getMessageKey()];
             $response = new JsonResponse($array, 401);
         } else {
             // if form login
diff --git a/src/Sulu/Bundle/SecurityBundle/Tests/Application/config/config.yml b/src/Sulu/Bundle/SecurityBundle/Tests/Application/config/config.yml
index a9a3cd20d52..6b9b5efbba4 100644
--- a/src/Sulu/Bundle/SecurityBundle/Tests/Application/config/config.yml
+++ b/src/Sulu/Bundle/SecurityBundle/Tests/Application/config/config.yml
@@ -8,8 +8,14 @@ security:
     access_control:
         - { path: ^/admin/reset, roles: PUBLIC_ACCESS }
         - { path: ^/admin/security/reset, roles: PUBLIC_ACCESS }
-        - { path: ^/admin/login, roles: PUBLIC_ACCESS }
-        - { path: ^/admin/2fa, role: PUBLIC_ACCESS }
+        - { path: ^/admin/login$, roles: PUBLIC_ACCESS }
+        - { path: ^/admin/2fa, roles: PUBLIC_ACCESS }
+        - { path: ^/admin/_wdt, roles: PUBLIC_ACCESS }
+        - { path: ^/admin/_profiler, roles: PUBLIC_ACCESS }
+        - { path: ^/admin/translations, roles: PUBLIC_ACCESS }
+        - { path: ^/admin$, roles: PUBLIC_ACCESS }
+        - { path: ^/admin/$, roles: PUBLIC_ACCESS }
+        - { path: ^/admin/p/, roles: PUBLIC_ACCESS }
         - { path: ^/admin, roles: ROLE_USER }
 
     firewalls:
@@ -17,13 +23,12 @@ security:
             pattern: ^/
             lazy: true
             entry_point: sulu_security.authentication_entry_point
-            form_login:
+            json_login:
                 check_path: sulu_admin.login_check
                 success_handler: sulu_security.authentication_handler
                 failure_handler: sulu_security.authentication_handler
             logout:
-                path: /admin/logout
-                target: /admin/
+                path: sulu_admin.logout
             two_factor:
                 prepare_on_login: true
                 prepare_on_access_denied: true
diff --git a/src/Sulu/Bundle/SecurityBundle/Tests/Functional/Controller/LoginControllerTest.php b/src/Sulu/Bundle/SecurityBundle/Tests/Functional/Controller/LoginControllerTest.php
deleted file mode 100644
index dbcfbe26f31..00000000000
--- a/src/Sulu/Bundle/SecurityBundle/Tests/Functional/Controller/LoginControllerTest.php
+++ /dev/null
@@ -1,29 +0,0 @@
-<?php
-
-/*
- * This file is part of Sulu.
- *
- * (c) Sulu GmbH
- *
- * This source file is subject to the MIT license that is bundled
- * with this source code in the file LICENSE.
- */
-
-namespace Sulu\Bundle\AdminBundle\Tests\Functional\Controller;
-
-use Sulu\Bundle\TestBundle\Testing\SuluTestCase;
-
-class LoginControllerTest extends SuluTestCase
-{
-    public function testFalseLoginRedirect(): void
-    {
-        $client = $this->createClient();
-        $client->request('POST', '/admin/login', [
-            '_username' => 'FalseUser',
-            '_password' => 'FalsePassword',
-        ]);
-        $response = $client->getResponse();
-        $this->assertHttpStatusCode(302, $response);
-        $this->assertSame('/admin/', $response->getTargetUrl());
-    }
-}
diff --git a/src/Sulu/Bundle/SecurityBundle/Tests/Functional/Security/AuthenticationHandlerTest.php b/src/Sulu/Bundle/SecurityBundle/Tests/Functional/Security/AuthenticationHandlerTest.php
new file mode 100644
index 00000000000..5d3f3598571
--- /dev/null
+++ b/src/Sulu/Bundle/SecurityBundle/Tests/Functional/Security/AuthenticationHandlerTest.php
@@ -0,0 +1,82 @@
+<?php
+
+/*
+ * This file is part of Sulu.
+ *
+ * (c) Sulu GmbH
+ *
+ * This source file is subject to the MIT license that is bundled
+ * with this source code in the file LICENSE.
+ */
+
+namespace Sulu\Bundle\SecurityBundle\Tests\Functional\Security;
+
+use Sulu\Bundle\TestBundle\Testing\SuluTestCase;
+
+class AuthenticationHandlerTest extends SuluTestCase
+{
+    public function testLoginFail(): void
+    {
+        $client = $this->createClient([], [
+            'CONTENT_TYPE' => 'application/json',
+            'HTTP_ACCEPT' => 'application/json',
+            'HTTP_X-Requested-With' => 'XMLHttpRequest',
+        ]);
+
+        $client->request('POST', '/admin/login', [], [], [], '{"username": "not-existing-user", "password": "wrong"}');
+
+        $response = $client->getResponse();
+        $this->assertHttpStatusCode(401, $response);
+        $notExistUserContent = $response->getContent();
+
+        $this->assertSame('{"message":"Invalid credentials."}', $notExistUserContent);
+    }
+
+    public function testLoginSuccess(): void
+    {
+        $client = $this->createClient([], [
+            'CONTENT_TYPE' => 'application/json',
+            'HTTP_ACCEPT' => 'application/json',
+            'HTTP_X-Requested-With' => 'XMLHttpRequest',
+        ]);
+
+        $testUser = $this->getTestUser();
+
+        $client->request('POST', '/admin/login', [], [], [], '{"username": "' . $testUser->getUsername() . '", "password": "test"}');
+
+        $response = $client->getResponse();
+        $this->assertHttpStatusCode(200, $response);
+        $notExistUserContent = $response->getContent();
+
+        $this->assertSame(
+            '{"url":"\/admin\/","username":"test","completed":true,"twoFactorMethods":["trusted_devices"]}',
+            $notExistUserContent
+        );
+    }
+
+    public function testLoginFailExistUserHasSameMessageAsNotExist(): void
+    {
+        $client = $this->createClient([], [
+            'CONTENT_TYPE' => 'application/json',
+            'HTTP_ACCEPT' => 'application/json',
+            'HTTP_X-Requested-With' => 'XMLHttpRequest',
+        ]);
+
+        $testUser = $this->getTestUser();
+
+        $client->request('POST', '/admin/login', [], [], [], '{"username": "not-existing-user", "password": "wrong"}');
+
+        $response = $client->getResponse();
+        $this->assertHttpStatusCode(401, $response);
+        $notExistUserContent = $response->getContent();
+
+        $client->request('POST', '/admin/login', [], [], [], '{"username": "' . $testUser->getUsername() . '", "password": "wrong"}');
+
+        $response = $client->getResponse();
+        $this->assertHttpStatusCode(401, $response);
+        $existUserContent = $response->getContent();
+
+        $this->assertSame($notExistUserContent, $existUserContent);
+        $this->assertSame('{"message":"Invalid credentials."}', $notExistUserContent);
+    }
+}