/**
  * 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)
 {
     $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');
 }
 /**
  * This interface must be implemented by firewall listeners.
  *
  * @param GetResponseEvent $event
  */
 public function handle(GetResponseEvent $event)
 {
     $request = $event->getRequest();
     $requestToken = $this->getToken($request->headers->get($this->options['header_name'], null));
     if (!empty($requestToken)) {
         try {
             $decoded = $this->encode->decode($requestToken);
             $token = new JWTToken();
             $token->setTokenContext($decoded);
             $authToken = $this->authenticationManager->authenticate($token);
             $this->securityContext->setToken($authToken);
         } catch (HttpEncodingException $e) {
         } catch (\UnexpectedValueException $e) {
         }
     }
 }
Exemple #3
0
 /**
  * This interface must be implemented by firewall listeners.
  *
  * @param GetResponseEvent $event
  */
 public function handle(GetResponseEvent $event)
 {
     $request = $event->getRequest();
     $requestToken = $request->headers->get($this->options['header_name'], '');
     if (!empty($requestToken)) {
         try {
             $decoded = $this->encode->decode($requestToken);
             $token = new JWTToken();
             $token->setTokenContext($decoded);
             $authToken = $this->authenticationManager->authenticate($token);
             $this->securityContext->setToken($authToken);
             return;
         } catch (HttpEncodingException $e) {
         } catch (\UnexpectedValueException $e) {
         }
     }
     $response = new Response();
     $response->setStatusCode(Response::HTTP_UNAUTHORIZED);
     $event->setResponse($response);
 }