Add authentication to an HTTP request.

@param \Psr\Http\Message\RequestInterface      $request
@param \League\OAuth2\Client\Token\AccessToken $token

@return \Psr\Http\Message\RequestInterface
500635
private function authenticateRequest(RequestInterface $request, AccessToken $token)
    {
        foreach ($this->provider->getHeaders($token->getToken()) as $name => $value) {
            $request = $request->withHeader($name, $value);
        }

        return $request;
    }
625214
public function sendRequest(RequestInterface $request):ResponseInterface{

		// get authorization only if we request the provider API
		if(strpos((string)$request->getUri(), $this->apiURL) === 0){
			$token = $this->storage->getAccessToken($this->serviceName);

			// attempt to refresh an expired token
			if($this instanceof TokenRefresh && $this->options->tokenAutoRefresh && ($token->isExpired() || $token->expires === $token::EOL_UNKNOWN)){
				$token = $this->refreshAccessToken($token);
			}

			$request = $this->getRequestAuthorization($request, $token);
		}

		return $this->http->sendRequest($request);
	}
1339712
public function withToken(Token $token)
    {
        $this->grant = 'provided_token';
        $this->token = new AccessToken([
            'access_token' => $token->jwt(),
            'expires' => $token->expires()->timestamp,
        ]);

        return $this;
    }
164004
public function onAuthenticationSuccess( Request $request, TokenInterface $token )
	{
		// if AJAX login
		if ( ! $request->isXmlHttpRequest() ) {
			throw new NotFoundHttpException('Must use AJAX Login');
		}

		return new JsonResponse(array(
			"success" => true
		));
	}
697225
public function onAuthenticationSuccess(Request $request, TokenInterface $token)
    {
        
        // if AJAX login
        if ( $request->isXmlHttpRequest() ) {

                $array = array( 'success' => true ); // data to return via JSON
                $response = new Response( json_encode( $array ) );
                $response->headers->set( 'Content-Type', 'application/json' );
                return $response;

        } 
        if ($this->hasRole('ROLE_ADMIN', $token->getUser()) || $this->hasRole('ROLE_SUPER_ADMIN', $token->getUser())) {
            return new RedirectResponse($this->container->get('router')->generate('admin_default_dashboard'));
        } elseif ($this->hasRole('ROLE_COMPANY', $token->getUser())) {
            return new RedirectResponse($this->container->get('router')->generate('company_default_dashboard'));
        } else {
            // set context with GET method of the previous ajax call
            $context = $this->container->get('router')->getContext();
            $currentMethod = $context->getMethod();
            $context->setMethod('GET');

            // match route
            $referer = $this->getRefererPath($request);
            $match = $this->container->get('router')->match($referer);
            // set back original http method
            $context->setMethod($currentMethod);
            
            if (isset($match['_route']) && $match['_route'] == 'ecommerce_checkout_identification' && isset($match['_locale'])) {
                return new RedirectResponse($this->container->get('router')->generate('ecommerce_checkout_deliveryinfo', array('_locale' => $match['_locale'])));
            }
            return new RedirectResponse($this->container->get('router')->generate('index'));
        }
    }
208526
public function authorize(RequestInterface $request)
    {
        if (!$area = $this->findMatchingArea($request)) {
            return $this;
        }

        if (!$this->user) {
            throw new AuthorizationException(sprintf('Access denied to area "%s". No authenticated user', $area->pattern()));
        }

        if (!$area->authorize($this->user, $request->clientIp())) {
            throw new AuthorizationException(sprintf('Access denied to area "%s". Authenticated user does not have access', $area->pattern()));
        }

        return $this;
    }
174726
private function isOAuth2(RequestInterface $request, array $options)
    {
        // The 'auth' option must be set to 'oauth2'.
        if (!isset($options['auth']) || $options['auth'] !== 'oauth2') {
            return false;
        }

        // The request must be not for an access token endpoint.
        if ($this->provider->getBaseAccessTokenUrl([]) === $request->getUri()) {
            return false;
        }

        return true;
    }
