package org.apache.http.impl.client;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.http.HttpHost;
import org.apache.http.annotation.Contract;
import org.apache.http.annotation.ThreadingBehavior;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.NTCredentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.AuthSchemes;
import org.apache.http.util.Args;
@Contract(threading = ThreadingBehavior.SAFE)
public class SystemDefaultCredentialsProvider implements CredentialsProvider {
private static final Map<String, String> SCHEME_MAP;
static {
SCHEME_MAP = new ConcurrentHashMap<String, String>();
SCHEME_MAP.put(AuthSchemes.BASIC.toUpperCase(Locale.ROOT), "Basic");
SCHEME_MAP.put(AuthSchemes.DIGEST.toUpperCase(Locale.ROOT), "Digest");
SCHEME_MAP.put(AuthSchemes.NTLM.toUpperCase(Locale.ROOT), "NTLM");
SCHEME_MAP.put(AuthSchemes.SPNEGO.toUpperCase(Locale.ROOT), "SPNEGO");
SCHEME_MAP.put(AuthSchemes.KERBEROS.toUpperCase(Locale.ROOT), "Kerberos");
}
private static String translateScheme(final String key) {
if (key == null) {
return null;
}
final String s = SCHEME_MAP.get(key);
return s != null ? s : key;
}
private final BasicCredentialsProvider internal;
public SystemDefaultCredentialsProvider() {
super();
this.internal = new BasicCredentialsProvider();
}
@Override
public void setCredentials(final AuthScope authscope, final Credentials credentials) {
internal.setCredentials(authscope, credentials);
}
private static PasswordAuthentication getSystemCreds(
final String protocol,
final AuthScope authscope,
final Authenticator.RequestorType requestorType) {
return Authenticator.requestPasswordAuthentication(
authscope.getHost(),
null,
authscope.getPort(),
protocol,
null,
translateScheme(authscope.getScheme()),
null,
requestorType);
}
@Override
public Credentials getCredentials(final AuthScope authscope) {
Args.notNull(authscope, "Auth scope");
final Credentials localcreds = internal.getCredentials(authscope);
if (localcreds != null) {
return localcreds;
}
final String host = authscope.getHost();
if (host != null) {
final HttpHost origin = authscope.getOrigin();
final String protocol = origin != null ? origin.getSchemeName() : (authscope.getPort() == 443 ? "https" : "http");
PasswordAuthentication systemcreds = getSystemCreds(protocol, authscope, Authenticator.RequestorType.SERVER);
if (systemcreds == null) {
systemcreds = getSystemCreds(protocol, authscope, Authenticator.RequestorType.PROXY);
}
if (systemcreds == null) {
systemcreds = getProxyCredentials("http", authscope);
if (systemcreds == null) {
systemcreds = getProxyCredentials("https", authscope);
}
}
if (systemcreds != null) {
final String domain = System.getProperty("http.auth.ntlm.domain");
if (domain != null) {
return new NTCredentials(
systemcreds.getUserName(),
new String(systemcreds.getPassword()),
null, domain);
}
return AuthSchemes.NTLM.equalsIgnoreCase(authscope.getScheme())
? new NTCredentials(systemcreds.getUserName(),
new String(systemcreds.getPassword()), null, null)
: new UsernamePasswordCredentials(systemcreds.getUserName(),
new String(systemcreds.getPassword()));
}
}
return null;
}
private static PasswordAuthentication getProxyCredentials(final String protocol, final AuthScope authscope) {
final String proxyHost = System.getProperty(protocol + ".proxyHost");
if (proxyHost == null) {
return null;
}
final String proxyPort = System.getProperty(protocol + ".proxyPort");
if (proxyPort == null) {
return null;
}
try {
final AuthScope systemScope = new AuthScope(proxyHost, Integer.parseInt(proxyPort));
if (authscope.match(systemScope) >= 0) {
final String proxyUser = System.getProperty(protocol + ".proxyUser");
if (proxyUser == null) {
return null;
}
final String proxyPassword = System.getProperty(protocol + ".proxyPassword");
return new PasswordAuthentication(proxyUser,
proxyPassword != null ? proxyPassword.toCharArray() : new char[] {});
}
} catch (final NumberFormatException ex) {
}
return null;
}
@Override
public void clear() {
internal.clear();
}
}