Aucune description

Guzzle6AuthHandler.php 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. use Google\Auth\CredentialsLoader;
  3. use Google\Auth\HttpHandler\HttpHandlerFactory;
  4. use Google\Auth\FetchAuthTokenCache;
  5. use Google\Auth\Middleware\AuthTokenMiddleware;
  6. use Google\Auth\Middleware\ScopedAccessTokenMiddleware;
  7. use Google\Auth\Middleware\SimpleMiddleware;
  8. use GuzzleHttp\Client;
  9. use GuzzleHttp\ClientInterface;
  10. use Psr\Cache\CacheItemPoolInterface;
  11. /**
  12. *
  13. */
  14. class Google_AuthHandler_Guzzle6AuthHandler
  15. {
  16. protected $cache;
  17. protected $cacheConfig;
  18. public function __construct(CacheItemPoolInterface $cache = null, array $cacheConfig = [])
  19. {
  20. $this->cache = $cache;
  21. $this->cacheConfig = $cacheConfig;
  22. }
  23. public function attachCredentials(
  24. ClientInterface $http,
  25. CredentialsLoader $credentials,
  26. callable $tokenCallback = null
  27. ) {
  28. // use the provided cache
  29. if ($this->cache) {
  30. $credentials = new FetchAuthTokenCache(
  31. $credentials,
  32. $this->cacheConfig,
  33. $this->cache
  34. );
  35. }
  36. // if we end up needing to make an HTTP request to retrieve credentials, we
  37. // can use our existing one, but we need to throw exceptions so the error
  38. // bubbles up.
  39. $authHttp = $this->createAuthHttp($http);
  40. $authHttpHandler = HttpHandlerFactory::build($authHttp);
  41. $middleware = new AuthTokenMiddleware(
  42. $credentials,
  43. $authHttpHandler,
  44. $tokenCallback
  45. );
  46. $config = $http->getConfig();
  47. $config['handler']->remove('google_auth');
  48. $config['handler']->push($middleware, 'google_auth');
  49. $config['auth'] = 'google_auth';
  50. $http = new Client($config);
  51. return $http;
  52. }
  53. public function attachToken(ClientInterface $http, array $token, array $scopes)
  54. {
  55. $tokenFunc = function ($scopes) use ($token) {
  56. return $token['access_token'];
  57. };
  58. $middleware = new ScopedAccessTokenMiddleware(
  59. $tokenFunc,
  60. $scopes,
  61. $this->cacheConfig,
  62. $this->cache
  63. );
  64. $config = $http->getConfig();
  65. $config['handler']->remove('google_auth');
  66. $config['handler']->push($middleware, 'google_auth');
  67. $config['auth'] = 'scoped';
  68. $http = new Client($config);
  69. return $http;
  70. }
  71. public function attachKey(ClientInterface $http, $key)
  72. {
  73. $middleware = new SimpleMiddleware(['key' => $key]);
  74. $config = $http->getConfig();
  75. $config['handler']->remove('google_auth');
  76. $config['handler']->push($middleware, 'google_auth');
  77. $config['auth'] = 'simple';
  78. $http = new Client($config);
  79. return $http;
  80. }
  81. private function createAuthHttp(ClientInterface $http)
  82. {
  83. return new Client(
  84. [
  85. 'base_uri' => $http->getConfig('base_uri'),
  86. 'exceptions' => true,
  87. 'verify' => $http->getConfig('verify'),
  88. 'proxy' => $http->getConfig('proxy'),
  89. ]
  90. );
  91. }
  92. }