class ScopedAccessTokenSubscriber implements SubscriberInterface (View source)

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 '

Traits

Constants

DEFAULT_CACHE_LIFETIME

Methods

__construct(callable $tokenFunc, array|string $scopes, array $cacheConfig = null, CacheItemPoolInterface $cache = null)

Creates a new ScopedAccessTokenSubscriber.

array
getEvents()

No description

onBefore(BeforeEvent $event)

Updates the request with an Authorization header when auth is 'scoped'.

Details

__construct(callable $tokenFunc, array|string $scopes, array $cacheConfig = null, CacheItemPoolInterface $cache = null)

Creates a new ScopedAccessTokenSubscriber.

Parameters

callable $tokenFunc

a token generator function

array|string $scopes

the token authentication scopes

array $cacheConfig

configuration for the cache when it's present

CacheItemPoolInterface $cache

an implementation of CacheItemPoolInterface

array getEvents()

No description

Return Value

array

onBefore(BeforeEvent $event)

Updates the request with an Authorization header when auth is 'scoped'.

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

BeforeEvent $event