Search in sources :

Example 1 with DCRException

use of org.wso2.carbon.identity.oauth.dcr.DCRException in project identity-inbound-auth-oauth by wso2-extensions.

the class RegistrationHandler method handle.

@Override
public IdentityResponse.IdentityResponseBuilder handle(DCRMessageContext dcrMessageContext) throws DCRException {
    if (log.isDebugEnabled()) {
        log.debug("Request processing started by RegistrationRequestProcessor.");
    }
    RegistrationResponse.DCRRegisterResponseBuilder dcrRegisterResponseBuilder = null;
    RegistrationRequest registerRequest = (RegistrationRequest) dcrMessageContext.getIdentityRequest();
    RegistrationRequestProfile registrationRequestProfile = registerRequest.getRegistrationRequestProfile();
    registrationRequestProfile.setTenantDomain(registerRequest.getTenantDomain());
    RegistrationResponseProfile registrationResponseProfile = DCRManagementService.getInstance().registerOAuthApplication(registrationRequestProfile);
    dcrRegisterResponseBuilder = new RegistrationResponse.DCRRegisterResponseBuilder();
    dcrRegisterResponseBuilder.setRegistrationResponseProfile(registrationResponseProfile);
    return dcrRegisterResponseBuilder;
}
Also used : RegistrationRequestProfile(org.wso2.carbon.identity.oauth.dcr.model.RegistrationRequestProfile) RegistrationResponseProfile(org.wso2.carbon.identity.oauth.dcr.model.RegistrationResponseProfile) RegistrationResponse(org.wso2.carbon.identity.oauth.dcr.model.RegistrationResponse) RegistrationRequest(org.wso2.carbon.identity.oauth.dcr.model.RegistrationRequest)

Example 2 with DCRException

use of org.wso2.carbon.identity.oauth.dcr.DCRException in project identity-inbound-auth-oauth by wso2-extensions.

the class DCRProcessorTest method testProcessWithException.

@Test(dataProvider = "instanceType&ErrorcodeProvider")
public void testProcessWithException(String request, Object identityRequest, String errorCode) throws Exception {
    mockHandlerManager = mock(HandlerManager.class);
    mockStatic(HandlerManager.class);
    when(HandlerManager.getInstance()).thenReturn(mockHandlerManager);
    DCRMessageContext dcrMessageContext = mock(DCRMessageContext.class);
    whenNew(DCRMessageContext.class).withArguments(identityRequest).thenReturn(dcrMessageContext);
    if (request.equals("RegistrationRequest")) {
        RegistrationHandler registrationHandler = mock(RegistrationHandler.class);
        when(mockHandlerManager.getRegistrationHandler(dcrMessageContext)).thenReturn(registrationHandler);
        if (errorCode.isEmpty()) {
            doThrow(new DCRException("")).when(registrationHandler).handle(dcrMessageContext);
        } else {
            doThrow(new DCRException(errorCode, "")).when(registrationHandler).handle(dcrMessageContext);
        }
        try {
            dcrProcessor.process((RegistrationRequest) identityRequest);
            fail("Expected exception IdentityException not thrown by process method");
        } catch (IdentityException ex) {
            if (errorCode.isEmpty()) {
                assertEquals(ex.getErrorCode(), ErrorCodes.BAD_REQUEST.toString());
            } else {
                assertEquals(ex.getErrorCode(), errorCode);
            }
        }
    } else if (request.equals("UnregistrationRequest")) {
        UnRegistrationHandler unRegistrationHandler = mock(UnRegistrationHandler.class);
        when(mockHandlerManager.getUnRegistrationHandler(dcrMessageContext)).thenReturn(unRegistrationHandler);
        if (errorCode.isEmpty()) {
            doThrow(new DCRException("")).when(unRegistrationHandler).handle(dcrMessageContext);
        } else {
            doThrow(new DCRException(errorCode, "")).when(unRegistrationHandler).handle(dcrMessageContext);
        }
        try {
            dcrProcessor.process((UnregistrationRequest) identityRequest);
            fail("Expected exception IdentityException not thrown by registerOAuthApplication");
        } catch (IdentityException ex) {
            if (errorCode.isEmpty()) {
                assertEquals(ex.getMessage(), ErrorCodes.BAD_REQUEST.toString());
            } else {
                assertEquals(ex.getMessage(), errorCode);
            }
        }
    }
}
Also used : UnRegistrationHandler(org.wso2.carbon.identity.oauth.dcr.handler.UnRegistrationHandler) UnregistrationRequest(org.wso2.carbon.identity.oauth.dcr.model.UnregistrationRequest) HandlerManager(org.wso2.carbon.identity.oauth.dcr.util.HandlerManager) RegistrationHandler(org.wso2.carbon.identity.oauth.dcr.handler.RegistrationHandler) UnRegistrationHandler(org.wso2.carbon.identity.oauth.dcr.handler.UnRegistrationHandler) DCRMessageContext(org.wso2.carbon.identity.oauth.dcr.context.DCRMessageContext) IdentityException(org.wso2.carbon.identity.base.IdentityException) DCRException(org.wso2.carbon.identity.oauth.dcr.DCRException) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 3 with DCRException

