Skip to content

Commit

Permalink
Dispatch JWTInvalidEvent in onAuthenticationFailure
Browse files Browse the repository at this point in the history
CS Fixes

Dispatch JWTNotFoundEvent (based on opened PR)
  • Loading branch information
chalasr committed Jul 5, 2016
1 parent 3512450 commit bca69f6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 20 deletions.
5 changes: 3 additions & 2 deletions Exception/JWTAuthenticationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public static function invalidToken(JWTDecodeFailureException $previous = null)
* To be used if no user can be loaded from the identity retrieved from
* the decoded token's payload.
*
* @param string|null $message
* @param string $identity
* @param string $identityField
*
* @return JWTAuthenticationException
*/
Expand All @@ -48,7 +49,7 @@ public static function invalidUser($identity, $identityField)
*
* To be used if a key in missing in the payload or contains an unexpected value.
*
* @param string|null $message
* @param string $message
*
* @return JWTAuthenticationException
*/
Expand Down
52 changes: 34 additions & 18 deletions Security/Guard/JWTTokenAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

use Lexik\Bundle\JWTAuthenticationBundle\Encoder\JWTEncoderInterface;
use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTAuthenticatedEvent;
use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTFailureEventInterface;
use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTInvalidEvent;
use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTNotFoundEvent;
use Lexik\Bundle\JWTAuthenticationBundle\Events;
use Lexik\Bundle\JWTAuthenticationBundle\Exception\JWTAuthenticationException;
use Lexik\Bundle\JWTAuthenticationBundle\Exception\JWTDecodeFailure\JWTDecodeFailureException;
Expand Down Expand Up @@ -71,17 +74,9 @@ public function __construct(
$this->userIdentityField = $userIdentityField;
}

/**
* {@inheritdoc}
*/
public function start(Request $request, AuthenticationException $authException = null)
{
return new JWTAuthenticationFailureResponse();
}

/**
* Returns a decoded JWT token extracted from a request.
*
*
* {@inheritdoc}
*
* @return BeforeAuthToken
Expand All @@ -91,7 +86,6 @@ public function start(Request $request, AuthenticationException $authException =
public function getCredentials(Request $request)
{
if (false === ($jsonWebToken = $this->tokenExtractor->extract($request))) {
// Dispatch JWTNotFoundEvent
return;
}

Expand Down Expand Up @@ -134,21 +128,28 @@ public function getUser($decodedToken, UserProviderInterface $userProvider)
$authToken->setUser($user);
$authToken->setRawToken($decodedToken->getCredentials());

$this->dispatcher->dispatch(
Events::JWT_AUTHENTICATED,
new JWTAuthenticatedEvent($payload, $authToken)
);
$this->dispatcher->dispatch(Events::JWT_AUTHENTICATED, new JWTAuthenticatedEvent($payload, $authToken));

return $user;
}


/**
* {@inheritdoc}
*
* @param JWTFailureEventInterface An event to be dispatched (default JWTInvalidEvent)
*/
public function onAuthenticationFailure(Request $request, AuthenticationException $authException)
public function onAuthenticationFailure(Request $request, AuthenticationException $authException, JWTFailureEventInterface $event = null)
{
return new JWTAuthenticationFailureResponse($authException->getMessage());
$response = new JWTAuthenticationFailureResponse($authException->getMessage());

if (null === $event) {
$event = new JWTInvalidEvent($request, $authException, $response);
$this->dispatcher->dispatch(Events::JWT_INVALID, $event);
} else {
$event->setResponse($response);
}

return $event->getResponse();
}

/**
Expand All @@ -158,7 +159,22 @@ public function onAuthenticationSuccess(Request $request, TokenInterface $token,
{
return;
}


/**
* {@inheritdoc}
*
* @return JWTAuthenticationFailureResponse
*/
public function start(Request $request, AuthenticationException $authException = null)
{
$authException = JWTAuthenticationException::invalidToken();
$event = new JWTNotFoundEvent($request, $authException);

$this->dispatcher->dispatch(Events::JWT_NOT_FOUND, $event);

return $this->onAuthenticationFailure($request, $authException, $event);
}

/**
* {@inheritdoc}
*/
Expand Down

0 comments on commit bca69f6

Please sign in to comment.