update
This commit is contained in:
130
wp-content/plugins/google-site-kit/third-party/google/auth/src/Middleware/AuthTokenMiddleware.php
vendored
Normal file
130
wp-content/plugins/google-site-kit/third-party/google/auth/src/Middleware/AuthTokenMiddleware.php
vendored
Normal file
@@ -0,0 +1,130 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright 2015 Google Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
namespace Google\Site_Kit_Dependencies\Google\Auth\Middleware;
|
||||
|
||||
use Google\Site_Kit_Dependencies\Google\Auth\FetchAuthTokenInterface;
|
||||
use Google\Site_Kit_Dependencies\Google\Auth\GetQuotaProjectInterface;
|
||||
use Google\Site_Kit_Dependencies\Psr\Http\Message\RequestInterface;
|
||||
/**
|
||||
* AuthTokenMiddleware is a Guzzle Middleware that adds an Authorization header
|
||||
* provided by an object implementing FetchAuthTokenInterface.
|
||||
*
|
||||
* The FetchAuthTokenInterface#fetchAuthToken is used to obtain a hash; one of
|
||||
* the values value in that hash is added as the authorization header.
|
||||
*
|
||||
* Requests will be accessed with the authorization header:
|
||||
*
|
||||
* 'authorization' 'Bearer <value of auth_token>'
|
||||
*/
|
||||
class AuthTokenMiddleware
|
||||
{
|
||||
/**
|
||||
* @var callable
|
||||
*/
|
||||
private $httpHandler;
|
||||
/**
|
||||
* @var FetchAuthTokenInterface
|
||||
*/
|
||||
private $fetcher;
|
||||
/**
|
||||
* @var ?callable
|
||||
*/
|
||||
private $tokenCallback;
|
||||
/**
|
||||
* Creates a new AuthTokenMiddleware.
|
||||
*
|
||||
* @param FetchAuthTokenInterface $fetcher is used to fetch the auth token
|
||||
* @param callable $httpHandler (optional) callback which delivers psr7 request
|
||||
* @param callable $tokenCallback (optional) function to be called when a new token is fetched.
|
||||
*/
|
||||
public function __construct(\Google\Site_Kit_Dependencies\Google\Auth\FetchAuthTokenInterface $fetcher, callable $httpHandler = null, callable $tokenCallback = null)
|
||||
{
|
||||
$this->fetcher = $fetcher;
|
||||
$this->httpHandler = $httpHandler;
|
||||
$this->tokenCallback = $tokenCallback;
|
||||
}
|
||||
/**
|
||||
* Updates the request with an Authorization header when auth is 'google_auth'.
|
||||
*
|
||||
* use Google\Auth\Middleware\AuthTokenMiddleware;
|
||||
* use Google\Auth\OAuth2;
|
||||
* use GuzzleHttp\Client;
|
||||
* use GuzzleHttp\HandlerStack;
|
||||
*
|
||||
* $config = [..<oauth config param>.];
|
||||
* $oauth2 = new OAuth2($config)
|
||||
* $middleware = new AuthTokenMiddleware($oauth2);
|
||||
* $stack = HandlerStack::create();
|
||||
* $stack->push($middleware);
|
||||
*
|
||||
* $client = new Client([
|
||||
* 'handler' => $stack,
|
||||
* 'base_uri' => 'https://www.googleapis.com/taskqueue/v1beta2/projects/',
|
||||
* 'auth' => 'google_auth' // authorize all requests
|
||||
* ]);
|
||||
*
|
||||
* $res = $client->get('myproject/taskqueues/myqueue');
|
||||
*
|
||||
* @param callable $handler
|
||||
* @return \Closure
|
||||
*/
|
||||
public function __invoke(callable $handler)
|
||||
{
|
||||
return function (\Google\Site_Kit_Dependencies\Psr\Http\Message\RequestInterface $request, array $options) use($handler) {
|
||||
// Requests using "auth"="google_auth" will be authorized.
|
||||
if (!isset($options['auth']) || $options['auth'] !== 'google_auth') {
|
||||
return $handler($request, $options);
|
||||
}
|
||||
$request = $request->withHeader('authorization', 'Bearer ' . $this->fetchToken());
|
||||
if ($quotaProject = $this->getQuotaProject()) {
|
||||
$request = $request->withHeader(\Google\Site_Kit_Dependencies\Google\Auth\GetQuotaProjectInterface::X_GOOG_USER_PROJECT_HEADER, $quotaProject);
|
||||
}
|
||||
return $handler($request, $options);
|
||||
};
|
||||
}
|
||||
/**
|
||||
* Call fetcher to fetch the token.
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
private function fetchToken()
|
||||
{
|
||||
$auth_tokens = (array) $this->fetcher->fetchAuthToken($this->httpHandler);
|
||||
if (\array_key_exists('access_token', $auth_tokens)) {
|
||||
// notify the callback if applicable
|
||||
if ($this->tokenCallback) {
|
||||
\call_user_func($this->tokenCallback, $this->fetcher->getCacheKey(), $auth_tokens['access_token']);
|
||||
}
|
||||
return $auth_tokens['access_token'];
|
||||
}
|
||||
if (\array_key_exists('id_token', $auth_tokens)) {
|
||||
return $auth_tokens['id_token'];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
private function getQuotaProject()
|
||||
{
|
||||
if ($this->fetcher instanceof \Google\Site_Kit_Dependencies\Google\Auth\GetQuotaProjectInterface) {
|
||||
return $this->fetcher->getQuotaProject();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright 2015 Google Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
namespace Google\Site_Kit_Dependencies\Google\Auth\Middleware;
|
||||
|
||||
use Google\Site_Kit_Dependencies\Google\Auth\FetchAuthTokenInterface;
|
||||
use Google\Site_Kit_Dependencies\Google\Auth\GetQuotaProjectInterface;
|
||||
use Google\Site_Kit_Dependencies\Psr\Http\Message\RequestInterface;
|
||||
/**
|
||||
* ProxyAuthTokenMiddleware is a Guzzle Middleware that adds an Authorization header
|
||||
* provided by an object implementing FetchAuthTokenInterface.
|
||||
*
|
||||
* The FetchAuthTokenInterface#fetchAuthToken is used to obtain a hash; one of
|
||||
* the values value in that hash is added as the authorization header.
|
||||
*
|
||||
* Requests will be accessed with the authorization header:
|
||||
*
|
||||
* 'proxy-authorization' 'Bearer <value of auth_token>'
|
||||
*/
|
||||
class ProxyAuthTokenMiddleware
|
||||
{
|
||||
/**
|
||||
* @var callable
|
||||
*/
|
||||
private $httpHandler;
|
||||
/**
|
||||
* @var FetchAuthTokenInterface
|
||||
*/
|
||||
private $fetcher;
|
||||
/**
|
||||
* @var ?callable
|
||||
*/
|
||||
private $tokenCallback;
|
||||
/**
|
||||
* Creates a new ProxyAuthTokenMiddleware.
|
||||
*
|
||||
* @param FetchAuthTokenInterface $fetcher is used to fetch the auth token
|
||||
* @param callable $httpHandler (optional) callback which delivers psr7 request
|
||||
* @param callable $tokenCallback (optional) function to be called when a new token is fetched.
|
||||
*/
|
||||
public function __construct(\Google\Site_Kit_Dependencies\Google\Auth\FetchAuthTokenInterface $fetcher, callable $httpHandler = null, callable $tokenCallback = null)
|
||||
{
|
||||
$this->fetcher = $fetcher;
|
||||
$this->httpHandler = $httpHandler;
|
||||
$this->tokenCallback = $tokenCallback;
|
||||
}
|
||||
/**
|
||||
* Updates the request with an Authorization header when auth is 'google_auth'.
|
||||
*
|
||||
* use Google\Auth\Middleware\ProxyAuthTokenMiddleware;
|
||||
* use Google\Auth\OAuth2;
|
||||
* use GuzzleHttp\Client;
|
||||
* use GuzzleHttp\HandlerStack;
|
||||
*
|
||||
* $config = [..<oauth config param>.];
|
||||
* $oauth2 = new OAuth2($config)
|
||||
* $middleware = new ProxyAuthTokenMiddleware($oauth2);
|
||||
* $stack = HandlerStack::create();
|
||||
* $stack->push($middleware);
|
||||
*
|
||||
* $client = new Client([
|
||||
* 'handler' => $stack,
|
||||
* 'base_uri' => 'https://www.googleapis.com/taskqueue/v1beta2/projects/',
|
||||
* 'proxy_auth' => 'google_auth' // authorize all requests
|
||||
* ]);
|
||||
*
|
||||
* $res = $client->get('myproject/taskqueues/myqueue');
|
||||
*
|
||||
* @param callable $handler
|
||||
* @return \Closure
|
||||
*/
|
||||
public function __invoke(callable $handler)
|
||||
{
|
||||
return function (\Google\Site_Kit_Dependencies\Psr\Http\Message\RequestInterface $request, array $options) use($handler) {
|
||||
// Requests using "proxy_auth"="google_auth" will be authorized.
|
||||
if (!isset($options['proxy_auth']) || $options['proxy_auth'] !== 'google_auth') {
|
||||
return $handler($request, $options);
|
||||
}
|
||||
$request = $request->withHeader('proxy-authorization', 'Bearer ' . $this->fetchToken());
|
||||
if ($quotaProject = $this->getQuotaProject()) {
|
||||
$request = $request->withHeader(\Google\Site_Kit_Dependencies\Google\Auth\GetQuotaProjectInterface::X_GOOG_USER_PROJECT_HEADER, $quotaProject);
|
||||
}
|
||||
return $handler($request, $options);
|
||||
};
|
||||
}
|
||||
/**
|
||||
* Call fetcher to fetch the token.
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
private function fetchToken()
|
||||
{
|
||||
$auth_tokens = $this->fetcher->fetchAuthToken($this->httpHandler);
|
||||
if (\array_key_exists('access_token', $auth_tokens)) {
|
||||
// notify the callback if applicable
|
||||
if ($this->tokenCallback) {
|
||||
\call_user_func($this->tokenCallback, $this->fetcher->getCacheKey(), $auth_tokens['access_token']);
|
||||
}
|
||||
return $auth_tokens['access_token'];
|
||||
}
|
||||
if (\array_key_exists('id_token', $auth_tokens)) {
|
||||
return $auth_tokens['id_token'];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
/**
|
||||
* @return string|null;
|
||||
*/
|
||||
private function getQuotaProject()
|
||||
{
|
||||
if ($this->fetcher instanceof \Google\Site_Kit_Dependencies\Google\Auth\GetQuotaProjectInterface) {
|
||||
return $this->fetcher->getQuotaProject();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright 2015 Google Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
namespace Google\Site_Kit_Dependencies\Google\Auth\Middleware;
|
||||
|
||||
use Google\Site_Kit_Dependencies\Google\Auth\CacheTrait;
|
||||
use Google\Site_Kit_Dependencies\Psr\Cache\CacheItemPoolInterface;
|
||||
use Google\Site_Kit_Dependencies\Psr\Http\Message\RequestInterface;
|
||||
/**
|
||||
* ScopedAccessTokenMiddleware is a Guzzle Middleware that adds an Authorization
|
||||
* header provided by a closure.
|
||||
*
|
||||
* The closure returns an access token, taking the scope, either a single
|
||||
* string or an array of strings, as its value. If provided, a cache will be
|
||||
* used to preserve the access token for a given lifetime.
|
||||
*
|
||||
* Requests will be accessed with the authorization header:
|
||||
*
|
||||
* 'authorization' 'Bearer <value of auth_token>'
|
||||
*/
|
||||
class ScopedAccessTokenMiddleware
|
||||
{
|
||||
use CacheTrait;
|
||||
const DEFAULT_CACHE_LIFETIME = 1500;
|
||||
/**
|
||||
* @var callable
|
||||
*/
|
||||
private $tokenFunc;
|
||||
/**
|
||||
* @var array<string>|string
|
||||
*/
|
||||
private $scopes;
|
||||
/**
|
||||
* Creates a new ScopedAccessTokenMiddleware.
|
||||
*
|
||||
* @param callable $tokenFunc a token generator function
|
||||
* @param array<string>|string $scopes the token authentication scopes
|
||||
* @param array<mixed> $cacheConfig configuration for the cache when it's present
|
||||
* @param CacheItemPoolInterface $cache an implementation of CacheItemPoolInterface
|
||||
*/
|
||||
public function __construct(callable $tokenFunc, $scopes, array $cacheConfig = null, \Google\Site_Kit_Dependencies\Psr\Cache\CacheItemPoolInterface $cache = null)
|
||||
{
|
||||
$this->tokenFunc = $tokenFunc;
|
||||
if (!(\is_string($scopes) || \is_array($scopes))) {
|
||||
throw new \InvalidArgumentException('wants scope should be string or array');
|
||||
}
|
||||
$this->scopes = $scopes;
|
||||
if (!\is_null($cache)) {
|
||||
$this->cache = $cache;
|
||||
$this->cacheConfig = \array_merge(['lifetime' => self::DEFAULT_CACHE_LIFETIME, 'prefix' => ''], $cacheConfig);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Updates the request with an Authorization header when auth is 'scoped'.
|
||||
*
|
||||
* E.g this could be used to authenticate using the AppEngine
|
||||
* AppIdentityService.
|
||||
*
|
||||
* use google\appengine\api\app_identity\AppIdentityService;
|
||||
* use Google\Auth\Middleware\ScopedAccessTokenMiddleware;
|
||||
* use GuzzleHttp\Client;
|
||||
* use GuzzleHttp\HandlerStack;
|
||||
*
|
||||
* $scope = 'https://www.googleapis.com/auth/taskqueue'
|
||||
* $middleware = new ScopedAccessTokenMiddleware(
|
||||
* 'AppIdentityService::getAccessToken',
|
||||
* $scope,
|
||||
* [ 'prefix' => 'Google\Auth\ScopedAccessToken::' ],
|
||||
* $cache = new Memcache()
|
||||
* );
|
||||
* $stack = HandlerStack::create();
|
||||
* $stack->push($middleware);
|
||||
*
|
||||
* $client = new Client([
|
||||
* 'handler' => $stack,
|
||||
* 'base_url' => 'https://www.googleapis.com/taskqueue/v1beta2/projects/',
|
||||
* 'auth' => 'scoped' // authorize all requests
|
||||
* ]);
|
||||
*
|
||||
* $res = $client->get('myproject/taskqueues/myqueue');
|
||||
*
|
||||
* @param callable $handler
|
||||
* @return \Closure
|
||||
*/
|
||||
public function __invoke(callable $handler)
|
||||
{
|
||||
return function (\Google\Site_Kit_Dependencies\Psr\Http\Message\RequestInterface $request, array $options) use($handler) {
|
||||
// Requests using "auth"="scoped" will be authorized.
|
||||
if (!isset($options['auth']) || $options['auth'] !== 'scoped') {
|
||||
return $handler($request, $options);
|
||||
}
|
||||
$request = $request->withHeader('authorization', 'Bearer ' . $this->fetchToken());
|
||||
return $handler($request, $options);
|
||||
};
|
||||
}
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
private function getCacheKey()
|
||||
{
|
||||
$key = null;
|
||||
if (\is_string($this->scopes)) {
|
||||
$key .= $this->scopes;
|
||||
} elseif (\is_array($this->scopes)) {
|
||||
$key .= \implode(':', $this->scopes);
|
||||
}
|
||||
return $key;
|
||||
}
|
||||
/**
|
||||
* Determine if token is available in the cache, if not call tokenFunc to
|
||||
* fetch it.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function fetchToken()
|
||||
{
|
||||
$cacheKey = $this->getCacheKey();
|
||||
$cached = $this->getCachedValue($cacheKey);
|
||||
if (!empty($cached)) {
|
||||
return $cached;
|
||||
}
|
||||
$token = \call_user_func($this->tokenFunc, $this->scopes);
|
||||
$this->setCachedValue($cacheKey, $token);
|
||||
return $token;
|
||||
}
|
||||
}
|
||||
86
wp-content/plugins/google-site-kit/third-party/google/auth/src/Middleware/SimpleMiddleware.php
vendored
Normal file
86
wp-content/plugins/google-site-kit/third-party/google/auth/src/Middleware/SimpleMiddleware.php
vendored
Normal file
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright 2015 Google Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
namespace Google\Site_Kit_Dependencies\Google\Auth\Middleware;
|
||||
|
||||
use Google\Site_Kit_Dependencies\GuzzleHttp\Psr7\Query;
|
||||
use Google\Site_Kit_Dependencies\Psr\Http\Message\RequestInterface;
|
||||
/**
|
||||
* SimpleMiddleware is a Guzzle Middleware that implements Google's Simple API
|
||||
* access.
|
||||
*
|
||||
* Requests are accessed using the Simple API access developer key.
|
||||
*/
|
||||
class SimpleMiddleware
|
||||
{
|
||||
/**
|
||||
* @var array<mixed>
|
||||
*/
|
||||
private $config;
|
||||
/**
|
||||
* Create a new Simple plugin.
|
||||
*
|
||||
* The configuration array expects one option
|
||||
* - key: required, otherwise InvalidArgumentException is thrown
|
||||
*
|
||||
* @param array<mixed> $config Configuration array
|
||||
*/
|
||||
public function __construct(array $config)
|
||||
{
|
||||
if (!isset($config['key'])) {
|
||||
throw new \InvalidArgumentException('requires a key to have been set');
|
||||
}
|
||||
$this->config = \array_merge(['key' => null], $config);
|
||||
}
|
||||
/**
|
||||
* Updates the request query with the developer key if auth is set to simple.
|
||||
*
|
||||
* use Google\Auth\Middleware\SimpleMiddleware;
|
||||
* use GuzzleHttp\Client;
|
||||
* use GuzzleHttp\HandlerStack;
|
||||
*
|
||||
* $my_key = 'is not the same as yours';
|
||||
* $middleware = new SimpleMiddleware(['key' => $my_key]);
|
||||
* $stack = HandlerStack::create();
|
||||
* $stack->push($middleware);
|
||||
*
|
||||
* $client = new Client([
|
||||
* 'handler' => $stack,
|
||||
* 'base_uri' => 'https://www.googleapis.com/discovery/v1/',
|
||||
* 'auth' => 'simple'
|
||||
* ]);
|
||||
*
|
||||
* $res = $client->get('drive/v2/rest');
|
||||
*
|
||||
* @param callable $handler
|
||||
* @return \Closure
|
||||
*/
|
||||
public function __invoke(callable $handler)
|
||||
{
|
||||
return function (\Google\Site_Kit_Dependencies\Psr\Http\Message\RequestInterface $request, array $options) use($handler) {
|
||||
// Requests using "auth"="scoped" will be authorized.
|
||||
if (!isset($options['auth']) || $options['auth'] !== 'simple') {
|
||||
return $handler($request, $options);
|
||||
}
|
||||
$query = \Google\Site_Kit_Dependencies\GuzzleHttp\Psr7\Query::parse($request->getUri()->getQuery());
|
||||
$params = \array_merge($query, $this->config);
|
||||
$uri = $request->getUri()->withQuery(\Google\Site_Kit_Dependencies\GuzzleHttp\Psr7\Query::build($params));
|
||||
$request = $request->withUri($uri);
|
||||
return $handler($request, $options);
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user