use of org.wso2.carbon.identity.oauth.dcr.DCRException in project identity-inbound-auth-oauth by wso2-extensions.

the class DCRManagementServiceTest method oAuthApplicationAvailableExceptionTest.

@Test
public void oAuthApplicationAvailableExceptionTest() throws Exception {
    startTenantFlow();
    mockApplicationManagementService = mock(ApplicationManagementService.class);
    DCRDataHolder dcrDataHolder = DCRDataHolder.getInstance();
    dcrDataHolder.setApplicationManagementService(mockApplicationManagementService);
    doThrow(new IdentityApplicationManagementException("")).when(mockApplicationManagementService).getServiceProvider(applicationName, tenantDomain);
    try {
        dcrManagementService.isOAuthApplicationAvailable(applicationName);
    } catch (DCRException ex) {
        assertEquals(ex.getMessage(), "Error occurred while retrieving information of OAuthApp " + applicationName);
        return;
    }
    fail("Expected IdentityException was not thrown by isOAuthApplicationAvailable method");
}
Also used : DCRDataHolder(org.wso2.carbon.identity.oauth.dcr.internal.DCRDataHolder) IdentityApplicationManagementException(org.wso2.carbon.identity.application.common.IdentityApplicationManagementException) ApplicationManagementService(org.wso2.carbon.identity.application.mgt.ApplicationManagementService) DCRException(org.wso2.carbon.identity.oauth.dcr.DCRException) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 4 with DCRException

use of org.wso2.carbon.identity.oauth.dcr.DCRException in project identity-inbound-auth-oauth by wso2-extensions.

the class DCRManagementService method unregisterOAuthApplication.

/**
 * This method will unregister a created OAuth application.
 *
 * @param userId          - UserId of the owner
 * @param applicationName - OAuth application name
 * @param consumerKey     - ConsumerKey of the OAuth application
 * @throws DCRException
 */
public void unregisterOAuthApplication(String userId, String applicationName, String consumerKey) throws DCRException {
    if (!StringUtils.isNotEmpty(userId) || !StringUtils.isNotEmpty(applicationName) || !StringUtils.isNotEmpty(consumerKey)) {
        throw new DCRException("Username, Application Name and Consumer Key cannot be null or empty");
    }
    String tenantDomain = MultitenantUtils.getTenantDomain(userId);
    String userName = MultitenantUtils.getTenantAwareUsername(userId);
    PrivilegedCarbonContext.startTenantFlow();
    PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain, true);
    PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(userName);
    OAuthAdminService oAuthAdminService = new OAuthAdminService();
    OAuthConsumerAppDTO oAuthConsumerApp = null;
    try {
        oAuthConsumerApp = oAuthAdminService.getOAuthApplicationData(consumerKey);
    } catch (Exception e) {
        // class.
        if (log.isDebugEnabled()) {
            log.debug("Error occurred while oauth application data by consumer id.", e);
        }
    }
    if (oAuthConsumerApp != null) {
        try {
            oAuthAdminService.removeOAuthApplicationData(consumerKey);
            ApplicationManagementService appMgtService = DCRDataHolder.getInstance().getApplicationManagementService();
            if (appMgtService == null) {
                throw new IllegalStateException("Error occurred while retrieving Application Management Service");
            }
            ServiceProvider createdServiceProvider = appMgtService.getServiceProvider(applicationName, tenantDomain);
            if (createdServiceProvider == null) {
                throw new DCRException("Couldn't retrieve Service Provider Application " + applicationName);
            }
            appMgtService.deleteApplication(applicationName, tenantDomain, userName);
        } catch (IdentityApplicationManagementException e) {
            throw new DCRException("Error occurred while removing ServiceProvider for application '" + applicationName + "'", e);
        } catch (IdentityOAuthAdminException e) {
            throw new DCRException("Error occurred while removing application '" + applicationName + "'", e);
        } finally {
            PrivilegedCarbonContext.endTenantFlow();
        }
    }
}
Also used : IdentityOAuthAdminException(org.wso2.carbon.identity.oauth.IdentityOAuthAdminException) OAuthAdminService(org.wso2.carbon.identity.oauth.OAuthAdminService) ServiceProvider(org.wso2.carbon.identity.application.common.model.ServiceProvider) IdentityApplicationManagementException(org.wso2.carbon.identity.application.common.IdentityApplicationManagementException) OAuthConsumerAppDTO(org.wso2.carbon.identity.oauth.dto.OAuthConsumerAppDTO) ApplicationManagementService(org.wso2.carbon.identity.application.mgt.ApplicationManagementService) DCRException(org.wso2.carbon.identity.oauth.dcr.DCRException) DCRException(org.wso2.carbon.identity.oauth.dcr.DCRException) IdentityValidationException(org.wso2.carbon.identity.base.IdentityValidationException) IdentityOAuthAdminException(org.wso2.carbon.identity.oauth.IdentityOAuthAdminException) IdentityApplicationManagementException(org.wso2.carbon.identity.application.common.IdentityApplicationManagementException) IdentityException(org.wso2.carbon.identity.base.IdentityException)

