Search in sources :

Example 1 with Credentials

use of com.burgstaller.okhttp.digest.Credentials in project okhttp-digest by rburgst.

the class AuthenticationCacheInterceptorTest method givenCachedAuthenticationFor.

private void givenCachedAuthenticationFor(String url, Map<String, CachingAuthenticator> authCache) throws IOException {
    Authenticator decorator = new CachingAuthenticatorDecorator(new BasicAuthenticator(new Credentials("user1", "user1")), authCache);
    Request dummyRequest = new Request.Builder().url(url).get().build();
    Response response = new Response.Builder().request(dummyRequest).protocol(Protocol.HTTP_1_1).code(HTTP_UNAUTHORIZED).message("Unauthorized").header("WWW-Authenticate", "Basic realm=\"myrealm\"").build();
    decorator.authenticate(null, response);
}
Also used : Response(okhttp3.Response) BasicAuthenticator(com.burgstaller.okhttp.basic.BasicAuthenticator) Request(okhttp3.Request) Authenticator(okhttp3.Authenticator) CachingAuthenticator(com.burgstaller.okhttp.digest.CachingAuthenticator) BasicAuthenticator(com.burgstaller.okhttp.basic.BasicAuthenticator) Credentials(com.burgstaller.okhttp.digest.Credentials)

Example 2 with Credentials

use of com.burgstaller.okhttp.digest.Credentials in project okhttp-digest by rburgst.

the class DispatchingAuthenticatorTest method testCaching_withBasicAuthenticatorPreferredOrder.

@Test
public void testCaching_withBasicAuthenticatorPreferredOrder() throws Exception {
    final Credentials credentials = new Credentials("user", "pwd");
    final BasicAuthenticator basicAuthenticator = new BasicAuthenticator(credentials);
    final DigestAuthenticator digestAuthenticator = new DigestAuthenticator(credentials);
    DispatchingAuthenticator authenticator = new DispatchingAuthenticator.Builder().with("basic", basicAuthenticator).with("digest", digestAuthenticator).build();
    Request request = authenticator.authenticate(mockRoute, createUnauthorizedServerResponse());
    assertNotNull(request);
    request = authenticator.authenticateWithState(mockRoute, createDummyRequest());
    assertNotNull(request);
}
Also used : BasicAuthenticator(com.burgstaller.okhttp.basic.BasicAuthenticator) DigestAuthenticator(com.burgstaller.okhttp.digest.DigestAuthenticator) Request(okhttp3.Request) Credentials(com.burgstaller.okhttp.digest.Credentials) Test(org.junit.Test)

Example 3 with Credentials

use of com.burgstaller.okhttp.digest.Credentials in project okhttp-digest by rburgst.

the class DispatchingAuthenticatorTest method testCaching_withDigestAuthenticatorPreferredOrder.

@Test
public void testCaching_withDigestAuthenticatorPreferredOrder() throws Exception {
    final Credentials credentials = new Credentials("user", "pwd");
    final BasicAuthenticator basicAuthenticator = new BasicAuthenticator(credentials);
    final DigestAuthenticator digestAuthenticator = new DigestAuthenticator(credentials);
    DispatchingAuthenticator authenticator = new DispatchingAuthenticator.Builder().with("digest", digestAuthenticator).with("basic", basicAuthenticator).build();
    Request request = authenticator.authenticate(mockRoute, createUnauthorizedServerResponse());
    assertNotNull(request);
    String authorizationHeader = request.header("Authorization");
    assertThat(authorizationHeader, CoreMatchers.startsWith("Basic"));
    request = authenticator.authenticateWithState(mockRoute, createDummyRequest());
    assertNotNull(request);
}
Also used : BasicAuthenticator(com.burgstaller.okhttp.basic.BasicAuthenticator) DigestAuthenticator(com.burgstaller.okhttp.digest.DigestAuthenticator) Request(okhttp3.Request) Credentials(com.burgstaller.okhttp.digest.Credentials) Test(org.junit.Test)

Example 4 with Credentials

use of com.burgstaller.okhttp.digest.Credentials in project LibreraReader by foobnix.

the class OPDS method getHttpResponse.

