Search in sources :

Example 1 with ServiceURLBuilder

use of org.wso2.carbon.identity.core.ServiceURLBuilder in project carbon-identity-framework by wso2.

the class DefaultServiceURLBuilderTest method testGetAbsolutePublicURL.

@Test(dataProvider = "getAbsolutePublicURLData")
public void testGetAbsolutePublicURL(String protocol, String hostName, int port, String proxyContextPath, String tenantNameFromContext, boolean enableTenantURLSupport, Map<String, String> parameters, String fragment, Map<String, String> fragmentParams, String expected, String urlPath) {
    when(CarbonUtils.getManagementTransport()).thenReturn(protocol);
    when(ServerConfiguration.getInstance().getFirstProperty(IdentityCoreConstants.HOST_NAME)).thenReturn(hostName);
    when(CarbonUtils.getTransportProxyPort(mockAxisConfiguration, protocol)).thenReturn(port);
    when(ServerConfiguration.getInstance().getFirstProperty(IdentityCoreConstants.PROXY_CONTEXT_PATH)).thenReturn(proxyContextPath);
    when(IdentityTenantUtil.isTenantQualifiedUrlsEnabled()).thenReturn(enableTenantURLSupport);
    when(IdentityTenantUtil.getTenantDomainFromContext()).thenReturn(tenantNameFromContext);
    when(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain()).thenReturn("carbon.super");
    String absoluteUrl = null;
    try {
        if (MapUtils.isNotEmpty(parameters) && MapUtils.isNotEmpty(fragmentParams)) {
            ServiceURLBuilder serviceURLBuilder = ServiceURLBuilder.create().addPath(urlPath).setFragment(fragment);
            for (String paramKey : parameters.keySet()) {
                serviceURLBuilder.addParameter(paramKey, parameters.get(paramKey));
            }
            for (String fragmentKey : fragmentParams.keySet()) {
                serviceURLBuilder.addFragmentParameter(fragmentKey, fragmentParams.get(fragmentKey));
            }
            absoluteUrl = serviceURLBuilder.build().getAbsolutePublicURL();
        } else if (MapUtils.isNotEmpty(fragmentParams)) {
            absoluteUrl = ServiceURLBuilder.create().addPath(urlPath).setFragment(fragment).addFragmentParameter("key1", "fragment").addFragmentParameter("key2", "fragment").addFragmentParameter("key3", "fragment").addFragmentParameter("key4", "fragment").build().getAbsolutePublicURL();
        } else if (MapUtils.isNotEmpty(parameters)) {
            absoluteUrl = ServiceURLBuilder.create().addPath(urlPath).setFragment(fragment).addParameter("key1", "v").addParameter("key2", "v").addParameter("key3", "v").addParameter("key4", "v").build().getAbsolutePublicURL();
        } else {
            absoluteUrl = ServiceURLBuilder.create().addPath(urlPath).setFragment(fragment).build().getAbsolutePublicURL();
        }
    } catch (URLBuilderException e) {
    // Mock behaviour, hence ignored.
    }
    assertEquals(absoluteUrl, expected);
}
Also used : URLBuilderException(org.wso2.carbon.identity.core.URLBuilderException) ServiceURLBuilder(org.wso2.carbon.identity.core.ServiceURLBuilder) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 2 with ServiceURLBuilder

use of org.wso2.carbon.identity.core.ServiceURLBuilder in project carbon-identity-framework by wso2.

the class JITProvisioningPostAuthenticationHandler method redirectToAccountCreateUI.

/**
 * Call the relevant URL to add the new user.
 *
 * @param externalIdPConfig Relevant external IDP.
 * @param context           Authentication context.
 * @param localClaimValues  Local claim values.
 * @param response          HttpServlet response.
 * @param username          Relevant user name
 * @throws PostAuthenticationFailedException Post Authentication Failed Exception.
 */
