Skip to content

Commit d4e56ef

Browse files
committed
test(oauth2): add unit test coverage for repeated 401 retry guard and non-mTLS actor token check
1 parent e5be0b4 commit d4e56ef

2 files changed

Lines changed: 72 additions & 0 deletions

File tree

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -960,6 +960,54 @@ public HttpTransport create() {
960960
assertEquals(mockTransport.getAccessToken(), accessToken.getTokenValue());
961961
}
962962

963+
@Test
964+
void exchangeExternalCredentialForAccessToken_withMtls401Repeated_doesNotRetryIndefinitely() {
965+
AtomicInteger rebuildCount = new AtomicInteger(0);
966+
MockExternalAccountCredentialsTransport mockTransport =
967+
new MockExternalAccountCredentialsTransport();
968+
mockTransport.addResponseStatusCodeSequence(401, 401);
969+
970+
ContextRebuildableTransportFactory rebuildableFactory =
971+
new ContextRebuildableTransportFactory() {
972+
@Override
973+
public synchronized void rebuildContext() throws IOException {
974+
rebuildCount.incrementAndGet();
975+
}
976+
977+
@Override
978+
public HttpTransport create() {
979+
return mockTransport;
980+
}
981+
};
982+
983+
ExternalAccountCredentials credential =
984+
ExternalAccountCredentials.fromJson(buildJsonIdentityPoolCredential(), rebuildableFactory);
985+
StsTokenExchangeRequest stsRequest =
986+
StsTokenExchangeRequest.newBuilder("credential", "subjectTokenType").build();
987+
988+
assertThrows(
989+
IOException.class, () -> credential.exchangeExternalCredentialForAccessToken(stsRequest));
990+
991+
assertEquals(1, rebuildCount.get());
992+
}
993+
994+
@Test
995+
void exchangeExternalCredentialForAccessToken_withNonRebuildableTransport401_doesNotRetry() {
996+
MockExternalAccountCredentialsTransport mockTransport =
997+
new MockExternalAccountCredentialsTransport();
998+
mockTransport.addResponseStatusCodeSequence(401);
999+
1000+
HttpTransportFactory standardFactory = () -> mockTransport;
1001+
1002+
ExternalAccountCredentials credential =
1003+
ExternalAccountCredentials.fromJson(buildJsonIdentityPoolCredential(), standardFactory);
1004+
StsTokenExchangeRequest stsRequest =
1005+
StsTokenExchangeRequest.newBuilder("credential", "subjectTokenType").build();
1006+
1007+
assertThrows(
1008+
IOException.class, () -> credential.exchangeExternalCredentialForAccessToken(stsRequest));
1009+
}
1010+
9631011
@Test
9641012
void exchangeExternalCredentialForAccessToken_withInternalOptions() throws IOException {
9651013
ExternalAccountCredentials credential =

google-auth-library-java/oauth2_http/javatests/com/google/auth/oauth2/IdentityPoolCredentialsTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1557,6 +1557,30 @@ public com.google.api.client.http.HttpTransport create() {
15571557
assertEquals("urn:ietf:params:oauth:token-type:jwt", query.get("actor_token_type"));
15581558
}
15591559

1560+
@Test
1561+
void builder_actorTokenSupplierWithNonMtlsTransport_throws() {
1562+
MockExternalAccountCredentialsTransportFactory standardFactory =
1563+
new MockExternalAccountCredentialsTransportFactory();
1564+
1565+
IllegalArgumentException exception =
1566+
assertThrows(
1567+
IllegalArgumentException.class,
1568+
() ->
1569+
IdentityPoolCredentials.newBuilder()
1570+
.setSubjectTokenSupplier(testProvider)
1571+
.setActorTokenSupplier(testActorSupplier)
1572+
.setActorTokenType("urn:ietf:params:oauth:token-type:jwt")
1573+
.setAudience("audience")
1574+
.setSubjectTokenType("subjectTokenType")
1575+
.setTokenUrl("https://sts.googleapis.com/v1/token")
1576+
.setHttpTransportFactory(standardFactory)
1577+
.build());
1578+
1579+
assertEquals(
1580+
"Actor tokens are only supported for mTLS token exchanges. Please configure a certificate source or MtlsHttpTransportFactory.",
1581+
exception.getMessage());
1582+
}
1583+
15601584
@Test
15611585
void serialization_withX509Provider_succeeds() throws Exception {
15621586
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());

0 commit comments

Comments
 (0)