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);
}
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;
}
Aggregations