1 | <?php
|
---|
2 |
|
---|
3 | namespace Google\AuthHandler;
|
---|
4 |
|
---|
5 | use Google\Auth\CredentialsLoader;
|
---|
6 | use Google\Auth\FetchAuthTokenCache;
|
---|
7 | use Google\Auth\HttpHandler\HttpHandlerFactory;
|
---|
8 | use Google\Auth\Middleware\AuthTokenMiddleware;
|
---|
9 | use Google\Auth\Middleware\ScopedAccessTokenMiddleware;
|
---|
10 | use Google\Auth\Middleware\SimpleMiddleware;
|
---|
11 | use GuzzleHttp\Client;
|
---|
12 | use GuzzleHttp\ClientInterface;
|
---|
13 | use Psr\Cache\CacheItemPoolInterface;
|
---|
14 |
|
---|
15 | /**
|
---|
16 | * This supports Guzzle 6
|
---|
17 | */
|
---|
18 | class 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 | // Derive a cache prefix from the token, to ensure setting a new token
|
---|
78 | // results in a cache-miss.
|
---|
79 | // Note: Supplying a custom "prefix" will bust this behavior.
|
---|
80 | $cacheConfig = $this->cacheConfig;
|
---|
81 | if (!isset($cacheConfig['prefix']) && isset($token['access_token'])) {
|
---|
82 | $cacheConfig['prefix'] = substr(sha1($token['access_token']), -10);
|
---|
83 | }
|
---|
84 |
|
---|
85 | $middleware = new ScopedAccessTokenMiddleware(
|
---|
86 | $tokenFunc,
|
---|
87 | $scopes,
|
---|
88 | $cacheConfig,
|
---|
89 | $this->cache
|
---|
90 | );
|
---|
91 |
|
---|
92 | $config = $http->getConfig();
|
---|
93 | $config['handler']->remove('google_auth');
|
---|
94 | $config['handler']->push($middleware, 'google_auth');
|
---|
95 | $config['auth'] = 'scoped';
|
---|
96 | $http = new Client($config);
|
---|
97 |
|
---|
98 | return $http;
|
---|
99 | }
|
---|
100 |
|
---|
101 | public function attachKey(ClientInterface $http, $key)
|
---|
102 | {
|
---|
103 | $middleware = new SimpleMiddleware(['key' => $key]);
|
---|
104 |
|
---|
105 | $config = $http->getConfig();
|
---|
106 | $config['handler']->remove('google_auth');
|
---|
107 | $config['handler']->push($middleware, 'google_auth');
|
---|
108 | $config['auth'] = 'simple';
|
---|
109 | $http = new Client($config);
|
---|
110 |
|
---|
111 | return $http;
|
---|
112 | }
|
---|
113 |
|
---|
114 | private function createAuthHttp(ClientInterface $http)
|
---|
115 | {
|
---|
116 | return new Client(['http_errors' => true] + $http->getConfig());
|
---|
117 | }
|
---|
118 | }
|
---|