Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1049,7 +1049,8 @@ sessionState, oauth2Params, getLoggedInUser(oAuthMessage).getAuthenticatedSubjec
authorizationResponseDTO.setSessionState(sessionStateValue);
}

if (OAuthServerConfiguration.getInstance().isOAuthResponseJspPageAvailable()) {
if (OAuthServerConfiguration.getInstance().isOAuthResponseJspPageAvailable()
&& !OAuthConstants.ResponseModes.FORM_POST_JWT.equals(oauth2Params.getResponseMode())) {
String params = buildParams(authorizationResponseDTO.getSuccessResponseDTO().getFormPostBody(),
authenticatedIdPs, sessionStateValue);
Comment on lines +1052 to 1055
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Log Improvement Suggestion No: 1

Suggested change
if (OAuthServerConfiguration.getInstance().isOAuthResponseJspPageAvailable()
&& !OAuthConstants.ResponseModes.FORM_POST_JWT.equals(oauth2Params.getResponseMode())) {
String params = buildParams(authorizationResponseDTO.getSuccessResponseDTO().getFormPostBody(),
authenticatedIdPs, sessionStateValue);
if (OAuthServerConfiguration.getInstance().isOAuthResponseJspPageAvailable()
&& !OAuthConstants.ResponseModes.FORM_POST_JWT.equals(oauth2Params.getResponseMode())) {
log.debug("OAuth response JSP page is available and response mode is not FORM_POST_JWT, building params for JSP response.");
String params = buildParams(authorizationResponseDTO.getSuccessResponseDTO().getFormPostBody(),

String redirectURI = oauth2Params.getRedirectURI();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@
import org.wso2.carbon.identity.oauth2.internal.OAuth2ServiceComponentHolder;
import org.wso2.carbon.identity.oauth2.model.FederatedTokenDO;
import org.wso2.carbon.identity.oauth2.model.OAuth2Parameters;
import org.wso2.carbon.identity.oauth2.responsemode.provider.AuthorizationResponseDTO;
import org.wso2.carbon.identity.oauth2.responsemode.provider.ResponseModeProvider;
import org.wso2.carbon.identity.oauth2.responsemode.provider.impl.DefaultResponseModeProvider;
import org.wso2.carbon.identity.oauth2.responsemode.provider.impl.FormPostResponseModeProvider;
Expand Down Expand Up @@ -2848,6 +2849,90 @@ public void testHandleFormPostResponseModeErrorWithoutDescription() throws Excep
}
}

@Test
public void testHandleFormPostResponseModeSkipsJSPForFormPostJwt() throws Exception {

Method handleFormPostResponseMode = AuthzUtil.class.getDeclaredMethod(
"handleFormPostResponseMode", OAuthMessage.class, OIDCSessionState.class,
AuthorizationResponseDTO.class, AuthenticatedUser.class);
handleFormPostResponseMode.setAccessible(true);

OAuth2Parameters oAuth2Parameters = new OAuth2Parameters();
oAuth2Parameters.setResponseMode(OAuthConstants.ResponseModes.FORM_POST_JWT);
oAuth2Parameters.setRedirectURI("https://localhost:9443/callback");
oAuth2Parameters.setScopes(new HashSet<>(Collections.singletonList("scope1")));

SessionDataCacheEntry cacheEntry = mock(SessionDataCacheEntry.class);
when(cacheEntry.getoAuth2Parameters()).thenReturn(oAuth2Parameters);
when(cacheEntry.getAuthenticatedIdPs()).thenReturn("testIdP");
when(oAuthMessage.getSessionDataCacheEntry()).thenReturn(cacheEntry);

AuthorizationResponseDTO authorizationResponseDTO = new AuthorizationResponseDTO();
authorizationResponseDTO.getSuccessResponseDTO()
.setFormPostBody("{\"code\":\"test_code\",\"state\":\"test_state\"}");

OIDCSessionState sessionState = new OIDCSessionState();

try (MockedStatic<OAuthServerConfiguration> oAuthServerConfiguration =
mockStatic(OAuthServerConfiguration.class)) {
oAuthServerConfiguration.when(OAuthServerConfiguration::getInstance)
.thenReturn(mockOAuthServerConfiguration);
when(mockOAuthServerConfiguration.isOAuthResponseJspPageAvailable()).thenReturn(true);

handleFormPostResponseMode.invoke(authzUtilObject, oAuthMessage, sessionState,
authorizationResponseDTO, null);

Assert.assertFalse(authorizationResponseDTO.getIsForwardToOAuthResponseJSP(),
"form_post.jwt should NOT forward to JSP even when JSP is available");
Assert.assertEquals(authorizationResponseDTO.getAuthenticatedIDPs(), "testIdP",
"AuthenticatedIDPs should be set on the DTO when JSP is skipped");
}
}

@Test
public void testHandleFormPostResponseModeUsesJSPForFormPost() throws Exception {

Method handleFormPostResponseMode = AuthzUtil.class.getDeclaredMethod(
"handleFormPostResponseMode", OAuthMessage.class, OIDCSessionState.class,
AuthorizationResponseDTO.class, AuthenticatedUser.class);
handleFormPostResponseMode.setAccessible(true);

OAuth2Parameters oAuth2Parameters = new OAuth2Parameters();
oAuth2Parameters.setResponseMode(OAuthConstants.ResponseModes.FORM_POST);
oAuth2Parameters.setRedirectURI("https://localhost:9443/callback");
oAuth2Parameters.setScopes(new HashSet<>(Collections.singletonList("scope1")));

SessionDataCacheEntry cacheEntry = mock(SessionDataCacheEntry.class);
when(cacheEntry.getoAuth2Parameters()).thenReturn(oAuth2Parameters);
when(cacheEntry.getAuthenticatedIdPs()).thenReturn("testIdP");
when(oAuthMessage.getSessionDataCacheEntry()).thenReturn(cacheEntry);

AuthorizationResponseDTO authorizationResponseDTO = new AuthorizationResponseDTO();
authorizationResponseDTO.getSuccessResponseDTO()
.setFormPostBody("{\"code\":\"test_code\",\"state\":\"test_state\"}");

OIDCSessionState sessionState = new OIDCSessionState();

try (MockedStatic<OAuthServerConfiguration> oAuthServerConfiguration =
mockStatic(OAuthServerConfiguration.class)) {
oAuthServerConfiguration.when(OAuthServerConfiguration::getInstance)
.thenReturn(mockOAuthServerConfiguration);
when(mockOAuthServerConfiguration.isOAuthResponseJspPageAvailable()).thenReturn(true);

when(oAuthMessage.getRequest()).thenReturn(httpServletRequest);
when(httpServletRequest.getServletContext()).thenReturn(servletContext);
when(servletContext.getContext(anyString())).thenReturn(servletContext);
when(servletContext.getRequestDispatcher(anyString())).thenReturn(requestDispatcher);
doNothing().when(requestDispatcher).forward(any(ServletRequest.class), any(ServletResponse.class));

handleFormPostResponseMode.invoke(authzUtilObject, oAuthMessage, sessionState,
authorizationResponseDTO, null);

Assert.assertTrue(authorizationResponseDTO.getIsForwardToOAuthResponseJSP(),
"form_post should forward to JSP when JSP is available");
}
}

private void mockSSOConsentService(boolean isConsentMgtEnabled) throws SSOConsentServiceException {

// TODO: Remove mocking consentUtil and test the consent flow as well
Expand Down
Loading