Search in sources :

Example 51 with PasswordAuthentication

use of java.net.PasswordAuthentication in project cxf by apache.

the class CXFAuthenticator method getPasswordAuthentication.

protected PasswordAuthentication getPasswordAuthentication() {
    PasswordAuthentication auth = null;
    Message m = PhaseInterceptorChain.getCurrentMessage();
    if (m != null) {
        Exchange exchange = m.getExchange();
        Conduit conduit = exchange.getConduit(m);
        if (conduit instanceof HTTPConduit) {
            HTTPConduit httpConduit = (HTTPConduit) conduit;
            if (getRequestorType() == RequestorType.PROXY && httpConduit.getProxyAuthorization() != null) {
                String un = httpConduit.getProxyAuthorization().getUserName();
                String pwd = httpConduit.getProxyAuthorization().getPassword();
                if (un != null && pwd != null) {
                    auth = new PasswordAuthentication(un, pwd.toCharArray());
                }
            } else if (getRequestorType() == RequestorType.SERVER && httpConduit.getAuthorization() != null) {
                if ("basic".equals(getRequestingScheme()) || "digest".equals(getRequestingScheme())) {
                    return null;
                }
                String un = httpConduit.getAuthorization().getUserName();
                String pwd = httpConduit.getAuthorization().getPassword();
                if (un != null && pwd != null) {
                    auth = new PasswordAuthentication(un, pwd.toCharArray());
                }
            }
        }
    }
    // this HTTP call has therefore not been generated by CXF
    return auth;
}
Also used : Exchange(org.apache.cxf.message.Exchange) Message(org.apache.cxf.message.Message) Conduit(org.apache.cxf.transport.Conduit) PasswordAuthentication(java.net.PasswordAuthentication)

Example 52 with PasswordAuthentication

use of java.net.PasswordAuthentication in project td-client-java by treasure-data.

the class TestProxyAccess method proxyServerTest.

@Test
public void proxyServerTest() throws Exception {
    Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("localhost", proxyPort));
    Authenticator auth = new Authenticator() {

        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(PROXY_USER, PROXY_PASS.toCharArray());
        }
    };
    String disabledSchemesProperty = "jdk.http.auth.tunneling.disabledSchemes";
    try {
        System.setProperty(disabledSchemesProperty, "");
        Authenticator.setDefault(auth);
        try (InputStream in = new URL("https://api.treasuredata.com/v3/system/server_status").openConnection(proxy).getInputStream()) {
            String ret = CharStreams.toString(new InputStreamReader(in));
            logger.info(ret);
        }
        assertEquals(1, proxyAccessCount.get());
    } finally {
        System.clearProperty(disabledSchemesProperty);
        Authenticator.setDefault(null);
    }
}
Also used : Proxy(java.net.Proxy) InputStreamReader(java.io.InputStreamReader) InetSocketAddress(java.net.InetSocketAddress) InputStream(java.io.InputStream) Authenticator(java.net.Authenticator) URL(java.net.URL) PasswordAuthentication(java.net.PasswordAuthentication) Test(org.junit.Test)

Example 53 with PasswordAuthentication

use of java.net.PasswordAuthentication in project ripme by RipMeApp.

the class Proxy method setSocks.

/**
 * Set a Socks Proxy Server (globally).
 *
 * @param fullsocks the socks server, using format [user:password]@host[:port]
 */
public static void setSocks(String fullsocks) {
    Map<String, String> socksServer = parseServer(fullsocks);
    if (socksServer.get("user") != null && socksServer.get("password") != null) {
        Authenticator.setDefault(new Authenticator() {

            protected PasswordAuthentication getPasswordAuthentication() {
                PasswordAuthentication p = new PasswordAuthentication(socksServer.get("user"), socksServer.get("password").toCharArray());
                return p;
            }
        });
        System.setProperty("java.net.socks.username", socksServer.get("user"));
        System.setProperty("java.net.socks.password", socksServer.get("password"));
    }
    if (socksServer.get("port") != null) {
        System.setProperty("socksProxyPort", socksServer.get("port"));
    }
    System.setProperty("socksProxyHost", socksServer.get("server"));
}
Also used : Authenticator(java.net.Authenticator) PasswordAuthentication(java.net.PasswordAuthentication)

Example 54 with PasswordAuthentication

use of java.net.PasswordAuthentication in project ripme by RipMeApp.

the class Proxy method setHTTPProxy.

/**
 * Set a HTTP Proxy.
 * WARNING: Authenticated HTTP Proxy won't work from jdk1.8.111 unless
 * passing the flag -Djdk.http.auth.tunneling.disabledSchemes="" to java
 * see https://stackoverflow.com/q/41505219
 *
 * @param fullproxy the proxy, using format [user:password]@host[:port]
 */
public static void setHTTPProxy(String fullproxy) {
    Map<String, String> proxyServer = parseServer(fullproxy);
    if (proxyServer.get("user") != null && proxyServer.get("password") != null) {
        Authenticator.setDefault(new Authenticator() {

            protected PasswordAuthentication getPasswordAuthentication() {
                PasswordAuthentication p = new PasswordAuthentication(proxyServer.get("user"), proxyServer.get("password").toCharArray());
                return p;
            }
        });
        System.setProperty("http.proxyUser", proxyServer.get("user"));
        System.setProperty("http.proxyPassword", proxyServer.get("password"));
        System.setProperty("https.proxyUser", proxyServer.get("user"));
        System.setProperty("https.proxyPassword", proxyServer.get("password"));
    }
    if (proxyServer.get("port") != null) {
        System.setProperty("http.proxyPort", proxyServer.get("port"));
        System.setProperty("https.proxyPort", proxyServer.get("port"));
    }
    System.setProperty("http.proxyHost", proxyServer.get("server"));
    System.setProperty("https.proxyHost", proxyServer.get("server"));
}
Also used : Authenticator(java.net.Authenticator) PasswordAuthentication(java.net.PasswordAuthentication)

