use of okhttp3.Authenticator in project okhttp-digest by rburgst.
the class DigestAuthenticatorTest method beforeMethod.
@Before
public void beforeMethod() {
Connection mockConnection = mock(Connection.class);
Dns mockDns = mock(Dns.class);
SocketFactory socketFactory = mock(SocketFactory.class);
Authenticator proxyAuthenticator = mock(Authenticator.class);
ProxySelector proxySelector = mock(ProxySelector.class);
Proxy proxy = mock(Proxy.class);
// setup some dummy data so that we dont get NPEs
Address address = new Address("localhost", 8080, mockDns, socketFactory, null, null, null, proxyAuthenticator, null, Collections.singletonList(Protocol.HTTP_1_1), Collections.singletonList(ConnectionSpec.MODERN_TLS), proxySelector);
InetSocketAddress inetSocketAddress = new InetSocketAddress("localhost", 8080);
mockRoute = new Route(address, proxy, inetSocketAddress);
given(mockConnection.route()).willReturn(mockRoute);
authenticator = new DigestAuthenticator(new Credentials("user1", "user1"));
}
use of okhttp3.Authenticator in project okhttp-digest by rburgst.
the class DigestAuthenticatorTest method testWWWAuthenticateWithState__whenNoInitialStateWasGiven__shouldNotThrowException.
/**
* Tests a case where the digest authenticator is used in tandem with another authenticator and
* DispatchingAuthenticator will call authenticateWithState on all registered authenticators
* even when they dont have an initial state.
*/
@Test
public void testWWWAuthenticateWithState__whenNoInitialStateWasGiven__shouldNotThrowException() throws Exception {
Request secondRequest = new Request.Builder().url("http://www.google.com/account").get().build();
CachingAuthenticator localAuthenticator = new DigestAuthenticator(new Credentials("user1", "user1"));
Request authenticatedRequest = localAuthenticator.authenticateWithState(mockRoute, secondRequest);
assertNull(authenticatedRequest);
}
use of okhttp3.Authenticator in project okhttp-digest by rburgst.
the class DispatchingAuthenticator method authenticate.
@Override
public Request authenticate(Route route, Response response) throws IOException {
List<Challenge> challenges = response.challenges();
if (!challenges.isEmpty()) {
for (Challenge challenge : challenges) {
final String scheme = challenge.scheme();
Authenticator authenticator = null;
if (scheme != null) {
authenticator = authenticatorRegistry.get(scheme.toLowerCase(Locale.getDefault()));
}
if (authenticator != null) {
return authenticator.authenticate(route, response);
}
}
}
throw new IllegalArgumentException("unsupported auth scheme " + challenges);
}
use of okhttp3.Authenticator 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;
}
use of okhttp3.Authenticator in project TeamCityApp by vase4kin.
the class CreateAccountDataManagerImpl method authUser.
/**
* {@inheritDoc}
*/
@Override
public void authUser(@NonNull final CustomOnLoadingListener<String> listener, final String url, final String userName, final String password) {
// Creating okHttpClient with authenticator
OkHttpClient okHttpClient = mOkHttpClient.newBuilder().authenticator(new Authenticator() {
@Override
public Request authenticate(Route route, Response response) throws IOException {
String credential = Credentials.basic(userName, password);
if (credential.equals(response.request().header(TeamCityService.AUTHORIZATION))) {
// If we already failed with these credentials, don't retry.
return null;
}
return response.request().newBuilder().header(TeamCityService.AUTHORIZATION, credential).build();
}
}).build();
// Handling request
handleAuthRequest(url, AUTH_URL, okHttpClient, listener);
}
Aggregations