use of okhttp3.Credentials 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 okhttp3.Credentials 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");
}
use of okhttp3.Credentials 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.Credentials 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.Credentials 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