Repository navigation
feat(auth): support universeDomain in JwtCredentials #15176
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
lqiu96
merged 1 commit into
googleapis:main
from
lqiu96:feat/auth-jwt-credentials-universe-domain
Nov 24, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -82,6 +82,7 @@ public class JwtCredentials extends Credentials implements JwtProvider { | |
| private final String privateKeyId; | ||
| private final JwtClaims jwtClaims; | ||
| private final Long lifeSpanSeconds; | ||
| private final String universeDomain; | ||
| @VisibleForTesting transient Clock clock; | ||
|
|
||
| private transient String jwt; | ||
|
|
@@ -95,6 +96,11 @@ private JwtCredentials(Builder builder) { | |
| Preconditions.checkState(jwtClaims.isComplete(), JWT_INCOMPLETE_ERROR_MESSAGE); | ||
| this.lifeSpanSeconds = Preconditions.checkNotNull(builder.getLifeSpanSeconds()); | ||
| this.clock = Preconditions.checkNotNull(builder.getClock()); | ||
| if (builder.universeDomain == null || builder.universeDomain.trim().isEmpty()) { | ||
| this.universeDomain = Credentials.GOOGLE_DEFAULT_UNIVERSE; | ||
| } else { | ||
| this.universeDomain = builder.universeDomain; | ||
| } | ||
| } | ||
|
|
||
| public static Builder newBuilder() { | ||
|
|
@@ -152,9 +158,22 @@ public JwtCredentials jwtWithClaims(JwtClaims newClaims) { | |
| .setPrivateKey(privateKey) | ||
| .setPrivateKeyId(privateKeyId) | ||
| .setJwtClaims(jwtClaims.merge(newClaims)) | ||
| .setUniverseDomain(this.universeDomain) | ||
| .build(); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the universe domain for the credential. | ||
| * | ||
| * @return the configured universe domain, or {@link Credentials#GOOGLE_DEFAULT_UNIVERSE} if none | ||
| * was explicitly configured | ||
| * @throws IOException if retrieving the universe domain fails | ||
| */ | ||
| @Override | ||
| public String getUniverseDomain() throws IOException { | ||
| return this.universeDomain; | ||
| } | ||
|
|
||
| @Override | ||
| public String getAuthenticationType() { | ||
| return "JWT"; | ||
|
|
@@ -190,12 +209,18 @@ public boolean equals(@Nullable Object obj) { | |
| return Objects.equals(this.privateKey, other.privateKey) | ||
| && Objects.equals(this.privateKeyId, other.privateKeyId) | ||
| && Objects.equals(this.jwtClaims, other.jwtClaims) | ||
| && Objects.equals(this.lifeSpanSeconds, other.lifeSpanSeconds); | ||
| && Objects.equals(this.lifeSpanSeconds, other.lifeSpanSeconds) | ||
| && Objects.equals(this.universeDomain, other.universeDomain); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(this.privateKey, this.privateKeyId, this.jwtClaims, this.lifeSpanSeconds); | ||
| return Objects.hash( | ||
| this.privateKey, | ||
| this.privateKeyId, | ||
| this.jwtClaims, | ||
| this.lifeSpanSeconds, | ||
| this.universeDomain); | ||
| } | ||
|
Comment on lines
209
to
224
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To ensure backward compatibility with older serialized instances where String thisUniverse = this.universeDomain == null ? Credentials.GOOGLE_DEFAULT_UNIVERSE : this.universeDomain;
String otherUniverse = other.universeDomain == null ? Credentials.GOOGLE_DEFAULT_UNIVERSE : other.universeDomain;
return Objects.equals(this.privateKey, other.privateKey)
&& Objects.equals(this.privateKeyId, other.privateKeyId)
&& Objects.equals(this.jwtClaims, other.jwtClaims)
&& Objects.equals(this.lifeSpanSeconds, other.lifeSpanSeconds)
&& Objects.equals(thisUniverse, otherUniverse);
}
@Override
public int hashCode() {
String effectiveUniverse = this.universeDomain == null ? Credentials.GOOGLE_DEFAULT_UNIVERSE : this.universeDomain;
return Objects.hash(
this.privateKey,
this.privateKeyId,
this.jwtClaims,
this.lifeSpanSeconds,
effectiveUniverse);
}References
|
||
|
|
||
| Clock getClock() { | ||
|
|
@@ -211,6 +236,7 @@ public static class Builder { | |
| private JwtClaims jwtClaims; | ||
| private Clock clock = Clock.SYSTEM; | ||
| private Long lifeSpanSeconds = TimeUnit.HOURS.toSeconds(1); | ||
| private @Nullable String universeDomain; | ||
|
|
||
| protected Builder() {} | ||
|
|
||
|
|
@@ -254,6 +280,24 @@ public Long getLifeSpanSeconds() { | |
| return lifeSpanSeconds; | ||
| } | ||
|
|
||
| /** | ||
| * Sets the universe domain for the credential. If null or blank, defaults to {@link | ||
| * Credentials#GOOGLE_DEFAULT_UNIVERSE}. | ||
| * | ||
| * @param universeDomain the universe domain (for example, {@code googleapis.com}) | ||
| * @return this builder | ||
| */ | ||
| @CanIgnoreReturnValue | ||
| public Builder setUniverseDomain(@Nullable String universeDomain) { | ||
| this.universeDomain = universeDomain; | ||
| return this; | ||
| } | ||
|
|
||
| /** Returns the configured universe domain, or {@code null} if not explicitly set. */ | ||
| public @Nullable String getUniverseDomain() { | ||
| return universeDomain; | ||
| } | ||
|
|
||
| @CanIgnoreReturnValue | ||
| Builder setClock(Clock clock) { | ||
| this.clock = Preconditions.checkNotNull(clock); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since
JwtCredentialsimplementsSerializable, adding the newuniverseDomainfield introduces a backward compatibility issue. When deserializing an older serialized instance ofJwtCredentials(which does not contain theuniverseDomainfield), Java serialization will initializeuniverseDomaintonull. To preventgetUniverseDomain()from returningnullin these cases, we should returnCredentials.GOOGLE_DEFAULT_UNIVERSEifthis.universeDomainisnull.References
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These serialization compatibility issues seem legit to me, wdyt?