/**
  * @param TokenInterface $token
  *
  * @return OAuthToken|TokenInterface
  * @throws \Symfony\Component\Security\Core\Exception\AuthenticationException
  */
 public function authenticate(TokenInterface $token)
 {
     try {
         $tokenString = $token->getToken();
         $user = $this->userProvider->loadUserByToken($tokenString);
         $token = new OAuthToken($user->getRoles());
         $token->setToken($tokenString);
         $token->setUser($user);
         $token->setAuthenticated(true);
         return $token;
     } catch (\Exception $e) {
         if ($this->logger) {
             $this->logger->alert('Can not authenticate user', array('message' => $e->getMessage()));
         }
     }
     throw new AuthenticationException('The OAuth authentication failed.');
 }