use of com.burgstaller.okhttp.digest.DigestAuthenticator 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.DigestAuthenticator in project okhttp-digest by rburgst.
the class DispatchingAuthenticatorTest method testCaching_withBasicAuthenticatorPreferredOrder.
@Test
public void testCaching_withBasicAuthenticatorPreferredOrder() throws Exception {
final Credentials credentials = new Credentials("user", "pwd");
final BasicAuthenticator basicAuthenticator = new BasicAuthenticator(credentials);
final DigestAuthenticator digestAuthenticator = new DigestAuthenticator(credentials);
DispatchingAuthenticator authenticator = new DispatchingAuthenticator.Builder().with("basic", basicAuthenticator).with("digest", digestAuthenticator).build();
Request request = authenticator.authenticate(mockRoute, createUnauthorizedServerResponse());
assertNotNull(request);
request = authenticator.authenticateWithState(mockRoute, createDummyRequest());
assertNotNull(request);
}
use of com.burgstaller.okhttp.digest.DigestAuthenticator in project okhttp-digest by rburgst.
the class DispatchingAuthenticatorTest method testCaching_withDigestAuthenticatorPreferredOrder.
@Test
public void testCaching_withDigestAuthenticatorPreferredOrder() throws Exception {
final Credentials credentials = new Credentials("user", "pwd");
final BasicAuthenticator basicAuthenticator = new BasicAuthenticator(credentials);
final DigestAuthenticator digestAuthenticator = new DigestAuthenticator(credentials);
DispatchingAuthenticator authenticator = new DispatchingAuthenticator.Builder().with("digest", digestAuthenticator).with("basic", basicAuthenticator).build();
Request request = authenticator.authenticate(mockRoute, createUnauthorizedServerResponse());
assertNotNull(request);
String authorizationHeader = request.header("Authorization");
assertThat(authorizationHeader, CoreMatchers.startsWith("Basic"));
request = authenticator.authenticateWithState(mockRoute, createDummyRequest());
assertNotNull(request);
}
use of com.burgstaller.okhttp.digest.DigestAuthenticator in project okhttp-digest by rburgst.
the class ProxyAuthenticationManualTest method testConnection_WithProxyDigestAuthWithNotAllowdSites_Expect403.
@Test
public void testConnection_WithProxyDigestAuthWithNotAllowdSites_Expect403() throws IOException {
final DigestAuthenticator authenticator = givenDigestAuthenticator();
final OkHttpClient client = givenHttpClientWithProxyAuth(authenticator);
final Request request = new Request.Builder().url("http://www.youtube.com").build();
Response response = client.newCall(request).execute();
assertEquals(403, response.code());
}
use of com.burgstaller.okhttp.digest.DigestAuthenticator 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;
}
Aggregations