Skip to content

Commit e3caf05

Browse files
aeitzmanlsirac
andauthored
fix: makes default token url universe aware (#1383)
* fix: makes default token url universe aware * lint and add test * Update oauth2_http/java/com/google/auth/oauth2/ExternalAccountCredentials.java Co-authored-by: Leo <[email protected]> * add back else * move code into override --------- Co-authored-by: Leo <[email protected]>
1 parent 75bd749 commit e3caf05

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

oauth2_http/java/com/google/auth/oauth2/ExternalAccountCredentials.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public abstract class ExternalAccountCredentials extends GoogleCredentials {
7373
static final String EXTERNAL_ACCOUNT_FILE_TYPE = "external_account";
7474
static final String EXECUTABLE_SOURCE_KEY = "executable";
7575

76-
static final String DEFAULT_TOKEN_URL = "https://sts.googleapis.com/v1/token";
76+
static final String DEFAULT_TOKEN_URL = "https://sts.{UNIVERSE_DOMAIN}/v1/token";
7777
static final String PROGRAMMATIC_METRICS_HEADER_VALUE = "programmatic";
7878

7979
private final String transportFactoryClassName;
@@ -235,7 +235,13 @@ protected ExternalAccountCredentials(ExternalAccountCredentials.Builder builder)
235235
this.serviceAccountImpersonationUrl = builder.serviceAccountImpersonationUrl;
236236
this.clientId = builder.clientId;
237237
this.clientSecret = builder.clientSecret;
238-
this.tokenUrl = builder.tokenUrl == null ? DEFAULT_TOKEN_URL : builder.tokenUrl;
238+
239+
if (builder.tokenUrl == null) {
240+
this.tokenUrl = DEFAULT_TOKEN_URL.replace("{UNIVERSE_DOMAIN}", this.getUniverseDomain());
241+
} else {
242+
this.tokenUrl = builder.tokenUrl;
243+
}
244+
239245
this.scopes =
240246
(builder.scopes == null || builder.scopes.isEmpty())
241247
? Arrays.asList(CLOUD_PLATFORM_SCOPE)
@@ -321,6 +327,17 @@ public void onFailure(Throwable exception) {
321327
});
322328
}
323329

330+
@Override
331+
public String getUniverseDomain() {
332+
try {
333+
return super.getUniverseDomain();
334+
} catch (IOException e) {
335+
// Throwing an IOException would be a breaking change, so wrap it here.
336+
// This should not happen for this credential type.
337+
throw new IllegalStateException(e);
338+
}
339+
}
340+
324341
@Override
325342
public Map<String, List<String>> getRequestMetadata(URI uri) throws IOException {
326343
Map<String, List<String>> requestMetadata = super.getRequestMetadata(uri);

oauth2_http/javatests/com/google/auth/oauth2/ExternalAccountCredentialsTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,24 @@ public void constructor_builder_defaultTokenUrl() {
565565
assertEquals(STS_URL, credentials.getTokenUrl());
566566
}
567567

568+
@Test
569+
public void constructor_builder_defaultTokenUrlwithUniverseDomain() {
570+
HashMap<String, Object> credentialSource = new HashMap<>();
571+
credentialSource.put("file", "file");
572+
573+
ExternalAccountCredentials credentials =
574+
IdentityPoolCredentials.newBuilder()
575+
.setHttpTransportFactory(transportFactory)
576+
.setAudience(
577+
"//iam.googleapis.com/locations/global/workforcePools/pool/providers/provider")
578+
.setSubjectTokenType("subjectTokenType")
579+
.setCredentialSource(new TestCredentialSource(credentialSource))
580+
.setUniverseDomain("testdomain.org")
581+
.build();
582+
583+
assertEquals("https://sts.testdomain.org/v1/token", credentials.getTokenUrl());
584+
}
585+
568586
@Test
569587
public void constructor_builder_subjectTokenTypeEnum() {
570588
HashMap<String, Object> credentialSource = new HashMap<>();

0 commit comments

Comments
 (0)