1<?php
2
3namespace Google\AuthHandler;
4
5use Google\Auth\CredentialsLoader;
6use Google\Auth\HttpHandler\HttpHandlerFactory;
7use Google\Auth\FetchAuthTokenCache;
8use Google\Auth\Subscriber\AuthTokenSubscriber;
9use Google\Auth\Subscriber\ScopedAccessTokenSubscriber;
10use Google\Auth\Subscriber\SimpleSubscriber;
11use GuzzleHttp\Client;
12use GuzzleHttp\ClientInterface;
13use Psr\Cache\CacheItemPoolInterface;
14
15/**
16*
17*/
18class Guzzle5AuthHandler
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    $subscriber = new AuthTokenSubscriber(
57        $credentials,
58        $authHttpHandler,
59        $tokenCallback
60    );
61
62    $http->setDefaultOption('auth', 'google_auth');
63    $http->getEmitter()->attach($subscriber);
64
65    return $http;
66  }
67
68  public function attachToken(ClientInterface $http, array $token, array $scopes)
69  {
70    $tokenFunc = function ($scopes) use ($token) {
71      return $token['access_token'];
72    };
73
74    $subscriber = new ScopedAccessTokenSubscriber(
75        $tokenFunc,
76        $scopes,
77        $this->cacheConfig,
78        $this->cache
79    );
80
81    $http->setDefaultOption('auth', 'scoped');
82    $http->getEmitter()->attach($subscriber);
83
84    return $http;
85  }
86
87  public function attachKey(ClientInterface $http, $key)
88  {
89    $subscriber = new SimpleSubscriber(['key' => $key]);
90
91    $http->setDefaultOption('auth', 'simple');
92    $http->getEmitter()->attach($subscriber);
93
94    return $http;
95  }
96
97  private function createAuthHttp(ClientInterface $http)
98  {
99    return new Client(
100        [
101          'base_url' => $http->getBaseUrl(),
102          'defaults' => [
103            'exceptions' => true,
104            'verify' => $http->getDefaultOption('verify'),
105            'proxy' => $http->getDefaultOption('proxy'),
106          ]
107        ]
108    );
109  }
110}
111