use of okhttp3.Credentials in project autorest-clientruntime-for-java by Azure.
the class CredentialsTests method tokenCredentialsTest.
@Test
public void tokenCredentialsTest() throws Exception {
TokenCredentials credentials = new TokenCredentials(null, "this_is_a_token");
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();
credentials.applyCredentialsFilter(clientBuilder);
clientBuilder.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
String header = chain.request().header("Authorization");
Assert.assertEquals("Bearer this_is_a_token", header);
return new Response.Builder().request(chain.request()).code(200).protocol(Protocol.HTTP_1_1).build();
}
});
ServiceClient serviceClient = new ServiceClient("http://localhost", clientBuilder, new Retrofit.Builder()) {
};
Response response = serviceClient.httpClient().newCall(new Request.Builder().url("http://localhost").build()).execute();
Assert.assertEquals(200, response.code());
}
use of okhttp3.Credentials in project xabber-android by redsolution.
the class BaseLoginActivity method bindSocial.
/**
* SOCIAL BIND / UNBIND
*/
protected void bindSocial(String provider, String credentials) {
showProgress(getResources().getString(R.string.progress_title_bind_social));
Subscription loginSocialSubscription = AuthManager.bindSocial(provider, credentials).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<ResponseBody>() {
@Override
public void call(ResponseBody s) {
hideProgress();
Toast.makeText(BaseLoginActivity.this, R.string.social_bind_success, Toast.LENGTH_SHORT).show();
synchronize(false);
}
}, new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
hideProgress();
Toast.makeText(BaseLoginActivity.this, R.string.social_bind_fail, Toast.LENGTH_SHORT).show();
}
});
compositeSubscription.add(loginSocialSubscription);
}
use of okhttp3.Credentials in project AntennaPod by AntennaPod.
the class BasicAuthorizationInterceptor method intercept.
@Override
@NonNull
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Response response = chain.proceed(request);
if (response.code() != HttpURLConnection.HTTP_UNAUTHORIZED) {
return response;
}
Request.Builder newRequest = request.newBuilder();
if (!TextUtils.equals(response.request().url().toString(), request.url().toString())) {
// Redirect detected. OkHTTP does not re-add the headers on redirect, so calling the new location directly.
newRequest.url(response.request().url());
List<String> authorizationHeaders = request.headers().values(HEADER_AUTHORIZATION);
if (!authorizationHeaders.isEmpty() && !TextUtils.isEmpty(authorizationHeaders.get(0))) {
// Call already had authorization headers. Try again with the same credentials.
newRequest.header(HEADER_AUTHORIZATION, authorizationHeaders.get(0));
return chain.proceed(newRequest.build());
}
}
String userInfo;
if (request.tag() instanceof DownloadRequest) {
DownloadRequest downloadRequest = (DownloadRequest) request.tag();
userInfo = URIUtil.getURIFromRequestUrl(downloadRequest.getSource()).getUserInfo();
if (TextUtils.isEmpty(userInfo)) {
userInfo = downloadRequest.getUsername() + ":" + downloadRequest.getPassword();
}
} else {
userInfo = DBReader.getImageAuthentication(request.url().toString());
}
if (TextUtils.isEmpty(userInfo)) {
Log.d(TAG, "no credentials for '" + request.url() + "'");
return response;
}
String[] parts = userInfo.split(":");
if (parts.length != 2) {
Log.d(TAG, "Invalid credentials for '" + request.url() + "'");
return response;
}
Log.d(TAG, "Authorization failed, re-trying with ISO-8859-1 encoded credentials");
String credentials = HttpDownloader.encodeCredentials(parts[0], parts[1], "ISO-8859-1");
newRequest.header(HEADER_AUTHORIZATION, credentials);
response = chain.proceed(newRequest.build());
if (response.code() != HttpURLConnection.HTTP_UNAUTHORIZED) {
return response;
}
Log.d(TAG, "Authorization failed, re-trying with UTF-8 encoded credentials");
credentials = HttpDownloader.encodeCredentials(parts[0], parts[1], "UTF-8");
newRequest.header(HEADER_AUTHORIZATION, credentials);
return chain.proceed(newRequest.build());
}
use of okhttp3.Credentials in project sonarqube by SonarSource.
the class HttpConnectorTest method use_basic_authentication_with_null_password.
@Test
public void use_basic_authentication_with_null_password() throws Exception {
answerHelloWorld();
underTest = HttpConnector.newBuilder().url(serverUrl).credentials("theLogin", null).build();
GetRequest request = new GetRequest("api/issues/search");
underTest.call(request);
RecordedRequest recordedRequest = server.takeRequest();
assertThat(recordedRequest.getHeader("Authorization")).isEqualTo(basic("theLogin", ""));
}
use of okhttp3.Credentials in project sonarqube by SonarSource.
the class HttpConnectorTest method use_basic_authentication_with_utf8_login_and_password.
@Test
public void use_basic_authentication_with_utf8_login_and_password() throws Exception {
answerHelloWorld();
String login = "ζθ½";
String password = "εδΈ";
underTest = HttpConnector.newBuilder().url(serverUrl).credentials(login, password).build();
GetRequest request = new GetRequest("api/issues/search");
underTest.call(request);
RecordedRequest recordedRequest = server.takeRequest();
// do not use OkHttp Credentials.basic() in order to not use the same code as the code under test
String expectedHeader = "Basic " + Base64.getEncoder().encodeToString((login + ":" + password).getBytes(UTF_8));
assertThat(recordedRequest.getHeader("Authorization")).isEqualTo(expectedHeader);
}
Aggregations