1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Google\Auth\CredentialsLoader; |
4
|
|
|
use Google\Auth\HttpHandler\HttpHandlerFactory; |
5
|
|
|
use Google\Auth\FetchAuthTokenCache; |
6
|
|
|
use Google\Auth\Subscriber\AuthTokenSubscriber; |
7
|
|
|
use Google\Auth\Subscriber\ScopedAccessTokenSubscriber; |
8
|
|
|
use Google\Auth\Subscriber\SimpleSubscriber; |
9
|
|
|
use GuzzleHttp\Client; |
10
|
|
|
use GuzzleHttp\ClientInterface; |
11
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* |
15
|
|
|
*/ |
16
|
|
|
class Google_AuthHandler_Guzzle5AuthHandler |
17
|
|
|
{ |
18
|
|
|
protected $cache; |
19
|
|
|
protected $cacheConfig; |
20
|
|
|
|
21
|
|
|
public function __construct(CacheItemPoolInterface $cache = null, array $cacheConfig = []) |
22
|
|
|
{ |
23
|
|
|
$this->cache = $cache; |
24
|
|
|
$this->cacheConfig = $cacheConfig; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function attachCredentials( |
28
|
|
|
ClientInterface $http, |
29
|
|
|
CredentialsLoader $credentials, |
30
|
|
|
callable $tokenCallback = null |
31
|
|
|
) { |
32
|
|
|
// use the provided cache |
33
|
|
|
if ($this->cache) { |
34
|
|
|
$credentials = new FetchAuthTokenCache( |
35
|
|
|
$credentials, |
36
|
|
|
$this->cacheConfig, |
37
|
|
|
$this->cache |
38
|
|
|
); |
39
|
|
|
} |
40
|
|
|
// if we end up needing to make an HTTP request to retrieve credentials, we |
41
|
|
|
// can use our existing one, but we need to throw exceptions so the error |
42
|
|
|
// bubbles up. |
43
|
|
|
$authHttp = $this->createAuthHttp($http); |
44
|
|
|
$authHttpHandler = HttpHandlerFactory::build($authHttp); |
45
|
|
|
$subscriber = new AuthTokenSubscriber( |
46
|
|
|
$credentials, |
47
|
|
|
$authHttpHandler, |
48
|
|
|
$tokenCallback |
49
|
|
|
); |
50
|
|
|
|
51
|
|
|
$http->setDefaultOption('auth', 'google_auth'); |
52
|
|
|
$http->getEmitter()->attach($subscriber); |
53
|
|
|
|
54
|
|
|
return $http; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function attachToken(ClientInterface $http, array $token, array $scopes) |
58
|
|
|
{ |
59
|
|
|
$tokenFunc = function ($scopes) use ($token) { |
|
|
|
|
60
|
|
|
return $token['access_token']; |
61
|
|
|
}; |
62
|
|
|
|
63
|
|
|
$subscriber = new ScopedAccessTokenSubscriber( |
64
|
|
|
$tokenFunc, |
65
|
|
|
$scopes, |
66
|
|
|
$this->cacheConfig, |
67
|
|
|
$this->cache |
68
|
|
|
); |
69
|
|
|
|
70
|
|
|
$http->setDefaultOption('auth', 'scoped'); |
71
|
|
|
$http->getEmitter()->attach($subscriber); |
72
|
|
|
|
73
|
|
|
return $http; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function attachKey(ClientInterface $http, $key) |
77
|
|
|
{ |
78
|
|
|
$subscriber = new SimpleSubscriber(['key' => $key]); |
79
|
|
|
|
80
|
|
|
$http->setDefaultOption('auth', 'simple'); |
81
|
|
|
$http->getEmitter()->attach($subscriber); |
82
|
|
|
|
83
|
|
|
return $http; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
private function createAuthHttp(ClientInterface $http) |
87
|
|
|
{ |
88
|
|
|
return new Client( |
89
|
|
|
[ |
90
|
|
|
'base_url' => $http->getBaseUrl(), |
91
|
|
|
'defaults' => [ |
92
|
|
|
'exceptions' => true, |
93
|
|
|
'verify' => $http->getDefaultOption('verify'), |
94
|
|
|
'proxy' => $http->getDefaultOption('proxy'), |
95
|
|
|
] |
96
|
|
|
] |
97
|
|
|
); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.