use of org.apache.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 = (proxyUser != null) ? proxyUser : username;
password = (proxyPass != null) ? proxyPass : password;
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.getHostName(), proxyHost.getPort(), ANY_REALM, NTLM);
Credentials creds = new NTCredentials(ntlmUsername, password, getWorkstation(), ntlmDomain);
credentialsProvider.setCredentials(authScope, creds);
authScope = new AuthScope(proxyHost.getHostName(), proxyHost.getPort());
creds = new UsernamePasswordCredentials(username, password);
credentialsProvider.setCredentials(authScope, creds);
return Optional.of(credentialsProvider);
}
use of org.apache.http.auth.NTCredentials in project estatio by estatio.
the class UrlDownloaderUsingNtlmCredentials method init.
@PostConstruct
public void init(Map<String, String> props) throws UnknownHostException {
String user = null;
String ntDomain = null;
final String userProp = PREFIX + "user";
final String fullyQualifiedUser = props.get(userProp);
if (fullyQualifiedUser != null) {
final List<String> userParts = Splitter.on(slash()).splitToList(fullyQualifiedUser);
ntDomain = userParts.get(0);
user = userParts.get(1);
}
String password = props.get(PREFIX + "password");
host = props.get(PREFIX + "host");
if (user != null && password != null && ntDomain != null && host != null) {
workstation = InetAddress.getLocalHost().getHostName();
// thread-safe according to HTTP Client
httpclient = HttpClientBuilder.create().build();
// implementations are required to be thread-safe, apparently
credsProvider = new BasicCredentialsProvider();
// immutable, so okay to reuse
final NTCredentials credentials = new NTCredentials(user, password, workstation, ntDomain);
credsProvider.setCredentials(AuthScope.ANY, credentials);
}
}
use of org.apache.http.auth.NTCredentials in project iaf by ibissource.
the class WebServiceNtlmSender method configure.
@Override
public void configure() throws ConfigurationException {
super.configure();
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, getTimeout());
HttpConnectionParams.setSoTimeout(httpParameters, getTimeout());
httpClient = new DefaultHttpClient(connectionManager, httpParameters);
httpClient.getAuthSchemes().register("NTLM", new NTLMSchemeFactory());
CredentialFactory cf = new CredentialFactory(getAuthAlias(), getUsername(), getPassword());
httpClient.getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), new NTCredentials(cf.getUsername(), cf.getPassword(), Misc.getHostname(), getAuthDomain()));
if (StringUtils.isNotEmpty(getProxyHost())) {
HttpHost proxy = new HttpHost(getProxyHost(), getProxyPort());
httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
}
}
use of org.apache.http.auth.NTCredentials in project androidquery by androidquery.
the class NTLMProxyHandle method applyProxy.
@Override
public void applyProxy(AbstractAjaxCallback<?, ?> cb, HttpRequest request, DefaultHttpClient client) {
if (!isIntranet(cb.getUrl())) {
if (!TextUtils.isEmpty(host) && !TextUtils.isEmpty(user)) {
AQUtility.debug("ntlm token");
client.getAuthSchemes().register("ntlm", new NTLMSchemeFactory());
client.getCredentialsProvider().setCredentials(new AuthScope(host, port), new NTCredentials(user, password, host, domain));
cb.proxy(host, port);
}
}
}
use of org.apache.http.auth.NTCredentials in project androidquery by androidquery.
the class NTLMScheme method authenticate.
public Header authenticate(final Credentials credentials, final HttpRequest request) throws AuthenticationException {
NTCredentials ntcredentials = null;
try {
ntcredentials = (NTCredentials) credentials;
} catch (ClassCastException e) {
throw new InvalidCredentialsException("Credentials cannot be used for NTLM authentication: " + credentials.getClass().getName());
}
String response = null;
if (this.state == State.CHALLENGE_RECEIVED || this.state == State.FAILED) {
response = this.engine.generateType1Msg(ntcredentials.getDomain(), ntcredentials.getWorkstation());
this.state = State.MSG_TYPE1_GENERATED;
} else if (this.state == State.MSG_TYPE2_RECEVIED) {
response = this.engine.generateType3Msg(ntcredentials.getUserName(), ntcredentials.getPassword(), ntcredentials.getDomain(), ntcredentials.getWorkstation(), this.challenge);
this.state = State.MSG_TYPE3_GENERATED;
} else {
throw new AuthenticationException("Unexpected state: " + this.state);
}
CharArrayBuffer buffer = new CharArrayBuffer(32);
if (isProxy()) {
buffer.append(AUTH.PROXY_AUTH_RESP);
} else {
buffer.append(AUTH.WWW_AUTH_RESP);
}
buffer.append(": NTLM ");
buffer.append(response);
return new BufferedHeader(buffer);
}
Aggregations