當前位置: 首頁>>代碼示例>>PHP>>正文


PHP TokenStorageInterface::setToken方法代碼示例

本文整理匯總了PHP中Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface::setToken方法的典型用法代碼示例。如果您正苦於以下問題:PHP TokenStorageInterface::setToken方法的具體用法?PHP TokenStorageInterface::setToken怎麽用?PHP TokenStorageInterface::setToken使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface的用法示例。


在下文中一共展示了TokenStorageInterface::setToken方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。

示例1: handle

 public function handle(GetResponseEvent $event)
 {
     // getting request
     $request = $event->getRequest();
     // getting attributes
     $apiClientToken = $request->attributes->get('apiClientToken');
     $apiUserToken = $request->attributes->get('apiUserToken');
     $apiServerAction = $request->attributes->get('apiAction');
     /* @var $apiServerAction ApiServerAction */
     // cleaning credentials and interface name
     $request->attributes->remove('apiClientToken');
     $request->attributes->remove('apiUserToken');
     // creating token
     $token = new Token($apiServerAction->getApiServerInterface());
     $token->setCredentials([$apiClientToken, $apiUserToken]);
     try {
         // authenticating
         $authenticatedToken = $this->authenticationManager->authenticate($token);
         $this->tokenStorage->setToken($authenticatedToken);
         // getting authenticated user
         $user = $authenticatedToken->getUser();
         /* @var $user User */
         // setting request attributes
         $request->attributes->set('apiConnection', $user->getApiConnection());
         $request->attributes->set('apiClient', $user->getApiClient());
         $request->attributes->set('apiUser', $user->getApiUser());
     } catch (\Exception $e) {
         $event->stopPropagation();
         throw new AccessDeniedHttpException(null, $e);
     }
 }
開發者ID:leoza,項目名稱:api-server-bundle,代碼行數:31,代碼來源:AuthenticationListener.php

示例2: handle

 public function handle(GetResponseEvent $event)
 {
     $request = $event->getRequest();
     //find out if the current request contains any information by which the user might be authenticated
     if (!$request->headers->has('X-WSSE')) {
         return;
     }
     $ae_message = null;
     $this->wsseHeader = $request->headers->get('X-WSSE');
     $wsseHeaderInfo = $this->parseHeader();
     if ($wsseHeaderInfo !== false) {
         $token = new Token($wsseHeaderInfo['Username'], $wsseHeaderInfo['PasswordDigest'], $this->providerKey);
         $token->setAttribute('nonce', $wsseHeaderInfo['Nonce']);
         $token->setAttribute('created', $wsseHeaderInfo['Created']);
         try {
             $returnValue = $this->authenticationManager->authenticate($token);
             if ($returnValue instanceof TokenInterface) {
                 return $this->tokenStorage->setToken($returnValue);
             } else {
                 if ($returnValue instanceof Response) {
                     return $event->setResponse($returnValue);
                 }
             }
         } catch (AuthenticationException $ae) {
             $event->setResponse($this->authenticationEntryPoint->start($request, $ae));
         }
     }
 }
開發者ID:MathiasDewelde,項目名稱:EscapeWSSEAuthenticationBundle,代碼行數:28,代碼來源:Listener.php

示例3: handle

 /**
  * @param GetResponseEvent $event
  */
 public function handle(GetResponseEvent $event)
 {
     $request = $event->getRequest();
     if (empty($request->headers->get("Authorization"))) {
         return;
     }
     $authHeader = $request->headers->get("Authorization");
     if (strpos($authHeader, " ") === false) {
         return;
     }
     list($tokenType, $token) = explode(" ", $authHeader, 2);
     if (strtolower($tokenType) !== "bearer") {
         return;
     }
     // Verify that there is an access_token present
     /*
             if(empty($request->get("access_token"))) {
                 return;
             }
             $token = $request->get("access_token");*/
     $unauthenticatedToken = new OAuthToken();
     $unauthenticatedToken->setToken($token);
     try {
         $authenticatedToken = $this->authenticationManager->authenticate($unauthenticatedToken);
         $this->tokenStorage->setToken($authenticatedToken);
         return;
     } catch (AuthenticationException $e) {
         if ($this->logger !== null) {
             $this->logger->notice("Access token authentication failed");
         }
     }
     $response = new Response();
     $response->setStatusCode(Response::HTTP_FORBIDDEN);
     $event->setResponse($response);
 }
開發者ID:koenreiniers,項目名稱:oauth-server-bundle,代碼行數:38,代碼來源:AccessTokenListener.php

