Search in sources :

Example 11 with ApplicationRegistrationRequest

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

the class DCRMServiceTest method registerApplicationTestWithFailedToDeleteCreatedSP.

@Test(dataProvider = "redirectUriProvider")
public void registerApplicationTestWithFailedToDeleteCreatedSP(List<String> redirectUri) throws Exception {
    mockStatic(IdentityProviderManager.class);
    mockApplicationManagementService = mock(ApplicationManagementService.class);
    Whitebox.setInternalState(dcrmService, "oAuthAdminService", mockOAuthAdminService);
    startTenantFlow();
    dummyGrantTypes.add(DCRConstants.GrantTypes.IMPLICIT);
    applicationRegistrationRequest.setGrantTypes(dummyGrantTypes);
    String grantType = StringUtils.join(applicationRegistrationRequest.getGrantTypes(), " ");
    ServiceProvider serviceProvider = new ServiceProvider();
    DCRDataHolder dcrDataHolder = DCRDataHolder.getInstance();
    dcrDataHolder.setApplicationManagementService(mockApplicationManagementService);
    when(mockApplicationManagementService.getServiceProvider(dummyClientName, dummyTenantDomain)).thenReturn(null, serviceProvider);
    applicationRegistrationRequest.setRedirectUris(redirectUri);
    OAuthConsumerAppDTO oAuthConsumerApp = new OAuthConsumerAppDTO();
    oAuthConsumerApp.setApplicationName(dummyClientName);
    oAuthConsumerApp.setGrantTypes(grantType);
    oAuthConsumerApp.setOAuthVersion(OAUTH_VERSION);
    whenNew(OAuthConsumerAppDTO.class).withNoArguments().thenReturn(oAuthConsumerApp);
    doThrow(new IdentityOAuthAdminException("")).when(mockOAuthAdminService).registerOAuthApplicationData(oAuthConsumerApp);
    doThrow(new IdentityApplicationManagementException("")).when(mockApplicationManagementService).deleteApplication(dummyClientName, dummyTenantDomain, dummyUserName);
    try {
        dcrmService.registerApplication(applicationRegistrationRequest);
    } catch (IdentityException ex) {
        assertEquals(ex.getErrorCode(), DCRMConstants.ErrorMessages.FAILED_TO_DELETE_SP.toString());
        return;
    }
    fail("Expected IdentityException was not thrown by registerApplication method");
}
Also used : IdentityOAuthAdminException(org.wso2.carbon.identity.oauth.IdentityOAuthAdminException) DCRDataHolder(org.wso2.carbon.identity.oauth.dcr.internal.DCRDataHolder) 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) Matchers.anyString(org.mockito.Matchers.anyString) 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 12 with ApplicationRegistrationRequest

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

the class DCRMServiceTest method registerApplicationTestWithSP.

