Search in sources :

Example 6 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 7 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)

Example 8 with Authenticator

use of java.net.Authenticator in project apjp by jvansteirteghem.

the class Main method onCreate.

public void onCreate() {
    try {
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
        APJP.APJP_KEY = sharedPreferences.getString("APJP_KEY", "");
        APJP.APJP_LOGGER_ID = "APJP";
        APJP.APJP_LOGGER_LEVEL = 1;
        APJP.APJP_LOCAL_PROXY_SERVER_ADDRESS = sharedPreferences.getString("APJP_LOCAL_PROXY_SERVER_ADDRESS", "");
        try {
            APJP.APJP_LOCAL_PROXY_SERVER_PORT = new Integer(sharedPreferences.getString("APJP_LOCAL_PROXY_SERVER_PORT", ""));
        } catch (Exception e) {
            APJP.APJP_LOCAL_PROXY_SERVER_PORT = 0;
        }
        APJP.APJP_LOCAL_PROXY_SERVER_LOGGER_ID = "APJP_LOCAL_PROXY_SERVER";
        APJP.APJP_LOCAL_PROXY_SERVER_LOGGER_LEVEL = 1;
        APJP.APJP_LOCAL_HTTP_PROXY_SERVER_ADDRESS = sharedPreferences.getString("APJP_LOCAL_HTTP_PROXY_SERVER_ADDRESS", "");
        try {
            APJP.APJP_LOCAL_HTTP_PROXY_SERVER_PORT = new Integer(sharedPreferences.getString("APJP_LOCAL_HTTP_PROXY_SERVER_PORT", ""));
        } catch (Exception e) {
            APJP.APJP_LOCAL_HTTP_PROXY_SERVER_PORT = 0;
        }
        APJP.APJP_LOCAL_HTTP_PROXY_SERVER_LOGGER_ID = "APJP_LOCAL_HTTP_PROXY_SERVER";
        APJP.APJP_LOCAL_HTTP_PROXY_SERVER_LOGGER_LEVEL = 1;
        APJP.APJP_LOCAL_HTTP_SERVER_ADDRESS = sharedPreferences.getString("APJP_LOCAL_HTTP_SERVER_ADDRESS", "");
        try {
            APJP.APJP_LOCAL_HTTP_SERVER_PORT = new Integer(sharedPreferences.getString("APJP_LOCAL_HTTP_SERVER_PORT", ""));
        } catch (Exception e) {
            APJP.APJP_LOCAL_HTTP_SERVER_PORT = 0;
        }
        APJP.APJP_LOCAL_HTTP_SERVER_LOGGER_ID = "APJP_LOCAL_HTTP_SERVER";
        APJP.APJP_LOCAL_HTTP_SERVER_LOGGER_LEVEL = 1;
        APJP.APJP_REMOTE_HTTP_SERVER_REQUEST_URL = new String[10];
        APJP.APJP_REMOTE_HTTP_SERVER_REQUEST_PROPERTY_KEY = new String[10][5];
        APJP.APJP_REMOTE_HTTP_SERVER_REQUEST_PROPERTY_VALUE = new String[10][5];
        for (int i = 0; i < APJP.APJP_REMOTE_HTTP_SERVER_REQUEST_PROPERTY_KEY.length; i = i + 1) {
            APJP.APJP_REMOTE_HTTP_SERVER_REQUEST_URL[i] = sharedPreferences.getString("APJP_REMOTE_HTTP_SERVER_" + (i + 1) + "_REQUEST_URL", "");
            for (int j = 0; j < APJP.APJP_REMOTE_HTTP_SERVER_REQUEST_PROPERTY_KEY[i].length; j = j + 1) {
                APJP.APJP_REMOTE_HTTP_SERVER_REQUEST_PROPERTY_KEY[i][j] = sharedPreferences.getString("APJP_REMOTE_HTTP_SERVER_" + (i + 1) + "_REQUEST_PROPERTY_" + (j + 1) + "_KEY", "");
                APJP.APJP_REMOTE_HTTP_SERVER_REQUEST_PROPERTY_VALUE[i][j] = sharedPreferences.getString("APJP_REMOTE_HTTP_SERVER_" + (i + 1) + "_REQUEST_PROPERTY_" + (j + 1) + "_VALUE", "");
            }
        }
        APJP.APJP_LOCAL_HTTPS_PROXY_SERVER_ADDRESS = sharedPreferences.getString("APJP_LOCAL_HTTPS_PROXY_SERVER_ADDRESS", "");
        try {
            APJP.APJP_LOCAL_HTTPS_PROXY_SERVER_PORT = new Integer(sharedPreferences.getString("APJP_LOCAL_HTTPS_PROXY_SERVER_PORT", ""));
        } catch (Exception e) {
            APJP.APJP_LOCAL_HTTPS_PROXY_SERVER_PORT = 0;
        }
        APJP.APJP_LOCAL_HTTPS_PROXY_SERVER_LOGGER_ID = "APJP_LOCAL_HTTPS_PROXY_SERVER";
        APJP.APJP_LOCAL_HTTPS_PROXY_SERVER_LOGGER_LEVEL = 1;
        APJP.APJP_LOCAL_HTTPS_SERVER_ADDRESS = sharedPreferences.getString("APJP_LOCAL_HTTPS_SERVER_ADDRESS", "");
        try {
            APJP.APJP_LOCAL_HTTPS_SERVER_PORT = new Integer(sharedPreferences.getString("APJP_LOCAL_HTTPS_SERVER_PORT", ""));
        } catch (Exception e) {
            APJP.APJP_LOCAL_HTTPS_SERVER_PORT = 0;
        }
        APJP.APJP_LOCAL_HTTPS_SERVER_LOGGER_ID = "APJP_LOCAL_HTTPS_SERVER";
        APJP.APJP_LOCAL_HTTPS_SERVER_LOGGER_LEVEL = 1;
        APJP.APJP_REMOTE_HTTPS_SERVER_REQUEST_URL = new String[10];
        APJP.APJP_REMOTE_HTTPS_SERVER_REQUEST_PROPERTY_KEY = new String[10][5];
        APJP.APJP_REMOTE_HTTPS_SERVER_REQUEST_PROPERTY_VALUE = new String[10][5];
        for (int i = 0; i < APJP.APJP_REMOTE_HTTPS_SERVER_REQUEST_PROPERTY_KEY.length; i = i + 1) {
            APJP.APJP_REMOTE_HTTPS_SERVER_REQUEST_URL[i] = sharedPreferences.getString("APJP_REMOTE_HTTPS_SERVER_" + (i + 1) + "_REQUEST_URL", "");
            for (int j = 0; j < APJP.APJP_REMOTE_HTTPS_SERVER_REQUEST_PROPERTY_KEY[i].length; j = j + 1) {
                APJP.APJP_REMOTE_HTTPS_SERVER_REQUEST_PROPERTY_KEY[i][j] = sharedPreferences.getString("APJP_REMOTE_HTTPS_SERVER_" + (i + 1) + "_REQUEST_PROPERTY_" + (j + 1) + "_KEY", "");
                APJP.APJP_REMOTE_HTTPS_SERVER_REQUEST_PROPERTY_VALUE[i][j] = sharedPreferences.getString("APJP_REMOTE_HTTPS_SERVER_" + (i + 1) + "_REQUEST_PROPERTY_" + (j + 1) + "_VALUE", "");
            }
        }
        APJP.APJP_HTTP_PROXY_SERVER_ADDRESS = sharedPreferences.getString("APJP_HTTP_PROXY_SERVER_ADDRESS", "");
        try {
            APJP.APJP_HTTP_PROXY_SERVER_PORT = new Integer(sharedPreferences.getString("APJP_HTTP_PROXY_SERVER_PORT", ""));
        } catch (Exception e) {
            APJP.APJP_HTTP_PROXY_SERVER_PORT = 0;
        }
        APJP.APJP_HTTP_PROXY_SERVER_USERNAME = sharedPreferences.getString("APJP_HTTP_PROXY_SERVER_USERNAME", "");
        APJP.APJP_HTTP_PROXY_SERVER_PASSWORD = sharedPreferences.getString("APJP_HTTP_PROXY_SERVER_PASSWORD", "");
        APJP.APJP_HTTPS_PROXY_SERVER_ADDRESS = sharedPreferences.getString("APJP_HTTPS_PROXY_SERVER_ADDRESS", "");
        try {
            APJP.APJP_HTTPS_PROXY_SERVER_PORT = new Integer(sharedPreferences.getString("APJP_HTTPS_PROXY_SERVER_PORT", ""));
        } catch (Exception e) {
            APJP.APJP_HTTPS_PROXY_SERVER_PORT = 0;
        }
        APJP.APJP_HTTPS_PROXY_SERVER_USERNAME = sharedPreferences.getString("APJP_HTTPS_PROXY_SERVER_USERNAME", "");
        APJP.APJP_HTTPS_PROXY_SERVER_PASSWORD = sharedPreferences.getString("APJP_HTTPS_PROXY_SERVER_PASSWORD", "");
        APJP.APJP_APPLICATION = getApplication();
        Authenticator.setDefault(new Authenticator() {

            protected PasswordAuthentication getPasswordAuthentication() {
                PasswordAuthentication passwordAuthentication = null;
                if (this.getRequestorType() == Authenticator.RequestorType.PROXY) {
                    if (this.getRequestingURL().getProtocol().equalsIgnoreCase("HTTP") == true) {
                        passwordAuthentication = new PasswordAuthentication(APJP.APJP_HTTP_PROXY_SERVER_USERNAME, APJP.APJP_HTTP_PROXY_SERVER_PASSWORD.toCharArray());
                    } else {
                        if (this.getRequestingURL().getProtocol().equalsIgnoreCase("HTTPS") == true) {
                            passwordAuthentication = new PasswordAuthentication(APJP.APJP_HTTPS_PROXY_SERVER_USERNAME, APJP.APJP_HTTPS_PROXY_SERVER_PASSWORD.toCharArray());
                        }
                    }
                }
                return passwordAuthentication;
            }
        });
        logger = Logger.getLogger(APJP.APJP_LOGGER_ID);
        proxyServer = new ProxyServer();
    } catch (Exception e) {
        logger.log(1, "EXCEPTION", e);
    }
}
Also used : SharedPreferences(android.content.SharedPreferences) Authenticator(java.net.Authenticator) PasswordAuthentication(java.net.PasswordAuthentication)

