Skip to content

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

Merged
lqiu96 merged 5 commits into
googleapis:mainfrom
lqiu96:feat/auth-oauth2-refresh-universe-domain
Nov 25, 2026
Merged

lqiu96 merged 5 commits 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().
  • Pins serialVersionUID to the prior computed value and normalizes null or blank universeDomain in readObject() for deserialization compatibility with older streams.
  • Updates OAuth2Credentials.create(AccessToken) and OAuth2Credentials.newBuilder() Javadocs to note the googleapis.com default and point custom universe domain callers to GoogleCredentials.
  • Adds unit tests in OAuth2CredentialsWithRefreshTest covering builder and resolved universe domains, protected constructor default, serialization round-trips and legacy null/blank deserialization, and equals()/hashCode() (including default normalization and null initial access tokens).

@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 +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);
    }
  }

@lqiu96 lqiu96 added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Nov 25, 2026
@lqiu96
lqiu96 enabled auto-merge (squash) November 25, 2026 15:33
@yoshi-kokoro yoshi-kokoro removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Nov 25, 2026
@lqiu96
lqiu96 merged commit 7dbeb7f into googleapis:main Nov 25, 2026
298 of 307 checks passed
renovate-bot pushed a commit to renovate-bot/google-cloud-java that referenced this pull request Nov 25, 2026
…oogleapis#15180)

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()`.
- Pins `serialVersionUID` to the prior computed value and normalizes
null or blank `universeDomain` in `readObject()` for deserialization
compatibility with older streams.
- Updates `OAuth2Credentials.create(AccessToken)` and
`OAuth2Credentials.newBuilder()` Javadocs to note the `googleapis.com`
default and point custom universe domain callers to `GoogleCredentials`.
- Adds unit tests in `OAuth2CredentialsWithRefreshTest` covering builder
and resolved universe domains, protected constructor default,
serialization round-trips and legacy null/blank deserialization, and
`equals()`/`hashCode()` (including default normalization and null
initial access tokens).
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.

3 participants