Search in sources :

Example 1 with CachingAuthenticatorDecorator

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

use of com.burgstaller.okhttp.CachingAuthenticatorDecorator 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)

Example 3 with CachingAuthenticatorDecorator

use of com.burgstaller.okhttp.CachingAuthenticatorDecorator 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)

Example 4 with CachingAuthenticatorDecorator

use of com.burgstaller.okhttp.CachingAuthenticatorDecorator 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 5 with CachingAuthenticatorDecorator

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

the class BasicAuthenticatorWithMockWebserverTest method setUp.

@Before
public void setUp() throws Exception {
    credentials = new Credentials("user1", "user1");
    sut = new BasicAuthenticator(credentials);
    OkHttpClient.Builder builder = new OkHttpClient.Builder();
    final Map<String, CachingAuthenticator> authCache = new ConcurrentHashMap<>();
    HttpLoggingInterceptor logger = new HttpLoggingInterceptor(new StdOutLogger());
    logger.setLevel(HttpLoggingInterceptor.Level.HEADERS);
    spy = spy(sut);
    client = builder.authenticator(new CachingAuthenticatorDecorator(spy, authCache)).addInterceptor(new AuthenticationCacheInterceptor(authCache)).addNetworkInterceptor(logger).build();
    unauthorizedResponse = new MockResponse().setResponseCode(401).addHeader("WWW-Authenticate", "Basic realm=\"myrealm\"");
    successResponse = new MockResponse().setBody("OK");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) OkHttpClient(okhttp3.OkHttpClient) CachingAuthenticator(com.burgstaller.okhttp.digest.CachingAuthenticator) CachingAuthenticatorDecorator(com.burgstaller.okhttp.CachingAuthenticatorDecorator) AuthenticationCacheInterceptor(com.burgstaller.okhttp.AuthenticationCacheInterceptor) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Credentials(com.burgstaller.okhttp.digest.Credentials) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) Before(org.junit.Before)

Aggregations

AuthenticationCacheInterceptor (com.burgstaller.okhttp.AuthenticationCacheInterceptor)5 CachingAuthenticatorDecorator (com.burgstaller.okhttp.CachingAuthenticatorDecorator)5 CachingAuthenticator (com.burgstaller.okhttp.digest.CachingAuthenticator)4 DigestAuthenticator (com.burgstaller.okhttp.digest.DigestAuthenticator)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 DispatchingAuthenticator (com.burgstaller.okhttp.DispatchingAuthenticator)3 BasicAuthenticator (com.burgstaller.okhttp.basic.BasicAuthenticator)3 Credentials (com.burgstaller.okhttp.digest.Credentials)3 OkHttpClient (okhttp3.OkHttpClient)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 DecoratedTrustManager (keepass2android.javafilestorage.webdav.DecoratedTrustManager)1 CacheControl (okhttp3.CacheControl)1 Credentials (okhttp3.Credentials)1