@Test(dataProvider = "redirectUriProvider")
public void registerApplicationTestWithSP(List<String> redirectUri) throws Exception {
    mockApplicationManagementService = mock(ApplicationManagementService.class);
    Whitebox.setInternalState(dcrmService, "oAuthAdminService", mockOAuthAdminService);
    startTenantFlow();
    dummyGrantTypes.add("implicit");
    applicationRegistrationRequest.setGrantTypes(dummyGrantTypes);
    applicationRegistrationRequest.setConsumerSecret(dummyConsumerSecret);
    applicationRegistrationRequest.setTokenType(dummyTokenType);
    applicationRegistrationRequest.setBackchannelLogoutUri(dummyBackchannelLogoutUri);
    applicationRegistrationRequest.setConsumerKey(dummyConsumerKey);
    String grantType = StringUtils.join(applicationRegistrationRequest.getGrantTypes(), " ");
    ServiceProvider serviceProvider = new ServiceProvider();
    DCRDataHolder dcrDataHolder = DCRDataHolder.getInstance();
    dcrDataHolder.setApplicationManagementService(mockApplicationManagementService);
    when(mockApplicationManagementService.getServiceProvider(dummyClientName, dummyTenantDomain)).thenReturn(null, serviceProvider);
    applicationRegistrationRequest.setRedirectUris(redirectUri);
    OAuthConsumerAppDTO oAuthConsumerApp = new OAuthConsumerAppDTO();
    oAuthConsumerApp.setApplicationName(dummyClientName);
    oAuthConsumerApp.setGrantTypes(dummyGrantTypes.get(0));
    oAuthConsumerApp.setOauthConsumerKey(dummyConsumerKey);
    oAuthConsumerApp.setOauthConsumerSecret(dummyConsumerSecret);
    oAuthConsumerApp.setCallbackUrl(redirectUri.get(0));
    oAuthConsumerApp.setGrantTypes(grantType);
    oAuthConsumerApp.setOAuthVersion(OAUTH_VERSION);
    when(mockOAuthAdminService.getOAuthApplicationDataByAppName(dummyClientName)).thenReturn(oAuthConsumerApp);
    when(mockOAuthAdminService.registerAndRetrieveOAuthApplicationData(any(OAuthConsumerAppDTO.class))).thenReturn(oAuthConsumerApp);
    OAuthServerConfiguration oAuthServerConfiguration = OAuthServerConfiguration.getInstance();
    assertNotNull(oAuthServerConfiguration);
    when(oAuthServerConfiguration.getClientIdValidationRegex()).thenReturn("[a-zA-Z0-9_]{15,30}");
    String toString = "Application {\n" + "  clientName: " + oAuthConsumerApp.getApplicationName() + "\n" + "  clientId: " + oAuthConsumerApp.getOauthConsumerKey() + "\n" + "  clientSecret: " + oAuthConsumerApp.getOauthConsumerSecret() + "\n" + "  redirectUris: " + Arrays.asList(oAuthConsumerApp.getCallbackUrl()) + "\n" + "  grantTypes: " + Arrays.asList(oAuthConsumerApp.getGrantTypes().split(" ")) + "\n" + "}\n";
    Application application = dcrmService.registerApplication(applicationRegistrationRequest);
    assertEquals(application.getClientName(), dummyClientName);
    assertEquals(application.getGrantTypes(), dummyGrantTypes);
    assertEquals(application.toString(), toString);
}
Also used : DCRDataHolder(org.wso2.carbon.identity.oauth.dcr.internal.DCRDataHolder) ServiceProvider(org.wso2.carbon.identity.application.common.model.ServiceProvider) OAuthConsumerAppDTO(org.wso2.carbon.identity.oauth.dto.OAuthConsumerAppDTO) OAuthServerConfiguration(org.wso2.carbon.identity.oauth.config.OAuthServerConfiguration) Matchers.anyString(org.mockito.Matchers.anyString) ApplicationManagementService(org.wso2.carbon.identity.application.mgt.ApplicationManagementService) Application(org.wso2.carbon.identity.oauth.dcr.bean.Application) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 13 with ApplicationRegistrationRequest

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

the class DCRMServiceTest method setUp.

@BeforeMethod
public void setUp() throws Exception {
    mockOAuthAdminService = mock(OAuthAdminService.class);
    applicationRegistrationRequest = new ApplicationRegistrationRequest();
    applicationRegistrationRequest.setClientName(dummyClientName);
    dcrmService = new DCRMService();
    mockApplicationManagementService = mock(ApplicationManagementService.class);
    DCRDataHolder dcrDataHolder = DCRDataHolder.getInstance();
    dcrDataHolder.setApplicationManagementService(mockApplicationManagementService);
    when(mockApplicationManagementService.getServiceProviderByClientId(anyString(), anyString(), anyString())).thenReturn(new ServiceProvider());
    oAuthServerConfiguration = mock(OAuthServerConfiguration.class);
    mockStatic(OAuthServerConfiguration.class);
    when(OAuthServerConfiguration.getInstance()).thenReturn(oAuthServerConfiguration);
    mockStatic(OAuth2Util.class);
    mockedUserRealm = mock(UserRealm.class);
    mockedUserStoreManager = mock(AbstractUserStoreManager.class);
}
Also used : ApplicationRegistrationRequest(org.wso2.carbon.identity.oauth.dcr.bean.ApplicationRegistrationRequest) DCRDataHolder(org.wso2.carbon.identity.oauth.dcr.internal.DCRDataHolder) UserRealm(org.wso2.carbon.user.api.UserRealm) OAuthAdminService(org.wso2.carbon.identity.oauth.OAuthAdminService) ServiceProvider(org.wso2.carbon.identity.application.common.model.ServiceProvider) OAuthServerConfiguration(org.wso2.carbon.identity.oauth.config.OAuthServerConfiguration) AbstractUserStoreManager(org.wso2.carbon.user.core.common.AbstractUserStoreManager) ApplicationManagementService(org.wso2.carbon.identity.application.mgt.ApplicationManagementService) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 14 with ApplicationRegistrationRequest

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

the class DCRMServiceTest method registerApplicationWithFailedToRegisterTest.

