Search in sources :

Example 1 with WebFingerEndpointException

use of org.wso2.carbon.identity.webfinger.WebFingerEndpointException in project identity-inbound-auth-oauth by wso2-extensions.

the class DefaultWebFingerRequestBuilderTest method testBuildRequest.

@Test
public void testBuildRequest() throws WebFingerEndpointException, UserStoreException {
    returnParams();
    returnRelAndResource(resource, rel);
    when(tenantManager.getTenantId(any(String.class))).thenReturn(MultitenantConstants.SUPER_TENANT_ID);
    WebFingerRequest webFingerRequest = defaultWebFingerRequestBuilder.buildRequest(request);
    assertNotNull(webFingerRequest, "WebFinger request is not null");
    assertEquals(webFingerRequest.getRel(), rel, "Rel is assigned properly");
    assertEquals(webFingerRequest.getResource(), resource, "Resource is assigned properly");
    assertEquals(webFingerRequest.getHost(), host, "Host is assigned properly");
    assertEquals(webFingerRequest.getTenant(), tenant, "Tenant is assigned properly");
    assertEquals(webFingerRequest.getPath(), path, "Path is assigned properly");
    assertEquals(webFingerRequest.getScheme(), scheme, "Scheme is assigned properly");
    assertEquals(webFingerRequest.getPort(), port, "Port is assigned properly");
}
Also used : Matchers.anyString(org.mockito.Matchers.anyString) WebFingerRequest(org.wso2.carbon.identity.webfinger.WebFingerRequest) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 2 with WebFingerEndpointException

use of org.wso2.carbon.identity.webfinger.WebFingerEndpointException in project identity-inbound-auth-oauth by wso2-extensions.

the class WebFingerOIDCResponseBuilderTest method testBuildWebFingerException.

