Search in sources :

Example 1 with DCRMException

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

the class DCRMServiceTest method registerApplicationTestWithExistSP.

@Test
public void registerApplicationTestWithExistSP() throws DCRMException, IdentityApplicationManagementException {
    dummyGrantTypes.add("dummy1");
    dummyGrantTypes.add("dummy2");
    applicationRegistrationRequest.setGrantTypes(dummyGrantTypes);
    startTenantFlow();
    mockApplicationManagementService = mock(ApplicationManagementService.class);
    DCRDataHolder dcrDataHolder = DCRDataHolder.getInstance();
    dcrDataHolder.setApplicationManagementService(mockApplicationManagementService);
    when(mockApplicationManagementService.getServiceProvider(dummyClientName, dummyTenantDomain)).thenReturn(new ServiceProvider());
    try {
        dcrmService.registerApplication(applicationRegistrationRequest);
    } catch (IdentityException ex) {
        assertEquals(ex.getErrorCode(), DCRMConstants.ErrorMessages.CONFLICT_EXISTING_APPLICATION.toString());
        return;
    }
    fail("Expected IdentityException was not thrown by registerApplication method");
}
Also used : DCRDataHolder(org.wso2.carbon.identity.oauth.dcr.internal.DCRDataHolder) ServiceProvider(org.wso2.carbon.identity.application.common.model.ServiceProvider) ApplicationManagementService(org.wso2.carbon.identity.application.mgt.ApplicationManagementService) IdentityException(org.wso2.carbon.identity.base.IdentityException) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 2 with DCRMException

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

the class DCRMServiceTest method validateRequestTenantDomainTestWitInvalidOAuthClientException.

@Test
public void validateRequestTenantDomainTestWitInvalidOAuthClientException() throws IdentityOAuth2Exception, InvalidOAuthClientException {
    when(OAuth2Util.getTenantDomainOfOauthApp(dummyConsumerKey)).thenThrow(new InvalidOAuthClientException(""));
    try {
        dcrmService.getApplication(dummyConsumerKey);
    } catch (DCRMException ex) {
        assertEquals(ex.getMessage(), String.format(DCRMConstants.ErrorMessages.TENANT_DOMAIN_MISMATCH.getMessage(), dummyConsumerKey));
        return;
    }
    fail("Expected IdentityException was not thrown by getApplication method");
}
Also used : DCRMException(org.wso2.carbon.identity.oauth.dcr.exception.DCRMException) InvalidOAuthClientException(org.wso2.carbon.identity.oauth.common.exception.InvalidOAuthClientException) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 3 with DCRMException

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

the class DCRMServiceTest method validateRequestTenantDomainTestWitIdentityOAuth2Exception.

