Skip to content

Commit

Permalink
Dispatch JWTInvalidEvent in onAuthenticationFailure
Browse files Browse the repository at this point in the history
CS Fixes
  • Loading branch information
chalasr committed Jul 4, 2016
1 parent 3512450 commit fd58b3f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 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
34 changes: 18 additions & 16 deletions Security/Guard/JWTTokenAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Lexik\Bundle\JWTAuthenticationBundle\Encoder\JWTEncoderInterface;
use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTAuthenticatedEvent;
use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTInvalidEvent;
use Lexik\Bundle\JWTAuthenticationBundle\Events;
use Lexik\Bundle\JWTAuthenticationBundle\Exception\JWTAuthenticationException;
use Lexik\Bundle\JWTAuthenticationBundle\Exception\JWTDecodeFailure\JWTDecodeFailureException;
Expand Down Expand Up @@ -71,17 +72,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 Down Expand Up @@ -134,21 +127,20 @@ 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}
*/
public function onAuthenticationFailure(Request $request, AuthenticationException $authException)
{
return new JWTAuthenticationFailureResponse($authException->getMessage());
$event = new JWTInvalidEvent($request, $authException, new JWTAuthenticationFailureResponse($authException->getMessage()));
$this->dispatcher->dispatch(Events::JWT_INVALID, $event);

return $event->getResponse();
}

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


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

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

0 comments on commit fd58b3f

Please sign in to comment.