Skip to content

Commit b58513c

Browse files
author
Johannes Schneider
authored
fix: AuthToken Regression (#433)
1 parent f64fe64 commit b58513c

File tree

3 files changed

+56
-12
lines changed

3 files changed

+56
-12
lines changed

cloudplatform/connectivity-destination-service/src/main/java/com/sap/cloud/sdk/cloudplatform/connectivity/DestinationServiceFactory.java

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ static Destination fromDestinationServiceV1Response(
5656
// enable tenant id
5757
baseBuilder.property(DestinationProperty.TENANT_ID, getDestinationTenantId(onBehalfOf));
5858

59+
// store auth tokens, if any
60+
storeAuthTokens(baseBuilder, response.getAuthTokens());
61+
5962
// finalize RFC destination
6063
if( baseBuilder.get(DestinationProperty.TYPE).contains(DestinationType.RFC) ) {
6164
return handleRfcDestination(baseBuilder.build());
@@ -108,18 +111,6 @@ private static Destination handleHttpDestination(
108111

109112
// enable auth tokens
110113
if( authTokens != null && !authTokens.isEmpty() ) {
111-
final AuthenticationType authType =
112-
builder.get(DestinationProperty.AUTH_TYPE).getOrElse(AuthenticationType.NO_AUTHENTICATION);
113-
authTokens.forEach(t -> throwOnTokenError(builder.get(DestinationProperty.NAME).getOrNull(), t));
114-
authTokens.forEach(t -> setExpirationTimestamp(t, authType));
115-
116-
// Note: it is important that the auth tokens are added as property here
117-
// for the HttpClientCache we need to include them in the cache key
118-
// since they may contain cookies for which we need to make sure they
119-
// are not shared between different http clients
120-
// we can't attach the tokens directly to the header provider,
121-
// because the header providers are excluded from the cache key
122-
builder.property(DestinationProperty.AUTH_TOKENS, authTokens);
123114
builder.headerProviders(new AuthTokenHeaderProvider());
124115
}
125116

@@ -140,6 +131,26 @@ private static void throwOnTokenError(
140131
}
141132
}
142133

134+
private static void storeAuthTokens(
135+
@Nonnull final DefaultDestination.Builder builder,
136+
@Nullable final List<DestinationServiceV1Response.DestinationAuthToken> authTokens )
137+
{
138+
if( authTokens == null || authTokens.isEmpty() ) {
139+
return;
140+
}
141+
142+
final AuthenticationType authType =
143+
builder.get(DestinationProperty.AUTH_TYPE).getOrElse(AuthenticationType.NO_AUTHENTICATION);
144+
authTokens.forEach(t -> throwOnTokenError(builder.get(DestinationProperty.NAME).getOrNull(), t));
145+
authTokens.forEach(t -> setExpirationTimestamp(t, authType));
146+
147+
// Note: it is important that the auth tokens are added as property here
148+
// for the HttpClientCache we need to include them in the cache key
149+
// since they may contain cookies for which we need to make sure they
150+
// are not shared between different http clients
151+
builder.property(DestinationProperty.AUTH_TOKENS, authTokens);
152+
}
153+
143154
private static void setExpirationTimestamp(
144155
@Nonnull final DestinationServiceV1Response.DestinationAuthToken authToken,
145156
@Nonnull final AuthenticationType authType )

cloudplatform/connectivity-destination-service/src/test/java/com/sap/cloud/sdk/cloudplatform/connectivity/DestinationServiceFactoryTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.util.List;
1010

1111
import org.junit.jupiter.api.BeforeEach;
12+
import org.junit.jupiter.api.DisplayName;
1213
import org.junit.jupiter.api.Test;
1314

1415
import com.sap.cloud.sdk.cloudplatform.connectivity.DestinationServiceV1Response.DestinationAuthToken;
@@ -135,4 +136,35 @@ void propertyForwardAuthTokenShouldSetAuthenticationType()
135136

136137
assertThat(result.asHttp().getAuthenticationType()).isEqualTo(AuthenticationType.TOKEN_FORWARDING);
137138
}
139+
140+
@Test
141+
@DisplayName( "Regression: Auth Tokens are always stored" )
142+
// see https://github.com/SAP/cloud-sdk-java/issues/428
143+
void regressionTestTokenIsStoredForNonHttpDestination()
144+
{
145+
response.getDestinationConfiguration().put("Type", "MAIL");
146+
147+
final DestinationAuthToken authToken = new DestinationAuthToken();
148+
response.setAuthTokens(List.of(authToken));
149+
150+
final Destination result = DestinationServiceFactory.fromDestinationServiceV1Response(response);
151+
152+
assertThat(result.get(DestinationProperty.AUTH_TOKENS)).containsExactly(List.of(authToken));
153+
}
154+
155+
@Test
156+
@DisplayName( "Regression: Auth Token error on non-HTTP destination leads to exception" )
157+
// see https://github.com/SAP/cloud-sdk-java/issues/428
158+
void regressionTestAuthTokenErrorLeadsToExceptionOnNonHttpDestination()
159+
{
160+
response.getDestinationConfiguration().put("Type", "MAIL");
161+
162+
final DestinationAuthToken authToken = new DestinationAuthToken();
163+
authToken.setError("some error");
164+
response.setAuthTokens(List.of(authToken));
165+
166+
assertThatThrownBy(() -> DestinationServiceFactory.fromDestinationServiceV1Response(response))
167+
.isExactlyInstanceOf(DestinationAccessException.class)
168+
.hasMessageContaining("some error");
169+
}
138170
}

release_notes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,4 @@
3232
### 🐛 Fixed Issues
3333

3434
- Fix a regression that was introduced with the SAP Cloud SDK 5.0 release where the principal would no longer be derived from a `Basic` authorization header, in cases where neither a JWT nor an OIDC token was present.
35+
- Fix a regression that was introduced with the SAP Cloud SDK 5.0 release where auth tokens sent by the Destination service would no longer be stored in the `cloudsdk.authTokens` destination property for non-HTTP destinations.

0 commit comments

Comments
 (0)