Example 9 with Authenticator

use of java.net.Authenticator in project sonarqube by SonarSource.

the class DefaultHttpDownloaderTest method configure_http_proxy_credentials.

@Test
public void configure_http_proxy_credentials() {
    DefaultHttpDownloader.AuthenticatorFacade system = mock(DefaultHttpDownloader.AuthenticatorFacade.class);
    Settings settings = new MapSettings();
    settings.setProperty("https.proxyHost", "1.2.3.4");
    settings.setProperty("http.proxyUser", "the_login");
    settings.setProperty("http.proxyPassword", "the_passwd");
    new DefaultHttpDownloader.BaseHttpDownloader(system, settings, null);
    verify(system).setDefaultAuthenticator(argThat(new TypeSafeMatcher<Authenticator>() {

        @Override
        protected boolean matchesSafely(Authenticator authenticator) {
            DefaultHttpDownloader.ProxyAuthenticator a = (DefaultHttpDownloader.ProxyAuthenticator) authenticator;
            PasswordAuthentication authentication = a.getPasswordAuthentication();
            return authentication.getUserName().equals("the_login") && new String(authentication.getPassword()).equals("the_passwd");
        }

        @Override
        public void describeTo(Description description) {
        }
    }));
}
Also used : TypeSafeMatcher(org.hamcrest.TypeSafeMatcher) Description(org.hamcrest.Description) MapSettings(org.sonar.api.config.MapSettings) Settings(org.sonar.api.config.Settings) MapSettings(org.sonar.api.config.MapSettings) Authenticator(java.net.Authenticator) PasswordAuthentication(java.net.PasswordAuthentication) Test(org.junit.Test)

