Skip to content

Commit 93165c2

Browse files
authored
CXF-9161: Some of the OIDCFlowTest fail with timeout (JPA only) when HttpClient instance is shared (#3109)
Signed-off-by: Andriy Redko <drreta@gmail.com>
1 parent 0ac9b4c commit 93165c2

4 files changed

Lines changed: 790 additions & 718 deletions

File tree

rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/client/OAuthClientUtils.java

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -295,29 +295,30 @@ public static ClientAccessToken getAccessToken(WebClient accessTokenService,
295295
// in this case the AccessToken service is expected to find a mapping between
296296
// the authenticated credentials and the client registration id
297297
}
298-
Response response = accessTokenService.form(form);
299-
final Map<String, String> map;
300-
try {
301-
map = response.getMediaType() == null
302-
|| response.getMediaType().isCompatible(MediaType.APPLICATION_JSON_TYPE)
303-
? new OAuthJSONProvider().readJSONResponse((InputStream) response.getEntity())
304-
: Collections.emptyMap();
305-
} catch (Exception ex) {
306-
throw new ResponseProcessingException(response, ex);
307-
}
308-
if (200 == response.getStatus()) {
309-
ClientAccessToken token = fromMapToClientToken(map, defaultTokenType);
310-
if (token == null) {
311-
throw new OAuthServiceException(OAuthConstants.SERVER_ERROR);
298+
try (Response response = accessTokenService.form(form)) {
299+
final Map<String, String> map;
300+
try {
301+
map = response.getMediaType() == null
302+
|| response.getMediaType().isCompatible(MediaType.APPLICATION_JSON_TYPE)
303+
? new OAuthJSONProvider().readJSONResponse((InputStream) response.getEntity())
304+
: Collections.emptyMap();
305+
} catch (Exception ex) {
306+
throw new ResponseProcessingException(response, ex);
312307
}
313-
return token;
314-
} else if (response.getStatus() >= 400 && map.containsKey(OAuthConstants.ERROR_KEY)) {
315-
OAuthError error = new OAuthError(map.get(OAuthConstants.ERROR_KEY),
316-
map.get(OAuthConstants.ERROR_DESCRIPTION_KEY));
317-
error.setErrorUri(map.get(OAuthConstants.ERROR_URI_KEY));
318-
throw new OAuthServiceException(error);
308+
if (200 == response.getStatus()) {
309+
ClientAccessToken token = fromMapToClientToken(map, defaultTokenType);
310+
if (token == null) {
311+
throw new OAuthServiceException(OAuthConstants.SERVER_ERROR);
312+
}
313+
return token;
314+
} else if (response.getStatus() >= 400 && map.containsKey(OAuthConstants.ERROR_KEY)) {
315+
OAuthError error = new OAuthError(map.get(OAuthConstants.ERROR_KEY),
316+
map.get(OAuthConstants.ERROR_DESCRIPTION_KEY));
317+
error.setErrorUri(map.get(OAuthConstants.ERROR_URI_KEY));
318+
throw new OAuthServiceException(error);
319+
}
320+
throw new OAuthServiceException(OAuthConstants.SERVER_ERROR);
319321
}
320-
throw new OAuthServiceException(OAuthConstants.SERVER_ERROR);
321322
}
322323

323324
public static ClientAccessToken fromMapToClientToken(Map<String, String> map) {

rt/rs/security/sso/oidc/src/main/java/org/apache/cxf/rs/security/oidc/utils/OidcUtils.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -218,12 +218,17 @@ public static void setStateClaimsProperty(OAuthRedirectionState state,
218218
}
219219

220220
public static OidcProviderMetadata getOidcProviderMetadata(String issuerURL) {
221-
Response response = WebClient.create(issuerURL).path("/.well-known/openid-configuration")
222-
.accept(MediaType.APPLICATION_JSON).get();
223-
if (Status.OK.getStatusCode() != response.getStatus()) {
224-
throw ExceptionUtils.toWebApplicationException(response);
221+
try (WebClient webClient = WebClient.create(issuerURL)) {
222+
try (Response response = webClient.path("/.well-known/openid-configuration")
223+
.accept(MediaType.APPLICATION_JSON).get()) {
224+
if (Status.OK.getStatusCode() != response.getStatus()) {
225+
throw ExceptionUtils.toWebApplicationException(response);
226+
}
227+
228+
return new OidcProviderMetadata(new JsonMapObjectReaderWriter()
229+
.fromJson(response.readEntity(String.class)));
230+
}
225231
}
226-
return new OidcProviderMetadata(new JsonMapObjectReaderWriter().fromJson(response.readEntity(String.class)));
227232
}
228233

229234
}

systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/oauth2/common/OAuth2TestUtils.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -162,18 +162,15 @@ public static String getLocation(WebClient client, OAuthAuthorizationData authzD
162162
form.param("response_type", authzData.getResponseType());
163163
form.param("oauthDecision", "allow");
164164

165-
Response response = client.post(form);
166-
String location;
167-
try {
168-
location = response.getHeaderString("Location");
169-
} finally {
170-
response.close();
171-
}
172-
if (state != null) {
173-
Assert.assertTrue(location.contains("state=" + state));
174-
}
165+
try (Response response = client.post(form)) {
166+
String location = response.getHeaderString("Location");
167+
168+
if (state != null) {
169+
Assert.assertTrue(location.contains("state=" + state));
170+
}
175171

176-
return location;
172+
return location;
173+
}
177174
}
178175

179176
public static ClientAccessToken getAccessTokenWithAuthorizationCode(WebClient client, String code) {

0 commit comments

Comments
 (0)