2424
2525package com .auth0 .authentication ;
2626
27- import com .auth0 .authentication .api .ParameterBuilder ;
28- import com .auth0 .authentication .api .ParameterizableRequest ;
29- import com .auth0 .authentication .api .Request ;
30- import com .auth0 .authentication .api .internal .RequestFactory ;
3127import com .auth0 .Auth0 ;
32- import com .auth0 .DatabaseUser ;
33- import com .auth0 .Token ;
34- import com .auth0 .UserProfile ;
28+ import com .auth0 .authentication .result .DatabaseUser ;
29+ import com .auth0 .authentication .result .Delegation ;
30+ import com .auth0 .authentication .result .Token ;
31+ import com .auth0 .authentication .result .UserProfile ;
32+ import com .auth0 .internal .RequestFactory ;
33+ import com .auth0 .request .ParameterizableRequest ;
34+ import com .auth0 .request .Request ;
35+ import com .auth0 .util .BaseMetrics ;
36+ import com .auth0 .util .Metrics ;
3537import com .fasterxml .jackson .databind .ObjectMapper ;
3638import com .squareup .okhttp .HttpUrl ;
3739import com .squareup .okhttp .OkHttpClient ;
3840
3941import java .util .Map ;
4042
41- import static com .auth0 .authentication .api . ParameterBuilder .GRANT_TYPE_PASSWORD ;
43+ import static com .auth0 .authentication .ParameterBuilder .GRANT_TYPE_PASSWORD ;
4244
4345/**
4446 * API client for Auth0 Authentication API.
@@ -58,6 +60,7 @@ public class AuthenticationAPIClient {
5860 private final Auth0 auth0 ;
5961 private final OkHttpClient client ;
6062 private final ObjectMapper mapper ;
63+ private final RequestFactory factory ;
6164
6265 private String defaultDbConnection = DEFAULT_DB_CONNECTION ;
6366
@@ -73,17 +76,22 @@ public AuthenticationAPIClient(Auth0 auth0) {
7376 * Creates a new API client instance providing Auth API and Configuration Urls different than the default. (Useful for on premise deploys).
7477 * @param clientID Your application clientID.
7578 * @param baseURL Auth0's auth API endpoint
76- * @param configurationURL Auth0's enpoint where App info can be retrieved.
79+ * @param configurationURL Auth0's endpoint where App info can be retrieved.
7780 */
7881 @ SuppressWarnings ("unused" )
7982 public AuthenticationAPIClient (String clientID , String baseURL , String configurationURL ) {
8083 this (new Auth0 (clientID , baseURL , configurationURL ));
8184 }
8285
83- protected AuthenticationAPIClient (Auth0 auth0 , OkHttpClient client , ObjectMapper mapper ) {
86+ private AuthenticationAPIClient (Auth0 auth0 , OkHttpClient client , ObjectMapper mapper ) {
8487 this .auth0 = auth0 ;
8588 this .client = client ;
8689 this .mapper = mapper ;
90+ this .factory = new RequestFactory ();
91+ final Metrics metrics = auth0 .getMetrics ();
92+ if (metrics != null ) {
93+ factory .setClientInfo (metrics .getValue ());
94+ }
8795 }
8896
8997 public String getClientId () {
@@ -94,6 +102,14 @@ public String getBaseURL() {
94102 return auth0 .getDomainUrl ();
95103 }
96104
105+ /**
106+ * Set the value of 'User-Agent' header for every request to Auth0 Authentication API
107+ * @param userAgent value to send in every request to Auth0
108+ */
109+ public void setUserAgent (String userAgent ) {
110+ factory .setUserAgent (userAgent );
111+ }
112+
97113 /**
98114 * Set the default DB connection name used. By default is 'Username-Password-Authentication'
99115 * @param defaultDbConnection name to use on every login with DB connection
@@ -136,7 +152,7 @@ public AuthenticationRequest loginWithOAuthAccessToken(String token, String conn
136152 .asDictionary ();
137153
138154 final ParameterizableRequest <UserProfile > profileRequest = profileRequest ();
139- ParameterizableRequest <Token > credentialsRequest = RequestFactory .POST (url , client , mapper , Token .class )
155+ ParameterizableRequest <Token > credentialsRequest = factory .POST (url , client , mapper , Token .class )
140156 .addParameters (parameters );
141157 return new AuthenticationRequest (credentialsRequest , profileRequest );
142158 }
@@ -208,7 +224,7 @@ public ParameterizableRequest<DatabaseUser> createUser(String email, String pass
208224 .setConnection (defaultDbConnection )
209225 .setClientId (getClientId ())
210226 .asDictionary ();
211- return RequestFactory .POST (url , client , mapper , DatabaseUser .class )
227+ return factory .POST (url , client , mapper , DatabaseUser .class )
212228 .addParameters (parameters );
213229 }
214230
@@ -266,7 +282,7 @@ public ChangePasswordRequest changePassword(String email) {
266282 .setConnection (defaultDbConnection )
267283 .asDictionary ();
268284
269- ParameterizableRequest <Void > request = RequestFactory .POST (url , client , mapper )
285+ ParameterizableRequest <Void > request = factory .POST (url , client , mapper )
270286 .addParameters (parameters );
271287 return new ChangePasswordRequest (request );
272288 }
@@ -355,7 +371,7 @@ public Request<Void> unlink(String userId, String accessToken) {
355371 HttpUrl url = HttpUrl .parse (auth0 .getDomainUrl ()).newBuilder ()
356372 .addPathSegment ("unlink" )
357373 .build ();
358- return RequestFactory .POST (url , client , mapper )
374+ return factory .POST (url , client , mapper )
359375 .addParameters (parameters );
360376 }
361377
@@ -407,7 +423,7 @@ public ParameterizableRequest<Map<String, Object>> delegation() {
407423 .setClientId (getClientId ())
408424 .setGrantType (ParameterBuilder .GRANT_TYPE_JWT )
409425 .asDictionary ();
410- return RequestFactory .rawPOST (url , client , mapper )
426+ return factory .rawPOST (url , client , mapper )
411427 .addParameters (parameters );
412428 }
413429
@@ -420,7 +436,7 @@ protected <T> ParameterizableRequest<T> delegation(Class<T> clazz) {
420436 .setClientId (getClientId ())
421437 .setGrantType (ParameterBuilder .GRANT_TYPE_JWT )
422438 .asDictionary ();
423- return RequestFactory .POST (url , client , mapper , clazz )
439+ return factory .POST (url , client , mapper , clazz )
424440 .addParameters (parameters );
425441 }
426442
@@ -439,7 +455,7 @@ public ParameterizableRequest<Void> passwordless() {
439455 .setClientId (getClientId ())
440456 .asDictionary ();
441457
442- return RequestFactory .POST (url , client , mapper )
458+ return factory .POST (url , client , mapper )
443459 .addParameters (parameters );
444460 }
445461
@@ -453,7 +469,7 @@ protected ParameterizableRequest<Token> loginWithResourceOwner() {
453469 .setClientId (getClientId ())
454470 .setConnection (defaultDbConnection )
455471 .asDictionary ();
456- ParameterizableRequest <Token > request = RequestFactory .POST (url , client , mapper , Token .class )
472+ ParameterizableRequest <Token > request = factory .POST (url , client , mapper , Token .class )
457473 .addParameters (requestParameters );
458474 return request ;
459475 }
@@ -462,7 +478,7 @@ private ParameterizableRequest<UserProfile> profileRequest() {
462478 HttpUrl url = HttpUrl .parse (auth0 .getDomainUrl ()).newBuilder ()
463479 .addPathSegment ("tokeninfo" )
464480 .build ();
465- return RequestFactory .POST (url , client , mapper , UserProfile .class );
481+ return factory .POST (url , client , mapper , UserProfile .class );
466482
467483 }
468484
0 commit comments