Example 10 with Authenticator

use of java.net.Authenticator in project twitter4j by yusuke.

the class AlternativeHttpClientImpl method prepareOkHttpClient.

private void prepareOkHttpClient() {
    if (okHttpClient == null) {
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        //set protocols
        List<Protocol> protocols = new ArrayList<Protocol>();
        protocols.add(Protocol.HTTP_1_1);
        if (sPreferHttp2)
            protocols.add(Protocol.HTTP_2);
        if (sPreferSpdy)
            protocols.add(Protocol.SPDY_3);
        builder.protocols(protocols);
        //connectionPool setup
        builder.connectionPool(new ConnectionPool(MAX_CONNECTIONS, KEEP_ALIVE_DURATION_MS, TimeUnit.MILLISECONDS));
        //redirect disable
        builder.followSslRedirects(false);
        //for proxy
        if (isProxyConfigured()) {
            if (CONF.getHttpProxyUser() != null && !CONF.getHttpProxyUser().equals("")) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Proxy AuthUser: " + CONF.getHttpProxyUser());
                    logger.debug("Proxy AuthPassword: " + CONF.getHttpProxyPassword().replaceAll(".", "*"));
                }
                Authenticator.setDefault(new Authenticator() {

                    @Override
                    protected PasswordAuthentication getPasswordAuthentication() {
                        if (getRequestorType().equals(RequestorType.PROXY)) {
                            return new PasswordAuthentication(CONF.getHttpProxyUser(), CONF.getHttpProxyPassword().toCharArray());
                        } else {
                            return null;
                        }
                    }
                });
            }
            final Proxy proxy = new Proxy(Proxy.Type.HTTP, InetSocketAddress.createUnresolved(CONF.getHttpProxyHost(), CONF.getHttpProxyPort()));
            if (logger.isDebugEnabled()) {
                logger.debug("Opening proxied connection(" + CONF.getHttpProxyHost() + ":" + CONF.getHttpProxyPort() + ")");
            }
            builder.proxy(proxy);
        }
        //connection timeout
        if (CONF.getHttpConnectionTimeout() > 0) {
            builder.connectTimeout(CONF.getHttpConnectionTimeout(), TimeUnit.MILLISECONDS);
        }
        //read timeout
        if (CONF.getHttpReadTimeout() > 0) {
            builder.readTimeout(CONF.getHttpReadTimeout(), TimeUnit.MILLISECONDS);
        }
        okHttpClient = builder.build();
    }
}
Also used : ArrayList(java.util.ArrayList) Authenticator(java.net.Authenticator)

Aggregations

Authenticator (java.net.Authenticator)15 PasswordAuthentication (java.net.PasswordAuthentication)14 InetSocketAddress (java.net.InetSocketAddress)5 Proxy (java.net.Proxy)5 URL (java.net.URL)5 IOException (java.io.IOException)3 ServerSocket (java.net.ServerSocket)3 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)3 File (java.io.File)2 URI (java.net.URI)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Properties (java.util.Properties)2 SSLServerSocket (javax.net.ssl.SSLServerSocket)2 SSLSocket (javax.net.ssl.SSLSocket)2 HTTPRequests (APJP.HTTP11.HTTPRequests)1 HTTPSRequests (APJP.HTTP11.HTTPSRequests)1 ProxyServer (APJP.ProxyServer)1 SharedPreferences (android.content.SharedPreferences)1 JCommander (com.beust.jcommander.JCommander)1