Search in sources :

Example 71 with Credentials

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);
}
Also used : CachingAuthenticator(com.burgstaller.okhttp.digest.CachingAuthenticator) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Interceptor(okhttp3.Interceptor) Test(org.junit.Test)

Example 72 with Credentials

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");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) OkHttpClient(okhttp3.OkHttpClient) CachingAuthenticator(com.burgstaller.okhttp.digest.CachingAuthenticator) CachingAuthenticatorDecorator(com.burgstaller.okhttp.CachingAuthenticatorDecorator) AuthenticationCacheInterceptor(com.burgstaller.okhttp.AuthenticationCacheInterceptor) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Credentials(com.burgstaller.okhttp.digest.Credentials) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) Before(org.junit.Before)

Example 73 with Credentials

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"));
}
Also used : ProxySelector(java.net.ProxySelector) Proxy(java.net.Proxy) Address(okhttp3.Address) InetSocketAddress(java.net.InetSocketAddress) SocketFactory(javax.net.SocketFactory) InetSocketAddress(java.net.InetSocketAddress) Connection(okhttp3.Connection) Dns(okhttp3.Dns) Authenticator(okhttp3.Authenticator) Route(okhttp3.Route) Before(org.junit.Before)

Example 74 with Credentials

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);
}
Also used : Request(okhttp3.Request) Test(org.junit.Test)

Example 75 with Credentials

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);
}
Also used : Response(okhttp3.Response) OkHttpClient(okhttp3.OkHttpClient) Authenticator(okhttp3.Authenticator) Route(okhttp3.Route)

Aggregations

Request (okhttp3.Request)39 Response (okhttp3.Response)29 OkHttpClient (okhttp3.OkHttpClient)22 IOException (java.io.IOException)20 Test (org.junit.Test)16 HttpUrl (okhttp3.HttpUrl)10 RequestBody (okhttp3.RequestBody)9 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)9 ResponseBody (okhttp3.ResponseBody)8 NonNull (androidx.annotation.NonNull)7 MockResponse (okhttp3.mockwebserver.MockResponse)7 BasicAuthenticator (com.burgstaller.okhttp.basic.BasicAuthenticator)6 CachingAuthenticator (com.burgstaller.okhttp.digest.CachingAuthenticator)6 Credentials (com.burgstaller.okhttp.digest.Credentials)6 DigestAuthenticator (com.burgstaller.okhttp.digest.DigestAuthenticator)6 Observable (rx.Observable)6 AuthenticationCacheInterceptor (com.burgstaller.okhttp.AuthenticationCacheInterceptor)5 CachingAuthenticatorDecorator (com.burgstaller.okhttp.CachingAuthenticatorDecorator)5 PokemonGo (com.pokegoapi.api.PokemonGo)5 PtcCredentialProvider (com.pokegoapi.auth.PtcCredentialProvider)5