Search in sources :

Example 1 with RegistrationRequestProfile

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

the class RegistrationRequestFactoryTest method getTestCreateData.

private JSONObject getTestCreateData() throws Exception {
    String grantType = "implicit";
    JSONArray redirectUrls = new JSONArray();
    redirectUrls.add("redirectUrl");
    JSONObject jsonObject = new JSONObject();
    jsonObject.put(RegistrationRequest.RegisterRequestConstant.GRANT_TYPES, grantType);
    jsonObject.put(RegistrationRequest.RegisterRequestConstant.REDIRECT_URIS, redirectUrls);
    RegistrationRequestProfile registrationRequestProfile = new RegistrationRequestProfile();
    whenNew(RegistrationRequestProfile.class).withNoArguments().thenReturn(registrationRequestProfile);
    suppress(methodsDeclaredIn(HttpIdentityRequestFactory.class));
    return jsonObject;
}
Also used : JSONObject(org.json.simple.JSONObject) RegistrationRequestProfile(org.wso2.carbon.identity.oauth.dcr.model.RegistrationRequestProfile) JSONArray(org.json.simple.JSONArray) Matchers.anyString(org.mockito.Matchers.anyString) HttpIdentityRequestFactory(org.wso2.carbon.identity.application.authentication.framework.inbound.HttpIdentityRequestFactory)

Example 2 with RegistrationRequestProfile

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

the class RegistrationHandlerTest method testHandle.

@Test
public void testHandle() throws Exception {
    RegistrationRequestProfile mockRegistrationRequestProfile = new RegistrationRequestProfile();
    when(mockDcrMessageContext.getIdentityRequest()).thenReturn(mockRegisterRequest);
    when(mockRegisterRequest.getRegistrationRequestProfile()).thenReturn(mockRegistrationRequestProfile);
    String testTenantDomain = "testTenantDomain";
    when(mockRegisterRequest.getTenantDomain()).thenReturn(testTenantDomain);
    mockStatic(DCRManagementService.class);
    when(DCRManagementService.getInstance()).thenReturn(mockDCRManagementService);
    when(mockDCRManagementService.registerOAuthApplication(mockRegistrationRequestProfile)).thenReturn(mockRegistrationResponseProfile);
    RegistrationResponse.DCRRegisterResponseBuilder registerResponseBuilder = new RegistrationResponse.DCRRegisterResponseBuilder();
    whenNew(RegistrationResponse.DCRRegisterResponseBuilder.class).withNoArguments().thenReturn(registerResponseBuilder);
    assertEquals(registrationHandler.handle(mockDcrMessageContext), registerResponseBuilder, "Expected response builder is different from the actual");
    assertEquals(mockRegistrationRequestProfile.getTenantDomain(), testTenantDomain, "Expected tenant domain is not equal to the actual tenant domain");
}
Also used : RegistrationRequestProfile(org.wso2.carbon.identity.oauth.dcr.model.RegistrationRequestProfile) RegistrationResponse(org.wso2.carbon.identity.oauth.dcr.model.RegistrationResponse) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 3 with RegistrationRequestProfile

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

the class RegistrationRequestFactory method parseJson.