@Test(expectedExceptions = ServerConfigurationException.class)
public void testBuildWebFingerException() throws WebFingerEndpointException, ServerConfigurationException, IdentityException {
    when(OAuth2Util.getIssuerLocation(any(String.class))).thenThrow(new IdentityOAuth2Exception("Error"));
    webFingerOIDCResponseBuilder.buildWebFingerResponse(webFingerRequest);
}
Also used : IdentityOAuth2Exception(org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 3 with WebFingerEndpointException

use of org.wso2.carbon.identity.webfinger.WebFingerEndpointException in project identity-inbound-auth-oauth by wso2-extensions.

the class WebFingerOIDCResponseBuilder method buildWebFingerResponse.

public WebFingerResponse buildWebFingerResponse(WebFingerRequest request) throws WebFingerEndpointException, ServerConfigurationException {
    WebFingerResponse response;
    String oidcIssuerLocation;
    try {
        oidcIssuerLocation = getOidcIssuerLocation(request.getTenant());
    } catch (URISyntaxException | IdentityOAuth2Exception e) {
        throw new ServerConfigurationException("Error while building discovery endpoint", e);
    }
    response = new WebFingerResponse();
    response.setSubject(request.getResource());
    response.addLink(WebFingerConstants.OPENID_CONNETCT_ISSUER_REL, oidcIssuerLocation);
    return response;
}
Also used : IdentityOAuth2Exception(org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception) ServerConfigurationException(org.wso2.carbon.base.ServerConfigurationException) WebFingerResponse(org.wso2.carbon.identity.webfinger.WebFingerResponse) URISyntaxException(java.net.URISyntaxException)

Example 4 with WebFingerEndpointException

use of org.wso2.carbon.identity.webfinger.WebFingerEndpointException in project identity-inbound-auth-oauth by wso2-extensions.

the class DefaultWebFingerRequestBuilder method buildRequest.

@Override
public WebFingerRequest buildRequest(HttpServletRequest request) throws WebFingerEndpointException {
    WebFingerRequest webFingerRequest = new WebFingerRequest();
    List<String> parameters = Collections.list(request.getParameterNames());
    if (parameters.size() != 2 || !parameters.contains(WebFingerConstants.REL) || !parameters.contains(WebFingerConstants.RESOURCE)) {
        throw new WebFingerEndpointException(WebFingerConstants.ERROR_CODE_INVALID_REQUEST, "Bad Web " + "Finger request.");
    }
    webFingerRequest.setServletRequest(request);
    String resource = request.getParameter(WebFingerConstants.RESOURCE);
    webFingerRequest.setRel(request.getParameter(WebFingerConstants.REL));
    webFingerRequest.setResource(resource);
    if (StringUtils.isBlank(resource)) {
        log.warn("Can't normalize null or empty URI: " + resource);
        throw new WebFingerEndpointException(WebFingerConstants.ERROR_CODE_INVALID_RESOURCE, "Null or empty URI.");
    } else {
        URI resourceURI = URI.create(resource);
        if (StringUtils.isBlank(resourceURI.getScheme())) {
            throw new WebFingerEndpointException("Scheme of the resource cannot be empty");
        }
        String userInfo;
        if (WebFingerConstants.ACCT_SCHEME.equals(resourceURI.getScheme())) {
            // acct scheme
            userInfo = resourceURI.getSchemeSpecificPart();
            if (!userInfo.contains("@")) {
                throw new WebFingerEndpointException(WebFingerConstants.ERROR_CODE_INVALID_REQUEST, "Invalid host value.");
            }
            userInfo = userInfo.substring(0, userInfo.lastIndexOf('@'));
        } else {
            // https scheme
            userInfo = resourceURI.getUserInfo();
            webFingerRequest.setScheme(resourceURI.getScheme());
            webFingerRequest.setHost(resourceURI.getHost());
            webFingerRequest.setPort(resourceURI.getPort());
            webFingerRequest.setPath(resourceURI.getPath());
            webFingerRequest.setQuery(resourceURI.getQuery());
        }
        String tenant;
        if (StringUtils.isNotBlank(userInfo)) {
            try {
                userInfo = URLDecoder.decode(userInfo, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                throw new WebFingerEndpointException("Cannot decode the userinfo");
            }
            tenant = MultitenantUtils.getTenantDomain(userInfo);
            webFingerRequest.setUserInfo(resourceURI.getUserInfo());
        } else {
            tenant = MultitenantConstants.SUPER_TENANT_DOMAIN_NAME;
        }
        validateTenant(tenant);
        webFingerRequest.setTenant(tenant);
    }
    return webFingerRequest;
}
Also used : UnsupportedEncodingException(java.io.UnsupportedEncodingException) WebFingerRequest(org.wso2.carbon.identity.webfinger.WebFingerRequest) WebFingerEndpointException(org.wso2.carbon.identity.webfinger.WebFingerEndpointException) URI(java.net.URI)

Example 5 with WebFingerEndpointException

use of org.wso2.carbon.identity.webfinger.WebFingerEndpointException in project identity-inbound-auth-oauth by wso2-extensions.

the class WebFingerServlet method getOIDProviderIssuer.

public void getOIDProviderIssuer(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException {
    WebFingerProcessor processor = WebFingerServiceComponentHolder.getWebFingerProcessor();
    String response = "";
    try {
        WebFingerResponseBuilder webFingerResponseBuilder = new JSONResponseBuilder();
        response = webFingerResponseBuilder.getOIDProviderIssuerString(processor.getResponse(httpServletRequest));
    } catch (WebFingerEndpointException e) {
        httpServletResponse.setStatus(processor.handleError(e));
        return;
    } catch (ServerConfigurationException e) {
        log.error("Server Configuration error occurred.", e);
        httpServletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        return;
    }
    httpServletResponse.setContentType(WebFingerConstants.RESPONSE_CONTENT_TYPE);
    PrintWriter out = httpServletResponse.getWriter();
    out.print(response);
}
Also used : ServerConfigurationException(org.wso2.carbon.base.ServerConfigurationException) WebFingerResponseBuilder(org.wso2.carbon.identity.webfinger.builders.WebFingerResponseBuilder) WebFingerProcessor(org.wso2.carbon.identity.webfinger.WebFingerProcessor) WebFingerEndpointException(org.wso2.carbon.identity.webfinger.WebFingerEndpointException) PrintWriter(java.io.PrintWriter)

Aggregations

PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 Test (org.testng.annotations.Test)3 WebFingerRequest (org.wso2.carbon.identity.webfinger.WebFingerRequest)3 ServerConfigurationException (org.wso2.carbon.base.ServerConfigurationException)2 IdentityOAuth2Exception (org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception)2 WebFingerEndpointException (org.wso2.carbon.identity.webfinger.WebFingerEndpointException)2 PrintWriter (java.io.PrintWriter)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 Matchers.anyString (org.mockito.Matchers.anyString)1 WebFingerProcessor (org.wso2.carbon.identity.webfinger.WebFingerProcessor)1 WebFingerResponse (org.wso2.carbon.identity.webfinger.WebFingerResponse)1 DefaultWebFingerRequestBuilder (org.wso2.carbon.identity.webfinger.builders.DefaultWebFingerRequestBuilder)1 WebFingerOIDCResponseBuilder (org.wso2.carbon.identity.webfinger.builders.WebFingerOIDCResponseBuilder)1 WebFingerRequestBuilder (org.wso2.carbon.identity.webfinger.builders.WebFingerRequestBuilder)1 WebFingerResponseBuilder (org.wso2.carbon.identity.webfinger.builders.WebFingerResponseBuilder)1