1<?php
2
3namespace Google\AuthHandler;
4
5use Google\Auth\CredentialsLoader;
6use Google\Auth\HttpHandler\HttpHandlerFactory;
7use Google\Auth\FetchAuthTokenCache;
8use Google\Auth\Middleware\AuthTokenMiddleware;
9use Google\Auth\Middleware\ScopedAccessTokenMiddleware;
10use Google\Auth\Middleware\SimpleMiddleware;
11use GuzzleHttp\Client;
12use GuzzleHttp\ClientInterface;
13use Psr\Cache\CacheItemPoolInterface;
14
15/**
16* This supports Guzzle 6
17*/
18class Guzzle6AuthHandler
19{
20  protected $cache;
21  protected $cacheConfig;
22
23  public function __construct(CacheItemPoolInterface $cache = null, array $cacheConfig = [])
24  {
25    $this->cache = $cache;
26    $this->cacheConfig = $cacheConfig;
27  }
28
29  public function attachCredentials(
30      ClientInterface $http,
31      CredentialsLoader $credentials,
32      callable $tokenCallback = null
33  ) {
34    // use the provided cache
35    if ($this->cache) {
36      $credentials = new FetchAuthTokenCache(
37          $credentials,
38          $this->cacheConfig,
39          $this->cache
40      );
41    }
42
43    return $this->attachCredentialsCache($http, $credentials, $tokenCallback);
44  }
45
46  public function attachCredentialsCache(
47      ClientInterface $http,
48      FetchAuthTokenCache $credentials,
49      callable $tokenCallback = null
50  ) {
51    // if we end up needing to make an HTTP request to retrieve credentials, we
52    // can use our existing one, but we need to throw exceptions so the error
53    // bubbles up.
54    $authHttp = $this->createAuthHttp($http);
55    $authHttpHandler = HttpHandlerFactory::build($authHttp);
56    $middleware = new AuthTokenMiddleware(
57        $credentials,
58        $authHttpHandler,
59        $tokenCallback
60    );
61
62    $config = $http->getConfig();
63    $config['handler']->remove('google_auth');
64    $config['handler']->push($middleware, 'google_auth');
65    $config['auth'] = 'google_auth';
66    $http = new Client($config);
67
68    return $http;
69  }
70
71  public function attachToken(ClientInterface $http, array $token, array $scopes)
72  {
73    $tokenFunc = function ($scopes) use ($token) {
74      return $token['access_token'];
75    };
76
77    $middleware = new ScopedAccessTokenMiddleware(
78        $tokenFunc,
79        $scopes,
80        $this->cacheConfig,
81        $this->cache
82    );
83
84    $config = $http->getConfig();
85    $config['handler']->remove('google_auth');
86    $config['handler']->push($middleware, 'google_auth');
87    $config['auth'] = 'scoped';
88    $http = new Client($config);
89
90    return $http;
91  }
92
93  public function attachKey(ClientInterface $http, $key)
94  {
95    $middleware = new SimpleMiddleware(['key' => $key]);
96
97    $config = $http->getConfig();
98    $config['handler']->remove('google_auth');
99    $config['handler']->push($middleware, 'google_auth');
100    $config['auth'] = 'simple';
101    $http = new Client($config);
102
103    return $http;
104  }
105
106  private function createAuthHttp(ClientInterface $http)
107  {
108    return new Client(
109        [
110          'base_uri' => $http->getConfig('base_uri'),
111          'http_errors' => true,
112          'verify' => $http->getConfig('verify'),
113          'proxy' => $http->getConfig('proxy'),
114        ]
115    );
116  }
117}
118