3535import com .google .api .client .json .JsonFactory ;
3636import com .google .api .client .json .JsonObjectParser ;
3737import com .google .api .client .util .Preconditions ;
38+ import com .google .auth .Credentials ;
3839import com .google .auth .http .HttpTransportFactory ;
40+ import com .google .common .base .MoreObjects ;
41+ import com .google .common .base .MoreObjects .ToStringHelper ;
3942import com .google .common .collect .ImmutableList ;
4043import com .google .errorprone .annotations .CanIgnoreReturnValue ;
4144import java .io .IOException ;
4750import java .util .HashMap ;
4851import java .util .List ;
4952import java .util .Map ;
53+ import java .util .Objects ;
5054import javax .annotation .Nullable ;
5155
5256/** Base type for credentials for authorizing calls to Google APIs using OAuth2. */
@@ -59,6 +63,8 @@ public class GoogleCredentials extends OAuth2Credentials implements QuotaProject
5963 static final String SERVICE_ACCOUNT_FILE_TYPE = "service_account" ;
6064 static final String GDCH_SERVICE_ACCOUNT_FILE_TYPE = "gdch_service_account" ;
6165
66+ private final String universeDomain ;
67+
6268 protected final String quotaProjectId ;
6369
6470 private static final DefaultCredentialsProvider defaultCredentialsProvider =
@@ -71,7 +77,10 @@ public class GoogleCredentials extends OAuth2Credentials implements QuotaProject
7177 * @return the credentials instance
7278 */
7379 public static GoogleCredentials create (AccessToken accessToken ) {
74- return GoogleCredentials .newBuilder ().setAccessToken (accessToken ).build ();
80+ return GoogleCredentials .newBuilder ()
81+ .setAccessToken (accessToken )
82+ .setUniverseDomain (Credentials .GOOGLE_DEFAULT_UNIVERSE )
83+ .build ();
7584 }
7685
7786 /**
@@ -170,6 +179,7 @@ public static GoogleCredentials fromStream(
170179 if (fileType == null ) {
171180 throw new IOException ("Error reading credentials from stream, 'type' field not specified." );
172181 }
182+
173183 if (USER_FILE_TYPE .equals (fileType )) {
174184 return UserCredentials .fromJson (fileContents , transportFactory );
175185 }
@@ -186,14 +196,20 @@ public static GoogleCredentials fromStream(
186196 fileType )) {
187197 return ExternalAccountAuthorizedUserCredentials .fromJson (fileContents , transportFactory );
188198 }
189- if ("impersonated_service_account" .equals (fileType )) {
199+ if (ImpersonatedCredentials . IMPERSONATED_CREDENTIALS_FILE_TYPE .equals (fileType )) {
190200 return ImpersonatedCredentials .fromJson (fileContents , transportFactory );
191201 }
192202 throw new IOException (
193203 String .format (
194204 "Error reading credentials from stream, 'type' value '%s' not recognized."
195- + " Expecting '%s' or '%s'." ,
196- fileType , USER_FILE_TYPE , SERVICE_ACCOUNT_FILE_TYPE ));
205+ + " Valid values are '%s', '%s', '%s', '%s', '%s', '%s'." ,
206+ fileType ,
207+ USER_FILE_TYPE ,
208+ SERVICE_ACCOUNT_FILE_TYPE ,
209+ GDCH_SERVICE_ACCOUNT_FILE_TYPE ,
210+ ExternalAccountCredentials .EXTERNAL_ACCOUNT_FILE_TYPE ,
211+ ExternalAccountAuthorizedUserCredentials .EXTERNAL_ACCOUNT_AUTHORIZED_USER_FILE_TYPE ,
212+ ImpersonatedCredentials .IMPERSONATED_CREDENTIALS_FILE_TYPE ));
197213 }
198214
199215 /**
@@ -206,6 +222,27 @@ public GoogleCredentials createWithQuotaProject(String quotaProject) {
206222 return this .toBuilder ().setQuotaProjectId (quotaProject ).build ();
207223 }
208224
225+ /**
226+ * Gets the universe domain for the credential.
227+ *
228+ * @return An explicit universe domain if it was explicitly provided, invokes the super
229+ * implementation otherwise
230+ */
231+ @ Override
232+ public String getUniverseDomain () throws IOException {
233+ return this .universeDomain ;
234+ }
235+
236+ /**
237+ * Checks if universe domain equals to {@link Credentials#GOOGLE_DEFAULT_UNIVERSE}.
238+ *
239+ * @return true if universeDomain equals to {@link Credentials#GOOGLE_DEFAULT_UNIVERSE}, false
240+ * otherwise
241+ */
242+ boolean isDefaultUniverseDomain () {
243+ return this .universeDomain .equals (Credentials .GOOGLE_DEFAULT_UNIVERSE );
244+ }
245+
209246 /**
210247 * Adds quota project ID to requestMetadata if present.
211248 *
@@ -237,33 +274,97 @@ protected GoogleCredentials() {
237274 this (new Builder ());
238275 }
239276
277+ /**
278+ * Constructor with an explicit access token and quotaProjectId.
279+ *
280+ * <p>Deprecated, please use the {@link GoogleCredentials#GoogleCredentials(Builder)} constructor
281+ * whenever possible.
282+ *
283+ * @param accessToken initial or temporary access token
284+ * @param quotaProjectId a quotaProjectId, a project id to be used for billing purposes
285+ */
286+ @ Deprecated
240287 protected GoogleCredentials (AccessToken accessToken , String quotaProjectId ) {
241288 super (accessToken );
242289 this .quotaProjectId = quotaProjectId ;
290+ this .universeDomain = Credentials .GOOGLE_DEFAULT_UNIVERSE ;
243291 }
244292
245293 /**
246294 * Constructor with explicit access token.
247295 *
248296 * @param accessToken initial or temporary access token
249297 */
298+ @ Deprecated
250299 public GoogleCredentials (AccessToken accessToken ) {
251300 this (accessToken , null );
252301 }
253302
303+ /**
304+ * Constructor that relies on a {@link GoogleCredential.Builder} to provide all the necessary
305+ * field values for initialization.
306+ *
307+ * @param builder an instance of a builder
308+ */
254309 protected GoogleCredentials (Builder builder ) {
255- this (builder .getAccessToken (), builder .getQuotaProjectId ());
310+ super (builder .getAccessToken ());
311+ this .quotaProjectId = builder .getQuotaProjectId ();
312+
313+ if (builder .universeDomain == null || builder .universeDomain .trim ().isEmpty ()) {
314+ this .universeDomain = Credentials .GOOGLE_DEFAULT_UNIVERSE ;
315+ } else {
316+ this .universeDomain = builder .getUniverseDomain ();
317+ }
256318 }
257319
258320 /**
259- * Constructor with explicit access token and refresh times
321+ * Constructor with explicit access token and refresh margins.
322+ *
323+ * <p>Deprecated, please use the {@link GoogleCredentials#GoogleCredentials(Builder)} constructor
324+ * whenever possible.
260325 *
261326 * @param accessToken initial or temporary access token
262327 */
328+ @ Deprecated
263329 protected GoogleCredentials (
264330 AccessToken accessToken , Duration refreshMargin , Duration expirationMargin ) {
265331 super (accessToken , refreshMargin , expirationMargin );
266332 this .quotaProjectId = null ;
333+ this .universeDomain = Credentials .GOOGLE_DEFAULT_UNIVERSE ;
334+ }
335+
336+ /**
337+ * A helper for overriding the toString() method. This allows inheritance of super class fields.
338+ * Extending classes can override this implementation and call super implementation and add more
339+ * fields. Same cannot be done with overriding the toString() directly.
340+ *
341+ * @return an instance of the ToStringHelper that has public fields added
342+ */
343+ protected ToStringHelper toStringHelper () {
344+ return MoreObjects .toStringHelper (this )
345+ .omitNullValues ()
346+ .add ("quotaProjectId" , this .quotaProjectId )
347+ .add ("universeDomain" , this .universeDomain );
348+ }
349+
350+ @ Override
351+ public String toString () {
352+ return toStringHelper ().toString ();
353+ }
354+
355+ @ Override
356+ public boolean equals (Object obj ) {
357+ if (!(obj instanceof GoogleCredentials )) {
358+ return false ;
359+ }
360+ GoogleCredentials other = (GoogleCredentials ) obj ;
361+ return Objects .equals (this .quotaProjectId , other .quotaProjectId )
362+ && Objects .equals (this .universeDomain , other .universeDomain );
363+ }
364+
365+ @ Override
366+ public int hashCode () {
367+ return Objects .hash (this .quotaProjectId , this .universeDomain );
267368 }
268369
269370 public static Builder newBuilder () {
@@ -348,12 +449,20 @@ public GoogleCredentials createDelegated(String user) {
348449
349450 public static class Builder extends OAuth2Credentials .Builder {
350451 @ Nullable protected String quotaProjectId ;
452+ @ Nullable protected String universeDomain ;
351453
352454 protected Builder () {}
353455
354456 protected Builder (GoogleCredentials credentials ) {
355457 setAccessToken (credentials .getAccessToken ());
356458 this .quotaProjectId = credentials .quotaProjectId ;
459+ this .universeDomain = credentials .universeDomain ;
460+ }
461+
462+ protected Builder (GoogleCredentials .Builder builder ) {
463+ setAccessToken (builder .getAccessToken ());
464+ this .quotaProjectId = builder .quotaProjectId ;
465+ this .universeDomain = builder .universeDomain ;
357466 }
358467
359468 public GoogleCredentials build () {
@@ -366,10 +475,19 @@ public Builder setQuotaProjectId(String quotaProjectId) {
366475 return this ;
367476 }
368477
478+ public Builder setUniverseDomain (String universeDomain ) {
479+ this .universeDomain = universeDomain ;
480+ return this ;
481+ }
482+
369483 public String getQuotaProjectId () {
370484 return this .quotaProjectId ;
371485 }
372486
487+ public String getUniverseDomain () {
488+ return this .universeDomain ;
489+ }
490+
373491 @ Override
374492 @ CanIgnoreReturnValue
375493 public Builder setAccessToken (AccessToken token ) {
0 commit comments