Skip to content

feat(auth): support universeDomain in OAuth2CredentialsWithRefresh - #15180

Open
lqiu96 wants to merge 1 commit into
googleapis:mainfrom
lqiu96:feat/auth-oauth2-refresh-universe-domain
Open

lqiu96 wants to merge 1 commit into
googleapis:mainfrom
lqiu96:feat/auth-oauth2-refresh-universe-domain

Conversation

@lqiu96

@lqiu96 lqiu96 commented Nov 24, 2026

Copy link
Copy Markdown
Member

Adds universeDomain support to OAuth2CredentialsWithRefresh:

  • Adds universeDomain field (defaulting null or blank values to googleapis.com), overrides getUniverseDomain(), adds Builder.setUniverseDomain() and Builder.getUniverseDomain(), and includes universeDomain in equals() and hashCode().
  • Updates OAuth2Credentials.create(AccessToken) Javadoc to note the googleapis.com default and point custom universe domain callers to GoogleCredentials.create(String, AccessToken).
  • Adds unit tests in OAuth2CredentialsWithRefreshTest covering builder and resolved universe domains, protected constructor default, and field-level equals()/hashCode() coverage.

@lqiu96
lqiu96 requested review from a team as code owners November 24, 2026 00:07

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces support for custom universe domains in OAuth2CredentialsWithRefresh by adding a universeDomain field, corresponding builder methods, and updating the equals and hashCode implementations. It also adds comprehensive unit tests for these changes. The feedback suggests a critical improvement to the equals and hashCode methods: excluding the refreshHandler functional interface from these checks, as comparing lambdas for equality is fragile. Additionally, the feedback recommends using the getUniverseDomain() getter instead of the direct field access to ensure consistency, along with corresponding updates to the unit tests.

Comment on lines +112 to +126
@Override
public boolean equals(@Nullable Object obj) {
if (!(obj instanceof OAuth2CredentialsWithRefresh)) {
return false;
}
OAuth2CredentialsWithRefresh other = (OAuth2CredentialsWithRefresh) obj;
return super.equals(other)
&& Objects.equals(this.refreshHandler, other.refreshHandler)
&& Objects.equals(this.universeDomain, other.universeDomain);
}

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), this.refreshHandler, this.universeDomain);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Comparing functional interfaces/callbacks (like OAuth2RefreshHandler) in equals and hashCode is fragile. Since lambdas do not implement equals in a meaningful way (defaulting to reference equality), two credentials with identical tokens and domains but different lambda instances for the refresh handler will be considered unequal. Excluding refreshHandler from equals and hashCode makes the equality check more robust and correct. Additionally, to maintain consistency between deserialized and newly constructed instances, ensure that equals() and hashCode() use the getter method getUniverseDomain() (or apply the same fallback logic) rather than accessing the field directly.

  @Override
  public boolean equals(@Nullable Object obj) {
    if (!(obj instanceof OAuth2CredentialsWithRefresh)) {
      return false;
    }
    OAuth2CredentialsWithRefresh other = (OAuth2CredentialsWithRefresh) obj;
    return super.equals(other)
        && Objects.equals(this.getUniverseDomain(), other.getUniverseDomain());
  }

  @Override
  public int hashCode() {
    return Objects.hash(super.hashCode(), this.getUniverseDomain());
  }
References
  1. When adding a new field to a serializable class that may be null when deserializing older versions, and providing a getter with a default fallback value, ensure that equals() and hashCode() use the getter method (or apply the same fallback logic) rather than accessing the field directly, to maintain consistency between deserialized and newly constructed instances.

