-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Security] Fix json_login default success/failure handling #22494
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\JsonLoginBundle\Security\Http; | ||
|
||
use Symfony\Component\HttpFoundation\JsonResponse; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\Security\Core\Exception\AuthenticationException; | ||
use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface; | ||
|
||
class JsonAuthenticationFailureHandler implements AuthenticationFailureHandlerInterface | ||
{ | ||
public function onAuthenticationFailure(Request $request, AuthenticationException $exception) | ||
{ | ||
return new JsonResponse(array('message' => 'Something went wrong'), 500); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\JsonLoginBundle\Security\Http; | ||
|
||
use Symfony\Component\HttpFoundation\JsonResponse; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | ||
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface; | ||
|
||
class JsonAuthenticationSuccessHandler implements AuthenticationSuccessHandlerInterface | ||
{ | ||
public function onAuthenticationSuccess(Request $request, TokenInterface $token) | ||
{ | ||
return new JsonResponse(array('message' => sprintf('Good game @%s!', $token->getUsername()))); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
imports: | ||
- { resource: ./../config/framework.yml } | ||
|
||
security: | ||
encoders: | ||
Symfony\Component\Security\Core\User\User: plaintext | ||
|
||
providers: | ||
in_memory: | ||
memory: | ||
users: | ||
dunglas: { password: foo, roles: [ROLE_USER] } | ||
|
||
firewalls: | ||
main: | ||
pattern: ^/ | ||
anonymous: true | ||
json_login: | ||
check_path: /chk | ||
username_path: user.login | ||
password_path: user.password | ||
success_handler: json_login.success_handler | ||
failure_handler: json_login.failure_handler | ||
|
||
access_control: | ||
- { path: ^/foo, roles: ROLE_USER } | ||
|
||
services: | ||
json_login.success_handler: | ||
class: Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\JsonLoginBundle\Security\Http\JsonAuthenticationSuccessHandler | ||
json_login.failure_handler: | ||
class: Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\JsonLoginBundle\Security\Http\JsonAuthenticationFailureHandler |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
|
||
use Psr\Log\LoggerInterface; | ||
use Symfony\Component\EventDispatcher\EventDispatcherInterface; | ||
use Symfony\Component\HttpFoundation\JsonResponse; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpKernel\Event\GetResponseEvent; | ||
|
@@ -53,7 +54,7 @@ class UsernamePasswordJsonAuthenticationListener implements ListenerInterface | |
private $eventDispatcher; | ||
private $propertyAccessor; | ||
|
||
public function __construct(TokenStorageInterface $tokenStorage, AuthenticationManagerInterface $authenticationManager, HttpUtils $httpUtils, $providerKey, AuthenticationSuccessHandlerInterface $successHandler, AuthenticationFailureHandlerInterface $failureHandler, array $options = array(), LoggerInterface $logger = null, EventDispatcherInterface $eventDispatcher = null, PropertyAccessorInterface $propertyAccessor = null) | ||
public function __construct(TokenStorageInterface $tokenStorage, AuthenticationManagerInterface $authenticationManager, HttpUtils $httpUtils, $providerKey, AuthenticationSuccessHandlerInterface $successHandler = null, AuthenticationFailureHandlerInterface $failureHandler = null, array $options = array(), LoggerInterface $logger = null, EventDispatcherInterface $eventDispatcher = null, PropertyAccessorInterface $propertyAccessor = null) | ||
{ | ||
$this->tokenStorage = $tokenStorage; | ||
$this->authenticationManager = $authenticationManager; | ||
|
@@ -117,6 +118,10 @@ public function handle(GetResponseEvent $event) | |
$response = $this->onFailure($request, $e); | ||
} | ||
|
||
if (null === $response) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what is the reason this is needed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In case of success, the listener just lets the original request succeeds so it doesn't return any response. Should it return a default success response instead? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah of course |
||
return; | ||
} | ||
|
||
$event->setResponse($response); | ||
} | ||
|
||
|
@@ -133,6 +138,10 @@ private function onSuccess(Request $request, TokenInterface $token) | |
$this->eventDispatcher->dispatch(SecurityEvents::INTERACTIVE_LOGIN, $loginEvent); | ||
} | ||
|
||
if (!$this->successHandler) { | ||
return; // let the original request succeeds | ||
} | ||
|
||
$response = $this->successHandler->onAuthenticationSuccess($request, $token); | ||
|
||
if (!$response instanceof Response) { | ||
|
@@ -153,6 +162,10 @@ private function onFailure(Request $request, AuthenticationException $failed) | |
$this->tokenStorage->setToken(null); | ||
} | ||
|
||
if (!$this->failureHandler) { | ||
return new JsonResponse(array('error' => $failed->getMessageKey()), 401); | ||
} | ||
|
||
$response = $this->failureHandler->onAuthenticationFailure($request, $failed); | ||
|
||
if (!$response instanceof Response) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we have a standard style how we word such defaults?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is a custom success handler fixture :)