/**
  * auth-profile request header used to set database
  */
 public function onKernelRequest()
 {
     if ($this->tokenStorage->getToken()) {
         if ($this->requestStack->getMasterRequest()->headers->has('auth-profile')) {
             $requestedDbName = $this->requestStack->getMasterRequest()->headers->get('auth-profile');
             $params = $this->connection->getParams();
             if ($requestedDbName != $params['dbname']) {
                 $params['driver'] = $this->databases[$requestedDbName . '_database_driver'];
                 $params['host'] = $this->databases[$requestedDbName . '_database_host'];
                 $params['port'] = $this->databases[$requestedDbName . '_database_port'];
                 $params['dbname'] = $this->databases[$requestedDbName . '_database_name'];
                 $params['user'] = $this->databases[$requestedDbName . '_database_user'];
                 $params['password'] = $this->databases[$requestedDbName . '_database_password'];
                 if ($this->connection->isConnected()) {
                     $this->connection->close();
                 }
                 $this->connection->__construct($params, $this->connection->getDriver(), $this->connection->getConfiguration(), $this->connection->getEventManager());
                 $this->connection->connect();
                 $this->registry->resetManager('default');
                 /** @var AuthUser $authUser */
                 $authUser = $this->registry->getManager('default')->getRepository('AppBundle:User')->findOneBy(array('username' => $this->tokenStorage->getToken()->getUsername()));
                 if (is_null($authUser)) {
                     throw new EntityNotFoundException('User not found in requested database');
                 }
                 $jwtUserToken = new JWTUserToken($authUser->getRoles());
                 $jwtUserToken->setUser($authUser);
                 $this->tokenStorage->setToken($jwtUserToken);
             }
         }
     }
 }
 /**
  * {@inheritdoc}
  */
 public function authenticate(TokenInterface $token)
 {
     if (!($payload = $this->jwtManager->decode($token))) {
         throw new AuthenticationException('Invalid JWT Token');
     }
     $user = $this->getUserFromPayload($payload);
     $authToken = new JWTUserToken($user->getRoles());
     $authToken->setUser($user);
     $authToken->setRawToken($token->getCredentials());
     $event = new JWTAuthenticatedEvent($payload, $authToken);
     $this->dispatcher->dispatch(Events::JWT_AUTHENTICATED, $event);
     return $authToken;
 }
 /**
  * {@inheritdoc}
  */
 public function handle(GetResponseEvent $event)
 {
     if (!($requestToken = $this->getRequestToken($event->getRequest()))) {
         return;
     }
     $token = new JWTUserToken();
     $token->setRawToken($requestToken);
     try {
         $authToken = $this->authenticationManager->authenticate($token);
         $this->tokenStorage->setToken($authToken);
         return;
     } catch (AuthenticationException $failed) {
         if ($this->config['throw_exceptions']) {
             throw $failed;
         }
         $response = new Response();
         $response->setStatusCode(401);
         $event->setResponse($response);
     }
 }