PluginProbe
WP-Stateless – Google Cloud Storage / 3.2.1
WP-Stateless – Google Cloud Storage v3.2.1
4.4.3 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.3.0 2.3.1 2.3.2 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.2.0 3.2.1 3.2.2 All 62 releases
wp-stateless / lib / Google / src / AuthHandler / Guzzle6AuthHandler.php

Guzzle6AuthHandler.php in WP-Stateless – Google Cloud Storage 3.2.1, at lib/Google/src/AuthHandler/Guzzle6AuthHandler.php

116 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 $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 'base_uri' => $http->getConfig('base_uri'),
110 'http_errors' => true,
111 'verify' => $http->getConfig('verify'),
112 'proxy' => $http->getConfig('proxy'),
113 ]);
114 }
115 }
116