From aae564268474a2b48c8e7b0535b2186d30973ce8 Mon Sep 17 00:00:00 2001 From: mRoca Date: Mon, 21 Sep 2015 17:08:11 +0200 Subject: [PATCH] Fix authentication event propagation --- Event/AuthenticationFailureEvent.php | 25 +++++++++++++++--- Event/AuthenticationSuccessEvent.php | 26 +++++++++++++++---- .../AuthenticationFailureHandler.php | 4 +-- .../AuthenticationSuccessHandler.php | 3 +-- 4 files changed, 45 insertions(+), 13 deletions(-) diff --git a/Event/AuthenticationFailureEvent.php b/Event/AuthenticationFailureEvent.php index 5b583320..4e0cc9a0 100644 --- a/Event/AuthenticationFailureEvent.php +++ b/Event/AuthenticationFailureEvent.php @@ -2,8 +2,9 @@ namespace Lexik\Bundle\JWTAuthenticationBundle\Event; +use Symfony\Component\EventDispatcher\Event; use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\HttpKernel\Event\GetResponseEvent; +use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Security\Core\Exception\AuthenticationException; /** @@ -11,7 +12,7 @@ * * @author Emmanuel Vella */ -class AuthenticationFailureEvent extends GetResponseEvent +class AuthenticationFailureEvent extends Event { /** * @var Request @@ -24,12 +25,20 @@ class AuthenticationFailureEvent extends GetResponseEvent protected $exception; /** - * @param Request $request + * @var Response */ - public function __construct(Request $request, AuthenticationException $exception) + protected $response; + + /** + * @param Request $request + * @param AuthenticationException $exception + * @param Response $response + */ + public function __construct(Request $request, AuthenticationException $exception, Response $response) { $this->request = $request; $this->exception = $exception; + $this->response = $response; } /** @@ -47,4 +56,12 @@ public function getException() { return $this->exception; } + + /** + * @return Response + */ + public function getResponse() + { + return $this->response; + } } diff --git a/Event/AuthenticationSuccessEvent.php b/Event/AuthenticationSuccessEvent.php index e8463772..8b1854e0 100644 --- a/Event/AuthenticationSuccessEvent.php +++ b/Event/AuthenticationSuccessEvent.php @@ -3,8 +3,9 @@ namespace Lexik\Bundle\JWTAuthenticationBundle\Event; +use Symfony\Component\EventDispatcher\Event; use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\HttpKernel\Event\GetResponseEvent; +use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Security\Core\User\UserInterface; /** @@ -12,7 +13,7 @@ * * @author Dev Lexik */ -class AuthenticationSuccessEvent extends GetResponseEvent +class AuthenticationSuccessEvent extends Event { /** * @var array @@ -29,16 +30,23 @@ class AuthenticationSuccessEvent extends GetResponseEvent */ protected $request; + /** + * @var Response + */ + protected $response; + /** * @param array $data * @param UserInterface $user * @param Request $request + * @param Response $response */ - public function __construct(array $data, UserInterface $user, Request $request) + public function __construct(array $data, UserInterface $user, Request $request, Response $response) { - $this->data = $data; - $this->user = $user; + $this->data = $data; + $this->user = $user; $this->request = $request; + $this->response = $response; } /** @@ -74,4 +82,12 @@ public function getRequest() { return $this->request; } + + /** + * @return Response + */ + public function getResponse() + { + return $this->response; + } } diff --git a/Security/Http/Authentication/AuthenticationFailureHandler.php b/Security/Http/Authentication/AuthenticationFailureHandler.php index 71c36459..c2474c19 100644 --- a/Security/Http/Authentication/AuthenticationFailureHandler.php +++ b/Security/Http/Authentication/AuthenticationFailureHandler.php @@ -43,8 +43,8 @@ public function onAuthenticationFailure(Request $request, AuthenticationExceptio 'message' => self::RESPONSE_MESSAGE, ); - $event = new AuthenticationFailureEvent($request, $exception); - $event->setResponse(new JsonResponse($data, self::RESPONSE_CODE)); + $response = new JsonResponse($data, self::RESPONSE_CODE); + $event = new AuthenticationFailureEvent($request, $exception, $response); $this->dispatcher->dispatch(Events::AUTHENTICATION_FAILURE, $event); diff --git a/Security/Http/Authentication/AuthenticationSuccessHandler.php b/Security/Http/Authentication/AuthenticationSuccessHandler.php index 39f5dc65..3a04b77b 100644 --- a/Security/Http/Authentication/AuthenticationSuccessHandler.php +++ b/Security/Http/Authentication/AuthenticationSuccessHandler.php @@ -48,8 +48,7 @@ public function onAuthenticationSuccess(Request $request, TokenInterface $token) $response = new JsonResponse(); - $event = new AuthenticationSuccessEvent(array('token' => $jwt), $user, $request); - $event->setResponse($response); + $event = new AuthenticationSuccessEvent(array('token' => $jwt), $user, $request, $response); $this->dispatcher->dispatch(Events::AUTHENTICATION_SUCCESS, $event);