use of org.wso2.carbon.identity.oauth.dto.OAuthErrorDTO in project identity-inbound-auth-oauth by wso2-extensions.
the class OAuth2AuthzEndpointTest method testUserConsentResponse.
@Test(dataProvider = "provideConsentData", groups = "testWithConnection")
public void testUserConsentResponse(String consent, String redirectUrl, Set<String> scopes, int expectedStatus, String oAuthErrorDTODescription, String expectedError) throws Exception {
initMocks(this);
spy(FrameworkUtils.class);
when(authCookie.getValue()).thenReturn("dummyValue");
doReturn(authCookie).when(FrameworkUtils.class, "getAuthCookie", any());
doNothing().when(FrameworkUtils.class, "startTenantFlow", anyString());
doNothing().when(FrameworkUtils.class, "endTenantFlow");
mockStatic(LoggerUtils.class);
when(LoggerUtils.isDiagnosticLogsEnabled()).thenReturn(true);
mockStatic(IdentityTenantUtil.class);
when(IdentityTenantUtil.getTenantId(anyString())).thenReturn(MultitenantConstants.SUPER_TENANT_ID);
SessionContext sessionContext = new SessionContext();
sessionContext.addProperty(FrameworkConstants.CREATED_TIMESTAMP, 1479249799770L);
doReturn(sessionContext).when(FrameworkUtils.class, "getSessionContextFromCache", anyString(), anyString());
when(openIDConnectClaimFilter.getClaimsFilteredByOIDCScopes(any(), anyString())).thenReturn(Arrays.asList("country"));
OAuth2AuthzEndpoint.setOpenIDConnectClaimFilter(openIDConnectClaimFilter);
Set<ExternalClaim> mappings = new HashSet<>();
ExternalClaim claim = new ExternalClaim(OIDC_DIALECT, "country", "http://wso2.org/country");
mappings.add(claim);
when(claimMetadataHandler.getMappingsFromOtherDialectToCarbon(anyString(), any(), anyString())).thenReturn(mappings);
mockStatic(ClaimMetadataHandler.class);
when(ClaimMetadataHandler.getInstance()).thenReturn(claimMetadataHandler);
mockStatic(SessionDataCache.class);
when(SessionDataCache.getInstance()).thenReturn(sessionDataCache);
SessionDataCacheKey consentDataCacheKey = new SessionDataCacheKey(SESSION_DATA_KEY_CONSENT_VALUE);
when(sessionDataCache.getValueFromCache(consentDataCacheKey)).thenReturn(consentCacheEntry);
Map<String, String[]> requestParams = new HashMap<>();
Map<String, Object> requestAttributes = new ConcurrentHashMap<>();
requestParams.put(OAuthConstants.SESSION_DATA_KEY_CONSENT, new String[] { SESSION_DATA_KEY_CONSENT_VALUE });
requestParams.put(FrameworkConstants.RequestParams.TO_COMMONAUTH, new String[] { "false" });
requestParams.put(OAuthConstants.OAuth20Params.SCOPE, new String[] { OAuthConstants.Scope.OPENID });
requestParams.put(OAuthConstants.Prompt.CONSENT, new String[] { consent });
requestParams.put(CLIENT_ID, new String[] { CLIENT_ID_VALUE });
requestAttributes.put(FrameworkConstants.RequestParams.FLOW_STATUS, AuthenticatorFlowStatus.INCOMPLETE);
mockHttpRequest(requestParams, requestAttributes, HttpMethod.POST);
OAuth2Parameters oAuth2Params = setOAuth2Parameters(scopes, APP_NAME, RESPONSE_MODE_FORM_POST, redirectUrl);
oAuth2Params.setClientId(CLIENT_ID_VALUE);
when(consentCacheEntry.getoAuth2Parameters()).thenReturn(oAuth2Params);
when(consentCacheEntry.getLoggedInUser()).thenReturn(new AuthenticatedUser());
mockStatic(OpenIDConnectUserRPStore.class);
when(OpenIDConnectUserRPStore.getInstance()).thenReturn(openIDConnectUserRPStore);
doNothing().when(openIDConnectUserRPStore).putUserRPToStore(any(AuthenticatedUser.class), anyString(), anyBoolean(), anyString());
mockOAuthServerConfiguration();
mockStatic(OAuth2Util.OAuthURL.class);
when(OAuth2Util.OAuthURL.getOAuth2ErrorPageUrl()).thenReturn(ERROR_PAGE_URL);
spy(OAuth2Util.class);
doReturn(new ServiceProvider()).when(OAuth2Util.class, "getServiceProvider", CLIENT_ID_VALUE);
mockEndpointUtil(true);
when(oAuth2Service.getOauthApplicationState(CLIENT_ID_VALUE)).thenReturn("ACTIVE");
mockApplicationManagementService();
when(oAuth2Service.handleUserConsentDenial(oAuth2Params)).thenReturn(oAuthErrorDTO);
when(oAuthErrorDTO.getErrorDescription()).thenReturn(oAuthErrorDTODescription);
Response response;
try {
response = oAuth2AuthzEndpoint.authorize(httpServletRequest, httpServletResponse);
} catch (InvalidRequestParentException ire) {
InvalidRequestExceptionMapper invalidRequestExceptionMapper = new InvalidRequestExceptionMapper();
response = invalidRequestExceptionMapper.toResponse(ire);
}
if (response != null) {
assertEquals(response.getStatus(), expectedStatus, "Unexpected HTTP response status");
MultivaluedMap<String, Object> responseMetadata = response.getMetadata();
assertNotNull(responseMetadata);
if (expectedError != null) {
if (response.getEntity() != null) {
String htmlPost = response.getEntity().toString();
assertTrue(htmlPost.contains(expectedError));
} else {
CollectionUtils.isNotEmpty(responseMetadata.get(HTTPConstants.HEADER_LOCATION));
assertTrue(CollectionUtils.isNotEmpty(responseMetadata.get(HTTPConstants.HEADER_LOCATION)), "Location header not found in the response");
String location = String.valueOf(responseMetadata.get(HTTPConstants.HEADER_LOCATION).get(0));
assertTrue(location.contains(expectedError), "Expected error code not found in URL");
}
}
}
}
use of org.wso2.carbon.identity.oauth.dto.OAuthErrorDTO in project identity-inbound-auth-oauth by wso2-extensions.
the class CibaResponseTypeHandler method handleAuthenticationFailure.
@Override
public OAuthErrorDTO handleAuthenticationFailure(OAuth2Parameters oAuth2Parameters) {
OAuthErrorDTO oAuthErrorDTO = new OAuthErrorDTO();
String authReqID = oAuth2Parameters.getNonce();
String authCodeKey = null;
try {
authCodeKey = CibaDAOFactory.getInstance().getCibaAuthMgtDAO().getCibaAuthCodeKey(authReqID);
CibaDAOFactory.getInstance().getCibaAuthMgtDAO().updateStatus(authCodeKey, AuthReqStatus.FAILED);
oAuthErrorDTO.setErrorDescription("Authentication failed.");
return oAuthErrorDTO;
} catch (CibaCoreException e) {
if (log.isDebugEnabled()) {
log.debug("Error occurred in updating the authentication_status for the ID : " + authReqID + "with responseType as (ciba). ");
}
}
return null;
}
use of org.wso2.carbon.identity.oauth.dto.OAuthErrorDTO in project identity-inbound-auth-oauth by wso2-extensions.
the class CibaResponseTypeHandler method handleUserConsentDenial.
@Override
public OAuthErrorDTO handleUserConsentDenial(OAuth2Parameters oAuth2Parameters) {
OAuthErrorDTO oAuthErrorDTO = new OAuthErrorDTO();
String authReqID = oAuth2Parameters.getNonce();
String authCodeKey;
try {
authCodeKey = CibaDAOFactory.getInstance().getCibaAuthMgtDAO().getCibaAuthCodeKey(authReqID);
// Update authenticationStatus when user denied the consent.
CibaDAOFactory.getInstance().getCibaAuthMgtDAO().updateStatus(authCodeKey, AuthReqStatus.CONSENT_DENIED);
oAuthErrorDTO.setErrorDescription("User denied the consent.");
return oAuthErrorDTO;
} catch (CibaCoreException e) {
if (log.isDebugEnabled()) {
log.debug("Error occurred in updating the authentication_status for the auth_req_id : " + authReqID + "with responseType as (ciba). ");
}
}
return null;
}
Aggregations