@Test
public void registerApplicationWithFailedToRegisterTest() throws IdentityApplicationManagementException, IdentityOAuthAdminException {
    mockApplicationManagementService = mock(ApplicationManagementService.class);
    Whitebox.setInternalState(dcrmService, "oAuthAdminService", mockOAuthAdminService);
    startTenantFlow();
    List<String> redirectUri = new ArrayList<>();
    redirectUri.add("redirectUri1");
    applicationRegistrationRequest.setRedirectUris(redirectUri);
    ServiceProvider serviceProvider = new ServiceProvider();
    DCRDataHolder dcrDataHolder = DCRDataHolder.getInstance();
    dcrDataHolder.setApplicationManagementService(mockApplicationManagementService);
    when(mockApplicationManagementService.getServiceProvider(dummyClientName, dummyTenantDomain)).thenReturn(null, serviceProvider);
    when(mockOAuthAdminService.getOAuthApplicationDataByAppName(dummyClientName)).thenReturn(new OAuthConsumerAppDTO());
    when(mockOAuthAdminService.registerAndRetrieveOAuthApplicationData(any(OAuthConsumerAppDTO.class))).thenThrow(IdentityOAuthAdminException.class);
    try {
        dcrmService.registerApplication(applicationRegistrationRequest);
    } catch (IdentityException ex) {
        assertEquals(ex.getErrorCode(), DCRMConstants.ErrorMessages.FAILED_TO_REGISTER_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) ArrayList(java.util.ArrayList) OAuthConsumerAppDTO(org.wso2.carbon.identity.oauth.dto.OAuthConsumerAppDTO) Matchers.anyString(org.mockito.Matchers.anyString) 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 15 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 createOAuthApp.

private OAuthConsumerAppDTO createOAuthApp(ApplicationRegistrationRequest registrationRequest, String applicationOwner, String tenantDomain, String spName) throws DCRMException {
    // Then Create OAuthApp
    OAuthConsumerAppDTO oAuthConsumerApp = new OAuthConsumerAppDTO();
    oAuthConsumerApp.setApplicationName(spName);
    oAuthConsumerApp.setCallbackUrl(validateAndSetCallbackURIs(registrationRequest.getRedirectUris(), registrationRequest.getGrantTypes()));
    String grantType = StringUtils.join(registrationRequest.getGrantTypes(), GRANT_TYPE_SEPARATOR);
    oAuthConsumerApp.setGrantTypes(grantType);
    oAuthConsumerApp.setOAuthVersion(OAUTH_VERSION);
    oAuthConsumerApp.setTokenType(registrationRequest.getTokenType());
    oAuthConsumerApp.setBackChannelLogoutUrl(validateBackchannelLogoutURI(registrationRequest.getBackchannelLogoutUri()));
    if (StringUtils.isNotEmpty(registrationRequest.getConsumerKey())) {
        String clientIdRegex = OAuthServerConfiguration.getInstance().getClientIdValidationRegex();
        if (clientIdMatchesRegex(registrationRequest.getConsumerKey(), clientIdRegex)) {
            oAuthConsumerApp.setOauthConsumerKey(registrationRequest.getConsumerKey());
        } else {
            throw DCRMUtils.generateClientException(DCRMConstants.ErrorMessages.BAD_REQUEST_CLIENT_ID_VIOLATES_PATTERN, clientIdRegex);
        }
    }
    if (StringUtils.isNotEmpty(registrationRequest.getConsumerSecret())) {
        oAuthConsumerApp.setOauthConsumerSecret(registrationRequest.getConsumerSecret());
    }
    if (log.isDebugEnabled()) {
        log.debug("Creating OAuth Application: " + spName + " in tenant: " + tenantDomain);
    }
    OAuthConsumerAppDTO createdApp;
    try {
        createdApp = oAuthAdminService.registerAndRetrieveOAuthApplicationData(oAuthConsumerApp);
    } catch (IdentityOAuthAdminException e) {
        throw DCRMUtils.generateServerException(DCRMConstants.ErrorMessages.FAILED_TO_REGISTER_APPLICATION, spName, e);
    }
    if (log.isDebugEnabled()) {
        log.debug("Created OAuth Application: " + spName + " in tenant: " + tenantDomain);
    }
    if (createdApp == null) {
        throw DCRMUtils.generateServerException(DCRMConstants.ErrorMessages.FAILED_TO_REGISTER_APPLICATION, spName);
    }
    return createdApp;
}
Also used : IdentityOAuthAdminException(org.wso2.carbon.identity.oauth.IdentityOAuthAdminException) 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