Search in sources :

Example 26 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 27 with PasswordAuthentication

use of java.net.PasswordAuthentication in project phonegap-facebook-plugin by Wizcorp.

the class HttpAuthenticator method getCredentials.

/**
   * Returns the authorization credentials that may satisfy the challenge.
   * Returns null if a challenge header was not provided or if credentials
   * were not available.
   */
private static String getCredentials(RawHeaders responseHeaders, String challengeHeader, Proxy proxy, URL url) throws IOException {
    List<Challenge> challenges = parseChallenges(responseHeaders, challengeHeader);
    if (challenges.isEmpty()) {
        return null;
    }
    for (Challenge challenge : challenges) {
        // Use the global authenticator to get the password.
        PasswordAuthentication auth;
        if (responseHeaders.getResponseCode() == HTTP_PROXY_AUTH) {
            InetSocketAddress proxyAddress = (InetSocketAddress) proxy.address();
            auth = Authenticator.requestPasswordAuthentication(proxyAddress.getHostName(), getConnectToInetAddress(proxy, url), proxyAddress.getPort(), url.getProtocol(), challenge.realm, challenge.scheme, url, Authenticator.RequestorType.PROXY);
        } else {
            auth = Authenticator.requestPasswordAuthentication(url.getHost(), getConnectToInetAddress(proxy, url), url.getPort(), url.getProtocol(), challenge.realm, challenge.scheme, url, Authenticator.RequestorType.SERVER);
        }
        if (auth == null) {
            continue;
        }
        // Use base64 to encode the username and password.
        String usernameAndPassword = auth.getUserName() + ":" + new String(auth.getPassword());
        byte[] bytes = usernameAndPassword.getBytes("ISO-8859-1");
        String encoded = Base64.encode(bytes);
        return challenge.scheme + " " + encoded;
    }
    return null;
}
Also used : InetSocketAddress(java.net.InetSocketAddress) PasswordAuthentication(java.net.PasswordAuthentication)

Example 28 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 29 with PasswordAuthentication

use of java.net.PasswordAuthentication in project cordova-android-chromeview by thedracle.

the class HttpAuthenticator method getCredentials.

/**
   * Returns the authorization credentials that may satisfy the challenge.
   * Returns null if a challenge header was not provided or if credentials
   * were not available.
   */
private static String getCredentials(RawHeaders responseHeaders, String challengeHeader, Proxy proxy, URL url) throws IOException {
    List<Challenge> challenges = parseChallenges(responseHeaders, challengeHeader);
    if (challenges.isEmpty()) {
        return null;
    }
    for (Challenge challenge : challenges) {
        // Use the global authenticator to get the password.
        PasswordAuthentication auth;
        if (responseHeaders.getResponseCode() == HTTP_PROXY_AUTH) {
            InetSocketAddress proxyAddress = (InetSocketAddress) proxy.address();
            auth = Authenticator.requestPasswordAuthentication(proxyAddress.getHostName(), getConnectToInetAddress(proxy, url), proxyAddress.getPort(), url.getProtocol(), challenge.realm, challenge.scheme, url, Authenticator.RequestorType.PROXY);
        } else {
            auth = Authenticator.requestPasswordAuthentication(url.getHost(), getConnectToInetAddress(proxy, url), url.getPort(), url.getProtocol(), challenge.realm, challenge.scheme, url, Authenticator.RequestorType.SERVER);
        }
        if (auth == null) {
            continue;
        }
        // Use base64 to encode the username and password.
        String usernameAndPassword = auth.getUserName() + ":" + new String(auth.getPassword());
        byte[] bytes = usernameAndPassword.getBytes("ISO-8859-1");
        String encoded = Base64.encode(bytes);
        return challenge.scheme + " " + encoded;
    }
    return null;
}
Also used : InetSocketAddress(java.net.InetSocketAddress) PasswordAuthentication(java.net.PasswordAuthentication)

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