/**
  * Attempts to authenticate a TokenInterface object.
  *
  * @param TokenInterface $token The TokenInterface instance to authenticate
  *
  * @return TokenInterface An authenticated TokenInterface instance, never null
  *
  * @throws AuthenticationException if the authentication fails
  */
 public function authenticate(TokenInterface $token)
 {
     if ($token instanceof JWTToken) {
         $userName = $token->getTokenContext()->name;
     } else {
         $userName = $token->getUsername();
     }
     $user = $this->userProvider->loadUserByUsername($userName);
     if (null != $user) {
         $lastContext = $token->getTokenContext();
         $token = new JWTToken($user->getRoles());
         $token->setTokenContext($lastContext);
         $token->setUser($user);
         return $token;
     }
     throw new AuthenticationException('JWT auth failed');
 }