Search in sources :

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

Example 22 with PasswordAuthentication

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

the class HttpDownloaderTest method shouldReturnFalseIfTryingBasicAuthOverHttp.

@Test
public void shouldReturnFalseIfTryingBasicAuthOverHttp() throws IOException, URISyntaxException {
    final HttpURLConnection connection = EasyMock.createNiceMock(HttpURLConnection.class);
    EasyMock.replay(connection);
    HttpDownloader downloader = getDownloader(connection);
    PasswordAuthentication credentials = new PasswordAuthentication("not", "used".toCharArray());
    boolean result = downloader.fetch(eventBus, new URI("http://example.com"), Optional.of(credentials), neverUsed);
    assertFalse(result);
    EasyMock.verify(connection);
}
Also used : HttpURLConnection(java.net.HttpURLConnection) URI(java.net.URI) PasswordAuthentication(java.net.PasswordAuthentication) Test(org.junit.Test)

Example 23 with PasswordAuthentication

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

the class HttpDownloaderTest method shouldAddAuthenticationHeader.

@Test
public void shouldAddAuthenticationHeader() throws IOException, URISyntaxException {
    Capture<String> capturedAuth = EasyMock.newCapture();
    final HttpURLConnection connection = EasyMock.createNiceMock(HttpsURLConnection.class);
    EasyMock.expect(connection.getResponseCode()).andStubReturn(HTTP_FORBIDDEN);
    connection.addRequestProperty(eq("Authorization"), capture(capturedAuth));
    EasyMock.expectLastCall();
    EasyMock.replay(connection);
    HttpDownloader downloader = getDownloader(connection);
    PasswordAuthentication credentials = new PasswordAuthentication("foo", "bar".toCharArray());
    boolean result = downloader.fetch(eventBus, new URI("https://example.com"), Optional.of(credentials), neverUsed);
    assertFalse(result);
    EasyMock.verify(connection);
    Matcher m = Pattern.compile("^Basic (.*)$").matcher(capturedAuth.getValue());
    assertTrue(m.matches());
    assertEquals("foo:bar", new String(BaseEncoding.base64().decode(m.group(1)), StandardCharsets.UTF_8));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) Matcher(java.util.regex.Matcher) URI(java.net.URI) PasswordAuthentication(java.net.PasswordAuthentication) Test(org.junit.Test)

Example 24 with PasswordAuthentication

use of java.net.PasswordAuthentication 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 25 with PasswordAuthentication

use of java.net.PasswordAuthentication 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)

Aggregations

PasswordAuthentication (java.net.PasswordAuthentication)40 Authenticator (java.net.Authenticator)14 URL (java.net.URL)11 InetSocketAddress (java.net.InetSocketAddress)9 Proxy (java.net.Proxy)8 HttpURLConnection (java.net.HttpURLConnection)4 InetAddress (java.net.InetAddress)4 MalformedURLException (java.net.MalformedURLException)4 URI (java.net.URI)4 IOException (java.io.IOException)3 ServerSocket (java.net.ServerSocket)3 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)3 Test (org.junit.Test)3 Dimension (java.awt.Dimension)2 Font (java.awt.Font)2 File (java.io.File)2 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 java.net (java.net)2 UnknownHostException (java.net.UnknownHostException)2