Skip to content

Commit 213c929

Browse files
rpanackalMatKuhr
andauthored
fix: [Connectivity] Change default token cache duration to 1hr (SAP#861)
Co-authored-by: Roshin Rajan Panackal <[email protected]> Co-authored-by: Matthias Kuhr <[email protected]>
1 parent 817c8fb commit 213c929

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

cloudplatform/connectivity-oauth/src/main/java/com/sap/cloud/sdk/cloudplatform/connectivity/OAuth2Service.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static com.sap.cloud.security.xsuaa.util.UriUtil.expandPath;
44

55
import java.net.URI;
6+
import java.time.Duration;
67
import java.util.HashMap;
78
import java.util.Map;
89
import java.util.concurrent.TimeUnit;
@@ -39,6 +40,7 @@
3940
import com.sap.cloud.security.xsuaa.client.OAuth2ServiceException;
4041
import com.sap.cloud.security.xsuaa.client.OAuth2TokenResponse;
4142
import com.sap.cloud.security.xsuaa.client.OAuth2TokenService;
43+
import com.sap.cloud.security.xsuaa.tokenflows.TokenCacheConfiguration;
4244

4345
import io.vavr.CheckedFunction0;
4446
import io.vavr.control.Try;
@@ -101,8 +103,10 @@ OAuth2TokenService getTokenService( @Nullable final String tenantId )
101103
@Nonnull
102104
private OAuth2TokenService createTokenService( @Nonnull final CacheKey ignored )
103105
{
106+
final var tokenCacheConfiguration =
107+
TokenCacheConfiguration.getInstance(Duration.ofHours(1), 1000, Duration.ofSeconds(30), false);
104108
if( !(identity instanceof ZtisClientIdentity) ) {
105-
return new DefaultOAuth2TokenService(HttpClientFactory.create(identity));
109+
return new DefaultOAuth2TokenService(HttpClientFactory.create(identity), tokenCacheConfiguration);
106110
}
107111

108112
final DefaultHttpDestination destination =
@@ -115,7 +119,9 @@ private OAuth2TokenService createTokenService( @Nonnull final CacheKey ignored )
115119
.keyStore(((ZtisClientIdentity) identity).getKeyStore())
116120
.build();
117121
try {
118-
return new DefaultOAuth2TokenService((CloseableHttpClient) HttpClientAccessor.getHttpClient(destination));
122+
return new DefaultOAuth2TokenService(
123+
(CloseableHttpClient) HttpClientAccessor.getHttpClient(destination),
124+
tokenCacheConfiguration);
119125
}
120126
catch( final ClassCastException e ) {
121127
final String msg =
@@ -214,6 +220,10 @@ private void setAppTidInCaseOfIAS( @Nullable final String tenantId )
214220
// the IAS property supplier will have set this to the provider ID by default
215221
// we have to override it here to match the current tenant, if the current tenant is defined
216222
additionalParameters.put("app_tid", tenantId);
223+
if( onBehalfOf == OnBehalfOf.NAMED_USER_CURRENT_TENANT ) {
224+
// workaround until a fix is provided by IAS
225+
additionalParameters.put("refresh_token", "0");
226+
}
217227
}
218228
}
219229

cloudplatform/connectivity-oauth/src/test/java/com/sap/cloud/sdk/cloudplatform/connectivity/OAuth2ServiceTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ void testSubdomainTenantStrategy()
226226
1,
227227
postRequestedFor(urlEqualTo("/oauth/token"))
228228
.withRequestBody(containing("app_tid=tenant"))
229+
.withRequestBody(containing("refresh_token=0"))
229230
.withRequestBody(
230231
containing("grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer".replace(":", "%3A")))
231232
.withRequestBody(containing("assertion=" + token)));

release_notes.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616

1717
### 📈 Improvements
1818

19-
-
19+
- Relax OAuth2 token cache duration to 1hr to avoid unnecessary token refreshes.
20+
- Disable refresh tokens when obtaining user tokens from IAS.
21+
This acts as a workaround for a limitation of IAS, where obtaining a refresh token invalidates the original token.
2022

2123
### 🐛 Fixed Issues
2224

2325
- OData v2 and OData v4: Fixes eager HTTP response evaluation for _Create_, _Update_, and _Delete_ request builders in convenience APIs.
24-
Previous change of `5.20.0` may have resulted in the HTTP connection being left open after the request was executed.
26+
Previous change of `5.20.0` may have resulted in the HTTP connection being left open after the request was executed.

0 commit comments

Comments
 (0)