Search in sources :

Example 61 with Authenticator

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"));
}
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 62 with Authenticator

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

Example 63 with Authenticator

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);
}
Also used : Authenticator(okhttp3.Authenticator) CachingAuthenticator(com.burgstaller.okhttp.digest.CachingAuthenticator) Challenge(okhttp3.Challenge)

Example 64 with Authenticator

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;
}
Also used : Response(okhttp3.Response) Request(okhttp3.Request) CachingAuthenticator(com.burgstaller.okhttp.digest.CachingAuthenticator) Connection(okhttp3.Connection) HttpUrl(okhttp3.HttpUrl) Route(okhttp3.Route)

Example 65 with Authenticator

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

Aggregations

Test (org.junit.Test)37 Request (okhttp3.Request)27 OkHttpClient (okhttp3.OkHttpClient)26 Response (okhttp3.Response)24 MockResponse (okhttp3.mockwebserver.MockResponse)23 Authenticator (okhttp3.Authenticator)20 IOException (java.io.IOException)15 RecordingOkAuthenticator (okhttp3.internal.RecordingOkAuthenticator)15 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)13 Route (okhttp3.Route)12 BasicAuthenticator (com.burgstaller.okhttp.basic.BasicAuthenticator)9 DigestAuthenticator (com.burgstaller.okhttp.digest.DigestAuthenticator)8 CachingAuthenticator (com.burgstaller.okhttp.digest.CachingAuthenticator)7 InetSocketAddress (java.net.InetSocketAddress)7 RecordingAuthenticator (okhttp3.internal.RecordingAuthenticator)6 Credentials (com.burgstaller.okhttp.digest.Credentials)5 InetAddress (java.net.InetAddress)5 Proxy (java.net.Proxy)5 Interceptor (okhttp3.Interceptor)5 HttpLoggingInterceptor (okhttp3.logging.HttpLoggingInterceptor)5