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