2727use Google \Auth \Credentials \UserRefreshCredentials ;
2828use Google \Auth \CredentialsLoader ;
2929use Google \Auth \FetchAuthTokenCache ;
30+ use Google \Auth \GetUniverseDomainInterface ;
3031use Google \Auth \HttpHandler \HttpHandlerFactory ;
3132use Google \Auth \OAuth2 ;
3233use Google \AuthHandler \AuthHandlerFactory ;
@@ -131,6 +132,10 @@ class Client
131132 * @type string $developer_key
132133 * Simple API access key, also from the API console. Ensure you get
133134 * a Server key, and not a Browser key.
135+ * **NOTE:** The universe domain is assumed to be "googleapis.com" unless
136+ * explicitly set. When setting an API ley directly via this option, there
137+ * is no way to verify the universe domain. Be sure to set the
138+ * "universe_domain" option if "googleapis.com" is not intended.
134139 * @type bool $use_application_default_credentials
135140 * For use with Google Cloud Platform
136141 * fetch the ApplicationDefaultCredentials, if applicable
@@ -164,6 +169,10 @@ class Client
164169 * @type bool $api_format_v2
165170 * Setting api_format_v2 will return more detailed error messages
166171 * from certain APIs.
172+ * @type string $universe_domain
173+ * Setting the universe domain will change the default rootUrl of the service.
174+ * If not set explicitly, the universe domain will be the value provided in the
175+ *. "GOOGLE_CLOUD_UNIVERSE_DOMAIN" environment variable, or "googleapis.com".
167176 * }
168177 */
169178 public function __construct (array $ config = [])
@@ -197,7 +206,9 @@ public function __construct(array $config = [])
197206 'cache_config ' => [],
198207 'token_callback ' => null ,
199208 'jwt ' => null ,
200- 'api_format_v2 ' => false
209+ 'api_format_v2 ' => false ,
210+ 'universe_domain ' => getenv ('GOOGLE_CLOUD_UNIVERSE_DOMAIN ' )
211+ ?: GetUniverseDomainInterface::DEFAULT_UNIVERSE_DOMAIN ,
201212 ], $ config );
202213
203214 if (!is_null ($ this ->config ['credentials ' ])) {
@@ -449,6 +460,7 @@ public function authorize(ClientInterface $http = null)
449460 // 3b. If access token exists but is expired, try to refresh it
450461 // 4. Check for API Key
451462 if ($ this ->credentials ) {
463+ $ this ->checkUniverseDomain ($ this ->credentials );
452464 return $ authHandler ->attachCredentials (
453465 $ http ,
454466 $ this ->credentials ,
@@ -458,6 +470,7 @@ public function authorize(ClientInterface $http = null)
458470
459471 if ($ this ->isUsingApplicationDefaultCredentials ()) {
460472 $ credentials = $ this ->createApplicationDefaultCredentials ();
473+ $ this ->checkUniverseDomain ($ credentials );
461474 return $ authHandler ->attachCredentialsCache (
462475 $ http ,
463476 $ credentials ,
@@ -473,6 +486,7 @@ public function authorize(ClientInterface $http = null)
473486 $ scopes ,
474487 $ token ['refresh_token ' ]
475488 );
489+ $ this ->checkUniverseDomain ($ credentials );
476490 return $ authHandler ->attachCredentials (
477491 $ http ,
478492 $ credentials ,
@@ -525,6 +539,11 @@ public function isUsingApplicationDefaultCredentials()
525539 * as calling `clear()` will remove all cache items, including any items not
526540 * related to Google API PHP Client.)
527541 *
542+ * **NOTE:** The universe domain is assumed to be "googleapis.com" unless
543+ * explicitly set. When setting an access token directly via this method, there
544+ * is no way to verify the universe domain. Be sure to set the "universe_domain"
545+ * option if "googleapis.com" is not intended.
546+ *
528547 * @param string|array $token
529548 * @throws InvalidArgumentException
530549 */
@@ -1318,4 +1337,23 @@ private function createUserRefreshCredentials($scope, $refreshToken)
13181337
13191338 return new UserRefreshCredentials ($ scope , $ creds );
13201339 }
1340+
1341+ private function checkUniverseDomain ($ credentials )
1342+ {
1343+ $ credentialsUniverse = $ credentials instanceof GetUniverseDomainInterface
1344+ ? $ credentials ->getUniverseDomain ()
1345+ : GetUniverseDomainInterface::DEFAULT_UNIVERSE_DOMAIN ;
1346+ if ($ credentialsUniverse !== $ this ->getUniverseDomain ()) {
1347+ throw new DomainException (sprintf (
1348+ 'The configured universe domain (%s) does not match the credential universe domain (%s) ' ,
1349+ $ this ->getUniverseDomain (),
1350+ $ credentialsUniverse
1351+ ));
1352+ }
1353+ }
1354+
1355+ public function getUniverseDomain ()
1356+ {
1357+ return $ this ->config ['universe_domain ' ];
1358+ }
13211359}
0 commit comments