/library/Google/vendor/google/auth/src/ApplicationDefaultCredentials.php

https://gitlab.com/vangtrangbac123/zinkaiuit.tk · PHP · 173 lines · 61 code · 11 blank · 101 comment · 4 complexity · 961ee005871e0789ea4f85a2dd051940 MD5 · raw file

  1. <?php
  2. /*
  3. * Copyright 2015 Google Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. namespace Google\Auth;
  18. use DomainException;
  19. use Google\Auth\Credentials\AppIdentityCredentials;
  20. use Google\Auth\Credentials\GCECredentials;
  21. use Google\Auth\Middleware\AuthTokenMiddleware;
  22. use Google\Auth\Subscriber\AuthTokenSubscriber;
  23. use Psr\Cache\CacheItemPoolInterface;
  24. /**
  25. * ApplicationDefaultCredentials obtains the default credentials for
  26. * authorizing a request to a Google service.
  27. *
  28. * Application Default Credentials are described here:
  29. * https://developers.google.com/accounts/docs/application-default-credentials
  30. *
  31. * This class implements the search for the application default credentials as
  32. * described in the link.
  33. *
  34. * It provides three factory methods:
  35. * - #get returns the computed credentials object
  36. * - #getSubscriber returns an AuthTokenSubscriber built from the credentials object
  37. * - #getMiddleware returns an AuthTokenMiddleware built from the credentials object
  38. *
  39. * This allows it to be used as follows with GuzzleHttp\Client:
  40. *
  41. * use Google\Auth\ApplicationDefaultCredentials;
  42. * use GuzzleHttp\Client;
  43. * use GuzzleHttp\HandlerStack;
  44. *
  45. * $middleware = ApplicationDefaultCredentials::getMiddleware(
  46. * 'https://www.googleapis.com/auth/taskqueue'
  47. * );
  48. * $stack = HandlerStack::create();
  49. * $stack->push($middleware);
  50. *
  51. * $client = new Client([
  52. * 'handler' => $stack,
  53. * 'base_uri' => 'https://www.googleapis.com/taskqueue/v1beta2/projects/',
  54. * 'auth' => 'google_auth' // authorize all requests
  55. * ]);
  56. *
  57. * $res = $client->get('myproject/taskqueues/myqueue');
  58. */
  59. class ApplicationDefaultCredentials
  60. {
  61. /**
  62. * Obtains an AuthTokenSubscriber that uses the default FetchAuthTokenInterface
  63. * implementation to use in this environment.
  64. *
  65. * If supplied, $scope is used to in creating the credentials instance if
  66. * this does not fallback to the compute engine defaults.
  67. *
  68. * @param string|array scope the scope of the access request, expressed
  69. * either as an Array or as a space-delimited String.
  70. * @param callable $httpHandler callback which delivers psr7 request
  71. * @param array $cacheConfig configuration for the cache when it's present
  72. * @param CacheItemPoolInterface $cache an implementation of CacheItemPoolInterface
  73. *
  74. * @return AuthTokenSubscriber
  75. *
  76. * @throws DomainException if no implementation can be obtained.
  77. */
  78. public static function getSubscriber(
  79. $scope = null,
  80. callable $httpHandler = null,
  81. array $cacheConfig = null,
  82. CacheItemPoolInterface $cache = null
  83. ) {
  84. $creds = self::getCredentials($scope, $httpHandler, $cacheConfig, $cache);
  85. return new AuthTokenSubscriber($creds, $cacheConfig);
  86. }
  87. /**
  88. * Obtains an AuthTokenMiddleware that uses the default FetchAuthTokenInterface
  89. * implementation to use in this environment.
  90. *
  91. * If supplied, $scope is used to in creating the credentials instance if
  92. * this does not fallback to the compute engine defaults.
  93. *
  94. * @param string|array scope the scope of the access request, expressed
  95. * either as an Array or as a space-delimited String.
  96. * @param callable $httpHandler callback which delivers psr7 request
  97. * @param array $cacheConfig configuration for the cache when it's present
  98. * @param CacheItemPoolInterface $cache
  99. *
  100. * @return AuthTokenMiddleware
  101. *
  102. * @throws DomainException if no implementation can be obtained.
  103. */
  104. public static function getMiddleware(
  105. $scope = null,
  106. callable $httpHandler = null,
  107. array $cacheConfig = null,
  108. CacheItemPoolInterface $cache = null
  109. ) {
  110. $creds = self::getCredentials($scope, $httpHandler, $cacheConfig, $cache);
  111. return new AuthTokenMiddleware($creds, $cacheConfig);
  112. }
  113. /**
  114. * Obtains the default FetchAuthTokenInterface implementation to use
  115. * in this environment.
  116. *
  117. * If supplied, $scope is used to in creating the credentials instance if
  118. * this does not fallback to the Compute Engine defaults.
  119. *
  120. * @param string|array scope the scope of the access request, expressed
  121. * either as an Array or as a space-delimited String.
  122. * @param callable $httpHandler callback which delivers psr7 request
  123. * @param array $cacheConfig configuration for the cache when it's present
  124. * @param CacheItemPoolInterface $cache
  125. *
  126. * @return CredentialsLoader
  127. *
  128. * @throws DomainException if no implementation can be obtained.
  129. */
  130. public static function getCredentials(
  131. $scope = null,
  132. callable $httpHandler = null,
  133. array $cacheConfig = null,
  134. CacheItemPoolInterface $cache = null
  135. ) {
  136. $creds = null;
  137. $jsonKey = CredentialsLoader::fromEnv()
  138. ?: CredentialsLoader::fromWellKnownFile();
  139. if (!is_null($jsonKey)) {
  140. $creds = CredentialsLoader::makeCredentials($scope, $jsonKey);
  141. } elseif (AppIdentityCredentials::onAppEngine() && !GCECredentials::onAppEngineFlexible()) {
  142. $creds = new AppIdentityCredentials($scope);
  143. } elseif (GCECredentials::onGce($httpHandler)) {
  144. $creds = new GCECredentials();
  145. }
  146. if (is_null($creds)) {
  147. throw new \DomainException(self::notFound());
  148. }
  149. if (!is_null($cache)) {
  150. $creds = new FetchAuthTokenCache($creds, $cacheConfig, $cache);
  151. }
  152. return $creds;
  153. }
  154. private static function notFound()
  155. {
  156. $msg = 'Could not load the default credentials. Browse to ';
  157. $msg .= 'https://developers.google.com';
  158. $msg .= '/accounts/docs/application-default-credentials';
  159. $msg .= ' for more information';
  160. return $msg;
  161. }
  162. }