Search in sources :

Example 6 with BasicAuthenticator

use of com.burgstaller.okhttp.basic.BasicAuthenticator in project keepass2android by PhilippC.

the class WebDavStorage method getClient.

private OkHttpClient getClient(ConnectionInfo ci) throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException {
    OkHttpClient.Builder builder = new OkHttpClient.Builder();
    final Map<String, CachingAuthenticator> authCache = new ConcurrentHashMap<>();
    com.burgstaller.okhttp.digest.Credentials credentials = new com.burgstaller.okhttp.digest.Credentials(ci.username, ci.password);
    final BasicAuthenticator basicAuthenticator = new BasicAuthenticator(credentials);
    final DigestAuthenticator digestAuthenticator = new DigestAuthenticator(credentials);
    // note that all auth schemes should be registered as lowercase!
    DispatchingAuthenticator authenticator = new DispatchingAuthenticator.Builder().with("digest", digestAuthenticator).with("basic", basicAuthenticator).build();
    builder = builder.authenticator(new CachingAuthenticatorDecorator(authenticator, authCache)).addInterceptor(new AuthenticationCacheInterceptor(authCache));
    if ((mCertificateErrorHandler != null) && (!mCertificateErrorHandler.alwaysFailOnValidationError())) {
        TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        trustManagerFactory.init((KeyStore) null);
        TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
        if (trustManagers.length != 1 || !(trustManagers[0] instanceof X509TrustManager)) {
            throw new IllegalStateException("Unexpected default trust managers:" + Arrays.toString(trustManagers));
        }
        X509TrustManager trustManager = (X509TrustManager) trustManagers[0];
        trustManager = new DecoratedTrustManager(trustManager, mCertificateErrorHandler);
        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(null, new TrustManager[] { trustManager }, null);
        SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
        builder = builder.sslSocketFactory(sslSocketFactory, trustManager).hostnameVerifier(new DecoratedHostnameVerifier(OkHostnameVerifier.INSTANCE, mCertificateErrorHandler));
    }
    OkHttpClient client = builder.build();
    return client;
}
Also used : OkHttpClient(okhttp3.OkHttpClient) DispatchingAuthenticator(com.burgstaller.okhttp.DispatchingAuthenticator) BasicAuthenticator(com.burgstaller.okhttp.basic.BasicAuthenticator) AuthenticationCacheInterceptor(com.burgstaller.okhttp.AuthenticationCacheInterceptor) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) CachingAuthenticator(com.burgstaller.okhttp.digest.CachingAuthenticator) CachingAuthenticatorDecorator(com.burgstaller.okhttp.CachingAuthenticatorDecorator) SSLContext(javax.net.ssl.SSLContext) TrustManager(javax.net.ssl.TrustManager) DecoratedTrustManager(keepass2android.javafilestorage.webdav.DecoratedTrustManager) X509TrustManager(javax.net.ssl.X509TrustManager) DecoratedHostnameVerifier(keepass2android.javafilestorage.webdav.DecoratedHostnameVerifier) DecoratedTrustManager(keepass2android.javafilestorage.webdav.DecoratedTrustManager) DigestAuthenticator(com.burgstaller.okhttp.digest.DigestAuthenticator) X509TrustManager(javax.net.ssl.X509TrustManager) TrustManagerFactory(javax.net.ssl.TrustManagerFactory)

Example 7 with BasicAuthenticator

use of com.burgstaller.okhttp.basic.BasicAuthenticator in project okhttp-digest by rburgst.

the class ProxyAuthenticationManualTest method testConnection_WithProxyBasicAuthWithTunnel_Expect200.

@Test
public void testConnection_WithProxyBasicAuthWithTunnel_Expect200() throws IOException {
    final BasicAuthenticator authenticator = givenBasicAuthenticator();
    final OkHttpClient client = givenHttpClientWithProxyAuth(authenticator);
    final Request request = new Request.Builder().url("https://www.google.com/favicon.ico").build();
    Response response = client.newCall(request).execute();
    assertEquals(200, response.code());
}
Also used : Response(okhttp3.Response) OkHttpClient(okhttp3.OkHttpClient) BasicAuthenticator(com.burgstaller.okhttp.basic.BasicAuthenticator) Request(okhttp3.Request) Test(org.junit.Test)

Example 8 with BasicAuthenticator

use of com.burgstaller.okhttp.basic.BasicAuthenticator in project okhttp-digest by rburgst.

the class ProxyAuthenticationManualTest method testConnection_WithProxyBasicAuthWithNotAllowedSites_Expect403.

@Test
public void testConnection_WithProxyBasicAuthWithNotAllowedSites_Expect403() throws IOException {
    final BasicAuthenticator authenticator = givenBasicAuthenticator();
    final OkHttpClient client = givenHttpClientWithProxyAuth(authenticator);
    final Request request = new Request.Builder().url("http://www.youtube.com").build();
    Response response = client.newCall(request).execute();
    assertEquals(403, response.code());
}
Also used : Response(okhttp3.Response) OkHttpClient(okhttp3.OkHttpClient) BasicAuthenticator(com.burgstaller.okhttp.basic.BasicAuthenticator) Request(okhttp3.Request) Test(org.junit.Test)

Example 9 with BasicAuthenticator

use of com.burgstaller.okhttp.basic.BasicAuthenticator in project okhttp-digest by rburgst.

the class ProxyAuthenticationManualTest method testConnection_WithProxyBasicAuthWithoutTunnel_Expect200.

@Test
public void testConnection_WithProxyBasicAuthWithoutTunnel_Expect200() throws IOException {
    final BasicAuthenticator authenticator = givenBasicAuthenticator();
    final OkHttpClient client = givenHttpClientWithProxyAuth(authenticator);
    final Request request = new Request.Builder().url("http://edition.cnn.com").build();
    Response response = client.newCall(request).execute();
    assertEquals(200, response.code());
}
Also used : Response(okhttp3.Response) OkHttpClient(okhttp3.OkHttpClient) BasicAuthenticator(com.burgstaller.okhttp.basic.BasicAuthenticator) Request(okhttp3.Request) Test(org.junit.Test)

Aggregations

BasicAuthenticator (com.burgstaller.okhttp.basic.BasicAuthenticator)9 Request (okhttp3.Request)7 Credentials (com.burgstaller.okhttp.digest.Credentials)5 Response (okhttp3.Response)5 Test (org.junit.Test)5 DigestAuthenticator (com.burgstaller.okhttp.digest.DigestAuthenticator)4 OkHttpClient (okhttp3.OkHttpClient)4 AuthenticationCacheInterceptor (com.burgstaller.okhttp.AuthenticationCacheInterceptor)2 CachingAuthenticatorDecorator (com.burgstaller.okhttp.CachingAuthenticatorDecorator)2 DispatchingAuthenticator (com.burgstaller.okhttp.DispatchingAuthenticator)2 CachingAuthenticator (com.burgstaller.okhttp.digest.CachingAuthenticator)2 InetSocketAddress (java.net.InetSocketAddress)1 Proxy (java.net.Proxy)1 Type (java.net.Proxy.Type)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 SSLContext (javax.net.ssl.SSLContext)1 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)1 TrustManager (javax.net.ssl.TrustManager)1 TrustManagerFactory (javax.net.ssl.TrustManagerFactory)1 X509TrustManager (javax.net.ssl.X509TrustManager)1