Comment on lines +244 to +256
@Test
void equals_true() {
OAuth2CredentialsWithRefresh.OAuth2RefreshHandler handler = () -> ACCESS_TOKEN;
OAuth2CredentialsWithRefresh credentials =
createTestBuilder(handler).setUniverseDomain("example.com").build();
OAuth2CredentialsWithRefresh other =
createTestBuilder(handler).setUniverseDomain("example.com").build();

assertEquals(credentials, credentials);
assertEquals(credentials, other);
assertEquals(other, credentials);
assertEquals(credentials.hashCode(), other.hashCode());
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Update the test to use two different lambda instances for the refresh handler. This demonstrates that different handler instances do not affect equality when the tokens and domains are identical.

Suggested change
@Test
void equals_true() {
OAuth2CredentialsWithRefresh.OAuth2RefreshHandler handler = () -> ACCESS_TOKEN;
OAuth2CredentialsWithRefresh credentials =
createTestBuilder(handler).setUniverseDomain("example.com").build();
OAuth2CredentialsWithRefresh other =
createTestBuilder(handler).setUniverseDomain("example.com").build();
assertEquals(credentials, credentials);
assertEquals(credentials, other);
assertEquals(other, credentials);
assertEquals(credentials.hashCode(), other.hashCode());
}
@Test
void equals_true() {
OAuth2CredentialsWithRefresh.OAuth2RefreshHandler handler1 = () -> ACCESS_TOKEN;
OAuth2CredentialsWithRefresh.OAuth2RefreshHandler handler2 = () -> ACCESS_TOKEN;
OAuth2CredentialsWithRefresh credentials =
createTestBuilder(handler1).setUniverseDomain("example.com").build();
OAuth2CredentialsWithRefresh other =
createTestBuilder(handler2).setUniverseDomain("example.com").build();
assertEquals(credentials, credentials);
assertEquals(credentials, other);
assertEquals(other, credentials);
assertEquals(credentials.hashCode(), other.hashCode());
}

Comment on lines +259 to 284
void equals_false() {
OAuth2CredentialsWithRefresh.OAuth2RefreshHandler handler = () -> ACCESS_TOKEN;
OAuth2CredentialsWithRefresh.OAuth2RefreshHandler otherHandler = () -> ACCESS_TOKEN;
AccessToken otherToken =
new AccessToken("otherAccessToken", new Date(ACCESS_TOKEN.getExpirationTime().getTime()));
OAuth2CredentialsWithRefresh baseline =
createTestBuilder(handler).setUniverseDomain("example.com").build();

List<Consumer<OAuth2CredentialsWithRefresh.Builder>> mutations =
Arrays.asList(
b -> b.setAccessToken(otherToken),
b -> b.setRefreshHandler(otherHandler),
b -> b.setUniverseDomain("other.example.com"));

assertNotEquals(baseline, null);
assertNotEquals(baseline, "not-a-credential");
for (Consumer<OAuth2CredentialsWithRefresh.Builder> mutation : mutations) {
OAuth2CredentialsWithRefresh.Builder builder =
createTestBuilder(handler).setUniverseDomain("example.com");
mutation.accept(builder);
OAuth2CredentialsWithRefresh other = builder.build();
assertNotEquals(baseline, other);
assertNotEquals(other, baseline);
}
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Remove the refreshHandler mutation from the list of mutations since the refresh handler is excluded from equals and hashCode comparisons.

  @Test
  void equals_false() {
    OAuth2CredentialsWithRefresh.OAuth2RefreshHandler handler = () -> ACCESS_TOKEN;
    AccessToken otherToken =
        new AccessToken("otherAccessToken", new Date(ACCESS_TOKEN.getExpirationTime().getTime()));
    OAuth2CredentialsWithRefresh baseline =
        createTestBuilder(handler).setUniverseDomain("example.com").build();

    List<Consumer<OAuth2CredentialsWithRefresh.Builder>> mutations =
        Arrays.asList(
            b -> b.setAccessToken(otherToken),
            b -> b.setUniverseDomain("other.example.com"));

    assertNotEquals(baseline, null);
    assertNotEquals(baseline, "not-a-credential");
    for (Consumer<OAuth2CredentialsWithRefresh.Builder> mutation : mutations) {
      OAuth2CredentialsWithRefresh.Builder builder =
          createTestBuilder(handler).setUniverseDomain("example.com");
      mutation.accept(builder);
      OAuth2CredentialsWithRefresh other = builder.build();
      assertNotEquals(baseline, other);
      assertNotEquals(other, baseline);
    }
  }

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant