Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions core/src/main/java/org/apache/iceberg/rest/auth/OAuth2Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,14 @@ public static AuthSession fromCredential(
parent.scope(),
parent.oauth2ServerUri(),
parent.optionalOAuthParams());
return fromTokenResponse(client, executor, response, startTimeMillis, parent, credential);
return fromTokenResponse(
client,
executor,
response,
startTimeMillis,
parent,
credential,
parent.config().exchangeEnabled());
}

public static AuthSession fromTokenResponse(
Expand All @@ -675,7 +682,13 @@ public static AuthSession fromTokenResponse(
long startTimeMillis,
AuthSession parent) {
return fromTokenResponse(
client, executor, response, startTimeMillis, parent, parent.credential());
client,
executor,
response,
startTimeMillis,
parent,
parent.credential(),
parent.config().exchangeEnabled());
}

private static AuthSession fromTokenResponse(
Expand All @@ -684,7 +697,8 @@ private static AuthSession fromTokenResponse(
OAuthTokenResponse response,
long startTimeMillis,
AuthSession parent,
String credential) {
String credential,
boolean exchangeEnabled) {
// issued_token_type is required in RFC 8693 but not in RFC 6749,
// thus assume type is access_token for compatibility with RFC 6749.
// See https://datatracker.ietf.org/doc/html/rfc6749#section-4.4.3
Expand All @@ -701,6 +715,7 @@ private static AuthSession fromTokenResponse(
.token(response.token())
.tokenType(issuedTokenType)
.credential(credential)
.exchangeEnabled(exchangeEnabled)
.expiresAtMillis(OAuth2Util.expiresAtMillis(response.token()))
.build());

Expand Down Expand Up @@ -734,7 +749,10 @@ public static AuthSession fromTokenExchange(
parent.scope(),
parent.oauth2ServerUri(),
parent.optionalOAuthParams());
return fromTokenResponse(client, executor, response, startTimeMillis, parent);
// Renew by exchange regardless of the parent's setting: the parent's credential identifies
// the parent, so refreshing with it would replace the exchanged identity.
return fromTokenResponse(
client, executor, response, startTimeMillis, parent, parent.credential(), true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,53 @@ private static void assertRefreshIncludesOptionalOAuthParams(long expiresAtMilli
}
}

@Test
void exchangedSessionRefreshesByExchangeWhenExchangeIsDisabled() throws IOException {
AuthConfig parentConfig =
AuthConfig.builder()
.token(tokenWithExp(7200))
.tokenType(OAuth2Properties.ACCESS_TOKEN_TYPE)
.credential("testClientId:testClientSecret")
.keepRefreshed(true)
.exchangeEnabled(false)
.oauth2ServerUri("/v1/token")
.build();

try (RESTClient client = Mockito.mock(RESTClient.class);
AuthSession parent = new AuthSession(Map.of(), parentConfig)) {
Mockito.when(client.postForm(any(), anyMap(), any(), anyMap(), any()))
.thenReturn(
childTokenResponse("exchanged_token", 3600),
childTokenResponse("refreshed_token", 3600));

try (AuthSession child =
AuthSession.fromTokenExchange(
client, null, "user_subject_token", OAuth2Properties.JWT_TOKEN_TYPE, parent)) {
child.refresh(client);

Mockito.verify(client)
.postForm(
any(),
argThat(
formData ->
"urn:ietf:params:oauth:grant-type:token-exchange"
.equals(formData.get(GRANT_TYPE))
&& "exchanged_token".equals(formData.get("subject_token"))),
Mockito.eq(OAuthTokenResponse.class),
anyMap(),
any());
Mockito.verify(client, Mockito.never())
.postForm(
any(),
argThat(formData -> CLIENT_CREDENTIALS.equals(formData.get(GRANT_TYPE))),
Mockito.eq(OAuthTokenResponse.class),
anyMap(),
any());
assertThat(child.token()).isEqualTo("refreshed_token");
}
}
}

private static AuthSession parentSession(long expSeconds) {
String parentToken = tokenWithExp(expSeconds);
AuthConfig parentConfig =
Expand Down
Loading