示例4: onKernelRequest

 /**
  * If user is logged-in in legacy_mode (e.g. legacy admin interface),
  * will inject currently logged-in user in the repository.
  *
  * @param GetResponseEvent $event
  */
 public function onKernelRequest(GetResponseEvent $event)
 {
     /** @var \eZ\Publish\Core\MVC\ConfigResolverInterface $configResolver */
     $request = $event->getRequest();
     $session = $request->getSession();
     if ($event->getRequestType() !== HttpKernelInterface::MASTER_REQUEST || !$this->configResolver->getParameter('legacy_mode') || !($session->isStarted() && $session->has('eZUserLoggedInID'))) {
         return;
     }
     try {
         $apiUser = $this->repository->getUserService()->loadUser($session->get('eZUserLoggedInID'));
         $this->repository->setCurrentUser($apiUser);
         $token = $this->tokenStorage->getToken();
         if ($token instanceof TokenInterface) {
             $token->setUser(new User($apiUser));
             // Don't embed if we already have a LegacyToken, to avoid nested session storage.
             if (!$token instanceof LegacyToken) {
                 $this->tokenStorage->setToken(new LegacyToken($token));
             }
         }
     } catch (NotFoundException $e) {
         // Invalid user ID, the user may have been removed => invalidate the token and the session.
         $this->tokenStorage->setToken(null);
         $session->invalidate();
     }
 }
開發者ID:emodric,項目名稱:LegacyBridge,代碼行數:31,代碼來源:RequestListener.php

示例5: logInUser

 public final function logInUser($firewallName, UserInterface $user, Response $response = null)
 {
     $this->userChecker->checkPostAuth($user);
     $token = $this->createToken($firewallName, $user);
     $request = null;
     if ($this->container->has('request_stack')) {
         $request = $this->container->get('request_stack')->getCurrentRequest();
     } elseif (method_exists($this->container, 'isScopeActive') && $this->container->isScopeActive('request')) {
         // BC for SF <2.4
         $request = $this->container->get('request');
     }
     if (null !== $request) {
         $this->sessionStrategy->onAuthentication($request, $token);
         if (null !== $response) {
             $rememberMeServices = null;
             if ($this->container->has('security.authentication.rememberme.services.persistent.' . $firewallName)) {
                 $rememberMeServices = $this->container->get('security.authentication.rememberme.services.persistent.' . $firewallName);
             } elseif ($this->container->has('security.authentication.rememberme.services.simplehash.' . $firewallName)) {
                 $rememberMeServices = $this->container->get('security.authentication.rememberme.services.simplehash.' . $firewallName);
             }
             if ($rememberMeServices instanceof RememberMeServicesInterface) {
                 $rememberMeServices->loginSuccess($request, $response, $token);
             }
         }
     }
     $this->tokenStorage->setToken($token);
 }
開發者ID:acorbel,項目名稱:FOSUserBundle,代碼行數:27,代碼來源:LoginManager.php

示例6: handle

 /**
  * @param GetResponseEvent $event
  */
 public function handle(GetResponseEvent $event)
 {
     if (null !== $this->tokenStorage->getToken()) {
         return;
     }
     $request = $event->getRequest();
     $token = new PluginToken($this->providerKey, $request->get('integration', null));
     try {
         $authToken = $this->authenticationManager->authenticate($token);
         if ($authToken instanceof PluginToken) {
             $response = $authToken->getResponse();
             if ($authToken->isAuthenticated()) {
                 $this->tokenStorage->setToken($authToken);
                 if ('api' != $this->providerKey) {
                     $response = $this->onSuccess($request, $authToken, $response);
                 }
             } elseif (empty($response)) {
                 throw new AuthenticationException('mautic.user.auth.error.invalidlogin');
             }
         }
     } catch (AuthenticationException $exception) {
         if ('api' != $this->providerKey) {
             $response = $this->onFailure($request, $exception);
         }
     }
     if ($response) {
         $event->setResponse($response);
     }
 }
開發者ID:Yame-,項目名稱:mautic,代碼行數:32,代碼來源:AuthenticationListener.php

