Skip to content

Commit 4e23438

Browse files
Avoid rehandling api based auth response
1 parent ac248ee commit 4e23438

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

components/org.wso2.carbon.identity.oauth.endpoint/src/main/java/org/wso2/carbon/identity/oauth/endpoint/authz/OAuth2AuthzEndpoint.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4341,14 +4341,19 @@ private Response handleApiBasedAuthenticationResponse(OAuthMessage oAuthMessage,
43414341
ObjectMapper objectMapper = new ObjectMapper();
43424342
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
43434343
String jsonString = objectMapper.writeValueAsString(authResponse);
4344+
oAuthMessage.getRequest().setAttribute(IS_API_BASED_AUTH_HANDLED, true);
43444345
return Response.ok().entity(jsonString).build();
43454346

43464347
} else {
4347-
String location = oauthResponse.getMetadata().get("Location").get(0).toString();
4348-
if (StringUtils.isNotBlank(location)) {
4349-
Map<String, String> queryParams = getQueryParamsFromUrl(location);
4350-
String jsonPayload = new Gson().toJson(queryParams);
4351-
return Response.status(HttpServletResponse.SC_OK).entity(jsonPayload).build();
4348+
List<Object> locationHeader = oauthResponse.getMetadata().get("Location");
4349+
if (CollectionUtils.isNotEmpty(locationHeader)) {
4350+
String location = locationHeader.get(0).toString();
4351+
if (StringUtils.isNotBlank(location)) {
4352+
Map<String, String> queryParams = getQueryParamsFromUrl(location);
4353+
String jsonPayload = new Gson().toJson(queryParams);
4354+
oAuthMessage.getRequest().setAttribute(IS_API_BASED_AUTH_HANDLED, true);
4355+
return Response.status(HttpServletResponse.SC_OK).entity(jsonPayload).build();
4356+
}
43524357
}
43534358
}
43544359
} catch (AuthServiceException | JsonProcessingException | UnsupportedEncodingException | URISyntaxException e) {
@@ -4357,6 +4362,7 @@ private Response handleApiBasedAuthenticationResponse(OAuthMessage oAuthMessage,
43574362
params.put(OAuthConstants.OAUTH_ERROR, OAuth2ErrorCodes.SERVER_ERROR);
43584363
params.put(OAuthConstants.OAUTH_ERROR_DESCRIPTION, "Server error occurred while performing authorization.");
43594364
String jsonString = new Gson().toJson(params);
4365+
oAuthMessage.getRequest().setAttribute(IS_API_BASED_AUTH_HANDLED, true);
43604366
return Response.status(HttpServletResponse.SC_INTERNAL_SERVER_ERROR).entity(jsonString).build();
43614367
}
43624368

components/org.wso2.carbon.identity.oauth.endpoint/src/test/java/org/wso2/carbon/identity/oauth/endpoint/authz/OAuth2AuthzEndpointTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1856,7 +1856,7 @@ public Object answer(InvocationOnMock invocation) {
18561856

18571857
Method sendRequestToFramework =
18581858
authzEndpointObject.getClass().getDeclaredMethod("handleAuthFlowThroughFramework",
1859-
OAuthMessage.class, String.class);
1859+
OAuthMessage.class, String.class, String.class);
18601860
sendRequestToFramework.setAccessible(true);
18611861

18621862
when(oAuthMessage.getRequest()).thenReturn(httpServletRequest);
@@ -1880,7 +1880,7 @@ public Object answer(InvocationOnMock invocation) {
18801880

18811881
Method sendRequestToFramework2 =
18821882
authzEndpointObject.getClass().getDeclaredMethod("handleAuthFlowThroughFramework",
1883-
OAuthMessage.class, String.class);
1883+
OAuthMessage.class, String.class, String.class);
18841884
sendRequestToFramework2.setAccessible(true);
18851885
try {
18861886
response = (Response) sendRequestToFramework.invoke(authzEndpointObject, oAuthMessage, "type");

0 commit comments

Comments
 (0)