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);
}
Aggregations