示例7: handle

 /**
  * This interface must be implemented by firewall listeners.
  *
  * @param GetResponseEvent $event
  */
 public function handle(GetResponseEvent $event)
 {
     $request = $event->getRequest();
     if (!$this->keyExtractor->hasKey($request)) {
         $response = new Response();
         $response->setStatusCode(401);
         $event->setResponse($response);
         return;
     }
     $apiKey = $this->keyExtractor->extractKey($request);
     $token = new ApiKeyUserToken();
     $token->setApiKey($apiKey);
     try {
         $authToken = $this->authenticationManager->authenticate($token);
         $this->tokenStorage->setToken($authToken);
         return;
     } catch (AuthenticationException $failed) {
         $token = $this->tokenStorage->getToken();
         if ($token instanceof ApiKeyUserToken && $token->getCredentials() == $apiKey) {
             $this->tokenStorage->setToken(null);
         }
         $message = $failed->getMessage();
     }
     $response = new Response();
     $response->setContent($message);
     $response->setStatusCode(403);
     $event->setResponse($response);
 }
開發者ID:feriatos,項目名稱:api-key-bundle,代碼行數:33,代碼來源:ApiKeyListener.php

示例8: handle

 /**
  * {@inheritdoc}
  */
 public function handle(GetResponseEvent $event)
 {
     $request = $event->getRequest();
     $wsseRegex = '/UsernameToken Username="([^"]+)", PasswordDigest="([^"]+)", Nonce="([^"]+)", Created="([^"]+)"/';
     if (!$request->headers->has('x-wsse') || 1 !== preg_match($wsseRegex, $request->headers->get('x-wsse'), $matches)) {
         $response = new Response();
         $response->setStatusCode(Response::HTTP_FORBIDDEN);
         $event->setResponse($response);
         return;
     }
     $token = new WsseToken();
     $token->setUser($matches[1]);
     $token->digest = $matches[2];
     $token->nonce = $matches[3];
     $token->created = $matches[4];
     try {
         $authToken = $this->authenticationManager->authenticate($token);
         $this->tokenStorage->setToken($authToken);
         return;
     } catch (AuthenticationException $failed) {
         //TODO: LOG
     }
     // By default deny authorization
     $response = new Response();
     $response->setStatusCode(Response::HTTP_FORBIDDEN);
     $event->setResponse($response);
 }
開發者ID:snieditions,項目名稱:rest-api,代碼行數:30,代碼來源:WsseListener.php

示例9: handle

 /**
  * {@InheritDoc}
  */
 public function handle(GetResponseEvent $event)
 {
     $request = $event->getRequest();
     if (!$request->headers->has('x-wsse')) {
         return;
     }
     $wsseRegex = '/UsernameToken Username="([^"]+)", PasswordDigest="([^"]+)", Nonce="([^"]+)", Created="([^"]+)"/';
     if (1 !== preg_match($wsseRegex, $request->headers->get('x-wsse'), $matches)) {
         return;
     }
     $token = new WsseUserToken();
     $token->setUser($matches[1]);
     $token->digest = $matches[2];
     $token->nonce = $matches[3];
     $token->created = $matches[4];
     try {
         $authToken = $this->authenticationManager->authenticate($token);
         $this->tokenStorage->setToken($authToken);
         return;
     } catch (AuthenticationException $e) {
         throw $e;
         // To deny the authentication clear the token. This will redirect to the login page.
         // Make sure to only clear your token, not those of other authentication listeners.
         // $token = $this->tokenStorage->getToken();
         // if ($token instanceof WsseUserToken && $this->providerKey === $token->getProviderKey()) {
         //     $this->tokenStorage->setToken(null);
         // }
         // return;
     }
     // By default deny authorization
     $response = new Response();
     $response->setStatusCode(Response::HTTP_FORBIDDEN);
     $event->setResponse($response);
 }
開發者ID:alcalyn,項目名稱:symfony-wsse,代碼行數:37,代碼來源:WsseListener.php

示例10: handle

 /**
  * {@inheritdoc}
  */
 public function handle(GetResponseEvent $event)
 {
     $request = $event->getRequest();
     if (null !== ($authorization = $request->headers->get($this->authenticationHeaderName))) {
         $headerParts = array_map('trim', explode(' ', $authorization, 2));
         if (2 === count($headerParts)) {
             $credentialParts = explode(':', $headerParts[1]);
             if (2 === count($credentialParts)) {
                 $token = new HmacUserToken();
                 $token->setServiceLabel($headerParts[0]);
                 $token->setUser($credentialParts[0]);
                 $token->setSignature($credentialParts[1]);
                 $token->setRequest($request);
                 try {
                     $authenticatedToken = $this->authenticationManager->authenticate($token);
                     // Call setToken() on an instance of SecurityContextInterface or TokenStorageInterface (>=2.6)
                     $this->tokenStorage->setToken($authenticatedToken);
                     // Success
                     return;
                 } catch (AuthenticationException $exception) {
                 }
             }
         }
     }
     $event->setResponse(new Response(null, 401));
 }
