ScopedAccessTokenSubscriber
in package
implements
SubscriberInterface
uses
CacheTrait
ScopedAccessTokenSubscriber is a Guzzle Subscriber 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
Interfaces
- SubscriberInterface
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 ScopedAccessTokenSubscriber.
- getEvents() : array<string|int, mixed>
- onBefore() : mixed
- 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
$maxKeyLength
private
mixed
$maxKeyLength
= 64
$scopes
private
array<string|int, mixed>|string
$scopes
The scopes used to generate the token
$tokenFunc
private
callable
$tokenFunc
The access token generator function
Methods
__construct()
Creates a new ScopedAccessTokenSubscriber.
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
getEvents()
public
getEvents() : array<string|int, mixed>
Return values
array<string|int, mixed>onBefore()
Updates the request with an Authorization header when auth is 'scoped'.
public
onBefore(BeforeEvent $event) : mixed
E.g this could be used to authenticate using the AppEngine AppIdentityService.
Example:
use google\appengine\api\app_identity\AppIdentityService;
use Google\Auth\Subscriber\ScopedAccessTokenSubscriber;
use GuzzleHttp\Client;
$scope = 'https://www.googleapis.com/auth/taskqueue'
$subscriber = new ScopedAccessToken(
'AppIdentityService::getAccessToken',
$scope,
['prefix' => 'Google\Auth\ScopedAccessToken::'],
$cache = new Memcache()
);
$client = new Client([
'base_url' => 'https://www.googleapis.com/taskqueue/v1beta2/projects/',
'defaults' => ['auth' => 'scoped']
]);
$client->getEmitter()->attach($subscriber);
$res = $client->get('myproject/taskqueues/myqueue');
Parameters
- $event : BeforeEvent
fetchToken()
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