本文整理汇总了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);
}
}
示例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));
}
}
}
示例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);
}
示例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();
}
}
示例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);
}
示例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);
}
}
示例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);
}
示例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);
}
示例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);
}
示例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));
}
示例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;
}
示例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));
}
}
示例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()));
}
}
示例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);
}
}