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);
}
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!");
}
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"));
}
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());
}
}
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();
}
});
}
Aggregations