/**
  * {@inheritdoc}
  */
 public function authenticate(TokenInterface $token)
 {
     if (!($payload = $this->jwtManager->decode($token))) {
         throw new AuthenticationException('Invalid JWT Token');
     }
     $user = $this->getUserFromPayload($payload);
     $authToken = new JWTUserToken($user->getRoles());
     $authToken->setUser($user);
     $authToken->setRawToken($token->getCredentials());
     $event = new JWTAuthenticatedEvent($payload, $authToken);
     $this->dispatcher->dispatch(Events::JWT_AUTHENTICATED, $event);
     return $authToken;
 }
 /**
  * {@inheritdoc}
  */
 public function handle(GetResponseEvent $event)
 {
     if (!($requestToken = $this->getRequestToken($event->getRequest()))) {
         return;
     }
     $token = new JWTUserToken();
     $token->setRawToken($requestToken);
     try {
         $authToken = $this->authenticationManager->authenticate($token);
         $this->tokenStorage->setToken($authToken);
         return;
     } catch (AuthenticationException $failed) {
         if ($this->config['throw_exceptions']) {
             throw $failed;
         }
         $response = new Response();
         $response->setStatusCode(401);
         $response->headers->set('WWW-Authenticate', 'Bearer');
         $event->setResponse($response);
     }
 }