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 '

public Google\Auth\Middleware\ScopedAccessTokenMiddleware::DEFAULT_CACHE_LIFETIME = 1500
public __construct(callable $tokenFunc, $scopes, ?array $cacheConfig = NULL, ?Psr\Cache\CacheItemPoolInterface $cache = NULL)

Creates a new ScopedAccessTokenMiddleware.

    public __invoke(callable $handler)

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

    E.g this could be used to authenticate using the AppEngine

    use google\appengine\api\app_identity\AppIdentityService;
    use Google\Auth\Middleware\ScopedAccessTokenMiddleware;
    use GuzzleHttp\Client;
    use GuzzleHttp\HandlerStack;

    $scope = ''
    $middleware = new ScopedAccessTokenMiddleware(

    [ 'prefix' => 'Google\Auth\ScopedAccessToken::' ],
    $cache = new Memcache()

    $stack = HandlerStack::create();

    $client = new Client([

    'handler' => $stack,
    'base_url' => '',
    'auth' => 'scoped' // authorize all requests


    $res = $client->get('myproject/taskqueues/myqueue');

    • return Closure
    private $cache
    • var CacheItemPoolInterface
    private $cacheConfig
    • var array configuration
    private $maxKeyLength
    private $scopes
    • var array|string
    private $tokenFunc
    • var callable
    private fetchToken()

    Determine if token is available in the cache, if not call tokenFunc to
    fetch it.

    • return string
    private getCachedValue( $k)

    Gets the cached value if it is present in the cache when that is

    private getCacheKey()
    • return string
    private getFullCacheKey( $key)
    private setCachedValue( $k, $v)

    Saves the value in the cache when that is available.

    © 2020 Bruce Wells
    Search Namespaces \ Classes
    ConfigurationNumbers (0-9.) only