use of com.burgstaller.okhttp.digest.CachingAuthenticator 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);
}
}
}
use of com.burgstaller.okhttp.digest.CachingAuthenticator in project okhttp-digest by rburgst.
the class AuthenticationCacheInterceptorTest method givenCachedAuthenticationFor.
private void givenCachedAuthenticationFor(String url, Map<String, CachingAuthenticator> authCache) throws IOException {
Authenticator decorator = new CachingAuthenticatorDecorator(new BasicAuthenticator(new Credentials("user1", "user1")), authCache);
Request dummyRequest = new Request.Builder().url(url).get().build();
Response response = new Response.Builder().request(dummyRequest).protocol(Protocol.HTTP_1_1).code(HTTP_UNAUTHORIZED).message("Unauthorized").header("WWW-Authenticate", "Basic realm=\"myrealm\"").build();
decorator.authenticate(null, response);
}
use of com.burgstaller.okhttp.digest.CachingAuthenticator in project okhttp-digest by rburgst.
the class AuthenticationCacheInterceptorTest method testCaching__whenNoConnectionExists__shouldNotBombOut.
@Test
public void testCaching__whenNoConnectionExists__shouldNotBombOut() throws IOException {
Map<String, CachingAuthenticator> authCache = new ConcurrentHashMap<>();
Interceptor interceptor = new AuthenticationCacheInterceptor(authCache);
String auth = whenInterceptAuthenticationForUrlWithNoConnection(interceptor, "https://myhost.com:443");
assertNull(auth);
}
use of com.burgstaller.okhttp.digest.CachingAuthenticator in project okhttp-digest by rburgst.
the class DispatchingAuthenticatorTest method createUnauthorizedServerResponse.
private Response createUnauthorizedServerResponse() throws IOException {
final Map<String, CachingAuthenticator> authCache = new ConcurrentHashMap<>();
Interceptor interceptor = new AuthenticationCacheInterceptor(authCache);
return interceptor.intercept(new ChainAdapter(createDummyRequest(), mockConnection) {
@Override
public Response proceed(Request request) throws IOException {
return new Response.Builder().body(ResponseBody.create(MediaType.parse("text/plain"), "Unauthorized")).request(request).protocol(Protocol.HTTP_1_1).code(HTTP_UNAUTHORIZED).message("Unauthorized").header("WWW-Authenticate", "Basic realm=\"myrealm\"").build();
}
});
}
use of com.burgstaller.okhttp.digest.CachingAuthenticator 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);
}
Aggregations