PluginProbe
Atarim – AI Agency for WordPress: Edit Pages, Fix Code, Update Plugins, SEO & Client Feedback / 3.4.3
Atarim – AI Agency for WordPress: Edit Pages, Fix Code, Update Plugins, SEO & Client Feedback v3.4.3
5.1.3 5.1.2 5.1.1 5.1 5.0 trunk 3.10 3.11 3.12 3.13 3.14 3.15 3.16 3.17 3.18 3.19 3.2.0 3.2.1 3.22 3.22.1 3.22.2 3.22.3 3.22.4 3.22.5 3.22.6 All 75 releases
atarim-visual-collaboration / src / AuthHandler / Guzzle5AuthHandler.php
Guzzle5AuthHandler.php
109 lines 3.0 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\Subscriber\AuthTokenSubscriber;
9 use Google\Auth\Subscriber\ScopedAccessTokenSubscriber;
10 use Google\Auth\Subscriber\SimpleSubscriber;
11 use GuzzleHttp\Client;
12 use GuzzleHttp\ClientInterface;
13 use Psr\Cache\CacheItemPoolInterface;
14
15 /**
16 * This supports Guzzle 5
17 */
18 class 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 'base_url' => $http->getBaseUrl(),
101 'defaults' => [
102 'exceptions' => true,
103 'verify' => $http->getDefaultOption('verify'),
104 'proxy' => $http->getDefaultOption('proxy'),
105 ]
106 ]);
107 }
108 }
109