Better handling of http(s).proxyUser and http(s).proxyPassword

This commit is contained in:
Jens Borgland 2018-12-12 15:58:39 +01:00 committed by Oleg Kalnichevski
parent b7a945ff91
commit eac6c062b5
1 changed files with 35 additions and 16 deletions

View File

@ -135,22 +135,13 @@ public class SystemDefaultCredentialsProvider implements CredentialsStore {
protocol, authscope, Authenticator.RequestorType.PROXY, clientContext); protocol, authscope, Authenticator.RequestorType.PROXY, clientContext);
} }
if (systemcreds == null) { if (systemcreds == null) {
final String proxyHost = System.getProperty(protocol + ".proxyHost"); // Look for values given using http.proxyUser/http.proxyPassword or
if (proxyHost != null) { // https.proxyUser/https.proxyPassword. We cannot simply use the protocol from
final String proxyPort = System.getProperty(protocol + ".proxyPort"); // the origin since a proxy retrieved from https.proxyHost/https.proxyPort will
if (proxyPort != null) { // still use http as protocol
try { systemcreds = getProxyCredentials("http", authscope);
final AuthScope systemScope = new AuthScope(proxyHost, Integer.parseInt(proxyPort)); if (systemcreds == null) {
if (authscope.match(systemScope) >= 0) { systemcreds = getProxyCredentials("https", authscope);
final String proxyUser = System.getProperty(protocol + ".proxyUser");
if (proxyUser != null) {
final String proxyPassword = System.getProperty(protocol + ".proxyPassword");
systemcreds = new PasswordAuthentication(proxyUser, proxyPassword != null ? proxyPassword.toCharArray() : new char[] {});
}
}
} catch (final NumberFormatException ex) {
}
}
} }
} }
if (systemcreds != null) { if (systemcreds != null) {
@ -169,6 +160,34 @@ public class SystemDefaultCredentialsProvider implements CredentialsStore {
return null; 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 @Override
public void clear() { public void clear() {
internal.clear(); internal.clear();