3

So in order to use digest authentication with OkHTTP I need to use the digest plugin or implement myself so naturally I am using the plugin. However the example usage on the repositories readme is for the previous version of OkHTTP and I cannot use the same code to implement it in version 3. I have tried to look at some of their tests (which use OkHTTP 3) to see how the code works but I am just not sure.

So how can I use this http digest plugin with the most recent version of OkHttp?

  • 1
    What makes you think it’s for the previous version of OkHttp? The code all imports from the `okhttp3` package. – Jesse Wilson Feb 04 '17 at 04:30
  • @JesseWilson you are right that the tests use okhttp3 however the readme example still uses the older methods from a prior version like client.setAuthentication . From the tests I am not sure what code in the most recent version replaces the no longer existing methods – user2844358 Feb 04 '17 at 04:52

1 Answers1

2
import com.burgstaller.okhttp.AuthenticationCacheInterceptor;
import com.burgstaller.okhttp.CachingAuthenticatorDecorator;
import com.burgstaller.okhttp.digest.CachingAuthenticator;
import com.burgstaller.okhttp.digest.Credentials;
import com.burgstaller.okhttp.digest.DigestAuthenticator;
import okhttp3.OkHttpClient;

private OkHttpClient buildHttpClient(String username, String password) {
    // Library used for digest authenticaton: https://github.com/rburgst/okhttp-digest
    var authenticator = new DigestAuthenticator(new Credentials(username, password));
    var authCache = new ConcurrentHashMap<String, CachingAuthenticator>();

    var decorator = new CachingAuthenticatorDecorator(authenticator, authCache);
    var interceptor = new AuthenticationCacheInterceptor(authCache);

    return httpClient.newBuilder()
        .authenticator(decorator)
        .addInterceptor(interceptor)
        .build();
}
Eduard Wirch
  • 9,785
  • 9
  • 61
  • 73