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 @@ -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 @@ +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); + } +}