private void redirectToAccountCreateUI(ExternalIdPConfig externalIdPConfig, AuthenticationContext context, Map<String, String> localClaimValues, HttpServletResponse response, String username, HttpServletRequest request) throws PostAuthenticationFailedException {
    try {
        ServiceURLBuilder uriBuilder = ServiceURLBuilder.create();
        if (externalIdPConfig.isModifyUserNameAllowed()) {
            context.setProperty(FrameworkConstants.CHANGING_USERNAME_ALLOWED, true);
            uriBuilder = uriBuilder.addPath(FrameworkUtils.getUserNameProvisioningUIUrl());
            uriBuilder.addParameter(FrameworkConstants.ALLOW_CHANGE_USER_NAME, String.valueOf(true));
            if (log.isDebugEnabled()) {
                log.debug(externalIdPConfig.getName() + " allow to change the username, redirecting to " + "registration endpoint to provision the user: " + username);
            }
        } else {
            uriBuilder = uriBuilder.addPath(FrameworkUtils.getPasswordProvisioningUIUrl());
            if (log.isDebugEnabled()) {
                if (externalIdPConfig.isPasswordProvisioningEnabled()) {
                    log.debug(externalIdPConfig.getName() + " supports password provisioning, redirecting to " + "sign up endpoint to provision the user : " + username);
                }
            }
        }
        if (externalIdPConfig.isPasswordProvisioningEnabled()) {
            uriBuilder.addParameter(FrameworkConstants.PASSWORD_PROVISION_ENABLED, String.valueOf(true));
        }
        if (!IdentityTenantUtil.isTenantQualifiedUrlsEnabled()) {
            uriBuilder.addParameter(MultitenantConstants.TENANT_DOMAIN_HEADER_NAME, context.getTenantDomain());
        }
        uriBuilder.addParameter(FrameworkConstants.SERVICE_PROVIDER, context.getSequenceConfig().getApplicationConfig().getApplicationName());
        uriBuilder.addParameter(FrameworkConstants.USERNAME, username);
        uriBuilder.addParameter(FrameworkConstants.SKIP_SIGN_UP_ENABLE_CHECK, String.valueOf(true));
        uriBuilder.addParameter(FrameworkConstants.SESSION_DATA_KEY, context.getContextIdentifier());
        addMissingClaims(uriBuilder, context);
        localClaimValues.forEach(uriBuilder::addParameter);
        response.sendRedirect(uriBuilder.build().getRelativePublicURL());
    } catch (IOException | URLBuilderException e) {
        handleExceptions(String.format(ErrorMessages.ERROR_WHILE_TRYING_CALL_SIGN_UP_ENDPOINT_FOR_PASSWORD_PROVISIONING.getMessage(), username, externalIdPConfig.getName()), ErrorMessages.ERROR_WHILE_TRYING_CALL_SIGN_UP_ENDPOINT_FOR_PASSWORD_PROVISIONING.getCode(), e);
    }
}
Also used : URLBuilderException(org.wso2.carbon.identity.core.URLBuilderException) IOException(java.io.IOException) ServiceURLBuilder(org.wso2.carbon.identity.core.ServiceURLBuilder)

Example 3 with ServiceURLBuilder

use of org.wso2.carbon.identity.core.ServiceURLBuilder in project identity-inbound-auth-oauth by wso2-extensions.

the class DeviceEndpointTest method testDevice.

/**
 * Test the device_authorize endpoint.
 *
 * @param clientId       Consumer key of the application.
 * @param expectedStatus Expected status for response.
 * @param status         Status of user code.
 * @throws IdentityOAuth2Exception If failed at device endpoint
 * @throws OAuthSystemException If failed at device endpoint.
 */
