Search in sources :

Example 1 with ProxyAuthenticator

use of org.apache.nifi.processors.standard.util.ProxyAuthenticator 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)

Aggregations

AuthenticationCacheInterceptor (com.burgstaller.okhttp.AuthenticationCacheInterceptor)1 CachingAuthenticatorDecorator (com.burgstaller.okhttp.CachingAuthenticatorDecorator)1 CachingAuthenticator (com.burgstaller.okhttp.digest.CachingAuthenticator)1 DigestAuthenticator (com.burgstaller.okhttp.digest.DigestAuthenticator)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Credentials (okhttp3.Credentials)1 ProxyAuthenticator (org.apache.nifi.processors.standard.util.ProxyAuthenticator)1