Search in sources :

Example 1 with OAuthErrorDTO

use of org.wso2.carbon.identity.oauth.dto.OAuthErrorDTO in project identity-inbound-auth-oauth by wso2-extensions.

the class OAuth2AuthzEndpointTest method testBuildOAuthProblemException.

@Test(dataProvider = "provideFailedAuthenticationErrorInfo")
public void testBuildOAuthProblemException(Object oAuthErrorDTOObject, Object authenticationResultObject, String expectedCode, String expectedMessage, String expectedURI) throws Exception {
    OAuthErrorDTO oAuthErrorDTO = (OAuthErrorDTO) oAuthErrorDTOObject;
    AuthenticationResult authenticationResult = (AuthenticationResult) authenticationResultObject;
    Assert.assertEquals(expectedCode, oAuth2AuthzEndpoint.buildOAuthProblemException(authenticationResult, oAuthErrorDTO).getError());
    Assert.assertEquals(expectedMessage, oAuth2AuthzEndpoint.buildOAuthProblemException(authenticationResult, oAuthErrorDTO).getDescription());
    Assert.assertEquals(expectedURI, oAuth2AuthzEndpoint.buildOAuthProblemException(authenticationResult, oAuthErrorDTO).getUri());
}
Also used : OAuthErrorDTO(org.wso2.carbon.identity.oauth.dto.OAuthErrorDTO) AuthenticationResult(org.wso2.carbon.identity.application.authentication.framework.model.AuthenticationResult) Test(org.testng.annotations.Test) AfterTest(org.testng.annotations.AfterTest) BeforeTest(org.testng.annotations.BeforeTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 2 with OAuthErrorDTO

use of org.wso2.carbon.identity.oauth.dto.OAuthErrorDTO in project identity-inbound-auth-oauth by wso2-extensions.

the class OAuth2AuthzEndpointTest method provideFailedAuthenticationErrorInfo.

@DataProvider(name = "provideFailedAuthenticationErrorInfo")
public Object[][] provideFailedAuthenticationErrorInfo() {
    OAuthErrorDTO oAuthErrorDTO = null;
    AuthenticationResult authenticationResult = new AuthenticationResult();
    authenticationResult.addProperty(FrameworkConstants.AUTH_ERROR_URI, null);
    authenticationResult.addProperty(FrameworkConstants.AUTH_ERROR_MSG, null);
    authenticationResult.addProperty(FrameworkConstants.AUTH_ERROR_CODE, null);
    OAuthErrorDTO oAuthErrorDTONull = new OAuthErrorDTO();
    AuthenticationResult authenticationResultEmpty = new AuthenticationResult();
    OAuthErrorDTO oAuthErrorDTOEmpty = new OAuthErrorDTO();
    AuthenticationResult authenticationResultWithURI = new AuthenticationResult();
    authenticationResultWithURI.addProperty(FrameworkConstants.AUTH_ERROR_URI, "http://sample_error_uri.com");
    authenticationResultWithURI.addProperty(FrameworkConstants.AUTH_ERROR_MSG, null);
    authenticationResultWithURI.addProperty(FrameworkConstants.AUTH_ERROR_CODE, null);
    OAuthErrorDTO oAuthErrorDTOEmptyTest = new OAuthErrorDTO();
    AuthenticationResult authenticationResultWithoutErrorcode = new AuthenticationResult();
    authenticationResultWithoutErrorcode.addProperty(FrameworkConstants.AUTH_ERROR_MSG, "OverRiddenMessage2");
    authenticationResultWithoutErrorcode.addProperty(FrameworkConstants.AUTH_ERROR_URI, "http://sample_error_uri2.com");
    authenticationResultWithoutErrorcode.addProperty(FrameworkConstants.AUTH_ERROR_CODE, null);
    OAuthErrorDTO oAuthErrorDTOWithDes = new OAuthErrorDTO();
    oAuthErrorDTOWithDes.setErrorDescription("messageFromErrorDTO");
    AuthenticationResult authenticationResultWithURIOnly = new AuthenticationResult();
    authenticationResultWithURIOnly.addProperty(FrameworkConstants.AUTH_ERROR_URI, "http://sample_error_uri3.com");
    authenticationResultWithURIOnly.addProperty(FrameworkConstants.AUTH_ERROR_MSG, null);
    authenticationResultWithURIOnly.addProperty(FrameworkConstants.AUTH_ERROR_CODE, null);
    OAuthErrorDTO oAuthErrorDTOOverWritable = new OAuthErrorDTO();
    oAuthErrorDTOOverWritable.setErrorDescription("messageFromErrorDTO");
    AuthenticationResult authenticationResultOverRiding = new AuthenticationResult();
    authenticationResultOverRiding.addProperty(FrameworkConstants.AUTH_ERROR_MSG, "OverRiddenMessage5");
    authenticationResultOverRiding.addProperty(FrameworkConstants.AUTH_ERROR_URI, "http://sample_error_uri4.com");
    authenticationResultOverRiding.addProperty(FrameworkConstants.AUTH_ERROR_CODE, null);
    return new Object[][] { { null, authenticationResult, "login_required", "Authentication required", null }, { oAuthErrorDTONull, authenticationResultEmpty, "login_required", "Authentication required", null }, { oAuthErrorDTOEmptyTest, authenticationResultWithURI, "login_required", "Authentication required", "http" + "://sample_error_uri.com" }, { oAuthErrorDTOEmptyTest, authenticationResultWithoutErrorcode, "login_required", "OverRiddenMessage2", "http" + "://sample_error_uri2.com" }, { oAuthErrorDTOWithDes, authenticationResultWithURIOnly, "login_required", "messageFromErrorDTO", "http" + "://sample_error_uri3.com" }, { oAuthErrorDTOOverWritable, authenticationResultOverRiding, "login_required", "OverRiddenMessage5", "http" + "://sample_error_uri4.com" } };
}
Also used : RequestObject(org.wso2.carbon.identity.openidconnect.model.RequestObject) OAuthErrorDTO(org.wso2.carbon.identity.oauth.dto.OAuthErrorDTO) AuthenticationResult(org.wso2.carbon.identity.application.authentication.framework.model.AuthenticationResult) DataProvider(org.testng.annotations.DataProvider)

Example 3 with OAuthErrorDTO

use of org.wso2.carbon.identity.oauth.dto.OAuthErrorDTO in project identity-inbound-auth-oauth by wso2-extensions.

the class OAuth2AuthzEndpoint method handleFailedAuthentication.

private Response handleFailedAuthentication(OAuthMessage oAuthMessage, OAuth2Parameters oauth2Params, AuthenticationResult authnResult) throws URISyntaxException {
    OAuthErrorDTO oAuthErrorDTO = EndpointUtil.getOAuth2Service().handleAuthenticationFailure(oauth2Params);
    OAuthProblemException oauthException = buildOAuthProblemException(authnResult, oAuthErrorDTO);
    return handleFailedState(oAuthMessage, oauth2Params, oauthException);
}
Also used : OAuthProblemException(org.apache.oltu.oauth2.common.exception.OAuthProblemException) OAuthErrorDTO(org.wso2.carbon.identity.oauth.dto.OAuthErrorDTO)

Example 4 with OAuthErrorDTO

use of org.wso2.carbon.identity.oauth.dto.OAuthErrorDTO in project identity-inbound-auth-oauth by wso2-extensions.

the class OAuth2AuthzEndpoint method handleDeniedConsent.

private Response handleDeniedConsent(OAuthMessage oAuthMessage) throws OAuthSystemException, URISyntaxException {
    OAuth2Parameters oauth2Params = getOauth2Params(oAuthMessage);
    OpenIDConnectUserRPStore.getInstance().putUserRPToStore(getLoggedInUser(oAuthMessage), getOauth2Params(oAuthMessage).getApplicationName(), false, oauth2Params.getClientId());
    OAuthErrorDTO oAuthErrorDTO = EndpointUtil.getOAuth2Service().handleUserConsentDenial(oauth2Params);
    OAuthProblemException consentDenialException = buildConsentDenialException(oAuthErrorDTO);
    String denyResponse = EndpointUtil.getErrorRedirectURL(oAuthMessage.getRequest(), consentDenialException, oauth2Params);
    if (StringUtils.equals(oauth2Params.getResponseMode(), RESPONSE_MODE_FORM_POST)) {
        return handleFailedState(oAuthMessage, oauth2Params, consentDenialException);
    }
    return Response.status(HttpServletResponse.SC_FOUND).location(new URI(denyResponse)).build();
}
Also used : OAuth2Parameters(org.wso2.carbon.identity.oauth2.model.OAuth2Parameters) OAuthProblemException(org.apache.oltu.oauth2.common.exception.OAuthProblemException) OAuthErrorDTO(org.wso2.carbon.identity.oauth.dto.OAuthErrorDTO) URI(java.net.URI) REDIRECT_URI(org.wso2.carbon.identity.oauth.common.OAuthConstants.OAuth20Params.REDIRECT_URI)

Example 5 with OAuthErrorDTO

use of org.wso2.carbon.identity.oauth.dto.OAuthErrorDTO in project identity-inbound-auth-oauth by wso2-extensions.

the class OAuth2AuthzEndpointTest method testAuthorizeForAuthenticationResponse.

@Test(dataProvider = "provideAuthenticatedData", groups = "testWithConnection")
public void testAuthorizeForAuthenticationResponse(boolean isResultInRequest, boolean isAuthenticated, Map<ClaimMapping, String> attributes, String errorCode, String errorMsg, String errorUri, Set<String> scopes, String responseMode, String redirectUri, int expected) throws Exception {
    mockStatic(SessionDataCache.class);
    when(SessionDataCache.getInstance()).thenReturn(sessionDataCache);
    SessionDataCacheKey loginDataCacheKey = new SessionDataCacheKey(SESSION_DATA_KEY_VALUE);
    when(sessionDataCache.getValueFromCache(loginDataCacheKey)).thenReturn(loginCacheEntry);
    mockStatic(LoggerUtils.class);
    when(LoggerUtils.isDiagnosticLogsEnabled()).thenReturn(true);
    AuthenticationResult result = setAuthenticationResult(isAuthenticated, attributes, errorCode, errorMsg, errorUri);
    AuthenticationResult resultInRequest = null;
    AuthenticationResultCacheEntry authResultCacheEntry = null;
    if (isResultInRequest) {
        resultInRequest = result;
    } else {
        authResultCacheEntry = new AuthenticationResultCacheEntry();
        authResultCacheEntry.setResult(result);
    }
    Map<String, String[]> requestParams = new HashMap<>();
    Map<String, Object> requestAttributes = new HashMap<>();
    requestParams.put(CLIENT_ID, new String[] { CLIENT_ID_VALUE });
    requestParams.put(FrameworkConstants.RequestParams.TO_COMMONAUTH, new String[] { "false" });
    requestParams.put(OAuthConstants.OAuth20Params.SCOPE, new String[] { OAuthConstants.Scope.OPENID });
    requestAttributes.put(FrameworkConstants.RequestParams.FLOW_STATUS, AuthenticatorFlowStatus.INCOMPLETE);
    requestAttributes.put(FrameworkConstants.SESSION_DATA_KEY, SESSION_DATA_KEY_VALUE);
    requestAttributes.put(FrameworkConstants.RequestAttribute.AUTH_RESULT, resultInRequest);
    mockHttpRequest(requestParams, requestAttributes, HttpMethod.POST);
    spy(FrameworkUtils.class);
    doReturn(requestCoordinator).when(FrameworkUtils.class, "getRequestCoordinator");
    doNothing().when(FrameworkUtils.class, "startTenantFlow", anyString());
    doNothing().when(FrameworkUtils.class, "endTenantFlow");
    spy(IdentityUtil.class);
    doReturn("https://localhost:9443/carbon").when(IdentityUtil.class, "getServerURL", anyString(), anyBoolean(), anyBoolean());
    OAuth2Parameters oAuth2Params = setOAuth2Parameters(scopes, APP_NAME, responseMode, redirectUri);
    oAuth2Params.setClientId(CLIENT_ID_VALUE);
    oAuth2Params.setState(STATE);
    when(loginCacheEntry.getoAuth2Parameters()).thenReturn(oAuth2Params);
    when(loginCacheEntry.getLoggedInUser()).thenReturn(result.getSubject());
    mockOAuthServerConfiguration();
    mockStatic(IdentityTenantUtil.class);
    when(IdentityTenantUtil.getTenantDomain(anyInt())).thenReturn(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
    when(IdentityTenantUtil.getTenantId(anyString())).thenReturn(MultitenantConstants.SUPER_TENANT_ID);
    spy(FrameworkUtils.class);
    doReturn("sample").when(FrameworkUtils.class, "resolveUserIdFromUsername", anyInt(), anyString(), anyString());
    doNothing().when(FrameworkUtils.class, "startTenantFlow", anyString());
    doNothing().when(FrameworkUtils.class, "endTenantFlow");
    try (Connection connection = getConnection()) {
        mockStatic(IdentityDatabaseUtil.class);
        when(IdentityDatabaseUtil.getDBConnection()).thenReturn(connection);
        mockStatic(OpenIDConnectUserRPStore.class);
        when(OpenIDConnectUserRPStore.getInstance()).thenReturn(openIDConnectUserRPStore);
        when(openIDConnectUserRPStore.hasUserApproved(any(AuthenticatedUser.class), anyString(), anyString())).thenReturn(true);
        mockEndpointUtil(false);
        when(oAuth2Service.getOauthApplicationState(CLIENT_ID_VALUE)).thenReturn("ACTIVE");
        mockApplicationManagementService();
        mockEndpointUtil(false);
        when(oAuth2Service.handleAuthenticationFailure(oAuth2Params)).thenReturn(oAuthErrorDTO);
        when(oAuth2ScopeService.hasUserProvidedConsentForAllRequestedScopes(anyString(), anyString(), anyInt(), anyList())).thenReturn(true);
        mockServiceURLBuilder();
        Response response = oAuth2AuthzEndpoint.authorize(httpServletRequest, httpServletResponse);
        assertEquals(response.getStatus(), expected, "Unexpected HTTP response status");
        if (!isAuthenticated) {
            String expectedState = "name=\"" + OAuthConstants.OAuth20Params.STATE + "\" value=\"" + STATE + "\"";
            assertTrue(response.getEntity().toString().contains(expectedState));
        }
    }
}
Also used : OAuth2Parameters(org.wso2.carbon.identity.oauth2.model.OAuth2Parameters) OAuth2ScopeConsentResponse(org.wso2.carbon.identity.oauth2.model.OAuth2ScopeConsentResponse) Response(javax.ws.rs.core.Response) OAuthResponse(org.apache.oltu.oauth2.common.message.OAuthResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) Connection(java.sql.Connection) SessionDataCacheKey(org.wso2.carbon.identity.oauth.cache.SessionDataCacheKey) RequestObject(org.wso2.carbon.identity.openidconnect.model.RequestObject) Matchers.anyString(org.mockito.Matchers.anyString) AuthenticatedUser(org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser) AuthenticationResultCacheEntry(org.wso2.carbon.identity.application.authentication.framework.cache.AuthenticationResultCacheEntry) AuthenticationResult(org.wso2.carbon.identity.application.authentication.framework.model.AuthenticationResult) Test(org.testng.annotations.Test) AfterTest(org.testng.annotations.AfterTest) BeforeTest(org.testng.annotations.BeforeTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

OAuthErrorDTO (org.wso2.carbon.identity.oauth.dto.OAuthErrorDTO)6 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 AfterTest (org.testng.annotations.AfterTest)3 BeforeTest (org.testng.annotations.BeforeTest)3 Test (org.testng.annotations.Test)3 AuthenticationResult (org.wso2.carbon.identity.application.authentication.framework.model.AuthenticationResult)3 OAuth2Parameters (org.wso2.carbon.identity.oauth2.model.OAuth2Parameters)3 RequestObject (org.wso2.carbon.identity.openidconnect.model.RequestObject)3 HashMap (java.util.HashMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)2 Response (javax.ws.rs.core.Response)2 OAuthProblemException (org.apache.oltu.oauth2.common.exception.OAuthProblemException)2 OAuthResponse (org.apache.oltu.oauth2.common.message.OAuthResponse)2 Matchers.anyString (org.mockito.Matchers.anyString)2 AuthenticatedUser (org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser)2 SessionDataCacheKey (org.wso2.carbon.identity.oauth.cache.SessionDataCacheKey)2 CibaCoreException (org.wso2.carbon.identity.oauth.ciba.exceptions.CibaCoreException)2 OAuth2ScopeConsentResponse (org.wso2.carbon.identity.oauth2.model.OAuth2ScopeConsentResponse)2