Search in sources :

Example 1 with ProxyOptions

use of com.auth0.client.ProxyOptions in project auth0-java by auth0.

the class ManagementAPI method buildNetworkingClient.

/**
 * Given a set of options, it creates a new instance of the {@link OkHttpClient}
 * configuring them according to their availability.
 *
 * @param options the options to set to the client.
 * @return a new networking client instance configured as requested.
 */
private OkHttpClient buildNetworkingClient(HttpOptions options) {
    OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();
    final ProxyOptions proxyOptions = options.getProxyOptions();
    if (proxyOptions != null) {
        // Set proxy
        clientBuilder.proxy(proxyOptions.getProxy());
        // Set authentication, if present
        final String proxyAuth = proxyOptions.getBasicAuthentication();
        if (proxyAuth != null) {
            clientBuilder.proxyAuthenticator(new Authenticator() {

                private static final String PROXY_AUTHORIZATION_HEADER = "Proxy-Authorization";

                @Override
                public okhttp3.Request authenticate(Route route, Response response) throws IOException {
                    if (response.request().header(PROXY_AUTHORIZATION_HEADER) != null) {
                        return null;
                    }
                    return response.request().newBuilder().header(PROXY_AUTHORIZATION_HEADER, proxyAuth).build();
                }
            });
        }
    }
    configureLogging(options.getLoggingOptions());
    Dispatcher dispatcher = new Dispatcher();
    dispatcher.setMaxRequestsPerHost(options.getMaxRequestsPerHost());
    dispatcher.setMaxRequests(options.getMaxRequests());
    return clientBuilder.addInterceptor(logging).addInterceptor(telemetry).addInterceptor(new RateLimitInterceptor(options.getManagementAPIMaxRetries())).connectTimeout(options.getConnectTimeout(), TimeUnit.SECONDS).readTimeout(options.getReadTimeout(), TimeUnit.SECONDS).dispatcher(dispatcher).build();
}
Also used : ProxyOptions(com.auth0.client.ProxyOptions) IOException(java.io.IOException) RateLimitInterceptor(com.auth0.net.RateLimitInterceptor)

Example 2 with ProxyOptions

use of com.auth0.client.ProxyOptions in project auth0-java by auth0.

the class ManagementAPITest method shouldUseProxy.

@Test
public void shouldUseProxy() throws Exception {
    Proxy proxy = Mockito.mock(Proxy.class);
    ProxyOptions proxyOptions = new ProxyOptions(proxy);
    HttpOptions httpOptions = new HttpOptions();
    httpOptions.setProxyOptions(proxyOptions);
    ManagementAPI api = new ManagementAPI(DOMAIN, API_TOKEN, httpOptions);
    assertThat(api.getClient().proxy(), is(proxy));
    Authenticator authenticator = api.getClient().proxyAuthenticator();
    assertThat(authenticator, is(notNullValue()));
    Route route = Mockito.mock(Route.class);
    okhttp3.Request nonAuthenticatedRequest = new okhttp3.Request.Builder().url("https://test.com/app").addHeader("some-header", "some-value").build();
    okhttp3.Response nonAuthenticatedResponse = new okhttp3.Response.Builder().protocol(Protocol.HTTP_2).code(200).message("OK").request(nonAuthenticatedRequest).build();
    okhttp3.Request processedRequest = authenticator.authenticate(route, nonAuthenticatedResponse);
    assertThat(processedRequest, is(nullValue()));
}
Also used : ProxyOptions(com.auth0.client.ProxyOptions) HttpOptions(com.auth0.client.HttpOptions) Proxy(java.net.Proxy) okhttp3(okhttp3) Test(org.junit.Test)

Example 3 with ProxyOptions

use of com.auth0.client.ProxyOptions in project auth0-java by auth0.

the class ManagementAPITest method proxyShouldNotProcessAlreadyAuthenticatedRequest.

@Test
public void proxyShouldNotProcessAlreadyAuthenticatedRequest() throws Exception {
    Proxy proxy = Mockito.mock(Proxy.class);
    ProxyOptions proxyOptions = new ProxyOptions(proxy);
    proxyOptions.setBasicAuthentication("johndoe", "psswd".toCharArray());
    assertThat(proxyOptions.getBasicAuthentication(), is("Basic am9obmRvZTpwc3N3ZA=="));
    HttpOptions httpOptions = new HttpOptions();
    httpOptions.setProxyOptions(proxyOptions);
    ManagementAPI api = new ManagementAPI(DOMAIN, API_TOKEN, httpOptions);
    assertThat(api.getClient().proxy(), is(proxy));
    Authenticator authenticator = api.getClient().proxyAuthenticator();
    assertThat(authenticator, is(notNullValue()));
    Route route = Mockito.mock(Route.class);
    okhttp3.Request alreadyAuthenticatedRequest = new okhttp3.Request.Builder().url("https://test.com/app").addHeader("some-header", "some-value").header("Proxy-Authorization", "pre-existing-value").build();
    okhttp3.Response alreadyAuthenticatedResponse = new okhttp3.Response.Builder().protocol(Protocol.HTTP_2).code(200).message("OK").request(alreadyAuthenticatedRequest).build();
    okhttp3.Request processedRequest = authenticator.authenticate(route, alreadyAuthenticatedResponse);
    assertThat(processedRequest, is(nullValue()));
}
Also used : Proxy(java.net.Proxy) ProxyOptions(com.auth0.client.ProxyOptions) okhttp3(okhttp3) HttpOptions(com.auth0.client.HttpOptions) Test(org.junit.Test)

