Search in sources :

Example 1 with DigestAuthenticator

use of com.burgstaller.okhttp.digest.DigestAuthenticator in project nifi by apache.

the class InvokeHTTP method setAuthenticator.

private void setAuthenticator(OkHttpClient.Builder okHttpClientBuilder, ProcessContext context) {
    final String authUser = trimToEmpty(context.getProperty(PROP_BASIC_AUTH_USERNAME).getValue());
    final String proxyUsername = trimToEmpty(context.getProperty(PROP_PROXY_USER).evaluateAttributeExpressions().getValue());
    // If the username/password properties are set then check if digest auth is being used
    if (!authUser.isEmpty() && "true".equalsIgnoreCase(context.getProperty(PROP_DIGEST_AUTH).getValue())) {
        final String authPass = trimToEmpty(context.getProperty(PROP_BASIC_AUTH_PASSWORD).getValue());
        /*
             * OkHttp doesn't have built-in Digest Auth Support. A ticket for adding it is here[1] but they authors decided instead to rely on a 3rd party lib.
             *
             * [1] https://github.com/square/okhttp/issues/205#issuecomment-154047052
             */
        final Map<String, CachingAuthenticator> authCache = new ConcurrentHashMap<>();
        com.burgstaller.okhttp.digest.Credentials credentials = new com.burgstaller.okhttp.digest.Credentials(authUser, authPass);
        final DigestAuthenticator digestAuthenticator = new DigestAuthenticator(credentials);
        if (!proxyUsername.isEmpty()) {
            final String proxyPassword = context.getProperty(PROP_PROXY_PASSWORD).evaluateAttributeExpressions().getValue();
            ProxyAuthenticator proxyAuthenticator = new ProxyAuthenticator(proxyUsername, proxyPassword);
            okHttpClientBuilder.proxyAuthenticator(proxyAuthenticator);
        }
        okHttpClientBuilder.interceptors().add(new AuthenticationCacheInterceptor(authCache));
        okHttpClientBuilder.authenticator(new CachingAuthenticatorDecorator(digestAuthenticator, authCache));
    } else {
        // Add proxy authentication only
        if (!proxyUsername.isEmpty()) {
            final String proxyPassword = context.getProperty(PROP_PROXY_PASSWORD).evaluateAttributeExpressions().getValue();
            ProxyAuthenticator proxyAuthenticator = new ProxyAuthenticator(proxyUsername, proxyPassword);
            okHttpClientBuilder.proxyAuthenticator(proxyAuthenticator);
        }
    }
}
Also used : CachingAuthenticator(com.burgstaller.okhttp.digest.CachingAuthenticator) CachingAuthenticatorDecorator(com.burgstaller.okhttp.CachingAuthenticatorDecorator) ProxyAuthenticator(org.apache.nifi.processors.standard.util.ProxyAuthenticator) DigestAuthenticator(com.burgstaller.okhttp.digest.DigestAuthenticator) AuthenticationCacheInterceptor(com.burgstaller.okhttp.AuthenticationCacheInterceptor) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Credentials(okhttp3.Credentials)

Example 2 with DigestAuthenticator

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

the class DispatchingAuthenticatorTest method testCaching_withBasicAuthenticatorPreferredOrder.

@Test
public void testCaching_withBasicAuthenticatorPreferredOrder() throws Exception {
    final Credentials credentials = new Credentials("user", "pwd");
    final BasicAuthenticator basicAuthenticator = new BasicAuthenticator(credentials);
    final DigestAuthenticator digestAuthenticator = new DigestAuthenticator(credentials);
    DispatchingAuthenticator authenticator = new DispatchingAuthenticator.Builder().with("basic", basicAuthenticator).with("digest", digestAuthenticator).build();
    Request request = authenticator.authenticate(mockRoute, createUnauthorizedServerResponse());
    assertNotNull(request);
    request = authenticator.authenticateWithState(mockRoute, createDummyRequest());
    assertNotNull(request);
}
Also used : BasicAuthenticator(com.burgstaller.okhttp.basic.BasicAuthenticator) DigestAuthenticator(com.burgstaller.okhttp.digest.DigestAuthenticator) Request(okhttp3.Request) Credentials(com.burgstaller.okhttp.digest.Credentials) Test(org.junit.Test)

Example 3 with DigestAuthenticator

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

the class DispatchingAuthenticatorTest method testCaching_withDigestAuthenticatorPreferredOrder.

