Search in sources :

Example 1 with PasswordAuthentication

use of java.net.PasswordAuthentication in project che by eclipse.

the class ProxyAuthenticatorTest method shouldInitHttpsProxyAuthenticator.

@Test
public void shouldInitHttpsProxyAuthenticator() throws Exception {
    //when
    ProxyAuthenticator.initAuthenticator(HTTPS_URL);
    PasswordAuthentication testAuthentication = Authenticator.requestPasswordAuthentication(null, 0, null, null, null);
    //then
    assertEquals(testAuthentication.getUserName(), "user2");
    assertEquals(String.valueOf(testAuthentication.getPassword()), "paswd2");
    //when
    ProxyAuthenticator.resetAuthenticator();
    //then
    testAuthentication = Authenticator.requestPasswordAuthentication(null, 0, null, null, null);
    assertEquals(testAuthentication, null);
}
Also used : PasswordAuthentication(java.net.PasswordAuthentication) Test(org.testng.annotations.Test)

Example 2 with PasswordAuthentication

use of java.net.PasswordAuthentication in project che by eclipse.

the class ProxyAuthenticatorTest method shouldInitHttpProxyAuthenticator.

@Test
public void shouldInitHttpProxyAuthenticator() throws Exception {
    //when
    ProxyAuthenticator.initAuthenticator(HTTP_URL);
    //then
    PasswordAuthentication testAuthentication = Authenticator.requestPasswordAuthentication(null, 0, null, null, null);
    assertEquals(testAuthentication.getUserName(), "user1");
    assertEquals(String.valueOf(testAuthentication.getPassword()), "paswd1");
    //when
    ProxyAuthenticator.resetAuthenticator();
    //then
    testAuthentication = Authenticator.requestPasswordAuthentication(null, 0, null, null, null);
    assertEquals(testAuthentication, null);
}
Also used : PasswordAuthentication(java.net.PasswordAuthentication) Test(org.testng.annotations.Test)

Example 3 with PasswordAuthentication

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

the class HttpDownloader method fetch.

@Override
public boolean fetch(BuckEventBus eventBus, URI uri, Optional<PasswordAuthentication> authentication, Path output) throws IOException {
    if (!("https".equals(uri.getScheme()) || "http".equals(uri.getScheme()))) {
        return false;
    }
    DownloadEvent.Started started = DownloadEvent.started(uri);
    eventBus.post(started);
    try {
        HttpURLConnection connection = createConnection(uri);
        if (authentication.isPresent()) {
            if ("https".equals(uri.getScheme()) && connection instanceof HttpsURLConnection) {
                PasswordAuthentication p = authentication.get();
                String authStr = p.getUserName() + ":" + new String(p.getPassword());
                String authEncoded = BaseEncoding.base64().encode(authStr.getBytes(StandardCharsets.UTF_8));
                connection.addRequestProperty("Authorization", "Basic " + authEncoded);
            } else {
                LOG.info("Refusing to send basic authentication over plain http.");
                return false;
            }
        }
        if (HttpURLConnection.HTTP_OK != connection.getResponseCode()) {
            LOG.info("Unable to download %s: %s", uri, connection.getResponseMessage());
            return false;
        }
        long contentLength = connection.getContentLengthLong();
        try (InputStream is = new BufferedInputStream(connection.getInputStream());
            OutputStream os = new BufferedOutputStream(Files.newOutputStream(output))) {
            long read = 0;
            while (true) {
                int r = is.read();
                read++;
                if (r == -1) {
                    break;
                }
                if (read % PROGRESS_REPORT_EVERY_N_BYTES == 0) {
                    eventBus.post(new DownloadProgressEvent(uri, contentLength, read));
                }
                os.write(r);
            }
        }
        return true;
    } finally {
        eventBus.post(DownloadEvent.finished(started));
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) BufferedOutputStream(java.io.BufferedOutputStream) HttpURLConnection(java.net.HttpURLConnection) BufferedInputStream(java.io.BufferedInputStream) BufferedOutputStream(java.io.BufferedOutputStream) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) PasswordAuthentication(java.net.PasswordAuthentication)

Example 4 with PasswordAuthentication

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

the class DownloadConfig method getRepoCredentials.

public Optional<PasswordAuthentication> getRepoCredentials(String repo) {
    Optional<String> user = delegate.getValue("credentials", repo + "_user");
    Optional<String> password = delegate.getValue("credentials", repo + "_pass");
    if (!user.isPresent() || !password.isPresent()) {
        return Optional.empty();
    }
    return Optional.of(new PasswordAuthentication(user.get(), password.get().toCharArray()));
}
Also used : PasswordAuthentication(java.net.PasswordAuthentication)

Example 5 with PasswordAuthentication

use of java.net.PasswordAuthentication in project hudson-2.x by hudson.

the class Launcher method run.

public void run() throws Exception {
    if (auth != null) {
        final int idx = auth.indexOf(':');
        if (idx < 0)
            throw new CmdLineException(null, "No ':' in the -auth option");
        Authenticator.setDefault(new Authenticator() {

            @Override
            public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(auth.substring(0, idx), auth.substring(idx + 1).toCharArray());
            }
        });
    }
    if (connectionTarget != null) {
        runAsTcpClient();
        System.exit(0);
    } else if (slaveJnlpURL != null) {
        List<String> jnlpArgs = parseJnlpArguments();
        try {
            hudson.remoting.jnlp.Main._main(jnlpArgs.toArray(new String[jnlpArgs.size()]));
        } catch (CmdLineException e) {
            System.err.println("JNLP file " + slaveJnlpURL + " has invalid arguments: " + jnlpArgs);
            System.err.println("Most likely a configuration error in the master");
            System.err.println(e.getMessage());
            System.exit(1);
        }
    } else if (tcpPortFile != null) {
        runAsTcpServer();
        System.exit(0);
    } else {
        runWithStdinStdout();
        System.exit(0);
    }
}
Also used : ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) List(java.util.List) CmdLineException(org.kohsuke.args4j.CmdLineException) Authenticator(java.net.Authenticator) PasswordAuthentication(java.net.PasswordAuthentication)

Aggregations

PasswordAuthentication (java.net.PasswordAuthentication)100 Authenticator (java.net.Authenticator)46 URL (java.net.URL)27 InetSocketAddress (java.net.InetSocketAddress)20 Proxy (java.net.Proxy)17 Test (org.junit.Test)16 InetAddress (java.net.InetAddress)11 HttpURLConnection (java.net.HttpURLConnection)10 MalformedURLException (java.net.MalformedURLException)10 IOException (java.io.IOException)9 URI (java.net.URI)9 File (java.io.File)7 SocketAddress (java.net.SocketAddress)7 UnknownHostException (java.net.UnknownHostException)6 PrivilegedActionException (java.security.PrivilegedActionException)6 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)5 InputStream (java.io.InputStream)4 HttpRetryException (java.net.HttpRetryException)4 ProtocolException (java.net.ProtocolException)4 SocketTimeoutException (java.net.SocketTimeoutException)4