Skip to content

Commit f9c5fa2

Browse files
authored
docs(auth): document default universe domain on access token constructors (#15217)
Documents that `GoogleCredentials` access-token factory methods and constructors without an explicit universe domain default to `googleapis.com` (`Credentials.GOOGLE_DEFAULT_UNIVERSE`), and delegates `GoogleCredentials.create(AccessToken)` to `GoogleCredentials.create(@nullable String, AccessToken)` while preserving `isExplicitUniverseDomain() == false`.
1 parent b92f845 commit f9c5fa2

1 file changed

Lines changed: 46 additions & 11 deletions

File tree

‎google-auth-library-java/oauth2_http/java/com/google/auth/oauth2/GoogleCredentials.java‎

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -115,25 +115,34 @@ String getCredentialName() {
115115
/**
116116
* Returns the credentials instance from the given access token.
117117
*
118+
* <p>The returned credentials default to {@link Credentials#GOOGLE_DEFAULT_UNIVERSE} ({@code
119+
* googleapis.com}). Callers with an access token for a non-default universe domain should use
120+
* {@link #create(String, AccessToken)} to specify the universe domain explicitly.
121+
*
118122
* @param accessToken the access token
119123
* @return the credentials instance
120124
*/
121125
public static GoogleCredentials create(AccessToken accessToken) {
122-
return GoogleCredentials.newBuilder().setAccessToken(accessToken).build();
126+
return create(null, accessToken);
123127
}
124128

125129
/**
126130
* Returns the credentials instance from the given access token and universe domain.
127131
*
128-
* @param universeDomain the universe domain
132+
* <p>If {@code universeDomain} is {@code null} or blank, the returned credentials default to
133+
* {@link Credentials#GOOGLE_DEFAULT_UNIVERSE} ({@code googleapis.com}).
134+
*
135+
* @param universeDomain the universe domain, or {@code null} or blank to default to {@link
136+
* Credentials#GOOGLE_DEFAULT_UNIVERSE} ({@code googleapis.com})
129137
* @param accessToken the access token
130138
* @return the credentials instance
131139
*/
132-
public static GoogleCredentials create(String universeDomain, AccessToken accessToken) {
133-
return GoogleCredentials.newBuilder()
134-
.setAccessToken(accessToken)
135-
.setUniverseDomain(universeDomain)
136-
.build();
140+
public static GoogleCredentials create(@Nullable String universeDomain, AccessToken accessToken) {
141+
Builder builder = GoogleCredentials.newBuilder().setAccessToken(accessToken);
142+
if (universeDomain != null && !universeDomain.trim().isEmpty()) {
143+
builder.setUniverseDomain(universeDomain);
144+
}
145+
return builder.build();
137146
}
138147

139148
/**
@@ -419,11 +428,17 @@ protected GoogleCredentials() {
419428
/**
420429
* Constructor with an explicit access token and quotaProjectId.
421430
*
422-
* <p>Deprecated, please use the {@link GoogleCredentials#GoogleCredentials(Builder)} constructor
423-
* whenever possible.
431+
* <p>Defaults to {@link Credentials#GOOGLE_DEFAULT_UNIVERSE} ({@code googleapis.com}). Callers
432+
* with an access token for a non-default universe domain should use {@link #create(String,
433+
* AccessToken)} (or {@link #newBuilder()} with {@link Builder#setAccessToken(AccessToken)} and
434+
* {@link Builder#setUniverseDomain(String)}) to specify the universe domain explicitly.
424435
*
425436
* @param accessToken initial or temporary access token
426437
* @param quotaProjectId a quotaProjectId, a project id to be used for billing purposes
438+
* @deprecated Use {@link #GoogleCredentials(Builder)} for subclasses, or {@link #newBuilder()}
439+
* with {@link Builder#setAccessToken(AccessToken)} and {@link
440+
* Builder#setQuotaProjectId(String)} (and {@link Builder#setUniverseDomain(String)} for a
441+
* non-default universe domain).
427442
*/
428443
@Deprecated
429444
protected GoogleCredentials(@Nullable AccessToken accessToken, @Nullable String quotaProjectId) {
@@ -436,7 +451,13 @@ protected GoogleCredentials(@Nullable AccessToken accessToken, @Nullable String
436451
/**
437452
* Constructor with explicit access token.
438453
*
454+
* <p>Defaults to {@link Credentials#GOOGLE_DEFAULT_UNIVERSE} ({@code googleapis.com}). Callers
455+
* with an access token for a non-default universe domain should use {@link #create(String,
456+
* AccessToken)} to specify the universe domain explicitly.
457+
*
439458
* @param accessToken initial or temporary access token
459+
* @deprecated Use {@link #create(AccessToken)} for {@code googleapis.com}, or {@link
460+
* #create(String, AccessToken)} to specify a universe domain explicitly.
440461
*/
441462
@Deprecated
442463
public GoogleCredentials(AccessToken accessToken) {
@@ -467,10 +488,15 @@ protected GoogleCredentials(Builder builder) {
467488
/**
468489
* Constructor with explicit access token and refresh margins.
469490
*
470-
* <p>Deprecated, please use the {@link GoogleCredentials#GoogleCredentials(Builder)} constructor
471-
* whenever possible.
491+
* <p>Defaults to {@link Credentials#GOOGLE_DEFAULT_UNIVERSE} ({@code googleapis.com}). Callers
492+
* with an access token for a non-default universe domain should use {@link #create(String,
493+
* AccessToken)} (or {@link #newBuilder()} with {@link Builder#setAccessToken(AccessToken)} and
494+
* {@link Builder#setUniverseDomain(String)}) to specify the universe domain explicitly.
472495
*
473496
* @param accessToken initial or temporary access token
497+
* @deprecated Use {@link #GoogleCredentials(Builder)} for subclasses, or {@link #newBuilder()}
498+
* with {@link Builder#setAccessToken(AccessToken)} and {@link
499+
* Builder#setUniverseDomain(String)} for a non-default universe domain.
474500
*/
475501
@Deprecated
476502
protected GoogleCredentials(
@@ -686,6 +712,15 @@ public Builder setQuotaProjectId(@Nullable String quotaProjectId) {
686712
return this;
687713
}
688714

715+
/**
716+
* Sets the universe domain for the credential.
717+
*
718+
* <p>If unset, {@code null}, or blank, defaults to {@link Credentials#GOOGLE_DEFAULT_UNIVERSE}
719+
* ({@code googleapis.com}).
720+
*
721+
* @param universeDomain the universe domain (for example, {@code googleapis.com})
722+
* @return this builder
723+
*/
689724
public Builder setUniverseDomain(String universeDomain) {
690725
this.universeDomain = universeDomain;
691726
return this;

0 commit comments

Comments
 (0)