use of com.burgstaller.okhttp.digest.CachingAuthenticator 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;
}
use of com.burgstaller.okhttp.digest.CachingAuthenticator in project okhttp-digest by rburgst.
the class AuthenticationCacheInterceptorTest method testCaching_withDifferentPorts.
@Test
public void testCaching_withDifferentPorts() throws Exception {
Map<String, CachingAuthenticator> authCache = new ConcurrentHashMap<>();
// Fill in authCache.
// https://myhost.com => basic auth user1:user1
givenCachedAuthenticationFor("https://myhost.com", authCache);
assertEquals(1, authCache.size());
Interceptor interceptor = new AuthenticationCacheInterceptor(authCache);
// Check that authenticator exists for https://myhost.com:443
final String authorization = whenInterceptAuthenticationForUrl(interceptor, "https://myhost.com:443");
thenAuthorizationHeaderShouldBePresent(authorization);
// Check that authenticator does not exist for http://myhost.com:8080
final String authorization2 = whenInterceptAuthenticationForUrl(interceptor, "http://myhost.com:8080");
thenNoAuthorizationHeaderShouldBePresent(authorization2);
}
use of com.burgstaller.okhttp.digest.CachingAuthenticator in project okhttp-digest by rburgst.
the class AuthenticationCacheInterceptorTest method testCaching_withExpiredAuthentication.
@Test
public void testCaching_withExpiredAuthentication() throws Exception {
Map<String, CachingAuthenticator> authCache = new ConcurrentHashMap<>();
final String dummyUrl = "https://myhost.com/path";
// Fill in authCache.
// https://myhost.com => basic auth user1:user1
givenCachedAuthenticationFor(dummyUrl, authCache);
assertThat(authCache).hasSize(1);
Interceptor interceptor = new AuthenticationCacheInterceptor(authCache);
// Check that unauthorized response (e.g. credentials changed or expired)
// removes cached authenticator
whenServerReturns401(dummyUrl, interceptor);
thenAuthCacheShouldBeEmpty(authCache);
}
use of com.burgstaller.okhttp.digest.CachingAuthenticator in project okhttp-digest by rburgst.
the class DispatchingAuthenticatorTest method testAuthenticateWithState__shouldCallAuthenticatorsInExpectedOrder.
/**
* Makes sure that in the case of cached authenticators the authenticators are called in the
* order in which they were registered.
*
* @throws Exception
*/
@Test
public void testAuthenticateWithState__shouldCallAuthenticatorsInExpectedOrder() throws Exception {
// given
CachingAuthenticator auth1 = mock(CachingAuthenticator.class);
CachingAuthenticator auth2 = mock(CachingAuthenticator.class);
DispatchingAuthenticator authenticator = new DispatchingAuthenticator.Builder().with("digest", auth1).with("basic", auth2).build();
Request request = createDummyRequest();
// make sure that the 2nd authenticator will not be called
given(auth2.authenticateWithState(eq(mockRoute), eq(request))).willThrow(IllegalStateException.class);
given(auth1.authenticateWithState(eq(mockRoute), eq(request))).willReturn(request);
// when
Request result = authenticator.authenticateWithState(mockRoute, request);
// then
assertEquals(request, result);
}
use of com.burgstaller.okhttp.digest.CachingAuthenticator 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");
}
Aggregations