Skip to content

Commit 0a13243

Browse files
committed
Merge pull request #15 from auth0/refactor-param-req
Provide access to the ParameterBuilder inside every request
2 parents 7c0737b + b9530ba commit 0a13243

10 files changed

Lines changed: 305 additions & 283 deletions

File tree

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

Lines changed: 140 additions & 135 deletions
Large diffs are not rendered by default.

lib/src/main/java/com/auth0/authentication/AuthenticationRequest.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
*/
4040
public class AuthenticationRequest implements Request<Authentication> {
4141

42+
private static final String ID_TOKEN_KEY = "id_token";
43+
4244
private final ParameterizableRequest<Token> credentialsRequest;
4345
private final ParameterizableRequest<UserProfile> tokenInfoRequest;
4446

@@ -49,49 +51,51 @@ public class AuthenticationRequest implements Request<Authentication> {
4951

5052
/**
5153
* Adds additional parameters for the login request
54+
*
5255
* @param parameters as a non-null dictionary
5356
* @return itself
5457
*/
5558
public AuthenticationRequest addParameters(Map<String, Object> parameters) {
56-
credentialsRequest.addParameters(parameters);
59+
credentialsRequest.getParameterBuilder().addAll(parameters);
5760
return this;
5861
}
5962

6063
/**
6164
* Set the scope used to authenticate the user
65+
*
6266
* @param scope value
6367
* @return itself
6468
*/
6569
public AuthenticationRequest setScope(String scope) {
66-
credentialsRequest.addParameters(new ParameterBuilder().clearAll().setScope(scope).asDictionary());
70+
credentialsRequest.getParameterBuilder().setScope(scope);
6771
return this;
6872
}
6973

7074
/**
7175
* Set the connection used to authenticate
76+
*
7277
* @param connection name
7378
* @return itself
7479
*/
7580
public AuthenticationRequest setConnection(String connection) {
76-
credentialsRequest.addParameters(new ParameterBuilder().clearAll().setConnection(connection).asDictionary());
81+
credentialsRequest.getParameterBuilder().setConnection(connection);
7782
return this;
7883
}
7984

8085
/**
8186
* Starts the log in request and then fetches the user's profile
87+
*
8288
* @param callback called on either success or failure
8389
*/
8490
@Override
8591
public void start(final BaseCallback<Authentication> callback) {
8692
credentialsRequest.start(new BaseCallback<Token>() {
8793
@Override
8894
public void onSuccess(final Token token) {
89-
Map<String, Object> parameters = new ParameterBuilder()
90-
.clearAll()
91-
.set("id_token", token.getIdToken())
92-
.asDictionary();
9395
tokenInfoRequest
94-
.addParameters(parameters)
96+
.getParameterBuilder()
97+
.set(ID_TOKEN_KEY, token.getIdToken());
98+
tokenInfoRequest
9599
.start(new BaseCallback<UserProfile>() {
96100
@Override
97101
public void onSuccess(UserProfile profile) {
@@ -114,18 +118,15 @@ public void onFailure(Auth0Exception error) {
114118

115119
/**
116120
* Logs in the user with Auth0 and fetches it's profile.
121+
*
117122
* @return authentication object containing the user's tokens and profile
118123
* @throws Auth0Exception when either authentication or profile fetch fails
119124
*/
120125
@Override
121126
public Authentication execute() throws Auth0Exception {
122127
Token token = credentialsRequest.execute();
123-
Map<String, Object> parameters = new ParameterBuilder()
124-
.clearAll()
125-
.set("id_token", token.getIdToken())
126-
.asDictionary();
128+
tokenInfoRequest.getParameterBuilder().set(ID_TOKEN_KEY, token.getIdToken());
127129
UserProfile profile = tokenInfoRequest
128-
.addParameters(parameters)
129130
.execute();
130131
return new Authentication(profile, token);
131132
}

lib/src/main/java/com/auth0/authentication/ChangePasswordRequest.java

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,37 +42,34 @@ public class ChangePasswordRequest implements ParameterizableRequest<Void> {
4242

4343
ChangePasswordRequest(ParameterizableRequest<Void> request) {
4444
this.request = request;
45-
}
46-
47-
private ChangePasswordRequest addParameter(String key, Object value) {
48-
Map<String, Object> parameters = new HashMap<>();
49-
parameters.put(key, value);
50-
return addParameters(parameters);
45+
request.getParameterBuilder();
5146
}
5247

5348
/**
5449
* Set the 'password' parameter to be sent in the request
50+
*
5551
* @param newPassword the new password
5652
* @return itself
5753
*/
5854
public ChangePasswordRequest setNewPassword(String newPassword) {
59-
return addParameter(PASSWORD_KEY, newPassword);
55+
request.getParameterBuilder().set(PASSWORD_KEY, newPassword);
56+
return this;
6057
}
6158

6259
/**
63-
* Adds additional parameters to be sent in the request
64-
* @param parameters as a non-null dictionary
65-
* @return itself
60+
* Getter for this request ParameterBuilder.
61+
*
62+
* @return the associated ParameterBuilder instance
6663
*/
6764
@Override
68-
public ChangePasswordRequest addParameters(Map<String, Object> parameters) {
69-
request.addParameters(parameters);
70-
return this;
65+
public ParameterBuilder getParameterBuilder() {
66+
return request.getParameterBuilder();
7167
}
7268

7369
/**
7470
* Adds an additional header for the request
75-
* @param name of the header
71+
*
72+
* @param name of the header
7673
* @param value of the header
7774
* @return itself
7875
*/
@@ -84,6 +81,7 @@ public ChangePasswordRequest addHeader(String name, String value) {
8481

8582
/**
8683
* Starts the change password request
84+
*
8785
* @param callback called either on success or failure
8886
*/
8987
@Override
@@ -93,6 +91,7 @@ public void start(final BaseCallback<Void> callback) {
9391

9492
/**
9593
* Executes the change password request
94+
*
9695
* @return nothing on success
9796
* @throws Auth0Exception when the change password request fails
9897
*/

lib/src/main/java/com/auth0/authentication/DelegationRequest.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,20 @@
2525
package com.auth0.authentication;
2626

2727
import com.auth0.Auth0Exception;
28+
import com.auth0.authentication.result.Delegation;
29+
import com.auth0.callback.BaseCallback;
2830
import com.auth0.request.ParameterizableRequest;
2931
import com.auth0.request.Request;
30-
import com.auth0.callback.BaseCallback;
31-
import com.auth0.authentication.result.Delegation;
3232

33-
import java.util.HashMap;
3433
import java.util.Map;
3534

3635
/**
3736
* Represents a delegation request for Auth0 tokens that will yield a new delegation token.
3837
* The delegation response depends on the 'api_type' parameter.
38+
*
3939
* @param <T> type of object that will hold the delegation response. When requesting Auth0’s 'id_token' you can
40-
* use {@link Delegation}, otherwise you’ll need to provide an object that can be created from the JSON
41-
* payload or just use {@code Map<String, Object>}
40+
* use {@link Delegation}, otherwise you’ll need to provide an object that can be created from the JSON
41+
* payload or just use {@code Map<String, Object>}
4242
*/
4343
public class DelegationRequest<T> implements Request<T> {
4444

@@ -54,23 +54,24 @@ public class DelegationRequest<T> implements Request<T> {
5454
}
5555

5656
private DelegationRequest<T> addParameter(String key, Object value) {
57-
Map<String, Object> parameters = new HashMap<>();
58-
parameters.put(key, value);
59-
return addParameters(parameters);
57+
request.getParameterBuilder().set(key, value);
58+
return this;
6059
}
6160

6261
/**
6362
* Add additional parameters to be sent in the request
63+
*
6464
* @param parameters as a non-null dictionary
6565
* @return itself
6666
*/
6767
public DelegationRequest<T> addParameters(Map<String, Object> parameters) {
68-
request.addParameters(parameters);
68+
request.getParameterBuilder().addAll(parameters);
6969
return this;
7070
}
7171

7272
/**
7373
* Set the 'api_type' parameter to be sent in the request
74+
*
7475
* @param apiType the delegation api type
7576
* @return itself
7677
*/
@@ -80,15 +81,18 @@ public DelegationRequest<T> setApiType(String apiType) {
8081

8182
/**
8283
* Set the 'scope' used to make the delegation
84+
*
8385
* @param scope value
8486
* @return itself
8587
*/
8688
public DelegationRequest<T> setScope(String scope) {
87-
return addParameters(new ParameterBuilder().clearAll().setScope(scope).asDictionary());
89+
request.getParameterBuilder().setScope(scope);
90+
return this;
8891
}
8992

9093
/**
9194
* Set the 'target' parameter to be sent in the request
95+
*
9296
* @param target the delegation target
9397
* @return itself
9498
*/
@@ -98,6 +102,7 @@ public DelegationRequest<T> setTarget(String target) {
98102

99103
/**
100104
* Starts the delegation request against Auth0 API
105+
*
101106
* @param callback called either on success or failure
102107
*/
103108
@Override
@@ -107,6 +112,7 @@ public void start(final BaseCallback<T> callback) {
107112

108113
/**
109114
* Executes the delegation request against Auth0 API
115+
*
110116
* @return the delegation response on success
111117
* @throws Auth0Exception when the delegation request fails
112118
*/

0 commit comments

Comments
 (0)