From bb6f8c29849d936c88d866cefe53765f892d4638 Mon Sep 17 00:00:00 2001 From: Luciano Balmaceda Date: Tue, 2 Jun 2020 20:45:39 -0300 Subject: [PATCH 1/4] add support for java Proxy with basic auth to AuthAPI --- .../java/com/auth0/client/ClientOptions.java | 27 ++++ .../java/com/auth0/client/ProxyOptions.java | 56 ++++++++ .../java/com/auth0/client/auth/AuthAPI.java | 64 +++++++-- .../com/auth0/client/auth/AuthAPITest.java | 123 +++++++++++++++++- 4 files changed, 261 insertions(+), 9 deletions(-) create mode 100644 src/main/java/com/auth0/client/ClientOptions.java create mode 100644 src/main/java/com/auth0/client/ProxyOptions.java diff --git a/src/main/java/com/auth0/client/ClientOptions.java b/src/main/java/com/auth0/client/ClientOptions.java new file mode 100644 index 00000000..b77a6b79 --- /dev/null +++ b/src/main/java/com/auth0/client/ClientOptions.java @@ -0,0 +1,27 @@ +package com.auth0.client; + +/** + * Class holder of additional options to set to the API client instance. + */ +public class ClientOptions { + + private ProxyOptions proxyOptions; + + /** + * Getter for the Proxy configuration options + * + * @return the Proxy configuration options if set, null otherwise. + */ + public ProxyOptions getProxyOptions() { + return proxyOptions; + } + + /** + * Setter for the Proxy configuration options + * + * @param proxyOptions the Proxy configuration options + */ + public void setProxyOptions(ProxyOptions proxyOptions) { + this.proxyOptions = proxyOptions; + } +} diff --git a/src/main/java/com/auth0/client/ProxyOptions.java b/src/main/java/com/auth0/client/ProxyOptions.java new file mode 100644 index 00000000..793798b6 --- /dev/null +++ b/src/main/java/com/auth0/client/ProxyOptions.java @@ -0,0 +1,56 @@ +package com.auth0.client; + +import com.auth0.utils.Asserts; +import okhttp3.Credentials; + +import java.net.Proxy; + +/** + * Holder class for Java Proxy related settings. + */ +public class ProxyOptions { + + private final Proxy proxy; + private String basicAuth; + + /** + * Builds a new instance using the given Proxy. + * The Proxy will not have authentication unless {@link #setBasicAuthentication(String, char[])} is set. + * + * @param proxy the Proxy to use. + */ + public ProxyOptions(Proxy proxy) { + Asserts.assertNotNull(proxy, "proxy"); + this.proxy = proxy; + } + + /** + * Setter that builds the authentication value to use for this Proxy. + * + * @param username the username to use. + * @param password the password to use. + */ + public void setBasicAuthentication(String username, char[] password) { + Asserts.assertNotNull(proxy, "username"); + Asserts.assertNotNull(proxy, "password"); + this.basicAuth = Credentials.basic(username, new String(password)); + } + + /** + * Getter of the Proxy instance to set + * + * @return the Proxy instance to set + */ + public Proxy getProxy() { + return proxy; + } + + /** + * Getter of the authentication value to use for this Proxy. + * + * @return the authentication value to use for this Proxy, or null if unset. + */ + public String getBasicAuthentication() { + return basicAuth; + } +} diff --git a/src/main/java/com/auth0/client/auth/AuthAPI.java b/src/main/java/com/auth0/client/auth/AuthAPI.java index 1ec063c8..91bf8e72 100644 --- a/src/main/java/com/auth0/client/auth/AuthAPI.java +++ b/src/main/java/com/auth0/client/auth/AuthAPI.java @@ -1,14 +1,18 @@ package com.auth0.client.auth; +import com.auth0.client.ClientOptions; +import com.auth0.client.ProxyOptions; import com.auth0.json.auth.UserInfo; +import com.auth0.net.Request; import com.auth0.net.*; import com.auth0.utils.Asserts; import com.fasterxml.jackson.core.type.TypeReference; -import okhttp3.HttpUrl; -import okhttp3.OkHttpClient; +import okhttp3.*; import okhttp3.logging.HttpLoggingInterceptor; import okhttp3.logging.HttpLoggingInterceptor.Level; +import java.io.IOException; + /** * Class that provides an implementation of some of the Authentication and Authorization API methods defined in https://auth0.com/docs/api/authentication. * To begin create a new instance of {@link #AuthAPI(String, String, String)} using the tenant domain, and the Application's client id and client secret. @@ -45,11 +49,13 @@ public class AuthAPI { * @param domain tenant's domain. * @param clientId the application's client id. * @param clientSecret the application's client secret. + * @param options configuration options for this client instance. */ - public AuthAPI(String domain, String clientId, String clientSecret) { + public AuthAPI(String domain, String clientId, String clientSecret, ClientOptions options) { Asserts.assertNotNull(domain, "domain"); Asserts.assertNotNull(clientId, "client id"); Asserts.assertNotNull(clientSecret, "client secret"); + Asserts.assertNotNull(options, "client options"); this.baseUrl = createBaseUrl(domain); if (baseUrl == null) { @@ -61,7 +67,53 @@ public AuthAPI(String domain, String clientId, String clientSecret) { telemetry = new TelemetryInterceptor(); logging = new HttpLoggingInterceptor(); logging.setLevel(Level.NONE); - client = new OkHttpClient.Builder() + client = buildNetworkingClient(options); + } + + /** + * Create a new instance with the given tenant's domain, application's client id and client secret. These values can be obtained at https://manage.auth0.com/#/applications/{YOUR_CLIENT_ID}/settings. + * + * @param domain tenant's domain. + * @param clientId the application's client id. + * @param clientSecret the application's client secret. + */ + public AuthAPI(String domain, String clientId, String clientSecret) { + this(domain, clientId, clientSecret, new ClientOptions()); + } + + /** + * 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(ClientOptions 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(); + } + }); + } + } + return clientBuilder .addInterceptor(logging) .addInterceptor(telemetry) .build(); @@ -249,7 +301,6 @@ public Request resetPassword(String email, String connection) { * @param password the desired user's password. * @param connection the database connection where the user is going to be created. * @return a Request to configure and execute. - * * @deprecated Use {@linkplain #signUp(String, String, char[], String)} instead. */ @Deprecated @@ -314,7 +365,6 @@ public SignUpRequest signUp(String email, String username, char[] password, Stri * @param password the desired user's password. * @param connection the database connection where the user is going to be created. * @return a Request to configure and execute. - * * @deprecated Use {@linkplain #signUp(String, char[], String)} instead. */ @Deprecated @@ -384,7 +434,6 @@ public SignUpRequest signUp(String email, char[] password, String connection) { * @param emailOrUsername the identity of the user. * @param password the password of the user. * @return a Request to configure and execute. - * * @deprecated Use {@linkplain #login(String, char[])} instead. */ @Deprecated @@ -451,7 +500,6 @@ public AuthRequest login(String emailOrUsername, char[] password) { * @param password the password of the user. * @param realm the realm to use. * @return a Request to configure and execute. - * * @deprecated Use {@linkplain #login(String, char[], String)} instead. */ @Deprecated diff --git a/src/test/java/com/auth0/client/auth/AuthAPITest.java b/src/test/java/com/auth0/client/auth/AuthAPITest.java index 310837a0..70b30769 100644 --- a/src/test/java/com/auth0/client/auth/AuthAPITest.java +++ b/src/test/java/com/auth0/client/auth/AuthAPITest.java @@ -1,14 +1,17 @@ package com.auth0.client.auth; +import com.auth0.client.ClientOptions; import com.auth0.client.MockServer; +import com.auth0.client.ProxyOptions; import com.auth0.exception.APIException; import com.auth0.json.auth.CreatedUser; import com.auth0.json.auth.TokenHolder; import com.auth0.json.auth.UserInfo; +import com.auth0.net.Request; import com.auth0.net.*; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; -import okhttp3.Interceptor; +import okhttp3.*; import okhttp3.logging.HttpLoggingInterceptor; import okhttp3.logging.HttpLoggingInterceptor.Level; import okhttp3.mockwebserver.RecordedRequest; @@ -20,6 +23,7 @@ import org.mockito.Mockito; import java.io.FileReader; +import java.net.Proxy; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -104,6 +108,123 @@ public void shouldThrowWhenClientSecretIsNull() throws Exception { new AuthAPI(DOMAIN, CLIENT_ID, null); } + @Test + public void shouldNotUseProxyByDefault() throws Exception { + AuthAPI api = new AuthAPI(DOMAIN, CLIENT_ID, CLIENT_SECRET); + assertThat(api.getClient().proxy(), is(nullValue())); + 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())); + } + + @Test + public void shouldUseProxy() throws Exception { + Proxy proxy = Mockito.mock(Proxy.class); + ProxyOptions proxyOptions = new ProxyOptions(proxy); + ClientOptions clientOptions = new ClientOptions(); + clientOptions.setProxyOptions(proxyOptions); + + AuthAPI api = new AuthAPI(DOMAIN, CLIENT_ID, CLIENT_SECRET, clientOptions); + 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())); + } + + @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==")); + ClientOptions clientOptions = new ClientOptions(); + clientOptions.setProxyOptions(proxyOptions); + + AuthAPI api = new AuthAPI(DOMAIN, CLIENT_ID, CLIENT_SECRET, clientOptions); + 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")); + } + + @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==")); + ClientOptions clientOptions = new ClientOptions(); + clientOptions.setProxyOptions(proxyOptions); + + AuthAPI api = new AuthAPI(DOMAIN, CLIENT_ID, CLIENT_SECRET, clientOptions); + 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())); + } + @Test public void shouldUseCustomTelemetry() throws Exception { AuthAPI api = new AuthAPI(DOMAIN, CLIENT_ID, CLIENT_SECRET); From 881861eaa78c40214f0282b879cdaede35b47ab5 Mon Sep 17 00:00:00 2001 From: Luciano Balmaceda Date: Tue, 2 Jun 2020 20:54:41 -0300 Subject: [PATCH 2/4] add support for java Proxy with basic auth to ManagementAPI --- .../com/auth0/client/mgmt/ManagementAPI.java | 58 +++++++- .../auth0/client/mgmt/ManagementAPITest.java | 124 +++++++++++++++++- 2 files changed, 177 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/auth0/client/mgmt/ManagementAPI.java b/src/main/java/com/auth0/client/mgmt/ManagementAPI.java index bcd53895..929c6c0d 100644 --- a/src/main/java/com/auth0/client/mgmt/ManagementAPI.java +++ b/src/main/java/com/auth0/client/mgmt/ManagementAPI.java @@ -1,13 +1,16 @@ package com.auth0.client.mgmt; +import com.auth0.client.ClientOptions; +import com.auth0.client.ProxyOptions; import com.auth0.net.Telemetry; import com.auth0.net.TelemetryInterceptor; import com.auth0.utils.Asserts; -import okhttp3.HttpUrl; -import okhttp3.OkHttpClient; +import okhttp3.*; import okhttp3.logging.HttpLoggingInterceptor; import okhttp3.logging.HttpLoggingInterceptor.Level; +import java.io.IOException; + /** * Class that provides an implementation of the Management API methods defined in https://auth0.com/docs/api/management/v2. * To begin create an instance of {@link #ManagementAPI(String, String)} using the tenant domain and API token. @@ -27,8 +30,9 @@ public class ManagementAPI { * * @param domain the tenant's domain. * @param apiToken the token to authenticate the calls with. + * @param options configuration options for this client instance. */ - public ManagementAPI(String domain, String apiToken) { + public ManagementAPI(String domain, String apiToken, ClientOptions options) { Asserts.assertNotNull(domain, "domain"); Asserts.assertNotNull(apiToken, "api token"); @@ -41,7 +45,53 @@ public ManagementAPI(String domain, String apiToken) { telemetry = new TelemetryInterceptor(); logging = new HttpLoggingInterceptor(); logging.setLevel(Level.NONE); - client = new OkHttpClient.Builder() + client = buildNetworkingClient(options); + } + + /** + * Create an instance with the given tenant's domain and API token. + * See the Management API section in the readme or visit https://auth0.com/docs/api/management/v2/tokens to learn how to obtain a token. + * + * @param domain the tenant's domain. + * @param apiToken the token to authenticate the calls with. + */ + public ManagementAPI(String domain, String apiToken) { + this(domain, apiToken, new ClientOptions()); + } + + /** + * 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(ClientOptions 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(); + } + }); + } + } + return clientBuilder .addInterceptor(logging) .addInterceptor(telemetry) .build(); diff --git a/src/test/java/com/auth0/client/mgmt/ManagementAPITest.java b/src/test/java/com/auth0/client/mgmt/ManagementAPITest.java index 50cd655f..108bbc98 100644 --- a/src/test/java/com/auth0/client/mgmt/ManagementAPITest.java +++ b/src/test/java/com/auth0/client/mgmt/ManagementAPITest.java @@ -1,9 +1,12 @@ package com.auth0.client.mgmt; +import com.auth0.client.ClientOptions; import com.auth0.client.MockServer; +import com.auth0.client.ProxyOptions; +import com.auth0.client.auth.AuthAPI; import com.auth0.net.Telemetry; import com.auth0.net.TelemetryInterceptor; -import okhttp3.Interceptor; +import okhttp3.*; import okhttp3.logging.HttpLoggingInterceptor; import org.junit.After; import org.junit.Before; @@ -12,6 +15,8 @@ import org.junit.rules.ExpectedException; import org.mockito.Mockito; +import java.net.Proxy; + import static com.auth0.client.UrlMatcher.isUrl; import static okhttp3.logging.HttpLoggingInterceptor.Level; import static org.hamcrest.Matchers.*; @@ -133,6 +138,123 @@ public void shouldUpdateApiToken() throws Exception { assertThat(api.users().apiToken, is("new token")); } + @Test + public void shouldNotUseProxyByDefault() throws Exception { + ManagementAPI api = new ManagementAPI(DOMAIN, API_TOKEN); + assertThat(api.getClient().proxy(), is(nullValue())); + 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())); + } + + @Test + public void shouldUseProxy() throws Exception { + Proxy proxy = Mockito.mock(Proxy.class); + ProxyOptions proxyOptions = new ProxyOptions(proxy); + ClientOptions clientOptions = new ClientOptions(); + clientOptions.setProxyOptions(proxyOptions); + + ManagementAPI api = new ManagementAPI(DOMAIN, API_TOKEN, clientOptions); + 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())); + } + + @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==")); + ClientOptions clientOptions = new ClientOptions(); + clientOptions.setProxyOptions(proxyOptions); + + ManagementAPI api = new ManagementAPI(DOMAIN, API_TOKEN, clientOptions); + 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")); + } + + @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==")); + ClientOptions clientOptions = new ClientOptions(); + clientOptions.setProxyOptions(proxyOptions); + + ManagementAPI api = new ManagementAPI(DOMAIN, API_TOKEN, clientOptions); + 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())); + } + @Test public void shouldAddAndEnableTelemetryInterceptor() throws Exception { ManagementAPI api = new ManagementAPI(DOMAIN, API_TOKEN); From 9cdbc763adbbc8a318785f47ba85a0e7e48d988d Mon Sep 17 00:00:00 2001 From: Luciano Balmaceda Date: Wed, 3 Jun 2020 15:13:50 -0300 Subject: [PATCH 3/4] rename ClientOptions to HttpOptions --- .../{ClientOptions.java => HttpOptions.java} | 2 +- .../java/com/auth0/client/auth/AuthAPI.java | 8 +++---- .../com/auth0/client/mgmt/ManagementAPI.java | 8 +++---- .../com/auth0/client/auth/AuthAPITest.java | 20 +++++++++--------- .../auth0/client/mgmt/ManagementAPITest.java | 21 +++++++++---------- 5 files changed, 29 insertions(+), 30 deletions(-) rename src/main/java/com/auth0/client/{ClientOptions.java => HttpOptions.java} (95%) diff --git a/src/main/java/com/auth0/client/ClientOptions.java b/src/main/java/com/auth0/client/HttpOptions.java similarity index 95% rename from src/main/java/com/auth0/client/ClientOptions.java rename to src/main/java/com/auth0/client/HttpOptions.java index b77a6b79..5aed1361 100644 --- a/src/main/java/com/auth0/client/ClientOptions.java +++ b/src/main/java/com/auth0/client/HttpOptions.java @@ -3,7 +3,7 @@ /** * Class holder of additional options to set to the API client instance. */ -public class ClientOptions { +public class HttpOptions { private ProxyOptions proxyOptions; diff --git a/src/main/java/com/auth0/client/auth/AuthAPI.java b/src/main/java/com/auth0/client/auth/AuthAPI.java index 91bf8e72..56606047 100644 --- a/src/main/java/com/auth0/client/auth/AuthAPI.java +++ b/src/main/java/com/auth0/client/auth/AuthAPI.java @@ -1,6 +1,6 @@ package com.auth0.client.auth; -import com.auth0.client.ClientOptions; +import com.auth0.client.HttpOptions; import com.auth0.client.ProxyOptions; import com.auth0.json.auth.UserInfo; import com.auth0.net.Request; @@ -51,7 +51,7 @@ public class AuthAPI { * @param clientSecret the application's client secret. * @param options configuration options for this client instance. */ - public AuthAPI(String domain, String clientId, String clientSecret, ClientOptions options) { + public AuthAPI(String domain, String clientId, String clientSecret, HttpOptions options) { Asserts.assertNotNull(domain, "domain"); Asserts.assertNotNull(clientId, "client id"); Asserts.assertNotNull(clientSecret, "client secret"); @@ -78,7 +78,7 @@ public AuthAPI(String domain, String clientId, String clientSecret, ClientOption * @param clientSecret the application's client secret. */ public AuthAPI(String domain, String clientId, String clientSecret) { - this(domain, clientId, clientSecret, new ClientOptions()); + this(domain, clientId, clientSecret, new HttpOptions()); } /** @@ -88,7 +88,7 @@ public AuthAPI(String domain, String clientId, String clientSecret) { * @param options the options to set to the client. * @return a new networking client instance configured as requested. */ - private OkHttpClient buildNetworkingClient(ClientOptions options) { + private OkHttpClient buildNetworkingClient(HttpOptions options) { OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder(); final ProxyOptions proxyOptions = options.getProxyOptions(); if (proxyOptions != null) { diff --git a/src/main/java/com/auth0/client/mgmt/ManagementAPI.java b/src/main/java/com/auth0/client/mgmt/ManagementAPI.java index 929c6c0d..ca89a904 100644 --- a/src/main/java/com/auth0/client/mgmt/ManagementAPI.java +++ b/src/main/java/com/auth0/client/mgmt/ManagementAPI.java @@ -1,6 +1,6 @@ package com.auth0.client.mgmt; -import com.auth0.client.ClientOptions; +import com.auth0.client.HttpOptions; import com.auth0.client.ProxyOptions; import com.auth0.net.Telemetry; import com.auth0.net.TelemetryInterceptor; @@ -32,7 +32,7 @@ public class ManagementAPI { * @param apiToken the token to authenticate the calls with. * @param options configuration options for this client instance. */ - public ManagementAPI(String domain, String apiToken, ClientOptions options) { + public ManagementAPI(String domain, String apiToken, HttpOptions options) { Asserts.assertNotNull(domain, "domain"); Asserts.assertNotNull(apiToken, "api token"); @@ -56,7 +56,7 @@ public ManagementAPI(String domain, String apiToken, ClientOptions options) { * @param apiToken the token to authenticate the calls with. */ public ManagementAPI(String domain, String apiToken) { - this(domain, apiToken, new ClientOptions()); + this(domain, apiToken, new HttpOptions()); } /** @@ -66,7 +66,7 @@ public ManagementAPI(String domain, String apiToken) { * @param options the options to set to the client. * @return a new networking client instance configured as requested. */ - private OkHttpClient buildNetworkingClient(ClientOptions options) { + private OkHttpClient buildNetworkingClient(HttpOptions options) { OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder(); final ProxyOptions proxyOptions = options.getProxyOptions(); if (proxyOptions != null) { diff --git a/src/test/java/com/auth0/client/auth/AuthAPITest.java b/src/test/java/com/auth0/client/auth/AuthAPITest.java index 70b30769..b81490d1 100644 --- a/src/test/java/com/auth0/client/auth/AuthAPITest.java +++ b/src/test/java/com/auth0/client/auth/AuthAPITest.java @@ -1,6 +1,6 @@ package com.auth0.client.auth; -import com.auth0.client.ClientOptions; +import com.auth0.client.HttpOptions; import com.auth0.client.MockServer; import com.auth0.client.ProxyOptions; import com.auth0.exception.APIException; @@ -135,10 +135,10 @@ public void shouldNotUseProxyByDefault() throws Exception { public void shouldUseProxy() throws Exception { Proxy proxy = Mockito.mock(Proxy.class); ProxyOptions proxyOptions = new ProxyOptions(proxy); - ClientOptions clientOptions = new ClientOptions(); - clientOptions.setProxyOptions(proxyOptions); + HttpOptions httpOptions = new HttpOptions(); + httpOptions.setProxyOptions(proxyOptions); - AuthAPI api = new AuthAPI(DOMAIN, CLIENT_ID, CLIENT_SECRET, clientOptions); + AuthAPI api = new AuthAPI(DOMAIN, CLIENT_ID, CLIENT_SECRET, httpOptions); assertThat(api.getClient().proxy(), is(proxy)); Authenticator authenticator = api.getClient().proxyAuthenticator(); assertThat(authenticator, is(notNullValue())); @@ -166,10 +166,10 @@ public void shouldUseProxyWithAuthentication() throws Exception { ProxyOptions proxyOptions = new ProxyOptions(proxy); proxyOptions.setBasicAuthentication("johndoe", "psswd".toCharArray()); assertThat(proxyOptions.getBasicAuthentication(), is("Basic am9obmRvZTpwc3N3ZA==")); - ClientOptions clientOptions = new ClientOptions(); - clientOptions.setProxyOptions(proxyOptions); + HttpOptions httpOptions = new HttpOptions(); + httpOptions.setProxyOptions(proxyOptions); - AuthAPI api = new AuthAPI(DOMAIN, CLIENT_ID, CLIENT_SECRET, clientOptions); + AuthAPI api = new AuthAPI(DOMAIN, CLIENT_ID, CLIENT_SECRET, httpOptions); assertThat(api.getClient().proxy(), is(proxy)); Authenticator authenticator = api.getClient().proxyAuthenticator(); assertThat(authenticator, is(notNullValue())); @@ -200,10 +200,10 @@ public void proxyShouldNotProcessAlreadyAuthenticatedRequest() throws Exception ProxyOptions proxyOptions = new ProxyOptions(proxy); proxyOptions.setBasicAuthentication("johndoe", "psswd".toCharArray()); assertThat(proxyOptions.getBasicAuthentication(), is("Basic am9obmRvZTpwc3N3ZA==")); - ClientOptions clientOptions = new ClientOptions(); - clientOptions.setProxyOptions(proxyOptions); + HttpOptions httpOptions = new HttpOptions(); + httpOptions.setProxyOptions(proxyOptions); - AuthAPI api = new AuthAPI(DOMAIN, CLIENT_ID, CLIENT_SECRET, clientOptions); + AuthAPI api = new AuthAPI(DOMAIN, CLIENT_ID, CLIENT_SECRET, httpOptions); assertThat(api.getClient().proxy(), is(proxy)); Authenticator authenticator = api.getClient().proxyAuthenticator(); assertThat(authenticator, is(notNullValue())); diff --git a/src/test/java/com/auth0/client/mgmt/ManagementAPITest.java b/src/test/java/com/auth0/client/mgmt/ManagementAPITest.java index 108bbc98..bbccae57 100644 --- a/src/test/java/com/auth0/client/mgmt/ManagementAPITest.java +++ b/src/test/java/com/auth0/client/mgmt/ManagementAPITest.java @@ -1,9 +1,8 @@ package com.auth0.client.mgmt; -import com.auth0.client.ClientOptions; +import com.auth0.client.HttpOptions; import com.auth0.client.MockServer; import com.auth0.client.ProxyOptions; -import com.auth0.client.auth.AuthAPI; import com.auth0.net.Telemetry; import com.auth0.net.TelemetryInterceptor; import okhttp3.*; @@ -165,10 +164,10 @@ public void shouldNotUseProxyByDefault() throws Exception { public void shouldUseProxy() throws Exception { Proxy proxy = Mockito.mock(Proxy.class); ProxyOptions proxyOptions = new ProxyOptions(proxy); - ClientOptions clientOptions = new ClientOptions(); - clientOptions.setProxyOptions(proxyOptions); + HttpOptions httpOptions = new HttpOptions(); + httpOptions.setProxyOptions(proxyOptions); - ManagementAPI api = new ManagementAPI(DOMAIN, API_TOKEN, clientOptions); + ManagementAPI api = new ManagementAPI(DOMAIN, API_TOKEN, httpOptions); assertThat(api.getClient().proxy(), is(proxy)); Authenticator authenticator = api.getClient().proxyAuthenticator(); assertThat(authenticator, is(notNullValue())); @@ -196,10 +195,10 @@ public void shouldUseProxyWithAuthentication() throws Exception { ProxyOptions proxyOptions = new ProxyOptions(proxy); proxyOptions.setBasicAuthentication("johndoe", "psswd".toCharArray()); assertThat(proxyOptions.getBasicAuthentication(), is("Basic am9obmRvZTpwc3N3ZA==")); - ClientOptions clientOptions = new ClientOptions(); - clientOptions.setProxyOptions(proxyOptions); + HttpOptions httpOptions = new HttpOptions(); + httpOptions.setProxyOptions(proxyOptions); - ManagementAPI api = new ManagementAPI(DOMAIN, API_TOKEN, clientOptions); + ManagementAPI api = new ManagementAPI(DOMAIN, API_TOKEN, httpOptions); assertThat(api.getClient().proxy(), is(proxy)); Authenticator authenticator = api.getClient().proxyAuthenticator(); assertThat(authenticator, is(notNullValue())); @@ -230,10 +229,10 @@ public void proxyShouldNotProcessAlreadyAuthenticatedRequest() throws Exception ProxyOptions proxyOptions = new ProxyOptions(proxy); proxyOptions.setBasicAuthentication("johndoe", "psswd".toCharArray()); assertThat(proxyOptions.getBasicAuthentication(), is("Basic am9obmRvZTpwc3N3ZA==")); - ClientOptions clientOptions = new ClientOptions(); - clientOptions.setProxyOptions(proxyOptions); + HttpOptions httpOptions = new HttpOptions(); + httpOptions.setProxyOptions(proxyOptions); - ManagementAPI api = new ManagementAPI(DOMAIN, API_TOKEN, clientOptions); + ManagementAPI api = new ManagementAPI(DOMAIN, API_TOKEN, httpOptions); assertThat(api.getClient().proxy(), is(proxy)); Authenticator authenticator = api.getClient().proxyAuthenticator(); assertThat(authenticator, is(notNullValue())); From 0dafe859e117deb4fcca3816117957becf32dd80 Mon Sep 17 00:00:00 2001 From: Luciano Balmaceda Date: Wed, 3 Jun 2020 16:33:02 -0300 Subject: [PATCH 4/4] update javadocs --- src/main/java/com/auth0/client/HttpOptions.java | 2 +- src/main/java/com/auth0/client/ProxyOptions.java | 2 +- src/main/java/com/auth0/client/auth/AuthAPI.java | 8 ++++++-- src/main/java/com/auth0/client/mgmt/ManagementAPI.java | 8 ++++++-- 4 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/auth0/client/HttpOptions.java b/src/main/java/com/auth0/client/HttpOptions.java index 5aed1361..f0e8c721 100644 --- a/src/main/java/com/auth0/client/HttpOptions.java +++ b/src/main/java/com/auth0/client/HttpOptions.java @@ -1,7 +1,7 @@ package com.auth0.client; /** - * Class holder of additional options to set to the API client instance. + * Used to configure additional configuration options when customizing the API client instance. */ public class HttpOptions { diff --git a/src/main/java/com/auth0/client/ProxyOptions.java b/src/main/java/com/auth0/client/ProxyOptions.java index 793798b6..53247518 100644 --- a/src/main/java/com/auth0/client/ProxyOptions.java +++ b/src/main/java/com/auth0/client/ProxyOptions.java @@ -6,7 +6,7 @@ import java.net.Proxy; /** - * Holder class for Java Proxy related settings. + * Used to configure Java Proxy-related configurations. */ public class ProxyOptions { diff --git a/src/main/java/com/auth0/client/auth/AuthAPI.java b/src/main/java/com/auth0/client/auth/AuthAPI.java index 56606047..5f420420 100644 --- a/src/main/java/com/auth0/client/auth/AuthAPI.java +++ b/src/main/java/com/auth0/client/auth/AuthAPI.java @@ -44,12 +44,15 @@ public class AuthAPI { private final HttpLoggingInterceptor logging; /** - * Create a new instance with the given tenant's domain, application's client id and client secret. These values can be obtained at https://manage.auth0.com/#/applications/{YOUR_CLIENT_ID}/settings. + * Create a new instance with the given tenant's domain, application's client id and client secret. + * These values can be obtained at https://manage.auth0.com/#/applications/{YOUR_CLIENT_ID}/settings. + * In addition, accepts an {@link HttpOptions} that will be used to configure the networking client. * * @param domain tenant's domain. * @param clientId the application's client id. * @param clientSecret the application's client secret. * @param options configuration options for this client instance. + * @see #AuthAPI(String, String, String) */ public AuthAPI(String domain, String clientId, String clientSecret, HttpOptions options) { Asserts.assertNotNull(domain, "domain"); @@ -71,7 +74,8 @@ public AuthAPI(String domain, String clientId, String clientSecret, HttpOptions } /** - * Create a new instance with the given tenant's domain, application's client id and client secret. These values can be obtained at https://manage.auth0.com/#/applications/{YOUR_CLIENT_ID}/settings. + * Create a new instance with the given tenant's domain, application's client id and client secret. + * These values can be obtained at https://manage.auth0.com/#/applications/{YOUR_CLIENT_ID}/settings. * * @param domain tenant's domain. * @param clientId the application's client id. diff --git a/src/main/java/com/auth0/client/mgmt/ManagementAPI.java b/src/main/java/com/auth0/client/mgmt/ManagementAPI.java index ca89a904..d539427a 100644 --- a/src/main/java/com/auth0/client/mgmt/ManagementAPI.java +++ b/src/main/java/com/auth0/client/mgmt/ManagementAPI.java @@ -26,11 +26,14 @@ public class ManagementAPI { /** * Create an instance with the given tenant's domain and API token. - * See the Management API section in the readme or visit https://auth0.com/docs/api/management/v2/tokens to learn how to obtain a token. + * In addition, accepts an {@link HttpOptions} that will be used to configure the networking client. + * See the Management API section in the readme or visit https://auth0.com/docs/api/management/v2/tokens + * to learn how to obtain a token. * * @param domain the tenant's domain. * @param apiToken the token to authenticate the calls with. * @param options configuration options for this client instance. + * @see #ManagementAPI(String, String) */ public ManagementAPI(String domain, String apiToken, HttpOptions options) { Asserts.assertNotNull(domain, "domain"); @@ -50,7 +53,8 @@ public ManagementAPI(String domain, String apiToken, HttpOptions options) { /** * Create an instance with the given tenant's domain and API token. - * See the Management API section in the readme or visit https://auth0.com/docs/api/management/v2/tokens to learn how to obtain a token. + * See the Management API section in the readme or visit https://auth0.com/docs/api/management/v2/tokens + * to learn how to obtain a token. * * @param domain the tenant's domain. * @param apiToken the token to authenticate the calls with.