開發者ID:wridgers,項目名稱:GremoHmacAuthenticationBundle,代碼行數:29,代碼來源:HmacAuthenticationListener.php

示例11: handle

 /**
  * @param GetResponseEvent $event
  *
  * @return bool
  */
 public function handle(GetResponseEvent $event)
 {
     $request = $event->getRequest();
     switch ($this->getAuthType()) {
         case self::AUTH_TYPE_HEADER:
             $requestToken = $request->headers->get($this->getAuthKeyName());
             break;
         case self::AUTH_TYPE_REQUEST:
             $requestToken = $request->get($this->getAuthKeyName());
             break;
         default:
             throw new InvalidConfigurationException("Unknown auth type" . $this->getAuthType());
     }
     if (!$requestToken) {
         throw new AuthenticationException('Empty authentication token');
     }
     $requestTokenData = $this->parseAuthToken($requestToken);
     $token = new SignedUserToken();
     $token->setUser($requestTokenData[0]);
     $token->setSignature($requestTokenData[1]);
     $token->setRequest($request);
     try {
         $authenticatedToken = $this->authManager->authenticate($token);
         $this->tokenStorage->setToken($authenticatedToken);
         return true;
     } catch (AuthenticationException $failed) {
     }
     $response = new Response('', Response::HTTP_FORBIDDEN);
     $event->setResponse($response);
     return false;
 }
開發者ID:epustobaev,項目名稱:signed-auth-bundle,代碼行數:36,代碼來源:SignedAuthListener.php

示例12: handle

 /**
  * This interface must be implemented by firewall listeners.
  *
  * @param GetResponseEvent $event
  */
 public function handle(GetResponseEvent $event)
 {
     $request = $event->getRequest();
     if ($request->headers->has(static::AUTH_HEADER)) {
         $unauthenticatedToken = new ApiUserToken();
         $unauthenticatedToken->setAttribute('key', $request->headers->get(static::AUTH_HEADER));
         $authenticatedToken = $this->authenticationManager->authenticate($unauthenticatedToken);
         $this->tokenStorage->setToken($authenticatedToken);
     }
 }
開發者ID:GrizliK1988,項目名稱:symfony-certification-prepare-project,代碼行數:15,代碼來源:ApiAuthenticationListener.php

示例13: handle

 /**
  * @param GetResponseEvent $event
  */
 public function handle(GetResponseEvent $event)
 {
     parent::handle($event);
     $request = $event->getRequest();
     $session = $request->getSession();
     if ($session->has('requested_logout')) {
         $session->invalidate();
         $this->securityTokenStorage->setToken(null);
         $event->setResponse($this->authenticationEntryPoint->start($request));
     }
 }
開發者ID:WeavingTheWeb,項目名稱:devobs,代碼行數:14,代碼來源:BasicAuthenticationListener.php

示例14: setUpSecurityContext

 /**
  * Initializes security context with an instance of a UserInterface 
  * returned by $this->getUser()
  *
  * Method will be called automatically from ApplicationTestCase and
  * WebTestCase from this bundle and their children
  */
 protected function setUpSecurityContext()
 {
     if (empty($this->container) || !$this->container instanceof ContainerInterface) {
         throw new \RuntimeException("Use the trait with a class that has an access to the container (ContainerInterface).");
     }
     $this->tokenStorage = $this->container->get('security.token_storage');
     $user = $this->getUser();
     if ($user != null && $user instanceof UserInterface) {
         $this->tokenStorage->setToken(new UsernamePasswordToken($user, null, $this->getFirewallContext(), $user->getRoles()));
     }
 }
開發者ID:tomaskadlec,項目名稱:symfony-test,代碼行數:18,代碼來源:SecurityAwareTrait.php

示例15: checkAuthentication

 /**
  * Checks if a Wordpress user is authenticated and authenticate him into Symfony security context.
  *
  * @param Request $request
  */
 protected function checkAuthentication(Request $request)
 {
     if (!$request->hasPreviousSession()) {
         return;
     }
     $session = $request->getSession();
     if ($session->has('token')) {
         $token = $session->get('token');
         $this->tokenStorage->setToken($token);
     }
 }
開發者ID:parenthesislab,項目名稱:ParenthesisWPBundle,代碼行數:16,代碼來源:WordpressRequestListener.php


注:本文中的Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface::setToken方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。