Search in sources :

Example 1 with CachingAuthenticator

use of com.burgstaller.okhttp.digest.CachingAuthenticator 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 CachingAuthenticator

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

the class AuthenticationCacheInterceptorTest method givenCachedAuthenticationFor.

private void givenCachedAuthenticationFor(String url, Map<String, CachingAuthenticator> authCache) throws IOException {
    Authenticator decorator = new CachingAuthenticatorDecorator(new BasicAuthenticator(new Credentials("user1", "user1")), authCache);
    Request dummyRequest = new Request.Builder().url(url).get().build();
    Response response = new Response.Builder().request(dummyRequest).protocol(Protocol.HTTP_1_1).code(HTTP_UNAUTHORIZED).message("Unauthorized").header("WWW-Authenticate", "Basic realm=\"myrealm\"").build();
    decorator.authenticate(null, response);
}
Also used : Response(okhttp3.Response) BasicAuthenticator(com.burgstaller.okhttp.basic.BasicAuthenticator) Request(okhttp3.Request) Authenticator(okhttp3.Authenticator) CachingAuthenticator(com.burgstaller.okhttp.digest.CachingAuthenticator) BasicAuthenticator(com.burgstaller.okhttp.basic.BasicAuthenticator) Credentials(com.burgstaller.okhttp.digest.Credentials)

Example 3 with CachingAuthenticator

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

the class AuthenticationCacheInterceptorTest method testCaching__whenNoConnectionExists__shouldNotBombOut.

@Test
public void testCaching__whenNoConnectionExists__shouldNotBombOut() throws IOException {
    Map<String, CachingAuthenticator> authCache = new ConcurrentHashMap<>();
    Interceptor interceptor = new AuthenticationCacheInterceptor(authCache);
    String auth = whenInterceptAuthenticationForUrlWithNoConnection(interceptor, "https://myhost.com:443");
    assertNull(auth);
}
Also used : CachingAuthenticator(com.burgstaller.okhttp.digest.CachingAuthenticator) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Interceptor(okhttp3.Interceptor) Test(org.junit.Test)

Example 4 with CachingAuthenticator

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

the class DispatchingAuthenticatorTest method createUnauthorizedServerResponse.

private Response createUnauthorizedServerResponse() throws IOException {
    final Map<String, CachingAuthenticator> authCache = new ConcurrentHashMap<>();
    Interceptor interceptor = new AuthenticationCacheInterceptor(authCache);
    return interceptor.intercept(new ChainAdapter(createDummyRequest(), mockConnection) {

        @Override
        public Response proceed(Request request) throws IOException {
            return new Response.Builder().body(ResponseBody.create(MediaType.parse("text/plain"), "Unauthorized")).request(request).protocol(Protocol.HTTP_1_1).code(HTTP_UNAUTHORIZED).message("Unauthorized").header("WWW-Authenticate", "Basic realm=\"myrealm\"").build();
        }
    });
}
Also used : Response(okhttp3.Response) CachingAuthenticator(com.burgstaller.okhttp.digest.CachingAuthenticator) Request(okhttp3.Request) IOException(java.io.IOException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Interceptor(okhttp3.Interceptor)

Example 5 with CachingAuthenticator

use of com.burgstaller.okhttp.digest.CachingAuthenticator in project collect by opendatakit.

the class OkHttpOpenRosaServerClientProvider method createNewClient.

@NonNull
private OkHttpOpenRosaServerClient createNewClient(String scheme, String userAgent, @Nullable HttpCredentialsInterface credentials) {
    OkHttpClient.Builder builder = baseClient.newBuilder().connectTimeout(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS).writeTimeout(WRITE_CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS).readTimeout(READ_CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS).followRedirects(true);
    // 7.0 and 7.1 (API 24/25) use network_security_config to get support.
    if (Build.VERSION.SDK_INT <= 23) {
        try {
            addTrustForLetsEncryptRoot(builder);
        } catch (CertificateException e) {
            Timber.w(e, "Failure attempting to add Let's Encrypt root");
        }
    }
    if (credentials != null) {
        Credentials cred = new Credentials(credentials.getUsername(), credentials.getPassword());
        DispatchingAuthenticator.Builder daBuilder = new DispatchingAuthenticator.Builder();
        daBuilder.with("digest", new DigestAuthenticator(cred));
        if (scheme.equalsIgnoreCase("https")) {
            daBuilder.with("basic", new BasicAuthenticator(cred));
        }
        DispatchingAuthenticator authenticator = daBuilder.build();
        ConcurrentHashMap<String, CachingAuthenticator> authCache = new ConcurrentHashMap<>();
        builder.authenticator(new CachingAuthenticatorDecorator(authenticator, authCache)).addInterceptor(new AuthenticationCacheInterceptor(authCache)).build();
    }
    return new OkHttpOpenRosaServerClient(builder.build(), userAgent);
}
Also used : OkHttpClient(okhttp3.OkHttpClient) CachingAuthenticator(com.burgstaller.okhttp.digest.CachingAuthenticator) CachingAuthenticatorDecorator(com.burgstaller.okhttp.CachingAuthenticatorDecorator) CertificateException(java.security.cert.CertificateException) DispatchingAuthenticator(com.burgstaller.okhttp.DispatchingAuthenticator) BasicAuthenticator(com.burgstaller.okhttp.basic.BasicAuthenticator) DigestAuthenticator(com.burgstaller.okhttp.digest.DigestAuthenticator) AuthenticationCacheInterceptor(com.burgstaller.okhttp.AuthenticationCacheInterceptor) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Credentials(com.burgstaller.okhttp.digest.Credentials) NonNull(androidx.annotation.NonNull)

Aggregations

CachingAuthenticator (com.burgstaller.okhttp.digest.CachingAuthenticator)13 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)9 Interceptor (okhttp3.Interceptor)5 Request (okhttp3.Request)5 Test (org.junit.Test)5 AuthenticationCacheInterceptor (com.burgstaller.okhttp.AuthenticationCacheInterceptor)4 CachingAuthenticatorDecorator (com.burgstaller.okhttp.CachingAuthenticatorDecorator)4 BasicAuthenticator (com.burgstaller.okhttp.basic.BasicAuthenticator)3 Credentials (com.burgstaller.okhttp.digest.Credentials)3 DigestAuthenticator (com.burgstaller.okhttp.digest.DigestAuthenticator)3 OkHttpClient (okhttp3.OkHttpClient)3 Response (okhttp3.Response)3 DispatchingAuthenticator (com.burgstaller.okhttp.DispatchingAuthenticator)2 HttpUrl (okhttp3.HttpUrl)2 NonNull (androidx.annotation.NonNull)1 IOException (java.io.IOException)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