Search in sources :

Example 1 with Authenticator

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);
}
Also used : Proxy(java.net.Proxy) InetSocketAddress(java.net.InetSocketAddress) SSLSocket(javax.net.ssl.SSLSocket) ServerSocket(java.net.ServerSocket) SSLServerSocket(javax.net.ssl.SSLServerSocket) Authenticator(java.net.Authenticator) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) PasswordAuthentication(java.net.PasswordAuthentication)

Example 2 with Authenticator

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();
}
Also used : Proxy(java.net.Proxy) InetSocketAddress(java.net.InetSocketAddress) SSLSocket(javax.net.ssl.SSLSocket) ServerSocket(java.net.ServerSocket) SSLServerSocket(javax.net.ssl.SSLServerSocket) Authenticator(java.net.Authenticator) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) PasswordAuthentication(java.net.PasswordAuthentication)

Example 3 with Authenticator

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);
    }
}
Also used : Proxy(java.net.Proxy) HttpURLConnection(java.net.HttpURLConnection) InetSocketAddress(java.net.InetSocketAddress) Authenticator(java.net.Authenticator) URL(java.net.URL) PasswordAuthentication(java.net.PasswordAuthentication) SideEffect(dalvik.annotation.SideEffect)

Example 4 with Authenticator

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));
}
Also used : Pattern(java.util.regex.Pattern) Proxy(java.net.Proxy) Matcher(java.util.regex.Matcher) InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) Authenticator(java.net.Authenticator) PasswordAuthentication(java.net.PasswordAuthentication)

Example 5 with Authenticator

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();
        }
    });
}
Also used : Authenticator(java.net.Authenticator) PasswordAuthentication(java.net.PasswordAuthentication)

Aggregations

Authenticator (java.net.Authenticator)80 PasswordAuthentication (java.net.PasswordAuthentication)50 URL (java.net.URL)18 Proxy (java.net.Proxy)14 InetSocketAddress (java.net.InetSocketAddress)12 HttpClient (java.net.http.HttpClient)11 Field (java.lang.reflect.Field)10 HttpURLConnection (java.net.HttpURLConnection)10 IOException (java.io.IOException)9 Test (org.junit.Test)7 Method (java.lang.reflect.Method)6 File (java.io.File)5 SocketAddress (java.net.SocketAddress)5 InputStream (java.io.InputStream)4 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 ArrayList (java.util.ArrayList)3 Content (org.sonatype.goodies.httpfixture.server.jetty.behaviour.Content)3 Debug (org.sonatype.goodies.httpfixture.server.jetty.behaviour.Debug)3 JCommander (com.beust.jcommander.JCommander)2