Skip to content

Commit

Permalink
Merge pull request #92 from mRoca/bugfix/event-dispacher-propagation
Browse files Browse the repository at this point in the history
Fix authentication event propagation
  • Loading branch information
slashfan committed Sep 28, 2015
2 parents a0e2f0a + aae5642 commit c3122de
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 13 deletions.
25 changes: 21 additions & 4 deletions Event/AuthenticationFailureEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@

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;

/**
* AuthenticationFailureEvent
*
* @author Emmanuel Vella <[email protected]>
*/
class AuthenticationFailureEvent extends GetResponseEvent
class AuthenticationFailureEvent extends Event
{
/**
* @var Request
Expand All @@ -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;
}

/**
Expand All @@ -47,4 +56,12 @@ public function getException()
{
return $this->exception;
}

/**
* @return Response
*/
public function getResponse()
{
return $this->response;
}
}
26 changes: 21 additions & 5 deletions Event/AuthenticationSuccessEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@

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;

/**
* AuthenticationSuccessEvent
*
* @author Dev Lexik <[email protected]>
*/
class AuthenticationSuccessEvent extends GetResponseEvent
class AuthenticationSuccessEvent extends Event
{
/**
* @var array
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -74,4 +82,12 @@ public function getRequest()
{
return $this->request;
}

/**
* @return Response
*/
public function getResponse()
{
return $this->response;
}
}
4 changes: 2 additions & 2 deletions Security/Http/Authentication/AuthenticationFailureHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit c3122de

Please sign in to comment.