Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for java Proxy with basic auth #266

Merged
merged 5 commits into from
Jun 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/main/java/com/auth0/client/HttpOptions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.auth0.client;

/**
* Used to configure additional configuration options when customizing the API client instance.
*/
public class HttpOptions {

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;
}
}
56 changes: 56 additions & 0 deletions src/main/java/com/auth0/client/ProxyOptions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.auth0.client;

import com.auth0.utils.Asserts;
import okhttp3.Credentials;

import java.net.Proxy;

/**
* Used to configure Java Proxy-related configurations.
*/
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;
}
}
70 changes: 61 additions & 9 deletions src/main/java/com/auth0/client/auth/AuthAPI.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package com.auth0.client.auth;

import com.auth0.client.HttpOptions;
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.
Expand Down Expand Up @@ -40,16 +44,21 @@ 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.
lbalmaceda marked this conversation as resolved.
Show resolved Hide resolved
* @see #AuthAPI(String, String, String)
*/
public AuthAPI(String domain, String clientId, String clientSecret) {
public AuthAPI(String domain, String clientId, String clientSecret, HttpOptions 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) {
Expand All @@ -61,7 +70,54 @@ 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 HttpOptions());
}

/**
* 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();
}
});
}
}
return clientBuilder
.addInterceptor(logging)
.addInterceptor(telemetry)
.build();
Expand Down Expand Up @@ -249,7 +305,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
Expand Down Expand Up @@ -314,7 +369,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
Expand Down Expand Up @@ -384,7 +438,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
Expand Down Expand Up @@ -451,7 +504,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
Expand Down
64 changes: 59 additions & 5 deletions src/main/java/com/auth0/client/mgmt/ManagementAPI.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package com.auth0.client.mgmt;

import com.auth0.client.HttpOptions;
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.
Expand All @@ -23,12 +26,16 @@ 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) {
public ManagementAPI(String domain, String apiToken, HttpOptions options) {
Asserts.assertNotNull(domain, "domain");
Asserts.assertNotNull(apiToken, "api token");

Expand All @@ -41,7 +48,54 @@ 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 HttpOptions());
}

/**
* 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();
}
});
}
}
jimmyjames marked this conversation as resolved.
Show resolved Hide resolved
return clientBuilder
.addInterceptor(logging)
.addInterceptor(telemetry)
.build();
Expand Down
Loading