/**
  * 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');
 }
 /**
  * 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 (!$this->supports($token)) {
         return;
     }
     if (!($user = $token->getUser())) {
         throw new AuthenticationException('JWT auth failed');
     }
     $user = $this->userProvider->loadUserByUsername($user);
     $this->userChecker->checkPostAuth($user);
     $token = new JWTToken($user, $token->getCredentials(), $this->providerKey, $this->getRoles($user, $token));
     return $token;
 }
 /**
  * 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) {
         }
     }
 }
예제 #4
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);
 }