From fd58b3f86ba4d651b9037776a6a2dd951c66efb3 Mon Sep 17 00:00:00 2001
From: Robin Chalas <robin.chalas@gmail.com>
Date: Mon, 4 Jul 2016 23:33:07 +0200
Subject: [PATCH] Dispatch JWTInvalidEvent in onAuthenticationFailure

CS Fixes
---
 Exception/JWTAuthenticationException.php |  5 ++--
 Security/Guard/JWTTokenAuthenticator.php | 34 +++++++++++++-----------
 2 files changed, 21 insertions(+), 18 deletions(-)

diff --git a/Exception/JWTAuthenticationException.php b/Exception/JWTAuthenticationException.php
index 0157fe3d..3c7567f8 100644
--- a/Exception/JWTAuthenticationException.php
+++ b/Exception/JWTAuthenticationException.php
@@ -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
      */
@@ -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
      */
diff --git a/Security/Guard/JWTTokenAuthenticator.php b/Security/Guard/JWTTokenAuthenticator.php
index faab3a93..7f9bb3e4 100644
--- a/Security/Guard/JWTTokenAuthenticator.php
+++ b/Security/Guard/JWTTokenAuthenticator.php
@@ -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;
@@ -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
@@ -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();
     }
 
     /**
@@ -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}
      */