Check if a request is configured to use OAuth2.

@param RequestInterface $request
@param array            $options

@return bool
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;
    }
364042
public function onRequest( RequestInterface $request ) {
        $localizedUri = Uri::withQueryValue( $request->getUri(), 'lang', $this->getEndpoint()->getLang() );
        return $request->withUri($localizedUri);
    }
428992
public function validateAuthorizationHeader(Request $request)
    {
        try {
            return parent::validateAuthorizationHeader($request);
        } catch (BadRequestHttpException $exception) {
            if (empty($request->query('token', ''))) {
                throw $exception;
            }
        }

        return true;
    }
890692
private function shouldAuthenticate(ServerRequestInterface $request): bool
    {
        /* If any of the rules in stack return false will not authenticate */
        foreach ($this->rules as $callable) {
            if (false === $callable($request)) {
                return false;
            }
        }
        return true;
    }
601340
public function filterRequest(RequestInterface $request)
    {
        if ($this->pattern->matchRequest($request)) {
            $this->securityInterceptor->invoke();
            return true;
        }
        return false;
    }
1286517
public function request($method, $uri, $options = [])
    {
        if ($this->authorization !== null) {
            $authHeader = sprintf('%s %s', $this->authorization->getAuthType(), $this->authorization->getCredentials());
            $headers = isset($options['headers']) ? $options['headers'] : [];
            $headers['Authorization'] = $authHeader;
            $options['headers'] = $headers;
        }

        if (!isset($options['retries'])) {
            $options['retries'] = $this->maxRetries;
        }

        return $this->requestHub($method, $uri, $options);
    }
97281
public function useAuthentication($auth, array $options = [])
    {
        if (!($auth instanceof AuthStrategyInterface)) {
            $auth = $this->getAuthFactory()->get($auth);
            $auth->setOptions($options);
        }

        $this->dispatcher->addListener(Events::ON_REQUEST_EVENT, [
            $auth,
            'auth'
        ]);
    }
1175444
private function setOAuthHeaders(RequestInterface $request): RequestInterface
    {
        $now = new \DateTime();
        if ($this->now instanceof \DateTime) {
            $now = clone $this->now;
        }

        //Generate HTTP headers
        $encodedKey = \rawurlencode($this->oauthConsumerSecret).'&'.\rawurlencode($this->oauthAccessTokenSecret);
        $oauthParams = [
            'oauth_consumer_key' => $this->oauthConsumerKey,
            'oauth_token' => $this->oauthAccessToken,
            'oauth_nonce' => \sha1(\microtime(true).\rand(10000, 99999)),
            'oauth_timestamp' => $now->getTimestamp(),
            'oauth_signature_method' => 'PLAINTEXT',
            'oauth_version' => '1.0',
            'oauth_signature' => $encodedKey,
        ];

        $request = $request->withHeader('Authorization', $this->encodeOAuthHeaders($oauthParams));

        return $request->withHeader('Expect', '');
    }
1316945
private function query(RequestInterface $request)
    {
        return $this->client->send($request, array_merge(
            [RequestOptions::HTTP_ERRORS => false],
            $this->clientOptions
        ));
    }
179190
protected function authentication(RequestInterface $request)
	{
		$uri = $request->getUri();
		$key = http_build_query(['key' => $this->getAccessKey()], null, '&');

		if (strlen($uri->getQuery()) > 0)
		{
			$key = '&'.$key;
		}

		return $request->withUri($uri->withQuery($uri->getQuery().$key));
	}
211870
public function matchRequest(RequestInterface $request)
    {
        if (!$request instanceof ActionRequest) {
            return false;
        }
        if (!isset($this->options['uriPattern'])) {
            throw new InvalidRequestPatternException('Missing option "uriPattern" in the Uri request pattern configuration', 1446224530);
        }
        return (boolean)preg_match('/^' . str_replace('/', '\/', $this->options['uriPattern']) . '$/', $request->getHttpRequest()->getUri()->getPath());
    }
430274
public function send(Request $request, Response $response, array $options = array()) {
    $this->setupAuth($request);
    $this->_client->send($request, $response, $options);
    return $response;
  }
308750
protected function checkResponse(RequestInterface $request, array $options = [], ResponseInterface $response)
    {
        if (!$this->needVerification($response)) {
            return $response;
        }

        if (empty($options['cookies'])) {
            throw new Exception('you have to use cookies');
        }

        if (empty($options['allow_redirects'])) {
            throw new Exception('you have to use the allow_redirects options');
        }

        return $this($this->modifyRequest($request, $response), $options);
    }
842649
public function handle(RequestInterface $request): RequestInterface
    {
        if ($this->token === '' || $this->key === '') {
            throw new AuthorizationException('Could not create api token, missing or empty arguments');
        }
        return $request->withHeader($this->key, $this->token);
    }
1304628
protected function multi($requests, $options = array()) {
        if ($this->accesstoken) {
            $this->setHeader('Authorization: Bearer '.$this->accesstoken->token);
        }
        return parent::multi($requests, $options);
    }
405525
public function checkRedirect(
        RequestInterface $request,
        array $options,
        ResponseInterface $response
    ) {
        if (substr($response->getStatusCode(), 0, 1) != '3'
            || !$response->hasHeader('Location')
        ) {
            return $response;
        }

        $this->guardMax($request, $options);
        $nextRequest = $this->modifyRequest($request, $options, $response);

        if (isset($options['allow_redirects']['on_redirect'])) {
            call_user_func(
                $options['allow_redirects']['on_redirect'],
                $request,
                $response,
                $nextRequest->getUri()
            );
        }

        /** @var PromiseInterface|ResponseInterface $promise */
        $promise = $this($nextRequest, $options);

        // Add headers to be able to track history of redirects.
        if (!empty($options['allow_redirects']['track_redirects'])) {
            return $this->withTracking(
                $promise,
                (string) $nextRequest->getUri(),
                $response->getStatusCode()
            );
        }

        return $promise;
    }
411076
public function verifyResourceRequest(RequestInterface $request, ResponseInterface $response, $scope = null)
    {
        $token = $this->getAccessTokenData($request, $response);

        // Check if we have token data
        if (is_null($token)) {
            return false;
        }

        /**
         * Check scope, if provided
         * If token doesn't have a scope, it's null/empty, or it's insufficient, then throw 403
         * @see http://tools.ietf.org/html/rfc6750#section-3.1
         */
        if ($scope && (!isset($token["scope"]) || !$token["scope"] || !$this->scopeUtil->checkScope($scope, $token["scope"]))) {
            $response->setError(403, 'insufficient_scope', 'The request requires higher privileges than provided by the access token');
            $response->addHttpHeaders(array(
                'WWW-Authenticate' => sprintf('%s realm="%s", scope="%s", error="%s", error_description="%s"',
                    $this->tokenType->getTokenType(),
                    $this->config['www_realm'],
                    $scope,
                    $response->getParameter('error'),
                    $response->getParameter('error_description')
                )
            ));

            return false;
        }

        // allow retrieval of the token
        $this->token = $token;

        return (bool) $token;
    }