@Test
public void testCaching_withDigestAuthenticatorPreferredOrder() throws Exception {
    final Credentials credentials = new Credentials("user", "pwd");
    final BasicAuthenticator basicAuthenticator = new BasicAuthenticator(credentials);
    final DigestAuthenticator digestAuthenticator = new DigestAuthenticator(credentials);
    DispatchingAuthenticator authenticator = new DispatchingAuthenticator.Builder().with("digest", digestAuthenticator).with("basic", basicAuthenticator).build();
    Request request = authenticator.authenticate(mockRoute, createUnauthorizedServerResponse());
    assertNotNull(request);
    String authorizationHeader = request.header("Authorization");
    assertThat(authorizationHeader, CoreMatchers.startsWith("Basic"));
    request = authenticator.authenticateWithState(mockRoute, createDummyRequest());
    assertNotNull(request);
}
Also used : BasicAuthenticator(com.burgstaller.okhttp.basic.BasicAuthenticator) DigestAuthenticator(com.burgstaller.okhttp.digest.DigestAuthenticator) Request(okhttp3.Request) Credentials(com.burgstaller.okhttp.digest.Credentials) Test(org.junit.Test)

Example 4 with DigestAuthenticator

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

the class ProxyAuthenticationManualTest method testConnection_WithProxyDigestAuthWithNotAllowdSites_Expect403.

@Test
public void testConnection_WithProxyDigestAuthWithNotAllowdSites_Expect403() throws IOException {
    final DigestAuthenticator authenticator = givenDigestAuthenticator();
    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) DigestAuthenticator(com.burgstaller.okhttp.digest.DigestAuthenticator) Request(okhttp3.Request) Test(org.junit.Test)

Example 5 with DigestAuthenticator

use of com.burgstaller.okhttp.digest.DigestAuthenticator in project LibreraReader by foobnix.

the class OPDS method getHttpResponse.

public static String getHttpResponse(String url) throws IOException {
    Request request = // 
    new Request.Builder().header("User-Agent", USER_AGENT).cacheControl(// 
    new CacheControl.Builder().maxAge(10, // 
    TimeUnit.MINUTES).build()).url(// 
    url).build();
    Response response = // 
    client.newCall(// 
    request).execute();
    LOG.d("Header: >>", url);
    LOG.d("Header: Status code:", response.code());
    for (int i = 0; i < response.headers().size(); i++) {
        String name = response.headers().name(i);
        String value = response.headers().value(i);
        LOG.d("Header: ", name, value);
    }
    if (response.code() == 401 && TxtUtils.isEmpty(TempHolder.get().login)) {
        return CODE_401;
    } else {
        Credentials credentials = new Credentials(TempHolder.get().login, TempHolder.get().password);
        final BasicAuthenticator basicAuthenticator = new BasicAuthenticator(credentials);
        final DigestAuthenticator digestAuthenticator = new DigestAuthenticator(credentials);
        DispatchingAuthenticator authenticator = // 
        new DispatchingAuthenticator.Builder().with("digest", // 
        digestAuthenticator).with("basic", // 
        basicAuthenticator).build();
        client = // 
        builder.authenticator(// 
        new CachingAuthenticatorDecorator(authenticator, authCache)).addInterceptor(// 
        new AuthenticationCacheInterceptor(authCache)).build();
        response = client.newCall(request).execute();
        if (response.code() == 401) {
            return CODE_401;
        }
    }
    String string = response.body().string();
    return string;
}
Also used : Request(okhttp3.Request) CachingAuthenticatorDecorator(com.burgstaller.okhttp.CachingAuthenticatorDecorator) Response(okhttp3.Response) DispatchingAuthenticator(com.burgstaller.okhttp.DispatchingAuthenticator) BasicAuthenticator(com.burgstaller.okhttp.basic.BasicAuthenticator) DigestAuthenticator(com.burgstaller.okhttp.digest.DigestAuthenticator) AuthenticationCacheInterceptor(com.burgstaller.okhttp.AuthenticationCacheInterceptor) CacheControl(okhttp3.CacheControl) Credentials(com.burgstaller.okhttp.digest.Credentials)

Aggregations

DigestAuthenticator (com.burgstaller.okhttp.digest.DigestAuthenticator)9 Request (okhttp3.Request)6 BasicAuthenticator (com.burgstaller.okhttp.basic.BasicAuthenticator)5 OkHttpClient (okhttp3.OkHttpClient)5 Test (org.junit.Test)5 AuthenticationCacheInterceptor (com.burgstaller.okhttp.AuthenticationCacheInterceptor)4 CachingAuthenticatorDecorator (com.burgstaller.okhttp.CachingAuthenticatorDecorator)4 Credentials (com.burgstaller.okhttp.digest.Credentials)4 Response (okhttp3.Response)4 DispatchingAuthenticator (com.burgstaller.okhttp.DispatchingAuthenticator)3 CachingAuthenticator (com.burgstaller.okhttp.digest.CachingAuthenticator)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 NonNull (androidx.annotation.NonNull)1 CertificateException (java.security.cert.CertificateException)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 DecoratedHostnameVerifier (keepass2android.javafilestorage.webdav.DecoratedHostnameVerifier)1