Search in sources :

Example 1 with NTCredentials

use of org.apache.hc.client5.http.auth.NTCredentials in project webdrivermanager by bonigarcia.

the class HttpClient method createBasicCredentialsProvider.

private final Optional<BasicCredentialsProvider> createBasicCredentialsProvider(String proxy, String proxyUser, String proxyPass, HttpHost proxyHost) throws MalformedURLException, UnsupportedEncodingException {
    Optional<URL> proxyUrl = determineProxyUrl(proxy);
    if (!proxyUrl.isPresent()) {
        return empty();
    }
    String username = null;
    String password = null;
    // apply env value
    String userInfo = proxyUrl.get().getUserInfo();
    if (userInfo != null) {
        StringTokenizer st = new StringTokenizer(userInfo, ":");
        username = st.hasMoreTokens() ? decode(st.nextToken(), UTF_8.name()) : null;
        password = st.hasMoreTokens() ? decode(st.nextToken(), UTF_8.name()) : null;
    }
    String envProxyUser = getenv("HTTPS_PROXY_USER");
    String envProxyPass = getenv("HTTPS_PROXY_PASS");
    username = (envProxyUser != null) ? envProxyUser : username;
    password = (envProxyPass != null) ? envProxyPass : password;
    // apply option value
    username = isNullOrEmpty(proxyUser) ? username : proxyUser;
    password = isNullOrEmpty(proxyPass) ? password : proxyPass;
    if (username == null) {
        return empty();
    }
    String ntlmUsername = username;
    String ntlmDomain = null;
    int index = username.indexOf('\\');
    if (index > 0) {
        ntlmDomain = username.substring(0, index);
        ntlmUsername = username.substring(index + 1);
    }
    BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    AuthScope authScope = new AuthScope(proxyHost, null, NTLM);
    char[] passwd = (password == null) ? new char[0] : password.toCharArray();
    Credentials creds = new NTCredentials(ntlmUsername, passwd, getWorkstation(), ntlmDomain);
    credentialsProvider.setCredentials(authScope, creds);
    authScope = new AuthScope(proxyHost.getHostName(), proxyHost.getPort());
    creds = new UsernamePasswordCredentials(username, passwd);
    credentialsProvider.setCredentials(authScope, creds);
    return Optional.of(credentialsProvider);
}
Also used : StringTokenizer(java.util.StringTokenizer) BasicCredentialsProvider(org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider) AuthScope(org.apache.hc.client5.http.auth.AuthScope) URL(java.net.URL) Credentials(org.apache.hc.client5.http.auth.Credentials) NTCredentials(org.apache.hc.client5.http.auth.NTCredentials) UsernamePasswordCredentials(org.apache.hc.client5.http.auth.UsernamePasswordCredentials) NTCredentials(org.apache.hc.client5.http.auth.NTCredentials) UsernamePasswordCredentials(org.apache.hc.client5.http.auth.UsernamePasswordCredentials)

Example 2 with NTCredentials

use of org.apache.hc.client5.http.auth.NTCredentials in project ecf by eclipse.

the class HttpClientProxyCredentialProvider method fixNTCredentials.

public static Credentials fixNTCredentials(Credentials credentials) {
    if (credentials == null) {
        return null;
    }
    if (credentials instanceof NTCredentials) {
        NTCredentials ntCreds = (NTCredentials) credentials;
        String userName = ntCreds.getUserName();
        String domainUser = getNTLMUserName(userName);
        if (ntCreds.getDomain() == null || domainUser != userName) {
            String domain = getNTLMDomainName(userName);
            String workstation = getNTLMWorkstation();
            return new NTCredentials(domainUser, ntCreds.getPassword(), workstation, domain);
        }
    } else if (credentials instanceof UsernamePasswordCredentials) {
        UsernamePasswordCredentials basicCredentials = (UsernamePasswordCredentials) credentials;
        return createNTLMCredentials(basicCredentials.getUserName(), basicCredentials.getPassword());
    }
    return credentials;
}
Also used : NTCredentials(org.apache.hc.client5.http.auth.NTCredentials) UsernamePasswordCredentials(org.apache.hc.client5.http.auth.UsernamePasswordCredentials)

Aggregations

NTCredentials (org.apache.hc.client5.http.auth.NTCredentials)2 UsernamePasswordCredentials (org.apache.hc.client5.http.auth.UsernamePasswordCredentials)2 URL (java.net.URL)1 StringTokenizer (java.util.StringTokenizer)1 AuthScope (org.apache.hc.client5.http.auth.AuthScope)1 Credentials (org.apache.hc.client5.http.auth.Credentials)1 BasicCredentialsProvider (org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider)1