Search in sources :

Example 11 with CachingAuthenticator

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

the class AuthenticationCacheInterceptorTest method testCaching__whenNoConnectionExistsButCachedInfo__shouldNotBombOut.

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

Example 12 with CachingAuthenticator

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

the class AuthenticationCacheInterceptor method intercept.

@Override
public Response intercept(Chain chain) throws IOException {
    final Request request = chain.request();
    final HttpUrl url = request.url();
    final String key = CachingUtils.getCachingKey(url);
    CachingAuthenticator authenticator = authCache.get(key);
    Request authRequest = null;
    Connection connection = chain.connection();
    Route route = connection != null ? connection.route() : null;
    if (authenticator != null) {
        authRequest = authenticator.authenticateWithState(route, request);
    }
    if (authRequest == null) {
        authRequest = request;
    }
    Response response = chain.proceed(authRequest);
    // Cached response was used, but it produced unauthorized response (cache expired).
    int responseCode = response != null ? response.code() : 0;
    if (authenticator != null && (responseCode == HTTP_UNAUTHORIZED || responseCode == HTTP_PROXY_AUTH)) {
        // Remove cached authenticator and resend request
        if (authCache.remove(key) != null) {
            response.body().close();
            Platform.get().log(Platform.INFO, "Cached authentication expired. Sending a new request.", null);
            // Force sending a new request without "Authorization" header
            response = chain.proceed(request);
        }
    }
    return response;
}
Also used : Response(okhttp3.Response) Request(okhttp3.Request) CachingAuthenticator(com.burgstaller.okhttp.digest.CachingAuthenticator) Connection(okhttp3.Connection) HttpUrl(okhttp3.HttpUrl) Route(okhttp3.Route)

Example 13 with CachingAuthenticator

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

the class CachingAuthenticatorDecorator method authenticate.

@Override
public Request authenticate(Route route, Response response) throws IOException {
    Request authenticated = innerAuthenticator.authenticate(route, response);
    if (authenticated != null) {
        String authorizationValue = authenticated.header("Authorization");
        if (authorizationValue != null && innerAuthenticator instanceof CachingAuthenticator) {
            final HttpUrl url = authenticated.url();
            final String key = CachingUtils.getCachingKey(url);
            authCache.put(key, (CachingAuthenticator) innerAuthenticator);
        }
    }
    return authenticated;
}
Also used : Request(okhttp3.Request) CachingAuthenticator(com.burgstaller.okhttp.digest.CachingAuthenticator) HttpUrl(okhttp3.HttpUrl)

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