Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using WAFFLE from a servlet and subsequently passing the authorization to another resource using HTTPClient from a servlet #846

Open
MMirabito opened this issue Dec 25, 2019 · 8 comments

Comments

@MMirabito
Copy link

Dear all,

I have the following use case but I am not sure if WAFFLE can help in resolving it. I am using Waffle 2.1.0 and immediately below s my high-level scenario:

Browser -> Web App with WAFFLE Filter -> Servlet uses HTTPClient -> Additional resource (authentication required)

I have a simple web app running on a windows sever which is running under Tomcat 9.x on JDK 1.8- 231. In the web app I use WAFFLE so that the user can be authenticated.

  1. Once the authentication process is complete the servlet needs to execute a call using the Apache HttpClient request to another server within our domain but as of a few days ago the URL resource is now requiring the request to be authenticated. For context this was recently implemented as a requirement by my organization. Therefore, while in the past anonymous access would have worked with HTTPClient just fine, now it is failing with unauthorized access.

  2. I was able to find a workaround by using a domain service account and create an NTCredential object which I pass to the HTTPClient (I found a few examples on the net that guided me through it java-httpclient-doesnt-authenticate-with-ntlm ) see code at end of post. And While this option works it is somewhat clumsy as it requires that I manage a domain account and its credentials, which means when the credentials expire I need to handle the change.

  3. Ideally I would like to tell HTTPClient to use or piggy back on the authentication and authorization that just occurred between the user browser and the servlet since the user that is logged into the domain will also have access to that resource that HTTPClinet will need to access. However, I am not sure if this is even possible or how I would to do it with WAFFLE , HTTPClient and a severlet - I think it might be referred to as pass-thru authentication, or preemptive authentication or trusted for delegation - but I am not sure - Ii am new to this so I am confused about my options.

Does anyone have any suggestions? Thank in advance for any guidance.

max

`

public static Map<String, Object> getResourceByUrl(String url) {
    
    Properties props = Utils.getAppProperties();
    String userId = props.getProperty("domain.id");
    String password = props.getProperty("domain.pwd");
    String domain = props.getProperty("domain.name");
    
    Map<String, Object> map = new HashMap<String, Object>();
    
    NTCredentials credentials = new NTCredentials(userId, password, null, domain);
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(AuthScope.ANY, credentials);

    RequestConfig requestConfig = RequestConfig.custom()
            .setSocketTimeout(30000)
            .setConnectTimeout(30000)
            .setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.NTLM))
            .setProxyPreferredAuthSchemes(Arrays.asList(AuthSchemes.BASIC))
            .build();
    
    HttpClient client = HttpClientBuilder.create()
            .setDefaultCredentialsProvider(credsProvider)
            .setDefaultRequestConfig(requestConfig)
            .build();
    
    try {
        HttpResponse response =  client.execute(new HttpGet(url));
        
        int statusCode = response.getStatusLine().getStatusCode();
        String reasonPhrase = response.getStatusLine().getReasonPhrase();
        String content = EntityUtils.toString(response.getEntity());
        
        if (statusCode != 200) {
            log.fatal("HTTP Status Code: {} Message: {}", statusCode, reasonPhrase);
            log.fatal("Unable to fetech: {}", url);
        }
        
        map.put("statusCode", statusCode);
        map.put("content", content);
        
    }
    catch (ClientProtocolException e) {
        log.error(e.getMessage(), e);
    }
    catch (IOException e) {
        log.error(e.getMessage(), e);
    }

    return map;
}

`

@MMirabito
Copy link
Author

Dear all, sorry to continue to pester you all but I am still curios if anyone using WAFFLE has had to deal with the use case I describe above.

In summary once a user using a browser has negotiated the authentication via Tomcat using the WAFFLE filter I would like to pass the valid authentication/token/information that just occurred to another call within the same servlet using an HTTP Client request.

Any guidance, suggestion or comments would be greatly appreciated.

Thanks
max

@hazendaz
Copy link
Member

hazendaz commented Jan 2, 2020 via email

@pedroneil
Copy link
Contributor

pedroneil commented Jan 2, 2020 via email

@MMirabito
Copy link
Author

MMirabito commented Jan 3, 2020

Please also post this on the waffle google group. You will likely get responses over there. Off hand I dont know if this is possible. Your write up is excellent though and worth getting the larger user group to weigh in.

@hazendaz thanks for the suggestion I will do that as well

max

@MMirabito
Copy link
Author

MMirabito commented Jan 3, 2020

@pedroneil thanks for your reply and no worries for the length. You confirmed my suspicion that it's not as simple as I would like it - I was hoping for a quick - ready made solution and declare victory.

I will read through your post and see were it takes me.

Thanks again,

max

@hazendaz
Copy link
Member

hazendaz commented Jan 5, 2020

@MMirabito I noticed our main github here did not show the google group directly as many other projects do. I've added the email there. For reference, it is [email protected]. I think that is listed either on the readme in within the various docs folder.

I would like to hear final thoughts back on what you accomplish. This entire issue seems like a great candidate to be converted to documentation regardless of success or not. At least that will assist others on this same journey.

@pedroneil
Copy link
Contributor

pedroneil commented Jan 6, 2020 via email

@MMirabito
Copy link
Author

@hazendaz thanks for the google group link. I just posted the same question her is the link for reference https://groups.google.com/forum/#!topic/waffle-users/MHANogskG48

Thanks,
max

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants