Search in sources :

Example 16 with ApplicationRegistrationRequest

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

the class DCRMService method createOAuthApplication.

private Application createOAuthApplication(ApplicationRegistrationRequest registrationRequest) throws DCRMException {
    String applicationOwner = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
    String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain();
    String spName = registrationRequest.getClientName();
    String templateName = registrationRequest.getSpTemplateName();
    // Regex validation of the application name.
    if (!DCRMUtils.isRegexValidated(spName)) {
        throw DCRMUtils.generateClientException(DCRMConstants.ErrorMessages.BAD_REQUEST_INVALID_SP_NAME, DCRMUtils.getSPValidatorRegex(), null);
    }
    // Check whether a service provider already exists for the name we are trying to register the OAuth app with.
    if (isServiceProviderExist(spName, tenantDomain)) {
        throw DCRMUtils.generateClientException(DCRMConstants.ErrorMessages.CONFLICT_EXISTING_APPLICATION, spName);
    }
    if (StringUtils.isNotEmpty(registrationRequest.getConsumerKey()) && isClientIdExist(registrationRequest.getConsumerKey())) {
        throw DCRMUtils.generateClientException(DCRMConstants.ErrorMessages.CONFLICT_EXISTING_CLIENT_ID, registrationRequest.getConsumerKey());
    }
    // Create a service provider.
    ServiceProvider serviceProvider = createServiceProvider(applicationOwner, tenantDomain, spName, templateName);
    OAuthConsumerAppDTO createdApp;
    try {
        // Register the OAuth app.
        createdApp = createOAuthApp(registrationRequest, applicationOwner, tenantDomain, spName);
    } catch (DCRMException ex) {
        if (log.isDebugEnabled()) {
            log.debug("OAuth app: " + spName + " registration failed in tenantDomain: " + tenantDomain + ". " + "Deleting the service provider: " + spName + " to rollback.");
        }
        deleteServiceProvider(spName, tenantDomain, applicationOwner);
        throw ex;
    }
    try {
        updateServiceProviderWithOAuthAppDetails(serviceProvider, createdApp, applicationOwner, tenantDomain);
    } catch (DCRMException ex) {
        // Delete the OAuth app created. This will also remove the registered SP for the OAuth app.
        deleteApplication(createdApp.getOauthConsumerKey());
        throw ex;
    }
    return buildResponse(createdApp);
}
Also used : DCRMException(org.wso2.carbon.identity.oauth.dcr.exception.DCRMException) ServiceProvider(org.wso2.carbon.identity.application.common.model.ServiceProvider) OAuthConsumerAppDTO(org.wso2.carbon.identity.oauth.dto.OAuthConsumerAppDTO)

Aggregations

PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)12 Test (org.testng.annotations.Test)12 ApplicationManagementService (org.wso2.carbon.identity.application.mgt.ApplicationManagementService)11 DCRDataHolder (org.wso2.carbon.identity.oauth.dcr.internal.DCRDataHolder)11 ServiceProvider (org.wso2.carbon.identity.application.common.model.ServiceProvider)10 IdentityException (org.wso2.carbon.identity.base.IdentityException)10 OAuthConsumerAppDTO (org.wso2.carbon.identity.oauth.dto.OAuthConsumerAppDTO)9 Matchers.anyString (org.mockito.Matchers.anyString)7 IdentityOAuthAdminException (org.wso2.carbon.identity.oauth.IdentityOAuthAdminException)5 IdentityApplicationManagementException (org.wso2.carbon.identity.application.common.IdentityApplicationManagementException)3 ArrayList (java.util.ArrayList)2 OAuthAdminService (org.wso2.carbon.identity.oauth.OAuthAdminService)2 OAuthServerConfiguration (org.wso2.carbon.identity.oauth.config.OAuthServerConfiguration)2 Application (org.wso2.carbon.identity.oauth.dcr.bean.Application)2 ApplicationRegistrationRequest (org.wso2.carbon.identity.oauth.dcr.bean.ApplicationRegistrationRequest)2 BeforeMethod (org.testng.annotations.BeforeMethod)1 DCRMException (org.wso2.carbon.identity.oauth.dcr.exception.DCRMException)1 UserRealm (org.wso2.carbon.user.api.UserRealm)1 AbstractUserStoreManager (org.wso2.carbon.user.core.common.AbstractUserStoreManager)1