Example 4 with ProxyOptions

use of com.auth0.client.ProxyOptions in project auth0-java by auth0.

the class ManagementAPITest method shouldUseProxyWithAuthentication.

@Test
public void shouldUseProxyWithAuthentication() throws Exception {
    Proxy proxy = Mockito.mock(Proxy.class);
    ProxyOptions proxyOptions = new ProxyOptions(proxy);
    proxyOptions.setBasicAuthentication("johndoe", "psswd".toCharArray());
    assertThat(proxyOptions.getBasicAuthentication(), is("Basic am9obmRvZTpwc3N3ZA=="));
    HttpOptions httpOptions = new HttpOptions();
    httpOptions.setProxyOptions(proxyOptions);
    ManagementAPI api = new ManagementAPI(DOMAIN, API_TOKEN, httpOptions);
    assertThat(api.getClient().proxy(), is(proxy));
    Authenticator authenticator = api.getClient().proxyAuthenticator();
    assertThat(authenticator, is(notNullValue()));
    Route route = Mockito.mock(Route.class);
    okhttp3.Request nonAuthenticatedRequest = new okhttp3.Request.Builder().url("https://test.com/app").addHeader("some-header", "some-value").build();
    okhttp3.Response nonAuthenticatedResponse = new okhttp3.Response.Builder().protocol(Protocol.HTTP_2).code(200).message("OK").request(nonAuthenticatedRequest).build();
    okhttp3.Request processedRequest = authenticator.authenticate(route, nonAuthenticatedResponse);
    assertThat(processedRequest, is(notNullValue()));
    assertThat(processedRequest.url(), is(HttpUrl.parse("https://test.com/app")));
    assertThat(processedRequest.header("Proxy-Authorization"), is(proxyOptions.getBasicAuthentication()));
    assertThat(processedRequest.header("some-header"), is("some-value"));
}
Also used : ProxyOptions(com.auth0.client.ProxyOptions) HttpOptions(com.auth0.client.HttpOptions) Proxy(java.net.Proxy) okhttp3(okhttp3) Test(org.junit.Test)

Example 5 with ProxyOptions

use of com.auth0.client.ProxyOptions in project auth0-java by auth0.

the class AuthAPI method buildNetworkingClient.

/**
 * Given a set of options, it creates a new instance of the {@link OkHttpClient}
 * configuring them according to their availability.
 *
 * @param options the options to set to the client.
 * @return a new networking client instance configured as requested.
 */
private OkHttpClient buildNetworkingClient(HttpOptions options) {
    OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();
    final ProxyOptions proxyOptions = options.getProxyOptions();
    if (proxyOptions != null) {
        // Set proxy
        clientBuilder.proxy(proxyOptions.getProxy());
        // Set authentication, if present
        final String proxyAuth = proxyOptions.getBasicAuthentication();
        if (proxyAuth != null) {
            clientBuilder.proxyAuthenticator(new Authenticator() {

                private static final String PROXY_AUTHORIZATION_HEADER = "Proxy-Authorization";

                @Override
                public okhttp3.Request authenticate(Route route, Response response) throws IOException {
                    if (response.request().header(PROXY_AUTHORIZATION_HEADER) != null) {
                        return null;
                    }
                    return response.request().newBuilder().header(PROXY_AUTHORIZATION_HEADER, proxyAuth).build();
                }
            });
        }
    }
    configureLogging(options.getLoggingOptions());
    Dispatcher dispatcher = new Dispatcher();
    dispatcher.setMaxRequests(options.getMaxRequests());
    dispatcher.setMaxRequestsPerHost(options.getMaxRequestsPerHost());
    return clientBuilder.addInterceptor(logging).addInterceptor(telemetry).connectTimeout(options.getConnectTimeout(), TimeUnit.SECONDS).readTimeout(options.getReadTimeout(), TimeUnit.SECONDS).dispatcher(dispatcher).build();
}
Also used : PasswordlessSmsResponse(com.auth0.json.auth.PasswordlessSmsResponse) PasswordlessEmailResponse(com.auth0.json.auth.PasswordlessEmailResponse) ProxyOptions(com.auth0.client.ProxyOptions) Request(com.auth0.net.Request) IOException(java.io.IOException)

Aggregations

ProxyOptions (com.auth0.client.ProxyOptions)8 HttpOptions (com.auth0.client.HttpOptions)6 Proxy (java.net.Proxy)6 okhttp3 (okhttp3)6 Test (org.junit.Test)6 Request (com.auth0.net.Request)3 IOException (java.io.IOException)2 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)2 PasswordlessEmailResponse (com.auth0.json.auth.PasswordlessEmailResponse)1 PasswordlessSmsResponse (com.auth0.json.auth.PasswordlessSmsResponse)1 RateLimitInterceptor (com.auth0.net.RateLimitInterceptor)1