| 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 |
|