@Test
public void validateRequestTenantDomainTestWitIdentityOAuth2Exception() throws IdentityOAuth2Exception, InvalidOAuthClientException {
    when(OAuth2Util.getTenantDomainOfOauthApp(dummyConsumerKey)).thenThrow(new IdentityOAuth2Exception(""));
    try {
        dcrmService.getApplication(dummyConsumerKey);
    } catch (DCRMException ex) {
        assertEquals(ex.getMessage(), String.format(DCRMConstants.ErrorMessages.FAILED_TO_VALIDATE_TENANT_DOMAIN.getMessage(), dummyConsumerKey));
        return;
    }
    fail("Expected DCRMException was not thrown by getApplication method");
}
Also used : DCRMException(org.wso2.carbon.identity.oauth.dcr.exception.DCRMException) IdentityOAuth2Exception(org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 4 with DCRMException

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

the class DCRMService method createServiceProvider.

private ServiceProvider createServiceProvider(String applicationOwner, String tenantDomain, String spName, String templateName) throws DCRMException {
    // Create the Service Provider
    ServiceProvider sp = new ServiceProvider();
    sp.setApplicationName(spName);
    User user = new User();
    user.setUserName(applicationOwner);
    user.setTenantDomain(tenantDomain);
    sp.setOwner(user);
    sp.setDescription("Service Provider for application " + spName);
    createServiceProvider(sp, tenantDomain, applicationOwner, templateName);
    // Get created service provider.
    ServiceProvider clientSP = getServiceProvider(spName, tenantDomain);
    if (clientSP == null) {
        throw DCRMUtils.generateClientException(DCRMConstants.ErrorMessages.FAILED_TO_REGISTER_SP, spName);
    }
    return clientSP;
}
Also used : User(org.wso2.carbon.identity.application.common.model.User) ServiceProvider(org.wso2.carbon.identity.application.common.model.ServiceProvider)

Example 5 with DCRMException

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

the class DCRMService method updateApplication.

/**
 * Update OAuth/OIDC application.
 *
 * @param updateRequest
 * @param clientId
 * @return
 * @throws DCRMException
 */
public Application updateApplication(ApplicationUpdateRequest updateRequest, String clientId) throws DCRMException {
    validateRequestTenantDomain(clientId);
    OAuthConsumerAppDTO appDTO = getApplicationById(clientId);
    String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain();
    String applicationOwner = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
    String clientName = updateRequest.getClientName();
    // Update Service Provider
    ServiceProvider sp = getServiceProvider(appDTO.getApplicationName(), tenantDomain);
    if (StringUtils.isNotEmpty(clientName)) {
        // to register the OAuth app with.
        if (!appDTO.getApplicationName().equals(clientName) && isServiceProviderExist(clientName, tenantDomain)) {
            throw DCRMUtils.generateClientException(DCRMConstants.ErrorMessages.CONFLICT_EXISTING_APPLICATION, clientName);
        }
        // Regex validation of the application name.
        if (!DCRMUtils.isRegexValidated(clientName)) {
            throw DCRMUtils.generateClientException(DCRMConstants.ErrorMessages.BAD_REQUEST_INVALID_SP_NAME, DCRMUtils.getSPValidatorRegex(), null);
        }
        if (sp == null) {
            throw DCRMUtils.generateClientException(DCRMConstants.ErrorMessages.FAILED_TO_GET_SP, appDTO.getApplicationName(), null);
        }
        // Need to create a deep clone, since modifying the fields of the original object,
        // will modify the cached SP object.
        ServiceProvider clonedSP = cloneServiceProvider(sp);
        clonedSP.setApplicationName(clientName);
        updateServiceProvider(clonedSP, tenantDomain, applicationOwner);
    }
    // Update application
    try {
        if (StringUtils.isNotEmpty(clientName)) {
            // Regex validation of the application name.
            if (!DCRMUtils.isRegexValidated(clientName)) {
                throw DCRMUtils.generateClientException(DCRMConstants.ErrorMessages.BAD_REQUEST_INVALID_SP_NAME, DCRMUtils.getSPValidatorRegex(), null);
            }
            appDTO.setApplicationName(clientName);
        }
        if (!updateRequest.getGrantTypes().isEmpty()) {
            String grantType = StringUtils.join(updateRequest.getGrantTypes(), GRANT_TYPE_SEPARATOR);
            appDTO.setGrantTypes(grantType);
        }
        if (!updateRequest.getRedirectUris().isEmpty()) {
            String callbackUrl = validateAndSetCallbackURIs(updateRequest.getRedirectUris(), updateRequest.getGrantTypes());
            appDTO.setCallbackUrl(callbackUrl);
        }
        if (updateRequest.getTokenType() != null) {
            appDTO.setTokenType(updateRequest.getTokenType());
        }
        if (StringUtils.isNotEmpty(updateRequest.getBackchannelLogoutUri())) {
            String backChannelLogoutUri = validateBackchannelLogoutURI(updateRequest.getBackchannelLogoutUri());
            appDTO.setBackChannelLogoutUrl(backChannelLogoutUri);
        }
        oAuthAdminService.updateConsumerApplication(appDTO);
    } catch (IdentityOAuthAdminException e) {
        throw DCRMUtils.generateServerException(DCRMConstants.ErrorMessages.FAILED_TO_UPDATE_APPLICATION, clientId, e);
    }
    return buildResponse(getApplicationById(clientId));
}
Also used : IdentityOAuthAdminException(org.wso2.carbon.identity.oauth.IdentityOAuthAdminException) 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)9 Test (org.testng.annotations.Test)9 DCRMException (org.wso2.carbon.identity.oauth.dcr.exception.DCRMException)6 DCRMEndpointException (org.wso2.carbon.identity.oauth2.dcr.endpoint.exceptions.DCRMEndpointException)5 ArrayList (java.util.ArrayList)4 ServiceProvider (org.wso2.carbon.identity.application.common.model.ServiceProvider)4 OAuthConsumerAppDTO (org.wso2.carbon.identity.oauth.dto.OAuthConsumerAppDTO)4 IdentityApplicationManagementException (org.wso2.carbon.identity.application.common.IdentityApplicationManagementException)3 RegistrationRequestDTO (org.wso2.carbon.identity.oauth2.dcr.endpoint.dto.RegistrationRequestDTO)3 DataProvider (org.testng.annotations.DataProvider)2 ApplicationManagementService (org.wso2.carbon.identity.application.mgt.ApplicationManagementService)2 IdentityException (org.wso2.carbon.identity.base.IdentityException)2 IdentityOAuthAdminException (org.wso2.carbon.identity.oauth.IdentityOAuthAdminException)2 InvalidOAuthClientException (org.wso2.carbon.identity.oauth.common.exception.InvalidOAuthClientException)2 DCRDataHolder (org.wso2.carbon.identity.oauth.dcr.internal.DCRDataHolder)2 IdentityOAuth2Exception (org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception)2 UpdateRequestDTO (org.wso2.carbon.identity.oauth2.dcr.endpoint.dto.UpdateRequestDTO)2 Response (javax.ws.rs.core.Response)1 Log (org.apache.commons.logging.Log)1 InboundAuthenticationConfig (org.wso2.carbon.identity.application.common.model.InboundAuthenticationConfig)1