public function authenticate(TokenInterface $token)
 {
     $user = new User($token->getUsername(), null, array('ROLE_USER'));
     $casProviderEvent = new CasProviderEvent($user);
     $this->eventDispatcher->dispatch('cas.security.provider', $casProviderEvent);
     if ($casProviderEvent->isPropagationStopped()) {
         if ($casProviderEvent->hasAuthenticationException()) {
             throw $casProviderEvent->getAuthenticationException();
         }
     }
     $authenticatedToken = new CasUserToken($casProviderEvent->getUser()->getRoles());
     $authenticatedToken->setUser($casProviderEvent->getUser());
     $authenticatedToken->setAuthenticated($casProviderEvent->isValidUser());
     return $authenticatedToken;
 }
 public function handle(GetResponseEvent $event)
 {
     try {
         $request = $event->getRequest();
         if ($this->cas->isValidationRequest($request)) {
             $response = $this->cas->getValidation($request);
             if ($response->isSuccess()) {
                 $token = new CasUserToken();
                 $token->setUser($response->getUsername());
                 $authToken = $this->authenticationManager->authenticate($token);
                 $this->securityContext->setToken($authToken);
             }
         }
     } catch (AuthenticationException $e) {
         $response = new Response();
         $response->setStatusCode(403);
         $event->setResponse($response);
     }
 }