use of java.net.Authenticator in project robovm by robovm.
the class HttpsURLConnectionTest method testProxyAuthConnection_doOutput.
/**
* Tests HTTPS connection process made through the proxy server.
* Proxy server needs authentication.
* Client sends data to the server.
*/
public void testProxyAuthConnection_doOutput() throws Throwable {
// setting up the properties pointing to the key/trust stores
setUpStoreProperties();
// create the SSLServerSocket which will be used by server side
ServerSocket ss = new ServerSocket(0);
// create the HostnameVerifier to check that Hostname verification
// is done
TestHostnameVerifier hnv = new TestHostnameVerifier();
HttpsURLConnection.setDefaultHostnameVerifier(hnv);
Authenticator.setDefault(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("user", "password".toCharArray());
}
});
// create HttpsURLConnection to be tested
URL url = new URL("https://requested.host:55554/requested.data");
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("localhost", ss.getLocalPort())));
connection.setSSLSocketFactory(getContext().getSocketFactory());
connection.setDoOutput(true);
// perform the interaction between the peers and check the results
SSLSocket peerSocket = (SSLSocket) doInteraction(connection, ss, OK_CODE, true);
checkConnectionStateParameters(connection, peerSocket);
}
use of java.net.Authenticator in project robovm by robovm.
the class HttpsURLConnectionTest method testProxyAuthConnection.
/**
* Tests HTTPS connection process made through the proxy server.
* Proxy server needs authentication.
*/
public void testProxyAuthConnection() throws Throwable {
// setting up the properties pointing to the key/trust stores
setUpStoreProperties();
// create the SSLServerSocket which will be used by server side
ServerSocket ss = new ServerSocket(0);
// create the HostnameVerifier to check that Hostname verification
// is done
TestHostnameVerifier hnv = new TestHostnameVerifier();
HttpsURLConnection.setDefaultHostnameVerifier(hnv);
Authenticator.setDefault(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("user", "password".toCharArray());
}
});
// create HttpsURLConnection to be tested
URL url = new URL("https://requested.host:55555/requested.data");
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("localhost", ss.getLocalPort())));
connection.setSSLSocketFactory(getContext().getSocketFactory());
// perform the interaction between the peers and check the results
SSLSocket peerSocket = (SSLSocket) doInteraction(connection, ss);
checkConnectionStateParameters(connection, peerSocket);
// should silently exit
connection.connect();
}
use of java.net.Authenticator in project robovm by robovm.
the class HttpURLConnectionTest method testProxyAuthorization.
@SideEffect("Suffers from side effect of other, currently unknown test")
public void testProxyAuthorization() throws Exception {
// Set up test Authenticator
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("user", "password".toCharArray());
}
});
try {
MockProxyServer proxy = new MockProxyServer("ProxyServer");
URL url = new URL("http://remotehost:55555/requested.data");
HttpURLConnection connection = (HttpURLConnection) url.openConnection(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("localhost", proxy.port())));
connection.setConnectTimeout(1000);
connection.setReadTimeout(1000);
proxy.start();
connection.connect();
assertEquals("unexpected response code", 200, connection.getResponseCode());
proxy.join();
assertTrue("Connection did not send proxy authorization request", proxy.acceptedAuthorizedRequest);
} finally {
// remove previously set authenticator
Authenticator.setDefault(null);
}
}
use of java.net.Authenticator in project bazel by bazelbuild.
the class ProxyHelper method createProxy.
/**
* This method takes a proxyAddress as a String (ex.
* http://userId:password@proxyhost.domain.com:8000) and sets JVM arguments for http and https
* proxy as well as returns a java.net.Proxy object for optional use.
*
* @param proxyAddress The fully qualified address of the proxy server
* @return Proxy
* @throws IOException
*/
public static Proxy createProxy(@Nullable String proxyAddress) throws IOException {
if (Strings.isNullOrEmpty(proxyAddress)) {
return Proxy.NO_PROXY;
}
// Here there be dragons.
Pattern urlPattern = Pattern.compile("^(https?)://(([^:@]+?)(?::([^@]+?))?@)?([^:]+)(?::(\\d+))?/?$");
Matcher matcher = urlPattern.matcher(proxyAddress);
if (!matcher.matches()) {
throw new IOException("Proxy address " + proxyAddress + " is not a valid URL");
}
final String protocol = matcher.group(1);
final String idAndPassword = matcher.group(2);
final String username = matcher.group(3);
final String password = matcher.group(4);
final String hostname = matcher.group(5);
final String portRaw = matcher.group(6);
String cleanProxyAddress = proxyAddress;
if (idAndPassword != null) {
cleanProxyAddress = // Used to remove id+pwd from logging
proxyAddress.replace(idAndPassword, "");
}
boolean https;
switch(protocol) {
case "https":
https = true;
break;
case "http":
https = false;
break;
default:
throw new IOException("Invalid proxy protocol for " + cleanProxyAddress);
}
// Default port numbers
int port = https ? 443 : 80;
if (portRaw != null) {
try {
port = Integer.parseInt(portRaw);
} catch (NumberFormatException e) {
throw new IOException("Error parsing proxy port: " + cleanProxyAddress);
}
}
// We need to set both of these because jgit uses whichever the resource dictates
System.setProperty("https.proxyHost", hostname);
System.setProperty("https.proxyPort", Integer.toString(port));
System.setProperty("http.proxyHost", hostname);
System.setProperty("http.proxyPort", Integer.toString(port));
if (username != null) {
if (password == null) {
throw new IOException("No password given for proxy " + cleanProxyAddress);
}
// We need to make sure the proxy password is not url encoded; some special characters in
// proxy passwords require url encoding for shells and other tools to properly consume.
final String decodedPassword = URLDecoder.decode(password, "UTF-8");
System.setProperty("http.proxyUser", username);
System.setProperty("http.proxyPassword", decodedPassword);
System.setProperty("https.proxyUser", username);
System.setProperty("https.proxyPassword", decodedPassword);
Authenticator.setDefault(new Authenticator() {
@Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, decodedPassword.toCharArray());
}
});
}
return new Proxy(Proxy.Type.HTTP, new InetSocketAddress(hostname, port));
}
use of java.net.Authenticator in project bnd by bndtools.
the class HttpClient method init.
synchronized void init() {
if (inited)
return;
inited = true;
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return passwordAuthentication.get();
}
});
}
Aggregations