Search in sources :

Example 56 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 57 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 58 with PasswordAuthentication

use of java.net.PasswordAuthentication in project okhttp by square.

the class JavaNetAuthenticator method authenticate.

@Override
public Request authenticate(Route route, Response response) throws IOException {
    List<Challenge> challenges = response.challenges();
    Request request = response.request();
    HttpUrl url = request.url();
    boolean proxyAuthorization = response.code() == 407;
    Proxy proxy = route.proxy();
    for (int i = 0, size = challenges.size(); i < size; i++) {
        Challenge challenge = challenges.get(i);
        if (!"Basic".equalsIgnoreCase(challenge.scheme()))
            continue;
        PasswordAuthentication auth;
        if (proxyAuthorization) {
            InetSocketAddress proxyAddress = (InetSocketAddress) proxy.address();
            auth = java.net.Authenticator.requestPasswordAuthentication(proxyAddress.getHostName(), getConnectToInetAddress(proxy, url), proxyAddress.getPort(), url.scheme(), challenge.realm(), challenge.scheme(), url.url(), RequestorType.PROXY);
        } else {
            auth = java.net.Authenticator.requestPasswordAuthentication(url.host(), getConnectToInetAddress(proxy, url), url.port(), url.scheme(), challenge.realm(), challenge.scheme(), url.url(), RequestorType.SERVER);
        }
        if (auth != null) {
            String credential = Credentials.basic(auth.getUserName(), new String(auth.getPassword()));
            return request.newBuilder().header(proxyAuthorization ? "Proxy-Authorization" : "Authorization", credential).build();
        }
    }
    // No challenges were satisfied!
    return null;
}
Also used : Proxy(java.net.Proxy) InetSocketAddress(java.net.InetSocketAddress) PasswordAuthentication(java.net.PasswordAuthentication)

Example 59 with PasswordAuthentication

use of java.net.PasswordAuthentication in project robovm by robovm.

the class OldPasswordAuthenticationTest method test_ConstructorLjava_lang_String$C.

public void test_ConstructorLjava_lang_String$C() {
    String name = "name";
    char[] password = "hunter2".toCharArray();
    try {
        new PasswordAuthentication(name, null);
        fail("NullPointerException was not thrown.");
    } catch (NullPointerException npe) {
    //expected
    }
    PasswordAuthentication pa = new PasswordAuthentication(null, password);
    assertNull(pa.getUserName());
    assertEquals(password.length, pa.getPassword().length);
}
Also used : PasswordAuthentication(java.net.PasswordAuthentication)

Example 60 with PasswordAuthentication

use of java.net.PasswordAuthentication in project XobotOS by xamarin.

the class HttpURLConnectionImpl method getAuthorizationCredentials.

/**
     * Returns the authorization credentials on the base of provided challenge.
     */
private String getAuthorizationCredentials(String challenge) throws IOException {
    int idx = challenge.indexOf(" ");
    if (idx == -1) {
        return null;
    }
    String scheme = challenge.substring(0, idx);
    int realm = challenge.indexOf("realm=\"") + 7;
    String prompt = null;
    if (realm != -1) {
        int end = challenge.indexOf('"', realm);
        if (end != -1) {
            prompt = challenge.substring(realm, end);
        }
    }
    // use the global authenticator to get the password
    PasswordAuthentication pa = Authenticator.requestPasswordAuthentication(getConnectToInetAddress(), getConnectToPort(), url.getProtocol(), prompt, scheme);
    if (pa == null) {
        return null;
    }
    // base64 encode the username and password
    String usernameAndPassword = pa.getUserName() + ":" + new String(pa.getPassword());
    byte[] bytes = usernameAndPassword.getBytes(Charsets.ISO_8859_1);
    String encoded = Base64.encode(bytes);
    return scheme + " " + encoded;
}
Also used : 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