| 1 |
<?php |
| 2 |
|
| 3 |
namespace Google\AuthHandler; |
| 4 |
|
| 5 |
use Google\Auth\FetchAuthTokenCache; |
| 6 |
use Google\Auth\FetchAuthTokenInterface; |
| 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 |
FetchAuthTokenInterface $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->attachToHttp($http, $credentials, $tokenCallback); |
| 44 |
} |
| 45 |
|
| 46 |
public function attachCredentialsCache( |
| 47 |
ClientInterface $http, |
| 48 |
FetchAuthTokenCache $credentials, |
| 49 |
?callable $tokenCallback = null |
| 50 |
) { |
| 51 |
return $this->attachToHttp($http, $credentials, $tokenCallback); |
| 52 |
} |
| 53 |
|
| 54 |
private function attachToHttp( |
| 55 |
ClientInterface $http, |
| 56 |
FetchAuthTokenInterface $credentials, |
| 57 |
?callable $tokenCallback = null |
| 58 |
) { |
| 59 |
// if we end up needing to make an HTTP request to retrieve credentials, we |
| 60 |
// can use our existing one, but we need to throw exceptions so the error |
| 61 |
// bubbles up. |
| 62 |
$authHttp = $this->createAuthHttp($http); |
| 63 |
$authHttpHandler = HttpHandlerFactory::build($authHttp); |
| 64 |
$middleware = new AuthTokenMiddleware( |
| 65 |
$credentials, |
| 66 |
$authHttpHandler, |
| 67 |
$tokenCallback |
| 68 |
); |
| 69 |
|
| 70 |
$config = $http->getConfig(); |
| 71 |
$config['handler']->remove('google_auth'); |
| 72 |
$config['handler']->push($middleware, 'google_auth'); |
| 73 |
$config['auth'] = 'google_auth'; |
| 74 |
return new Client($config); |
| 75 |
} |
| 76 |
|
| 77 |
public function attachToken(ClientInterface $http, array $token, array $scopes) |
| 78 |
{ |
| 79 |
$tokenFunc = function ($scopes) use ($token) { |
| 80 |
return $token['access_token']; |
| 81 |
}; |
| 82 |
|
| 83 |
// Derive a cache prefix from the token, to ensure setting a new token |
| 84 |
// results in a cache-miss. |
| 85 |
// Note: Supplying a custom "prefix" will bust this behavior. |
| 86 |
$cacheConfig = $this->cacheConfig; |
| 87 |
if (!isset($cacheConfig['prefix']) && isset($token['access_token'])) { |
| 88 |
$cacheConfig['prefix'] = substr(sha1($token['access_token']), -10); |
| 89 |
} |
| 90 |
|
| 91 |
$middleware = new ScopedAccessTokenMiddleware( |
| 92 |
$tokenFunc, |
| 93 |
$scopes, |
| 94 |
$cacheConfig, |
| 95 |
$this->cache |
| 96 |
); |
| 97 |
|
| 98 |
$config = $http->getConfig(); |
| 99 |
$config['handler']->remove('google_auth'); |
| 100 |
$config['handler']->push($middleware, 'google_auth'); |
| 101 |
$config['auth'] = 'scoped'; |
| 102 |
$http = new Client($config); |
| 103 |
|
| 104 |
return $http; |
| 105 |
} |
| 106 |
|
| 107 |
public function attachKey(ClientInterface $http, $key) |
| 108 |
{ |
| 109 |
$middleware = new SimpleMiddleware(['key' => $key]); |
| 110 |
|
| 111 |
$config = $http->getConfig(); |
| 112 |
$config['handler']->remove('google_auth'); |
| 113 |
$config['handler']->push($middleware, 'google_auth'); |
| 114 |
$config['auth'] = 'simple'; |
| 115 |
$http = new Client($config); |
| 116 |
|
| 117 |
return $http; |
| 118 |
} |
| 119 |
|
| 120 |
private function createAuthHttp(ClientInterface $http) |
| 121 |
{ |
| 122 |
return new Client(['http_errors' => true] + $http->getConfig()); |
| 123 |
} |
| 124 |
} |
| 125 |
|