@Test(dataProvider = "dataValues")
public void testDevice(String clientId, int expectedStatus, boolean status) throws Exception {
    DeviceEndpoint deviceEndpoint = PowerMockito.spy(new DeviceEndpoint());
    mockOAuthServerConfiguration();
    mockStatic(ServiceURLBuilder.class);
    mockStatic(ServiceURL.class);
    ServiceURLBuilder mockServiceURLBuilder = Mockito.mock(ServiceURLBuilder.class);
    ServiceURL mockServiceURL = Mockito.mock(ServiceURL.class);
    when(ServiceURLBuilder.create()).thenReturn(mockServiceURLBuilder);
    when(mockServiceURLBuilder.addPath(anyString())).thenReturn(mockServiceURLBuilder);
    when(mockServiceURLBuilder.addParameter(anyString(), anyString())).thenReturn(mockServiceURLBuilder);
    when(mockServiceURLBuilder.build()).thenReturn(mockServiceURL);
    when(mockServiceURL.getAbsolutePublicURL()).thenReturn("http://localhost:9443/authenticationendpoint/device.do");
    mockStatic(HttpServletRequest.class);
    OAuthClientAuthnContext oAuthClientAuthnContext = new OAuthClientAuthnContext();
    oAuthClientAuthnContext.setClientId(clientId);
    oAuthClientAuthnContext.setAuthenticated(status);
    when(request.getAttribute(anyString())).thenReturn(oAuthClientAuthnContext);
    DeviceAuthServiceImpl deviceAuthService = new DeviceAuthServiceImpl();
    deviceEndpoint.setDeviceAuthService(deviceAuthService);
    mockStatic(IdentityDatabaseUtil.class);
    when(IdentityDatabaseUtil.getDBConnection(true)).thenReturn(connection);
    when(IdentityDatabaseUtil.getDBConnection(false)).thenReturn(connection);
    when(httpServletRequest.getParameter(anyString())).thenReturn(clientId);
    Response response;
    mockStatic(IdentityUtil.class);
    when(IdentityUtil.getServerURL(anyString(), anyBoolean(), anyBoolean())).thenReturn(TEST_URL);
    mockStatic(DeviceFlowPersistenceFactory.class);
    when(DeviceFlowPersistenceFactory.getInstance()).thenReturn(deviceFlowPersistenceFactory);
    when(deviceFlowPersistenceFactory.getDeviceFlowDAO()).thenReturn(deviceFlowDAO);
    when(deviceFlowDAO.checkClientIdExist(anyString())).thenReturn(status);
    PowerMockito.when(deviceEndpoint, "getValidationObject", httpServletRequest).thenReturn(oAuthClientAuthnContext);
    response = deviceEndpoint.authorize(httpServletRequest, new MultivaluedHashMap<String, String>(), httpServletResponse);
    Assert.assertEquals(expectedStatus, response.getStatus());
}
Also used : Response(javax.ws.rs.core.Response) OAuthResponse(org.apache.oltu.oauth2.common.message.OAuthResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) ServiceURL(org.wso2.carbon.identity.core.ServiceURL) OAuthClientAuthnContext(org.wso2.carbon.identity.oauth2.bean.OAuthClientAuthnContext) ServiceURLBuilder(org.wso2.carbon.identity.core.ServiceURLBuilder) DeviceAuthServiceImpl(org.wso2.carbon.identity.oauth2.device.api.DeviceAuthServiceImpl) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 4 with ServiceURLBuilder

use of org.wso2.carbon.identity.core.ServiceURLBuilder in project identity-inbound-auth-oauth by wso2-extensions.

the class UserAuthenticationEndpointTest method testDeviceAuthorizeForURLBuilderExceptionPath.

/**
 * Test device endpoint throwing URLBuilderException.
 *
 * @param userCode      User code of the user.
 * @param clientId      Consumer key of the application.
 * @param expectedValue Expected http status.
 * @param status        Status of user code.
 * @param uri           Redirection uri.
 * @throws Exception Error while testing device endpoint throwing URLBuilderException.
 */
