Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ private static OAuthTokenResponse refreshToken(
config.credential(),
config.scope(),
config.oauth2ServerUri(),
ImmutableMap.of());
optionalOAuthParams);
}
}

Expand Down Expand Up @@ -563,7 +563,7 @@ private OAuthTokenResponse refreshExpiredToken(RESTClient client) {
client, config, basicHeaders, token(), tokenType(), optionalOAuthParams());
} else {
return fetchToken(
client, Map.of(), credential(), scope(), oauth2ServerUri(), ImmutableMap.of());
client, Map.of(), credential(), scope(), oauth2ServerUri(), optionalOAuthParams());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import java.io.IOException;
import java.util.Map;
import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
import org.apache.iceberg.rest.RESTClient;
import org.apache.iceberg.rest.responses.OAuthTokenResponse;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -135,4 +136,77 @@ public void testCredentialFlowForSessionRefresh() throws IOException {
any());
}
}

@Test
void refreshExpiredTokenShouldIncludeOptionalOAuthParams() throws IOException {
// expired token triggers refreshExpiredToken() which should forward optionalOAuthParams
String audience = "https://my-catalog.example.com";
AuthConfig authConfig =
ImmutableAuthConfig.builder()
.keepRefreshed(true)
.exchangeEnabled(false)
.token("expired_token")
.credential("testClientId:testClientSecret")
.oauth2ServerUri("/v1/token")
.expiresAtMillis(System.currentTimeMillis() - 10_000)
.optionalOAuthParams(ImmutableMap.of("audience", audience, "scope", "catalog"))
.build();

OAuthTokenResponse response =
OAuthTokenResponse.builder().withToken("refreshed_token").withTokenType(BEARER).build();

try (RESTClient client = Mockito.mock(RESTClient.class);
OAuth2Util.AuthSession session = new OAuth2Util.AuthSession(Map.of(), authConfig)) {
Mockito.when(client.postForm(any(), anyMap(), any(), anyMap(), any())).thenReturn(response);

session.refresh(client);

Mockito.verify(client)
.postForm(
any(),
argThat(
formData ->
CLIENT_CREDENTIALS.equals(formData.get(GRANT_TYPE))
&& audience.equals(formData.get("audience"))),
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.

It's a bit odd to have the audience parameter propagated on a client_credentials grant request, but I guess it's too convoluted to distinguish which optional parameters should be included depending on the grant. And most IDPs would just ignore this parameter, I guess (except Okta / Auth0).

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.

Also, shouldn't you assert that the scope is present too?

Mockito.eq(OAuthTokenResponse.class),
anyMap(),
any());
}
}

@Test
void refreshCurrentTokenNonExchangeShouldIncludeOptionalOAuthParams() throws IOException {
String audience = "https://my-catalog.example.com";
AuthConfig authConfig =
ImmutableAuthConfig.builder()
.keepRefreshed(true)
.exchangeEnabled(false)
.token("valid_token")
.credential("testClientId:testClientSecret")
.oauth2ServerUri("/v1/token")
.expiresAtMillis(System.currentTimeMillis() + 300_000)
.optionalOAuthParams(ImmutableMap.of("audience", audience, "scope", "catalog"))
.build();

OAuthTokenResponse response =
OAuthTokenResponse.builder().withToken("refreshed_token").withTokenType(BEARER).build();

try (RESTClient client = Mockito.mock(RESTClient.class);
OAuth2Util.AuthSession session = new OAuth2Util.AuthSession(Map.of(), authConfig)) {
Mockito.when(client.postForm(any(), anyMap(), any(), anyMap(), any())).thenReturn(response);

session.refresh(client);

Mockito.verify(client)
.postForm(
any(),
argThat(
formData ->
CLIENT_CREDENTIALS.equals(formData.get(GRANT_TYPE))
&& audience.equals(formData.get("audience"))),
Mockito.eq(OAuthTokenResponse.class),
anyMap(),
any());
}
}
}
Loading