Skip to content

Commit

Permalink
hotfix cache key header mutability in AbstractOAuth2TokenService (#745)
Browse files Browse the repository at this point in the history
* hotfix cache key mutability in AbstractOAuth2TokenService

* fix implementation

Co-authored-by: Nena Raab <nena.raab@sap.com>
  • Loading branch information
liga-oz and nenaraab authored Dec 7, 2021
1 parent 9a62a4c commit 6c4896e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,11 @@ public DefaultOAuth2TokenService(@Nonnull CloseableHttpClient httpClient,
@Override
protected OAuth2TokenResponse requestAccessToken(URI tokenEndpointUri, HttpHeaders headers,
Map<String, String> parameters) throws OAuth2ServiceException {
headers.withHeader(MDCHelper.CORRELATION_HEADER, MDCHelper.getOrCreateCorrelationId());
HttpPost httpPost = createHttpPost(tokenEndpointUri, headers, parameters);
HttpHeaders requestHeaders = new HttpHeaders();
headers.getHeaders().forEach(h -> requestHeaders.withHeader(h.getName(), h.getValue()));
requestHeaders.withHeader(MDCHelper.CORRELATION_HEADER, MDCHelper.getOrCreateCorrelationId());

HttpPost httpPost = createHttpPost(tokenEndpointUri, requestHeaders, parameters);
LOGGER.debug("access token request {} - {}", headers, parameters);
return executeRequest(httpPost);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.read.ListAppender;
import com.sap.cloud.security.config.ClientCredentials;
import com.sap.cloud.security.servlet.MDCHelper;
import com.sap.cloud.security.xsuaa.http.HttpHeaders;
import com.sap.cloud.security.xsuaa.http.HttpHeadersFactory;
Expand Down Expand Up @@ -136,6 +137,20 @@ public void httpResponseWithErrorStatusCode_throwsExceptionContainingMessage() t
.extracting("httpStatusCode").isEqualTo(HttpStatus.SC_UNAUTHORIZED);
}

@Test
public void retrieveToken_testCache() throws IOException {
CloseableHttpResponse response = HttpClientTestFactory.createHttpResponse(VALID_JSON_RESPONSE);
when(mockHttpClient.execute(any(HttpPost.class)))
.thenReturn(response);

cut.retrieveAccessTokenViaClientCredentialsGrant(TOKEN_ENDPOINT_URI,
new ClientCredentials("myClientId", "mySecret"), null, null, emptyMap(), false);
cut.retrieveAccessTokenViaClientCredentialsGrant(TOKEN_ENDPOINT_URI,
new ClientCredentials("myClientId", "mySecret"), null, null, emptyMap(), false);

verify(mockHttpClient, times(1)).execute(any(HttpPost.class));
}

private OAuth2TokenResponse requestAccessToken(Map<String, String> optionalParameters)
throws OAuth2ServiceException {
HttpHeaders withoutAuthorizationHeader = HttpHeadersFactory.createWithoutAuthorizationHeader();
Expand Down

0 comments on commit 6c4896e

Please sign in to comment.