1083855
public function sign(RequestInterface $request)
    {
        $oAuthParams = array_merge(
            $this->getDefaultOAuthParams(),
            ['oauth_signature_method' => $this->signatureMethod->getName()]
        );

        $oAuthParams['oauth_signature'] = $this->signatureMethod->buildSignature(
            $request,
            $oAuthParams,
            $this->consumer,
            $this->token
        );

        return $request->withHeader(
            'authorization',
            $this->buildAuthorizationHeader($oAuthParams)
        );
    }
175100
protected function authentication(RequestInterface $request)
	{
		if ($this->tokenRequired)
		{
			return $request->withHeader('Authorization', sprintf('OAuth %s', $this->getAccessToken()));
		}

		return $request;
	}
757389
public function setAccessToken(\Gpupo\CommonSchema\ORM\Entity\Application\API\OAuth\Client\AccessToken $accessToken = null)
    {
        $this->access_token = $accessToken;

        return $this;
    }
71091
public function setAccessToken($accessToken)
    {
        $this->_accessToken = $accessToken;
        $this->_request->setAccessToken($this->_accessToken);
        $this->_request->setAccessTokenName('component_access_token');
        return $this;
    }
243696
public function setAccessToken($accessToken)
    {
        $this->accessToken = $accessToken;
        if ($accessToken instanceof AccessToken) {
            $this->accessToken = $accessToken->getValue();
        }

        return $this;
    }
259623
public function handle(RequestInterface $request): RequestInterface
    {
        return
            $request->withHeader(
                'Authorization',
                sprintf('Basic %s', base64_encode(sprintf('%s:%s', $this->user, $this->password)))
            );
    }
160033
public static function create($accessToken = '')
    {
        $client = ClientFactory::create();
        $responseTransformer = new ArrayBodyTransformer();
        $apiExceptionTransformer = new GuzzleTransformer();

        return new Api($client, $responseTransformer, $apiExceptionTransformer, $accessToken);
    }
587107
public function storeAccessToken(string $service, AccessToken $token):OAuthStorageInterface{
		$token = $token->toJSON();

		if(isset($_SESSION[$this->sessionVar]) && is_array($_SESSION[$this->sessionVar])){
			$_SESSION[$this->sessionVar][$service] = $token;
		}
		else{
			$_SESSION[$this->sessionVar] = [$service => $token];
		}

		return $this;
	}
560443
protected function saveToken(AccessToken $token)
    {
        if ($this->config['api_token'] && $this->config['api_token_type'] === 'access') {
            return;
        }
        foreach ($token->jsonSerialize() as $name => $value) {
            if (isset($this->storageKeys[$name])) {
                $this->session->set($this->storageKeys[$name], $value);
            }
        }
        $this->session->save();
    }
546368
public function send(\Psr\Http\Message\RequestInterface $request)
    {
        if ($this->credentials instanceof Container) {
            if (strpos($request->getUri()->getPath(), '/v1/calls') === 0) {
                $request = $request->withHeader('Authorization', 'Bearer '.$this->credentials->get(Keypair::class)->generateJwt());
            } else {
                $request = self::authRequest($request, $this->credentials->get(Basic::class));
            }
        } elseif ($this->credentials instanceof Keypair) {
            $request = $request->withHeader('Authorization', 'Bearer '.$this->credentials->get(Keypair::class)->generateJwt());
        } elseif ($this->credentials instanceof SharedSecret) {
            $request = self::signRequest($request, $this->credentials);
        } elseif ($this->credentials instanceof Basic) {
            $request = self::authRequest($request, $this->credentials);
        }

        //todo: add oauth support

        //allow any part of the URI to be replaced with a simple search
        if (isset($this->options['url'])) {
            foreach ($this->options['url'] as $search => $replace) {
                $uri = (string) $request->getUri();

                $new = str_replace($search, $replace, $uri);
                if ($uri !== $new) {
                    $request = $request->withUri(new Uri($new));
                }
            }
        }

        $response = $this->client->sendRequest($request);

        return $response;
    }