protected void parseJson(JSONObject jsonData, RegistrationRequest.RegistrationRequestBuilder registerRequestBuilder) throws FrameworkClientException {
    RegistrationRequestProfile registrationRequestProfile = registerRequestBuilder.getRegistrationRequestProfile();
    if (registrationRequestProfile == null) {
        registrationRequestProfile = new RegistrationRequestProfile();
    }
    Object obj = jsonData.get(RegistrationRequest.RegisterRequestConstant.GRANT_TYPES);
    if (obj instanceof JSONArray) {
        JSONArray grantTypes = (JSONArray) obj;
        for (Object grantType : grantTypes) {
            if (grantType instanceof String && IdentityUtil.isNotBlank((String) grantType)) {
                registrationRequestProfile.getGrantTypes().add((String) grantType);
            }
        }
    } else if (obj instanceof String) {
        String grantType = (String) obj;
        if (IdentityUtil.isNotBlank(grantType)) {
            registrationRequestProfile.getGrantTypes().add(grantType);
        }
    }
    obj = jsonData.get(RegistrationRequest.RegisterRequestConstant.REDIRECT_URIS);
    if (obj instanceof JSONArray) {
        JSONArray redirectUris = (JSONArray) obj;
        for (Object redirectUri : redirectUris) {
            if (redirectUri instanceof String) {
                registrationRequestProfile.getRedirectUris().add((String) redirectUri);
            }
        }
    } else if (obj instanceof String) {
        registrationRequestProfile.getRedirectUris().add((String) obj);
    } else if (registrationRequestProfile.getGrantTypes().contains(DCRConstants.GrantTypes.AUTHORIZATION_CODE) || registrationRequestProfile.getGrantTypes().contains(DCRConstants.GrantTypes.IMPLICIT)) {
        throw IdentityException.error(FrameworkClientException.class, "RedirectUris property must have at least one URI value.");
    }
    registrationRequestProfile.setTokenEndpointAuthMethod((String) jsonData.get(RegistrationRequest.RegisterRequestConstant.TOKEN_ENDPOINT_AUTH_METHOD));
    obj = jsonData.get(RegistrationRequest.RegisterRequestConstant.RESPONSE_TYPES);
    if (obj instanceof JSONArray) {
        JSONArray responseTypes = (JSONArray) obj;
        for (int i = 0; i < responseTypes.size(); i++) {
            registrationRequestProfile.getResponseTypes().add(responseTypes.get(i).toString());
        }
    } else if (obj instanceof String) {
        registrationRequestProfile.getResponseTypes().add((String) obj);
    }
    // Get client Name if not available generate a uuid
    Object objClient = jsonData.get(RegistrationRequest.RegisterRequestConstant.CLIENT_NAME);
    if (objClient != null) {
        registrationRequestProfile.setClientName((String) objClient);
    } else {
        registrationRequestProfile.setClientName(UUIDGenerator.generateUUID());
    }
    registrationRequestProfile.setClientUri((String) jsonData.get(RegistrationRequest.RegisterRequestConstant.CLIENT_URI));
    registrationRequestProfile.setLogoUri((String) jsonData.get(RegistrationRequest.RegisterRequestConstant.LOGO_URI));
    obj = jsonData.get(RegistrationRequest.RegisterRequestConstant.SCOPE);
    if (obj instanceof JSONArray) {
        JSONArray scopes = (JSONArray) obj;
        for (int i = 0; i < scopes.size(); i++) {
            registrationRequestProfile.getScopes().add(scopes.get(i).toString());
        }
    } else if (obj instanceof String) {
        registrationRequestProfile.getScopes().add((String) obj);
    }
    obj = jsonData.get(RegistrationRequest.RegisterRequestConstant.CONTACTS);
    if (obj instanceof JSONArray) {
        JSONArray redirectUris = (JSONArray) obj;
        for (int i = 0; i < redirectUris.size(); i++) {
            registrationRequestProfile.getContacts().add(redirectUris.get(i).toString());
        }
    } else if (obj instanceof String) {
        registrationRequestProfile.getContacts().add((String) obj);
    }
    registrationRequestProfile.setTosUri((String) jsonData.get(RegistrationRequest.RegisterRequestConstant.TOS_URI));
    registrationRequestProfile.setPolicyUri((String) jsonData.get(RegistrationRequest.RegisterRequestConstant.POLICY_URI));
    registrationRequestProfile.setJwksUri((String) jsonData.get(RegistrationRequest.RegisterRequestConstant.JWKS_URI));
    registrationRequestProfile.setJkws((String) jsonData.get(RegistrationRequest.RegisterRequestConstant.JWKS));
    registrationRequestProfile.setSoftwareId((String) jsonData.get(RegistrationRequest.RegisterRequestConstant.SOFTWARE_ID));
    registrationRequestProfile.setSoftwareVersion((String) jsonData.get(RegistrationRequest.RegisterRequestConstant.SOFTWARE_VERSION));
    // TODO:This parameter is a custom one and we have to remove if we can collect the user name by having
    // some authentication mechanism.
    String username = CarbonContext.getThreadLocalCarbonContext().getUsername();
    if (StringUtils.isBlank(username)) {
        Object objOwner = jsonData.get(RegistrationRequest.RegisterRequestConstant.EXT_PARAM_OWNER);
        if (objOwner != null) {
            username = (String) objOwner;
            try {
                UserRealm userRealm = CarbonContext.getThreadLocalCarbonContext().getUserRealm();
                if (!userRealm.getUserStoreManager().isExistingUser(username)) {
                    throw IdentityException.error(FrameworkClientException.class, "Invalid application " + "owner.");
                }
            } catch (UserStoreException e) {
                String errorMessage = "Invalid application owner, " + e.getMessage();
                throw IdentityException.error(FrameworkClientException.class, errorMessage, e);
            }
        } else {
            throw IdentityException.error(FrameworkClientException.class, "Invalid application owner.");
        }
    }
    registrationRequestProfile.setOwner(username);
    registerRequestBuilder.setRegistrationRequestProfile(registrationRequestProfile);
}
Also used : UserRealm(org.wso2.carbon.user.api.UserRealm) RegistrationRequestProfile(org.wso2.carbon.identity.oauth.dcr.model.RegistrationRequestProfile) JSONArray(org.json.simple.JSONArray) UserStoreException(org.wso2.carbon.user.api.UserStoreException) JSONObject(org.json.simple.JSONObject) FrameworkClientException(org.wso2.carbon.identity.application.authentication.framework.inbound.FrameworkClientException)

