Search in sources :

Example 11 with Authenticator

use of okhttp3.Authenticator in project okhttp by square.

the class URLConnectionTest method attemptAuthorization20Times.

@Test
public void attemptAuthorization20Times() throws Exception {
    for (int i = 0; i < 20; i++) {
        server.enqueue(new MockResponse().setResponseCode(401));
    }
    server.enqueue(new MockResponse().setBody("Success!"));
    String credential = Credentials.basic("jesse", "peanutbutter");
    urlFactory.setClient(urlFactory.client().newBuilder().authenticator(new RecordingOkAuthenticator(credential)).build());
    connection = urlFactory.open(server.url("/0").url());
    assertContent("Success!", connection);
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) RecordingOkAuthenticator(okhttp3.internal.RecordingOkAuthenticator) Test(org.junit.Test)

Example 12 with Authenticator

use of okhttp3.Authenticator in project okhttp by square.

the class CallTest method attemptAuthorization20Times.

@Test
public void attemptAuthorization20Times() throws Exception {
    for (int i = 0; i < 20; i++) {
        server.enqueue(new MockResponse().setResponseCode(401));
    }
    server.enqueue(new MockResponse().setBody("Success!"));
    String credential = Credentials.basic("jesse", "secret");
    client = client.newBuilder().authenticator(new RecordingOkAuthenticator(credential)).build();
    executeSynchronously("/").assertCode(200).assertBody("Success!");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) RecordingOkAuthenticator(okhttp3.internal.RecordingOkAuthenticator) Test(org.junit.Test)

Example 13 with Authenticator

use of okhttp3.Authenticator in project okhttp by square.

the class CallTest method postBodyRetransmittedAfterAuthorizationFail.

private void postBodyRetransmittedAfterAuthorizationFail(String body) throws Exception {
    server.enqueue(new MockResponse().setResponseCode(401));
    server.enqueue(new MockResponse());
    Request request = new Request.Builder().url(server.url("/")).method("POST", RequestBody.create(null, body)).build();
    String credential = Credentials.basic("jesse", "secret");
    client = client.newBuilder().authenticator(new RecordingOkAuthenticator(credential)).build();
    Response response = client.newCall(request).execute();
    assertEquals(200, response.code());
    response.body().close();
    RecordedRequest recordedRequest1 = server.takeRequest();
    assertEquals("POST", recordedRequest1.getMethod());
    assertEquals(body, recordedRequest1.getBody().readUtf8());
    assertNull(recordedRequest1.getHeader("Authorization"));
    RecordedRequest recordedRequest2 = server.takeRequest();
    assertEquals("POST", recordedRequest2.getMethod());
    assertEquals(body, recordedRequest2.getBody().readUtf8());
    assertEquals(credential, recordedRequest2.getHeader("Authorization"));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) RecordingOkAuthenticator(okhttp3.internal.RecordingOkAuthenticator) RecordedRequest(okhttp3.mockwebserver.RecordedRequest)

Example 14 with Authenticator

use of okhttp3.Authenticator in project okhttp by square.

the class CallTest method doesNotAttemptAuthorization21Times.

@Test
public void doesNotAttemptAuthorization21Times() throws Exception {
    for (int i = 0; i < 21; i++) {
        server.enqueue(new MockResponse().setResponseCode(401));
    }
    String credential = Credentials.basic("jesse", "secret");
    client = client.newBuilder().authenticator(new RecordingOkAuthenticator(credential)).build();
    try {
        client.newCall(new Request.Builder().url(server.url("/0")).build()).execute();
        fail();
    } catch (IOException expected) {
        assertEquals("Too many follow-up requests: 21", expected.getMessage());
    }
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) RecordingOkAuthenticator(okhttp3.internal.RecordingOkAuthenticator) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) Test(org.junit.Test)

Example 15 with Authenticator

use of okhttp3.Authenticator in project azure-sdk-for-java by Azure.

the class KeyVaultCredentials method applyCredentialsFilter.

@Override
public void applyCredentialsFilter(OkHttpClient.Builder clientBuilder) {
    clientBuilder.addInterceptor(new Interceptor() {

        @Override
        public Response intercept(Chain chain) throws IOException {
            HttpUrl url = chain.request().url();
            Map<String, String> challengeMap = cache.getCachedChallenge(url);
            if (challengeMap != null) {
                // Get the bearer token
                String credential = getAuthenticationCredentials(challengeMap);
                Request newRequest = chain.request().newBuilder().header(AUTHENTICATE, BEARER_TOKEP_REFIX + credential).build();
                return chain.proceed(newRequest);
            } else {
                // response
                return chain.proceed(chain.request());
            }
        }
    });
    // Caches the challenge for failed request and re-send the request with
    // access token.
    clientBuilder.authenticator(new Authenticator() {

        @Override
        public Request authenticate(Route route, Response response) throws IOException {
            // if challenge is not cached then extract and cache it
            String authenticateHeader = response.header(WWW_AUTHENTICATE);
            Map<String, String> challengeMap = extractChallenge(authenticateHeader, BEARER_TOKEP_REFIX);
            // Cache the challenge
            cache.addCachedChallenge(response.request().url(), challengeMap);
            // Get the bearer token from the callback by providing the
            // challenges
            String credential = getAuthenticationCredentials(challengeMap);
            if (credential == null) {
                return null;
            }
            // be cached anywhere in our code.
            return response.request().newBuilder().header(AUTHENTICATE, BEARER_TOKEP_REFIX + credential).build();
        }
    });
}
Also used : Response(okhttp3.Response) Request(okhttp3.Request) IOException(java.io.IOException) Interceptor(okhttp3.Interceptor) Map(java.util.Map) HashMap(java.util.HashMap) HttpUrl(okhttp3.HttpUrl) Authenticator(okhttp3.Authenticator) Route(okhttp3.Route)

Aggregations

MockResponse (okhttp3.mockwebserver.MockResponse)20 Test (org.junit.Test)20 RecordingOkAuthenticator (okhttp3.internal.RecordingOkAuthenticator)14 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)10 RecordingAuthenticator (okhttp3.internal.RecordingAuthenticator)6 IOException (java.io.IOException)4 OutputStream (java.io.OutputStream)3 InetAddress (java.net.InetAddress)3 InetSocketAddress (java.net.InetSocketAddress)3 SocketAddress (java.net.SocketAddress)3 Address (okhttp3.Address)3 Request (okhttp3.Request)3 Authenticator (okhttp3.Authenticator)2 Interceptor (okhttp3.Interceptor)2 Response (okhttp3.Response)2 Route (okhttp3.Route)2 RecordingProxySelector (okhttp3.internal.http.RecordingProxySelector)2 InterruptedIOException (java.io.InterruptedIOException)1 HttpRetryException (java.net.HttpRetryException)1 ProtocolException (java.net.ProtocolException)1