Example 5 with DCRException

use of org.wso2.carbon.identity.oauth.dcr.DCRException in project identity-inbound-auth-oauth by wso2-extensions.

the class DCRProcessor method process.

@Override
public IdentityResponse.IdentityResponseBuilder process(IdentityRequest identityRequest) throws DCRException {
    if (log.isDebugEnabled()) {
        log.debug("Request processing started by DCRProcessor.");
    }
    DCRMessageContext dcrMessageContext = new DCRMessageContext(identityRequest);
    IdentityResponse.IdentityResponseBuilder identityResponseBuilder = null;
    if (identityRequest instanceof RegistrationRequest) {
        identityResponseBuilder = registerOAuthApplication(dcrMessageContext);
    } else if (identityRequest instanceof UnregistrationRequest) {
        identityResponseBuilder = unRegisterOAuthApplication(dcrMessageContext);
    }
    return identityResponseBuilder;
}
Also used : UnregistrationRequest(org.wso2.carbon.identity.oauth.dcr.model.UnregistrationRequest) IdentityResponse(org.wso2.carbon.identity.application.authentication.framework.inbound.IdentityResponse) DCRMessageContext(org.wso2.carbon.identity.oauth.dcr.context.DCRMessageContext) RegistrationRequest(org.wso2.carbon.identity.oauth.dcr.model.RegistrationRequest)

Aggregations

DCRException (org.wso2.carbon.identity.oauth.dcr.DCRException)8 IdentityResponse (org.wso2.carbon.identity.application.authentication.framework.inbound.IdentityResponse)4 RegistrationResponseProfile (org.wso2.carbon.identity.oauth.dcr.model.RegistrationResponseProfile)4 IdentityApplicationManagementException (org.wso2.carbon.identity.application.common.IdentityApplicationManagementException)3 ApplicationManagementService (org.wso2.carbon.identity.application.mgt.ApplicationManagementService)3 UnRegistrationHandler (org.wso2.carbon.identity.oauth.dcr.handler.UnRegistrationHandler)3 RegistrationRequest (org.wso2.carbon.identity.oauth.dcr.model.RegistrationRequest)3 UnregistrationRequest (org.wso2.carbon.identity.oauth.dcr.model.UnregistrationRequest)3 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 Test (org.testng.annotations.Test)2 ServiceProvider (org.wso2.carbon.identity.application.common.model.ServiceProvider)2 IdentityException (org.wso2.carbon.identity.base.IdentityException)2 IdentityValidationException (org.wso2.carbon.identity.base.IdentityValidationException)2 IdentityOAuthAdminException (org.wso2.carbon.identity.oauth.IdentityOAuthAdminException)2 OAuthAdminService (org.wso2.carbon.identity.oauth.OAuthAdminService)2 DCRMessageContext (org.wso2.carbon.identity.oauth.dcr.context.DCRMessageContext)2 UnRegistrationException (org.wso2.carbon.identity.oauth.dcr.exception.UnRegistrationException)2 RegistrationHandler (org.wso2.carbon.identity.oauth.dcr.handler.RegistrationHandler)2 RegistrationRequestProfile (org.wso2.carbon.identity.oauth.dcr.model.RegistrationRequestProfile)2 RegistrationResponse (org.wso2.carbon.identity.oauth.dcr.model.RegistrationResponse)2