Skip to content

Commit 6b9b122

Browse files
committed
RequestFactory has no static methods now.
Added some doc to Auth0.java
1 parent fa82701 commit 6b9b122

3 files changed

Lines changed: 109 additions & 58 deletions

File tree

core/src/main/java/com/auth0/Auth0.java

Lines changed: 76 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,22 @@
2929
import com.auth0.util.Metrics;
3030
import com.squareup.okhttp.HttpUrl;
3131

32+
/**
33+
* Represents your Auth0 account information (clientId & domain),
34+
* and it's used to obtain clients for Auth0's APIs.
35+
* <pre>{@code
36+
* Auth0 auth0 = new Auth0("YOUR_CLIENT_ID", "YOUR_DOMAIN");
37+
* }</pre>
38+
*/
3239
public class Auth0 {
3340

41+
/**
42+
* Version of auth0-java
43+
*/
3444
public static final String VERSION = "1.0.0";
45+
/**
46+
* Name of the library auth0-java
47+
*/
3548
public static final String NAME = "auth0-java";
3649

3750
private static final String AUTH0_US_CDN_URL = "https://cdn.auth0.com";
@@ -42,10 +55,23 @@ public class Auth0 {
4255
private final String configurationUrl;
4356
private Metrics metrics;
4457

58+
/**
59+
* Creates a new object using clientId & domain
60+
* @param clientId of your Auth0 application
61+
* @param domain of your Auth0 account
62+
*/
4563
public Auth0(String clientId, String domain) {
4664
this(clientId, domain, null);
4765
}
4866

67+
/**
68+
* Creates a new object using clientId, domain and configuration domain.
69+
* Useful when using a on-premise auth0 server that is not in the public cloud,
70+
* otherwise we recommend using the constructor {@link #Auth0(String, String)}
71+
* @param clientId of your Auth0 application
72+
* @param domain of your Auth0 account
73+
* @param configurationDomain where Auth0's configuration will be fetched. By default is Auth0 public cloud
74+
*/
4975
public Auth0(String clientId, String domain, String configurationDomain) {
5076
this.clientId = clientId;
5177
this.domainUrl = ensureUrlString(domain);
@@ -54,65 +80,88 @@ public Auth0(String clientId, String domain, String configurationDomain) {
5480
this.metrics.usingLibrary(Auth0.NAME, Auth0.VERSION);
5581
}
5682

57-
private String resolveConfiguration(String configurationDomain, String domainUrl) {
58-
String url = ensureUrlString(configurationDomain);
59-
if (configurationDomain == null && domainUrl != null) {
60-
final HttpUrl domainUri = HttpUrl.parse(domainUrl);
61-
final String host = domainUri.host();
62-
if (host.endsWith(DOT_AUTH0_DOT_COM)) {
63-
String[] parts = host.split("\\.");
64-
if (parts.length > 3) {
65-
url = "https://cdn." + parts[parts.length-3] + DOT_AUTH0_DOT_COM;
66-
} else {
67-
url = AUTH0_US_CDN_URL;
68-
}
69-
} else {
70-
url = domainUrl;
71-
}
72-
}
73-
return url;
74-
}
75-
76-
private String ensureUrlString(String url) {
77-
String safeUrl = null;
78-
if (url != null) {
79-
safeUrl = url.startsWith("http") ? url : "https://" + url;
80-
}
81-
return safeUrl;
82-
}
83-
83+
/**
84+
* @return your Auth0 application client identifier
85+
*/
8486
public String getClientId() {
8587
return clientId;
8688
}
8789

90+
/**
91+
* @return your Auth0 account domain url
92+
*/
8893
public String getDomainUrl() {
8994
return domainUrl;
9095
}
9196

97+
/**
98+
* @return your account configuration url
99+
*/
92100
public String getConfigurationUrl() {
93101
return configurationUrl;
94102
}
95103

104+
/**
105+
* @return a new Authentication API client using your account credentials
106+
*/
96107
public AuthenticationAPIClient newAuthenticationAPIClient() {
97108
return new AuthenticationAPIClient(this);
98109
}
99110

111+
/**
112+
* @return Url to perform the web flow of OAuth
113+
*/
100114
public String getAuthorizeUrl() {
101115
return HttpUrl.parse(domainUrl).newBuilder()
102116
.addEncodedPathSegment("authorize")
103117
.build()
104118
.toString();
105119
}
106120

121+
/**
122+
* @return Auth0 metrics sent in every requests
123+
*/
107124
public Metrics getMetrics() {
108125
return metrics;
109126
}
110127

128+
/**
129+
* Define what metrics are sent to Auth0 on every request.
130+
* @param metrics to send or nil to send nothing
131+
*/
111132
public void setMetrics(Metrics metrics) {
112133
if (metrics == null) {
113134
this.metrics = new BaseMetrics();
114135
} else {
115136
this.metrics = metrics;
116137
}
117138
}
139+
140+
private String resolveConfiguration(String configurationDomain, String domainUrl) {
141+
String url = ensureUrlString(configurationDomain);
142+
if (configurationDomain == null && domainUrl != null) {
143+
final HttpUrl domainUri = HttpUrl.parse(domainUrl);
144+
final String host = domainUri.host();
145+
if (host.endsWith(DOT_AUTH0_DOT_COM)) {
146+
String[] parts = host.split("\\.");
147+
if (parts.length > 3) {
148+
url = "https://cdn." + parts[parts.length-3] + DOT_AUTH0_DOT_COM;
149+
} else {
150+
url = AUTH0_US_CDN_URL;
151+
}
152+
} else {
153+
url = domainUrl;
154+
}
155+
}
156+
return url;
157+
}
158+
159+
private String ensureUrlString(String url) {
160+
String safeUrl = null;
161+
if (url != null) {
162+
safeUrl = url.startsWith("http") ? url : "https://" + url;
163+
}
164+
return safeUrl;
165+
}
166+
118167
}

core/src/main/java/com/auth0/authentication/AuthenticationAPIClient.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public class AuthenticationAPIClient {
6060
private final Auth0 auth0;
6161
private final OkHttpClient client;
6262
private final ObjectMapper mapper;
63+
private final RequestFactory factory;
6364

6465
private String defaultDbConnection = DEFAULT_DB_CONNECTION;
6566

@@ -82,13 +83,14 @@ public AuthenticationAPIClient(String clientID, String baseURL, String configura
8283
this(new Auth0(clientID, baseURL, configurationURL));
8384
}
8485

85-
protected AuthenticationAPIClient(Auth0 auth0, OkHttpClient client, ObjectMapper mapper) {
86+
private AuthenticationAPIClient(Auth0 auth0, OkHttpClient client, ObjectMapper mapper) {
8687
this.auth0 = auth0;
8788
this.client = client;
8889
this.mapper = mapper;
90+
this.factory = new RequestFactory();
8991
final Metrics metrics = auth0.getMetrics();
9092
if (metrics != null) {
91-
RequestFactory.setClientInfo(metrics.getValue());
93+
factory.setClientInfo(metrics.getValue());
9294
}
9395
}
9496

@@ -105,7 +107,7 @@ public String getBaseURL() {
105107
* @param userAgent value to send in every request to Auth0
106108
*/
107109
public void setUserAgent(String userAgent) {
108-
RequestFactory.setUserAgent(userAgent);
110+
factory.setUserAgent(userAgent);
109111
}
110112

111113
/**
@@ -150,7 +152,7 @@ public AuthenticationRequest loginWithOAuthAccessToken(String token, String conn
150152
.asDictionary();
151153

152154
final ParameterizableRequest<UserProfile> profileRequest = profileRequest();
153-
ParameterizableRequest<Token> credentialsRequest = RequestFactory.POST(url, client, mapper, Token.class)
155+
ParameterizableRequest<Token> credentialsRequest = factory.POST(url, client, mapper, Token.class)
154156
.addParameters(parameters);
155157
return new AuthenticationRequest(credentialsRequest, profileRequest);
156158
}
@@ -222,7 +224,7 @@ public ParameterizableRequest<DatabaseUser> createUser(String email, String pass
222224
.setConnection(defaultDbConnection)
223225
.setClientId(getClientId())
224226
.asDictionary();
225-
return RequestFactory.POST(url, client, mapper, DatabaseUser.class)
227+
return factory.POST(url, client, mapper, DatabaseUser.class)
226228
.addParameters(parameters);
227229
}
228230

@@ -280,7 +282,7 @@ public ChangePasswordRequest changePassword(String email) {
280282
.setConnection(defaultDbConnection)
281283
.asDictionary();
282284

283-
ParameterizableRequest<Void> request = RequestFactory.POST(url, client, mapper)
285+
ParameterizableRequest<Void> request = factory.POST(url, client, mapper)
284286
.addParameters(parameters);
285287
return new ChangePasswordRequest(request);
286288
}
@@ -369,7 +371,7 @@ public Request<Void> unlink(String userId, String accessToken) {
369371
HttpUrl url = HttpUrl.parse(auth0.getDomainUrl()).newBuilder()
370372
.addPathSegment("unlink")
371373
.build();
372-
return RequestFactory.POST(url, client, mapper)
374+
return factory.POST(url, client, mapper)
373375
.addParameters(parameters);
374376
}
375377

@@ -421,7 +423,7 @@ public ParameterizableRequest<Map<String, Object>> delegation() {
421423
.setClientId(getClientId())
422424
.setGrantType(ParameterBuilder.GRANT_TYPE_JWT)
423425
.asDictionary();
424-
return RequestFactory.rawPOST(url, client, mapper)
426+
return factory.rawPOST(url, client, mapper)
425427
.addParameters(parameters);
426428
}
427429

@@ -434,7 +436,7 @@ protected <T> ParameterizableRequest<T> delegation(Class<T> clazz) {
434436
.setClientId(getClientId())
435437
.setGrantType(ParameterBuilder.GRANT_TYPE_JWT)
436438
.asDictionary();
437-
return RequestFactory.POST(url, client, mapper, clazz)
439+
return factory.POST(url, client, mapper, clazz)
438440
.addParameters(parameters);
439441
}
440442

@@ -453,7 +455,7 @@ public ParameterizableRequest<Void> passwordless() {
453455
.setClientId(getClientId())
454456
.asDictionary();
455457

456-
return RequestFactory.POST(url, client, mapper)
458+
return factory.POST(url, client, mapper)
457459
.addParameters(parameters);
458460
}
459461

@@ -467,7 +469,7 @@ protected ParameterizableRequest<Token> loginWithResourceOwner() {
467469
.setClientId(getClientId())
468470
.setConnection(defaultDbConnection)
469471
.asDictionary();
470-
ParameterizableRequest<Token> request = RequestFactory.POST(url, client, mapper, Token.class)
472+
ParameterizableRequest<Token> request = factory.POST(url, client, mapper, Token.class)
471473
.addParameters(requestParameters);
472474
return request;
473475
}
@@ -476,7 +478,7 @@ private ParameterizableRequest<UserProfile> profileRequest() {
476478
HttpUrl url = HttpUrl.parse(auth0.getDomainUrl()).newBuilder()
477479
.addPathSegment("tokeninfo")
478480
.build();
479-
return RequestFactory.POST(url, client, mapper, UserProfile.class);
481+
return factory.POST(url, client, mapper, UserProfile.class);
480482

481483
}
482484

core/src/main/java/com/auth0/internal/RequestFactory.java

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,59 +35,59 @@
3535

3636
public class RequestFactory {
3737

38-
private static String CLIENT_INFO;
39-
private static String USER_AGENT;
38+
private String clientInfo;
39+
private String userAgent;
4040

41-
public static void setClientInfo(String clientInfo) {
42-
CLIENT_INFO = clientInfo;
41+
public void setClientInfo(String clientInfo) {
42+
this.clientInfo = clientInfo;
4343
}
4444

45-
public static void setUserAgent(String userAgent) {
46-
USER_AGENT = userAgent;
45+
public void setUserAgent(String userAgent) {
46+
this.userAgent = userAgent;
4747
}
4848

49-
public static <T> ParameterizableRequest<T> GET(HttpUrl url, OkHttpClient client, ObjectMapper mapper, Class<T> clazz) {
49+
public <T> ParameterizableRequest<T> GET(HttpUrl url, OkHttpClient client, ObjectMapper mapper, Class<T> clazz) {
5050
return addMetricHeader(new SimpleRequest<>(url, client, mapper, "GET", clazz));
5151
}
5252

53-
public static <T> ParameterizableRequest<T> POST(HttpUrl url, OkHttpClient client, ObjectMapper mapper, Class<T> clazz) {
53+
public <T> ParameterizableRequest<T> POST(HttpUrl url, OkHttpClient client, ObjectMapper mapper, Class<T> clazz) {
5454
return addMetricHeader(new SimpleRequest<>(url, client, mapper, "POST", clazz));
5555
}
5656

57-
public static ParameterizableRequest<Map<String, Object>> rawPOST(HttpUrl url, OkHttpClient client, ObjectMapper mapper) {
57+
public ParameterizableRequest<Map<String, Object>> rawPOST(HttpUrl url, OkHttpClient client, ObjectMapper mapper) {
5858
final SimpleRequest<Map<String, Object>> request = new SimpleRequest<>(url, client, mapper, "POST");
5959
addMetricHeader(request);
6060
return request;
6161
}
6262

63-
public static ParameterizableRequest<Void> POST(HttpUrl url, OkHttpClient client, ObjectMapper mapper) {
63+
public ParameterizableRequest<Void> POST(HttpUrl url, OkHttpClient client, ObjectMapper mapper) {
6464
return addMetricHeader(new VoidRequest(url, client, mapper, "POST"));
6565
}
6666

67-
public static ParameterizableRequest<Void> POST(HttpUrl url, OkHttpClient client, ObjectMapper mapper, String jwt) {
67+
public ParameterizableRequest<Void> POST(HttpUrl url, OkHttpClient client, ObjectMapper mapper, String jwt) {
6868
final AuthorizableRequest<Void> request = new VoidRequest(url, client, mapper, "POST")
6969
.setBearer(jwt);
7070
return addMetricHeader(request);
7171
}
7272

73-
public static <T> ParameterizableRequest<T> PUT(HttpUrl url, OkHttpClient client, ObjectMapper mapper, Class<T> clazz) {
73+
public <T> ParameterizableRequest<T> PUT(HttpUrl url, OkHttpClient client, ObjectMapper mapper, Class<T> clazz) {
7474
return addMetricHeader(new SimpleRequest<>(url, client, mapper, "PUT", clazz));
7575
}
7676

77-
public static <T> ParameterizableRequest<T> PATCH(HttpUrl url, OkHttpClient client, ObjectMapper mapper, Class<T> clazz) {
77+
public <T> ParameterizableRequest<T> PATCH(HttpUrl url, OkHttpClient client, ObjectMapper mapper, Class<T> clazz) {
7878
return addMetricHeader(new SimpleRequest<>(url, client, mapper, "GET", clazz));
7979
}
8080

81-
public static <T> ParameterizableRequest<T> DELETE(HttpUrl url, OkHttpClient client, ObjectMapper mapper, Class<T> clazz) {
81+
public <T> ParameterizableRequest<T> DELETE(HttpUrl url, OkHttpClient client, ObjectMapper mapper, Class<T> clazz) {
8282
return addMetricHeader(new SimpleRequest<>(url, client, mapper, "DELETE", clazz));
8383
}
8484

85-
private static <T> ParameterizableRequest<T> addMetricHeader(ParameterizableRequest<T> request) {
86-
if (CLIENT_INFO != null) {
87-
request.addHeader(Metrics.HEADER_NAME, CLIENT_INFO);
85+
private <T> ParameterizableRequest<T> addMetricHeader(ParameterizableRequest<T> request) {
86+
if (this.clientInfo != null) {
87+
request.addHeader(Metrics.HEADER_NAME, this.clientInfo);
8888
}
89-
if (USER_AGENT != null) {
90-
request.addHeader("User-Agent", USER_AGENT);
89+
if (this.userAgent!= null) {
90+
request.addHeader("User-Agent", this.userAgent);
9191
}
9292
return request;
9393
}

0 commit comments

Comments
 (0)