ScopedAccessTokenMiddleware
in package
uses
CacheTrait
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
Table of Contents
Constants
- DEFAULT_CACHE_LIFETIME = 1500
Properties
- $cache : CacheItemPoolInterface
- $cacheConfig : array<string|int, mixed>
- $maxKeyLength : mixed
- $scopes : array<string|int, mixed>|string
- $tokenFunc : callable
Methods
- __construct() : mixed
- Creates a new ScopedAccessTokenMiddleware.
- __invoke() : Closure
- Updates the request with an Authorization header when auth is 'scoped'.
- fetchToken() : string
- Determine if token is available in the cache, if not call tokenFunc to fetch it.
- getCachedValue() : mixed
- Gets the cached value if it is present in the cache when that is available.
- getCacheKey() : string
- getFullCacheKey() : mixed
- setCachedValue() : mixed
- Saves the value in the cache when that is available.
Constants
DEFAULT_CACHE_LIFETIME
public
mixed
DEFAULT_CACHE_LIFETIME
= 1500
Properties
$cache
private
CacheItemPoolInterface
$cache
$cacheConfig
private
array<string|int, mixed>
$cacheConfig
configuration
$maxKeyLength
private
mixed
$maxKeyLength
= 64
$scopes
private
array<string|int, mixed>|string
$scopes
$tokenFunc
private
callable
$tokenFunc
Methods
__construct()
Creates a new ScopedAccessTokenMiddleware.
public
__construct(callable $tokenFunc, array<string|int, mixed>|string $scopes[, array<string|int, mixed> $cacheConfig = null ][, CacheItemPoolInterface $cache = null ]) : mixed
Parameters
- $tokenFunc : callable
-
a token generator function
- $scopes : array<string|int, mixed>|string
-
the token authentication scopes
- $cacheConfig : array<string|int, mixed> = null
-
configuration for the cache when it's present
- $cache : CacheItemPoolInterface = null
-
an implementation of CacheItemPoolInterface
__invoke()
Updates the request with an Authorization header when auth is 'scoped'.
public
__invoke(callable $handler) : Closure
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');
Parameters
- $handler : callable
Return values
ClosurefetchToken()
Determine if token is available in the cache, if not call tokenFunc to fetch it.
private
fetchToken() : string
Return values
stringgetCachedValue()
Gets the cached value if it is present in the cache when that is available.
private
getCachedValue(mixed $k) : mixed
Parameters
- $k : mixed
getCacheKey()
private
getCacheKey() : string
Return values
stringgetFullCacheKey()
private
getFullCacheKey(mixed $key) : mixed
Parameters
- $key : mixed
setCachedValue()
Saves the value in the cache when that is available.
private
setCachedValue(mixed $k, mixed $v) : mixed
Parameters
- $k : mixed
- $v : mixed