public static String getHttpResponse(String url) throws IOException {
    Request request = // 
    new Request.Builder().header("User-Agent", USER_AGENT).cacheControl(// 
    new CacheControl.Builder().maxAge(10, // 
    TimeUnit.MINUTES).build()).url(// 
    url).build();
    Response response = // 
    client.newCall(// 
    request).execute();
    LOG.d("Header: >>", url);
    LOG.d("Header: Status code:", response.code());
    for (int i = 0; i < response.headers().size(); i++) {
        String name = response.headers().name(i);
        String value = response.headers().value(i);
        LOG.d("Header: ", name, value);
    }
    if (response.code() == 401 && TxtUtils.isEmpty(TempHolder.get().login)) {
        return CODE_401;
    } else {
        Credentials credentials = new Credentials(TempHolder.get().login, TempHolder.get().password);
        final BasicAuthenticator basicAuthenticator = new BasicAuthenticator(credentials);
        final DigestAuthenticator digestAuthenticator = new DigestAuthenticator(credentials);
        DispatchingAuthenticator authenticator = // 
        new DispatchingAuthenticator.Builder().with("digest", // 
        digestAuthenticator).with("basic", // 
        basicAuthenticator).build();
        client = // 
        builder.authenticator(// 
        new CachingAuthenticatorDecorator(authenticator, authCache)).addInterceptor(// 
        new AuthenticationCacheInterceptor(authCache)).build();
        response = client.newCall(request).execute();
        if (response.code() == 401) {
            return CODE_401;
        }
    }
    String string = response.body().string();
    return string;
}
Also used : Request(okhttp3.Request) CachingAuthenticatorDecorator(com.burgstaller.okhttp.CachingAuthenticatorDecorator) Response(okhttp3.Response) DispatchingAuthenticator(com.burgstaller.okhttp.DispatchingAuthenticator) BasicAuthenticator(com.burgstaller.okhttp.basic.BasicAuthenticator) DigestAuthenticator(com.burgstaller.okhttp.digest.DigestAuthenticator) AuthenticationCacheInterceptor(com.burgstaller.okhttp.AuthenticationCacheInterceptor) CacheControl(okhttp3.CacheControl) Credentials(com.burgstaller.okhttp.digest.Credentials)

Example 5 with Credentials

use of com.burgstaller.okhttp.digest.Credentials in project LibreraReader by foobnix.

the class OPDS method buildProxy.

public static void buildProxy() {
    new Thread(new Runnable() {

        @Override
        public void run() {
            if (AppState.get().proxyEnable && TxtUtils.isNotEmpty(AppState.get().proxyServer) && AppState.get().proxyPort != 0) {
                Type http = AppState.PROXY_SOCKS.equals(AppState.get().proxyType) ? Type.SOCKS : Type.HTTP;
                LOG.d("Proxy: Server", http.name(), AppState.get().proxyServer, AppState.get().proxyPort);
                builder.proxy(new Proxy(http, new InetSocketAddress(AppState.get().proxyServer, AppState.get().proxyPort)));
                if (TxtUtils.isNotEmpty(AppState.get().proxyUser)) {
                    LOG.d("Proxy: User", AppState.get().proxyUser, AppState.get().proxyPassword);
                    builder.proxyAuthenticator(new BasicAuthenticator(new Credentials(AppState.get().proxyUser, AppState.get().proxyPassword)));
                }
            } else {
                builder.proxy(null);
            }
            client = builder.build();
        }
    }).start();
}
Also used : Type(java.net.Proxy.Type) Proxy(java.net.Proxy) BasicAuthenticator(com.burgstaller.okhttp.basic.BasicAuthenticator) InetSocketAddress(java.net.InetSocketAddress) Credentials(com.burgstaller.okhttp.digest.Credentials)

Aggregations

Credentials (com.burgstaller.okhttp.digest.Credentials)6 BasicAuthenticator (com.burgstaller.okhttp.basic.BasicAuthenticator)5 Request (okhttp3.Request)4 DigestAuthenticator (com.burgstaller.okhttp.digest.DigestAuthenticator)3 AuthenticationCacheInterceptor (com.burgstaller.okhttp.AuthenticationCacheInterceptor)2 CachingAuthenticatorDecorator (com.burgstaller.okhttp.CachingAuthenticatorDecorator)2 CachingAuthenticator (com.burgstaller.okhttp.digest.CachingAuthenticator)2 Response (okhttp3.Response)2 Test (org.junit.Test)2 DispatchingAuthenticator (com.burgstaller.okhttp.DispatchingAuthenticator)1 InetSocketAddress (java.net.InetSocketAddress)1 Proxy (java.net.Proxy)1 Type (java.net.Proxy.Type)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Authenticator (okhttp3.Authenticator)1 CacheControl (okhttp3.CacheControl)1 OkHttpClient (okhttp3.OkHttpClient)1 HttpLoggingInterceptor (okhttp3.logging.HttpLoggingInterceptor)1 MockResponse (okhttp3.mockwebserver.MockResponse)1 Before (org.junit.Before)1