Example 4 with RegistrationRequestProfile

use of org.wso2.carbon.identity.oauth.dcr.model.RegistrationRequestProfile 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;
}
Also used : RegistrationRequestProfile(org.wso2.carbon.identity.oauth.dcr.model.RegistrationRequestProfile) RegistrationResponseProfile(org.wso2.carbon.identity.oauth.dcr.model.RegistrationResponseProfile) RegistrationResponse(org.wso2.carbon.identity.oauth.dcr.model.RegistrationResponse) RegistrationRequest(org.wso2.carbon.identity.oauth.dcr.model.RegistrationRequest)

Example 5 with RegistrationRequestProfile

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

the class DCRManagementServiceTest method registerOAuthApplicationWithNewSPNoRedirectUri.

@Test
public void registerOAuthApplicationWithNewSPNoRedirectUri() throws Exception {
    registerOAuthApplication();
    registrationRequestProfile.setRedirectUris(new ArrayList<>());
    mockApplicationManagementService = mock(ApplicationManagementService.class);
    DCRDataHolder dcrDataHolder = DCRDataHolder.getInstance();
    dcrDataHolder.setApplicationManagementService(mockApplicationManagementService);
    when(mockApplicationManagementService.getServiceProvider(applicationName, tenantDomain)).thenReturn(null, new ServiceProvider());
    try {
        dcrManagementService.registerOAuthApplication(registrationRequestProfile);
    } catch (IdentityException ex) {
        assertEquals(ex.getMessage(), "RedirectUris property must have at least one URI value.");
        return;
    }
    fail("Expected IdentityException was not thrown by registerOAuthApplication 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) BeforeTest(org.testng.annotations.BeforeTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

Test (org.testng.annotations.Test)14 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)13 RegistrationRequestProfile (org.wso2.carbon.identity.oauth.dcr.model.RegistrationRequestProfile)10 BeforeTest (org.testng.annotations.BeforeTest)9 ApplicationManagementService (org.wso2.carbon.identity.application.mgt.ApplicationManagementService)9 DCRDataHolder (org.wso2.carbon.identity.oauth.dcr.internal.DCRDataHolder)8 JSONObject (org.json.simple.JSONObject)7 ServiceProvider (org.wso2.carbon.identity.application.common.model.ServiceProvider)7 IdentityException (org.wso2.carbon.identity.base.IdentityException)7 RegistrationResponseProfile (org.wso2.carbon.identity.oauth.dcr.model.RegistrationResponseProfile)7 Matchers.anyString (org.mockito.Matchers.anyString)5 RegistrationRequest (org.wso2.carbon.identity.oauth.dcr.model.RegistrationRequest)4 JSONArray (org.json.simple.JSONArray)3 HttpIdentityRequestFactory (org.wso2.carbon.identity.application.authentication.framework.inbound.HttpIdentityRequestFactory)3 OAuthAdminService (org.wso2.carbon.identity.oauth.OAuthAdminService)3 DCRException (org.wso2.carbon.identity.oauth.dcr.DCRException)3 RegistrationResponse (org.wso2.carbon.identity.oauth.dcr.model.RegistrationResponse)3 OAuthConsumerAppDTO (org.wso2.carbon.identity.oauth.dto.OAuthConsumerAppDTO)3 ArrayList (java.util.ArrayList)2 IdentityApplicationManagementException (org.wso2.carbon.identity.application.common.IdentityApplicationManagementException)2