@Test(dataProvider = "providePostParamsForURLBuilderExceptionPath")
public void testDeviceAuthorizeForURLBuilderExceptionPath(String userCode, String clientId, int expectedValue, String status, String uri) throws Exception {
    mockOAuthServerConfiguration();
    WhiteboxImpl.setInternalState(userAuthenticationEndpoint, "oAuth2AuthzEndpoint", oAuth2AuthzEndpoint);
    mockStatic(IdentityDatabaseUtil.class);
    when(IdentityDatabaseUtil.getDBConnection()).thenReturn(connection);
    mockStatic(DeviceFlowPersistenceFactory.class);
    when(DeviceFlowPersistenceFactory.getInstance()).thenReturn(deviceFlowPersistenceFactory);
    when(deviceFlowPersistenceFactory.getDeviceFlowDAO()).thenReturn(deviceFlowDAO);
    when(deviceFlowDAO.getClientIdByUserCode(anyString())).thenReturn(clientId);
    when(deviceFlowDAO.getDetailsForUserCode(anyString())).thenReturn(deviceFlowDOAsNotExpired);
    when(deviceFlowDAO.getScopesForUserCode(anyString())).thenReturn(scopes);
    when(httpServletRequest.getParameter(anyString())).thenReturn(userCode);
    mockStatic(OAuth2Util.class);
    when(OAuth2Util.getAppInformationByClientId(anyString())).thenReturn(oAuthAppDO);
    when(oAuthAppDO.getCallbackUrl()).thenReturn(uri);
    Response response1;
    mockStatic(IdentityTenantUtil.class);
    when(IdentityTenantUtil.getTenantDomain(anyInt())).thenReturn(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
    when(IdentityTenantUtil.getTenantId(anyString())).thenReturn(MultitenantConstants.SUPER_TENANT_ID);
    mockStatic(ServiceURLBuilder.class);
    when(ServiceURLBuilder.create()).thenReturn(serviceURLBuilder);
    when(serviceURLBuilder.addPath(any())).thenReturn(serviceURLBuilder);
    when(serviceURLBuilder.addParameter(any(), any())).thenReturn(serviceURLBuilder);
    when(serviceURLBuilder.build()).thenThrow(new URLBuilderException("Throwing URLBuilderException."));
    when(serviceURL.getAbsolutePublicURL()).thenReturn(TEST_URL);
    when(oAuth2AuthzEndpoint.authorize(any(CommonAuthRequestWrapper.class), any(HttpServletResponse.class))).thenReturn(response);
    DeviceAuthServiceImpl deviceAuthService = new DeviceAuthServiceImpl();
    userAuthenticationEndpoint = new UserAuthenticationEndpoint();
    userAuthenticationEndpoint.setDeviceAuthService(deviceAuthService);
    WhiteboxImpl.setInternalState(userAuthenticationEndpoint, OAuth2AuthzEndpoint.class, oAuth2AuthzEndpoint);
    response1 = userAuthenticationEndpoint.deviceAuthorize(httpServletRequest, httpServletResponse);
    if (expectedValue == HttpServletResponse.SC_ACCEPTED) {
        Assert.assertNotNull(response1);
    } else {
        Assert.assertNull(response1);
    }
}
Also used : Response(javax.ws.rs.core.Response) OAuthResponse(org.apache.oltu.oauth2.common.message.OAuthResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) URLBuilderException(org.wso2.carbon.identity.core.URLBuilderException) CommonAuthRequestWrapper(org.wso2.carbon.identity.application.authentication.framework.model.CommonAuthRequestWrapper) HttpServletResponse(javax.servlet.http.HttpServletResponse) DeviceAuthServiceImpl(org.wso2.carbon.identity.oauth2.device.api.DeviceAuthServiceImpl) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 5 with ServiceURLBuilder

use of org.wso2.carbon.identity.core.ServiceURLBuilder in project identity-inbound-auth-oauth by wso2-extensions.

the class OIDCLogoutServletTest method mockServiceURLBuilder.

private void mockServiceURLBuilder(String context) throws URLBuilderException {
    mockStatic(ServiceURLBuilder.class);
    ServiceURLBuilder serviceURLBuilder = mock(ServiceURLBuilder.class);
    when(ServiceURLBuilder.create()).thenReturn(serviceURLBuilder);
    when(serviceURLBuilder.addPath(any())).thenReturn(serviceURLBuilder);
    ServiceURL serviceURL = mock(ServiceURL.class);
    when(serviceURL.getRelativeInternalURL()).thenReturn(context);
    when(serviceURLBuilder.build()).thenReturn(serviceURL);
}
Also used : ServiceURL(org.wso2.carbon.identity.core.ServiceURL) ServiceURLBuilder(org.wso2.carbon.identity.core.ServiceURLBuilder)

Aggregations

ServiceURLBuilder (org.wso2.carbon.identity.core.ServiceURLBuilder)6 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 Test (org.testng.annotations.Test)5 HttpServletResponse (javax.servlet.http.HttpServletResponse)4 Response (javax.ws.rs.core.Response)4 OAuthResponse (org.apache.oltu.oauth2.common.message.OAuthResponse)4 BeforeTest (org.testng.annotations.BeforeTest)4 ServiceURL (org.wso2.carbon.identity.core.ServiceURL)4 DeviceAuthServiceImpl (org.wso2.carbon.identity.oauth2.device.api.DeviceAuthServiceImpl)4 CommonAuthRequestWrapper (org.wso2.carbon.identity.application.authentication.framework.model.CommonAuthRequestWrapper)3 URLBuilderException (org.wso2.carbon.identity.core.URLBuilderException)3 IOException (java.io.IOException)2 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)1 Matchers.anyString (org.mockito.Matchers.anyString)1 OAuthClientAuthnContext (org.wso2.carbon.identity.oauth2.bean.OAuthClientAuthnContext)1