Example 55 with PasswordAuthentication

use of java.net.PasswordAuthentication in project buck by facebook.

the class StackedDownloader method createFromConfig.

public static Downloader createFromConfig(BuckConfig config, Optional<Path> androidSdkRoot) {
    ImmutableList.Builder<Downloader> downloaders = ImmutableList.builder();
    DownloadConfig downloadConfig = new DownloadConfig(config);
    Optional<Proxy> proxy = downloadConfig.getProxy();
    HttpDownloader httpDownloader = new HttpDownloader(proxy);
    for (Map.Entry<String, String> kv : downloadConfig.getAllMavenRepos().entrySet()) {
        String repo = Preconditions.checkNotNull(kv.getValue());
        // Check the type.
        if (repo.startsWith("http:") || repo.startsWith("https://")) {
            String repoName = kv.getKey();
            Optional<PasswordAuthentication> credentials = downloadConfig.getRepoCredentials(repoName);
            downloaders.add(new RemoteMavenDownloader(httpDownloader, repo, credentials));
        } else if (repo.startsWith("file:")) {
            try {
                URL url = new URL(repo);
                Preconditions.checkNotNull(url.getPath());
                downloaders.add(new OnDiskMavenDownloader(config.resolvePathThatMayBeOutsideTheProjectFilesystem(Paths.get(url.getPath()))));
            } catch (FileNotFoundException e) {
                throw new HumanReadableException(e, "Error occurred when attempting to use %s " + "as a local Maven repository as configured in .buckconfig.  See " + "https://buckbuild.com/concept/buckconfig.html#maven_repositories for how to " + "configure this setting", repo);
            } catch (MalformedURLException e) {
                throw new HumanReadableException("Unable to determine path from %s", repo);
            }
        } else {
            try {
                downloaders.add(new OnDiskMavenDownloader(Preconditions.checkNotNull(config.resolvePathThatMayBeOutsideTheProjectFilesystem(Paths.get(repo)))));
            } catch (FileNotFoundException e) {
                throw new HumanReadableException(e, "Error occurred when attempting to use %s " + "as a local Maven repository as configured in .buckconfig.  See " + "https://buckbuild.com/concept/buckconfig.html#maven_repositories for how to " + "configure this setting", repo);
            }
        }
    }
    if (androidSdkRoot.isPresent()) {
        Path androidMavenRepo = androidSdkRoot.get().resolve("extras/android/m2repository");
        try {
            downloaders.add(new OnDiskMavenDownloader(androidMavenRepo));
        } catch (FileNotFoundException e) {
            LOG.warn("Android Maven repo %s doesn't exist", androidMavenRepo.toString());
        }
        Path googleMavenRepo = androidSdkRoot.get().resolve("extras/google/m2repository");
        try {
            downloaders.add(new OnDiskMavenDownloader(googleMavenRepo));
        } catch (FileNotFoundException e) {
            LOG.warn("Google Maven repo '%s' doesn't exist", googleMavenRepo.toString());
        }
    }
    // Add a default downloader
    // TODO(shs96c): Remove the maven_repo check
    Optional<String> defaultMavenRepo = downloadConfig.getMavenRepo();
    if (defaultMavenRepo.isPresent()) {
        LOG.warn("Please configure maven repos by adding them to a 'maven_repositories' " + "section in your buckconfig");
    }
    downloaders.add(downloadConfig.getMaxNumberOfRetries().map(retries -> (Downloader) RetryingDownloader.from(httpDownloader, retries)).orElse(httpDownloader));
    return new StackedDownloader(downloaders.build());
}
Also used : Path(java.nio.file.Path) MalformedURLException(java.net.MalformedURLException) ImmutableList(com.google.common.collect.ImmutableList) FileNotFoundException(java.io.FileNotFoundException) URL(java.net.URL) Proxy(java.net.Proxy) DownloadConfig(com.facebook.buck.cli.DownloadConfig) HumanReadableException(com.facebook.buck.util.HumanReadableException) Map(java.util.Map) PasswordAuthentication(java.net.PasswordAuthentication)

Aggregations

PasswordAuthentication (java.net.PasswordAuthentication)108 Authenticator (java.net.Authenticator)52 URL (java.net.URL)27 InetSocketAddress (java.net.InetSocketAddress)22 Proxy (java.net.Proxy)19 Test (org.junit.Test)16 HttpURLConnection (java.net.HttpURLConnection)11 InetAddress (java.net.InetAddress)11 URI (java.net.URI)10 MalformedURLException (java.net.MalformedURLException)9 File (java.io.File)8 IOException (java.io.IOException)8 SocketAddress (java.net.SocketAddress)8 UnknownHostException (java.net.UnknownHostException)6 PrivilegedActionException (java.security.PrivilegedActionException)6 InputStream (java.io.InputStream)5 SocketTimeoutException (java.net.SocketTimeoutException)5 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)5 HttpRetryException (java.net.HttpRetryException)4 ProtocolException (java.net.ProtocolException)4