1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Google\Auth\CredentialsLoader; |
4
|
|
|
use Google\Auth\HttpHandler\HttpHandlerFactory; |
5
|
|
|
use Google\Auth\FetchAuthTokenCache; |
6
|
|
|
use Google\Auth\Middleware\AuthTokenMiddleware; |
7
|
|
|
use Google\Auth\Middleware\ScopedAccessTokenMiddleware; |
8
|
|
|
use Google\Auth\Middleware\SimpleMiddleware; |
9
|
|
|
use GuzzleHttp\Client; |
10
|
|
|
use GuzzleHttp\ClientInterface; |
11
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* |
15
|
|
|
*/ |
16
|
|
|
class Google_AuthHandler_Guzzle6AuthHandler |
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
|
|
|
$middleware = new AuthTokenMiddleware( |
46
|
|
|
$credentials, |
47
|
|
|
$authHttpHandler, |
48
|
|
|
$tokenCallback |
49
|
|
|
); |
50
|
|
|
|
51
|
|
|
$config = $http->getConfig(); |
52
|
|
|
$config['handler']->remove('google_auth'); |
53
|
|
|
$config['handler']->push($middleware, 'google_auth'); |
54
|
|
|
$config['auth'] = 'google_auth'; |
55
|
|
|
$http = new Client($config); |
56
|
|
|
|
57
|
|
|
return $http; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function attachToken(ClientInterface $http, array $token, array $scopes) |
61
|
|
|
{ |
62
|
|
|
$tokenFunc = function ($scopes) use ($token) { |
|
|
|
|
63
|
|
|
return $token['access_token']; |
64
|
|
|
}; |
65
|
|
|
|
66
|
|
|
$middleware = new ScopedAccessTokenMiddleware( |
67
|
|
|
$tokenFunc, |
68
|
|
|
$scopes, |
69
|
|
|
$this->cacheConfig, |
70
|
|
|
$this->cache |
71
|
|
|
); |
72
|
|
|
|
73
|
|
|
$config = $http->getConfig(); |
74
|
|
|
$config['handler']->remove('google_auth'); |
75
|
|
|
$config['handler']->push($middleware, 'google_auth'); |
76
|
|
|
$config['auth'] = 'scoped'; |
77
|
|
|
$http = new Client($config); |
78
|
|
|
|
79
|
|
|
return $http; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function attachKey(ClientInterface $http, $key) |
83
|
|
|
{ |
84
|
|
|
$middleware = new SimpleMiddleware(['key' => $key]); |
85
|
|
|
|
86
|
|
|
$config = $http->getConfig(); |
87
|
|
|
$config['handler']->remove('google_auth'); |
88
|
|
|
$config['handler']->push($middleware, 'google_auth'); |
89
|
|
|
$config['auth'] = 'simple'; |
90
|
|
|
$http = new Client($config); |
91
|
|
|
|
92
|
|
|
return $http; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
private function createAuthHttp(ClientInterface $http) |
96
|
|
|
{ |
97
|
|
|
return new Client( |
98
|
|
|
[ |
99
|
|
|
'base_uri' => $http->getConfig('base_uri'), |
100
|
|
|
'exceptions' => true, |
101
|
|
|
'verify' => $http->getConfig('verify'), |
102
|
|
|
'proxy' => $http->getConfig('proxy'), |
103
|
|
|
] |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.