Search in sources :

Example 61 with Credentials

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());
}
Also used : OkHttpClient(okhttp3.OkHttpClient) IOException(java.io.IOException) Response(okhttp3.Response) Retrofit(retrofit2.Retrofit) Interceptor(okhttp3.Interceptor) TokenCredentials(com.microsoft.rest.credentials.TokenCredentials) Test(org.junit.Test)

Example 62 with Credentials

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);
}
Also used : Subscription(rx.Subscription) CompositeSubscription(rx.subscriptions.CompositeSubscription) ResponseBody(okhttp3.ResponseBody)

Example 63 with Credentials

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());
}
Also used : Response(okhttp3.Response) Request(okhttp3.Request) DownloadRequest(de.danoeh.antennapod.core.service.download.DownloadRequest) DownloadRequest(de.danoeh.antennapod.core.service.download.DownloadRequest) NonNull(androidx.annotation.NonNull)

Example 64 with Credentials

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

Example 65 with Credentials

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

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