Skip to content

Commit ccb52ef

Browse files
committed
Merge pull request #28 from auth0/bugfix-json-mappings
Fix JSON mappings and auth result objects
2 parents 8cb95fb + 4f032cb commit ccb52ef

31 files changed

Lines changed: 835 additions & 296 deletions

auth0/build.gradle

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ dependencies {
99
compile "com.google.code.gson:gson:2.6.2"
1010

1111
testCompile 'junit:junit:4.12'
12-
testCompile 'org.hamcrest:hamcrest-integration:1.3'
13-
testCompile 'org.hamcrest:hamcrest-core:1.3'
14-
testCompile 'org.hamcrest:hamcrest-library:1.3'
12+
testCompile 'org.hamcrest:java-hamcrest:2.0.0.0'
1513
testCompile 'org.mockito:mockito-core:1.10.19'
1614
testCompile 'com.squareup.okhttp:mockwebserver:2.5.0'
1715
testCompile 'com.jayway.awaitility:awaitility:1.6.4'

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

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,13 @@
2828
import com.auth0.authentication.result.Credentials;
2929
import com.auth0.authentication.result.DatabaseUser;
3030
import com.auth0.authentication.result.Delegation;
31-
import com.auth0.authentication.result.JsonRequiredTypeAdapterFactory;
3231
import com.auth0.authentication.result.UserProfile;
3332
import com.auth0.request.AuthenticationRequest;
3433
import com.auth0.request.ParameterizableRequest;
3534
import com.auth0.request.Request;
3635
import com.auth0.request.internal.RequestFactory;
3736
import com.auth0.util.Telemetry;
3837
import com.google.gson.Gson;
39-
import com.google.gson.GsonBuilder;
4038
import com.squareup.okhttp.HttpUrl;
4139
import com.squareup.okhttp.OkHttpClient;
4240

@@ -90,7 +88,7 @@ public class AuthenticationAPIClient {
9088
* @param auth0 account information
9189
*/
9290
public AuthenticationAPIClient(Auth0 auth0) {
93-
this(auth0, new OkHttpClient(), buildGson());
91+
this(auth0, new OkHttpClient(), GsonProvider.buildGson());
9492
}
9593

9694
private AuthenticationAPIClient(Auth0 auth0, OkHttpClient client, Gson gson) {
@@ -117,6 +115,7 @@ public String getBaseURL() {
117115
*
118116
* @param userAgent value to send in every request to Auth0
119117
*/
118+
@SuppressWarnings("unused")
120119
public void setUserAgent(String userAgent) {
121120
factory.setUserAgent(userAgent);
122121
}
@@ -126,6 +125,7 @@ public void setUserAgent(String userAgent) {
126125
*
127126
* @param defaultDatabaseConnection name to use on every login with DB connection
128127
*/
128+
@SuppressWarnings("unused")
129129
public void setDefaultDatabaseConnection(String defaultDatabaseConnection) {
130130
this.defaultDatabaseConnection = defaultDatabaseConnection;
131131
}
@@ -149,6 +149,7 @@ public void setDefaultDatabaseConnection(String defaultDatabaseConnection) {
149149
* @param password of the user
150150
* @return a request to configure and start that will yield {@link Credentials}
151151
*/
152+
@SuppressWarnings("WeakerAccess")
152153
public AuthenticationRequest login(String usernameOrEmail, String password) {
153154
Map<String, Object> requestParameters = ParameterBuilder.newAuthenticationBuilder()
154155
.set(USERNAME_KEY, usernameOrEmail)
@@ -177,6 +178,7 @@ public AuthenticationRequest login(String usernameOrEmail, String password) {
177178
* @param connection that will be used to authenticate the user, e.g. 'facebook'
178179
* @return a request to configure and start that will yield {@link Credentials}
179180
*/
181+
@SuppressWarnings("WeakerAccess")
180182
public AuthenticationRequest loginWithOAuthAccessToken(String token, String connection) {
181183
HttpUrl url = HttpUrl.parse(auth0.getDomainUrl()).newBuilder()
182184
.addPathSegment(OAUTH_PATH)
@@ -211,6 +213,7 @@ public AuthenticationRequest loginWithOAuthAccessToken(String token, String conn
211213
* @param verificationCode sent by Auth0 via SMS
212214
* @return a request to configure and start that will yield {@link Credentials}
213215
*/
216+
@SuppressWarnings("WeakerAccess")
214217
public AuthenticationRequest loginWithPhoneNumber(String phoneNumber, String verificationCode) {
215218
Map<String, Object> parameters = ParameterBuilder.newAuthenticationBuilder()
216219
.set(USERNAME_KEY, phoneNumber)
@@ -240,6 +243,7 @@ public AuthenticationRequest loginWithPhoneNumber(String phoneNumber, String ver
240243
* @param verificationCode sent by Auth0 via Email
241244
* @return a request to configure and start that will yield {@link Credentials}
242245
*/
246+
@SuppressWarnings("WeakerAccess")
243247
public AuthenticationRequest loginWithEmail(String email, String verificationCode) {
244248
Map<String, Object> parameters = ParameterBuilder.newAuthenticationBuilder()
245249
.set(USERNAME_KEY, email)
@@ -268,6 +272,7 @@ public AuthenticationRequest loginWithEmail(String email, String verificationCod
268272
* @param idToken used to fetch it's information
269273
* @return a request to start
270274
*/
275+
@SuppressWarnings("WeakerAccess")
271276
public Request<UserProfile> tokenInfo(String idToken) {
272277
return profileRequest()
273278
.addParameter(ParameterBuilder.ID_TOKEN_KEY, idToken);
@@ -293,6 +298,7 @@ public Request<UserProfile> tokenInfo(String idToken) {
293298
* @param username of the user and must be non null
294299
* @return a request to start
295300
*/
301+
@SuppressWarnings("WeakerAccess")
296302
public DatabaseConnectionRequest<DatabaseUser> createUser(String email, String password, String username) {
297303
HttpUrl url = HttpUrl.parse(auth0.getDomainUrl()).newBuilder()
298304
.addPathSegment(DB_CONNECTIONS_PATH)
@@ -330,6 +336,7 @@ public DatabaseConnectionRequest<DatabaseUser> createUser(String email, String p
330336
* @param password of the user and must be non null
331337
* @return a request to start
332338
*/
339+
@SuppressWarnings("WeakerAccess")
333340
public DatabaseConnectionRequest<DatabaseUser> createUser(String email, String password) {
334341
return createUser(email, password, null);
335342
}
@@ -355,6 +362,7 @@ public DatabaseConnectionRequest<DatabaseUser> createUser(String email, String p
355362
* @param username of the user and must be non null
356363
* @return a request to configure and start that will yield {@link Credentials}
357364
*/
365+
@SuppressWarnings("WeakerAccess")
358366
public SignUpRequest signUp(String email, String password, String username) {
359367
final DatabaseConnectionRequest<DatabaseUser> createUserRequest = createUser(email, password, username);
360368
final AuthenticationRequest authenticationRequest = login(email, password);
@@ -381,6 +389,7 @@ public SignUpRequest signUp(String email, String password, String username) {
381389
* @param password of the user and must be non null
382390
* @return a request to configure and start that will yield {@link Credentials}
383391
*/
392+
@SuppressWarnings("WeakerAccess")
384393
public SignUpRequest signUp(String email, String password) {
385394
DatabaseConnectionRequest<DatabaseUser> createUserRequest = createUser(email, password);
386395
final AuthenticationRequest authenticationRequest = login(email, password);
@@ -404,6 +413,7 @@ public SignUpRequest signUp(String email, String password) {
404413
* @param email of the user that changes the password. It's also where the email will be sent with the link to perform the change password.
405414
* @return a request to configure and start
406415
*/
416+
@SuppressWarnings("WeakerAccess")
407417
public DatabaseConnectionRequest<Void> requestChangePassword(String email) {
408418
HttpUrl url = HttpUrl.parse(auth0.getDomainUrl()).newBuilder()
409419
.addPathSegment(DB_CONNECTIONS_PATH)
@@ -437,6 +447,7 @@ public DatabaseConnectionRequest<Void> requestChangePassword(String email) {
437447
* @param idToken issued by Auth0 for the user. The token must not be expired.
438448
* @return a request to configure and start
439449
*/
450+
@SuppressWarnings("WeakerAccess")
440451
public DelegationRequest<Delegation> delegationWithIdToken(String idToken) {
441452
ParameterizableRequest<Delegation> request = delegation(Delegation.class)
442453
.addParameter(ParameterBuilder.ID_TOKEN_KEY, idToken);
@@ -463,6 +474,7 @@ public DelegationRequest<Delegation> delegationWithIdToken(String idToken) {
463474
* @param refreshToken issued by Auth0 for the user when using the 'offline_access' scope when logging in.
464475
* @return a request to configure and start
465476
*/
477+
@SuppressWarnings("WeakerAccess")
466478
public DelegationRequest<Delegation> delegationWithRefreshToken(String refreshToken) {
467479
ParameterizableRequest<Delegation> request = delegation(Delegation.class)
468480
.addParameter(ParameterBuilder.REFRESH_TOKEN_KEY, refreshToken);
@@ -489,6 +501,7 @@ public DelegationRequest<Delegation> delegationWithRefreshToken(String refreshTo
489501
* @param apiType the delegation 'api_type' parameter
490502
* @return a request to configure and start
491503
*/
504+
@SuppressWarnings("WeakerAccess")
492505
public DelegationRequest<Map<String, Object>> delegationWithIdToken(String idToken, String apiType) {
493506
ParameterizableRequest<Map<String, Object>> request = delegation()
494507
.addParameter(ParameterBuilder.ID_TOKEN_KEY, idToken);
@@ -515,6 +528,7 @@ public DelegationRequest<Map<String, Object>> delegationWithIdToken(String idTok
515528
* @param accessToken of the main identity obtained after login
516529
* @return a request to start
517530
*/
531+
@SuppressWarnings("WeakerAccess")
518532
public Request<Void> unlink(String userId, String accessToken) {
519533
HttpUrl url = HttpUrl.parse(auth0.getDomainUrl()).newBuilder()
520534
.addPathSegment(UNLINK_PATH)
@@ -548,6 +562,7 @@ public Request<Void> unlink(String userId, String accessToken) {
548562
* @param passwordlessType indicate whether the email should contain a code, link or magic link (android & iOS)
549563
* @return a request to configure and start
550564
*/
565+
@SuppressWarnings("WeakerAccess")
551566
public ParameterizableRequest<Void> passwordlessWithEmail(String email, PasswordlessType passwordlessType) {
552567
final Map<String, Object> parameters = ParameterBuilder.newBuilder()
553568
.set(EMAIL_KEY, email)
@@ -577,6 +592,7 @@ public ParameterizableRequest<Void> passwordlessWithEmail(String email, Password
577592
* @param passwordlessType indicate whether the SMS should contain a code, link or magic link (android & iOS)
578593
* @return a request to configure and start
579594
*/
595+
@SuppressWarnings("WeakerAccess")
580596
public ParameterizableRequest<Void> passwordlessWithSMS(String phoneNumber, PasswordlessType passwordlessType) {
581597
final Map<String, Object> parameters = ParameterBuilder.newBuilder()
582598
.set(PHONE_NUMBER_KEY, phoneNumber)
@@ -605,6 +621,7 @@ public ParameterizableRequest<Void> passwordlessWithSMS(String phoneNumber, Pass
605621
*
606622
* @return a request to configure and start
607623
*/
624+
@SuppressWarnings("WeakerAccess")
608625
public ParameterizableRequest<Map<String, Object>> delegation() {
609626
HttpUrl url = HttpUrl.parse(auth0.getDomainUrl()).newBuilder()
610627
.addPathSegment(DELEGATION_PATH)
@@ -618,7 +635,7 @@ public ParameterizableRequest<Map<String, Object>> delegation() {
618635
.addParameters(parameters);
619636
}
620637

621-
protected <T> ParameterizableRequest<T> delegation(Class<T> clazz) {
638+
private <T> ParameterizableRequest<T> delegation(Class<T> clazz) {
622639
HttpUrl url = HttpUrl.parse(auth0.getDomainUrl()).newBuilder()
623640
.addPathSegment(DELEGATION_PATH)
624641
.build();
@@ -637,6 +654,7 @@ protected <T> ParameterizableRequest<T> delegation(Class<T> clazz) {
637654
*
638655
* @return a request to configure and start
639656
*/
657+
@SuppressWarnings("WeakerAccess")
640658
public ParameterizableRequest<Void> passwordless() {
641659
HttpUrl url = HttpUrl.parse(auth0.getDomainUrl()).newBuilder()
642660
.addPathSegment(PASSWORDLESS_PATH)
@@ -710,10 +728,4 @@ public AuthenticationRequest token(String authorizationCode, String codeVerifier
710728
return factory.authenticationPOST(url, client, gson)
711729
.addAuthenticationParameters(parameters);
712730
}
713-
714-
static Gson buildGson() {
715-
return new GsonBuilder()
716-
.registerTypeAdapterFactory(new JsonRequiredTypeAdapterFactory())
717-
.create();
718-
}
719731
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.auth0.authentication;
2+
3+
import com.auth0.util.JsonRequiredTypeAdapterFactory;
4+
import com.auth0.authentication.result.UserProfile;
5+
import com.google.gson.Gson;
6+
import com.google.gson.GsonBuilder;
7+
8+
abstract class GsonProvider {
9+
10+
static Gson buildGson() {
11+
return new GsonBuilder()
12+
.registerTypeAdapterFactory(new JsonRequiredTypeAdapterFactory())
13+
.registerTypeAdapter(UserProfile.class, new UserProfileDeserializer())
14+
.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
15+
.create();
16+
}
17+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.auth0.authentication;
2+
3+
import com.auth0.authentication.result.UserIdentity;
4+
import com.auth0.authentication.result.UserProfile;
5+
import com.google.gson.*;
6+
import com.google.gson.reflect.TypeToken;
7+
import com.sun.org.apache.xpath.internal.operations.Bool;
8+
9+
import java.lang.reflect.Type;
10+
import java.util.Date;
11+
import java.util.List;
12+
import java.util.Map;
13+
14+
class UserProfileDeserializer implements JsonDeserializer<UserProfile> {
15+
@Override
16+
public UserProfile deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
17+
if (!json.isJsonObject() || json.isJsonNull()) {
18+
throw new JsonParseException("user profile json is not a valid json object");
19+
}
20+
21+
JsonObject object = json.getAsJsonObject();
22+
final String id = requiredValue("user_id", String.class, object, context);
23+
final String name = requiredValue("name", String.class, object, context);
24+
final String nickname = requiredValue("nickname", String.class, object, context);
25+
final String picture = requiredValue("picture", String.class, object, context);
26+
27+
final String email = context.deserialize(object.remove("email"), String.class);
28+
final String givenName = context.deserialize(object.remove("given_name"), String.class);
29+
final String familyName = context.deserialize(object.remove("family_name"), String.class);
30+
final Boolean emailVerified = object.has("email_verified") ? context.<Boolean>deserialize(object.remove("email_verified"), Boolean.class) : false;
31+
final Date createdAt = context.deserialize(object.remove("created_at"), Date.class);
32+
33+
final Type identitiesType = new TypeToken<List<UserIdentity>>(){}.getType();
34+
final List<UserIdentity> identities = context.deserialize(object.remove("identities"), identitiesType);
35+
36+
final Type metadataType = new TypeToken<Map<String, Object>>() {}.getType();
37+
Map<String, Object> userMetadata = context.deserialize(object.remove("user_metadata"), metadataType);
38+
Map<String, Object> appMetadata = context.deserialize(object.remove("app_metadata"), metadataType);
39+
Map<String, Object> extraInfo = context.deserialize(object, metadataType);
40+
return new UserProfile(id, name, nickname, picture, email, emailVerified, familyName, createdAt, identities, extraInfo, userMetadata, appMetadata, givenName);
41+
}
42+
43+
private <T> T requiredValue(String name, Type type, JsonObject object, JsonDeserializationContext context) throws JsonParseException {
44+
T value = context.deserialize(object.remove(name), type);
45+
if (value == null) {
46+
throw new JsonParseException(String.format("Missing required attribute %s", name));
47+
}
48+
return value;
49+
}
50+
}

auth0/src/main/java/com/auth0/authentication/result/Credentials.java

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

2727

28+
import com.auth0.util.JsonRequired;
2829
import com.google.gson.annotations.SerializedName;
2930

3031
/**
@@ -37,25 +38,20 @@
3738
*/
3839
public class Credentials {
3940

40-
@SerializedName("id_token")
4141
@JsonRequired
42-
protected String idToken;
4342
@SerializedName("access_token")
44-
protected String accessToken;
43+
private String accessToken;
44+
45+
@JsonRequired
4546
@SerializedName("token_type")
46-
protected String type;
47-
@SerializedName("refresh_token")
48-
protected String refreshToken;
47+
private String type;
4948

50-
protected Credentials(Credentials credentials) {
51-
idToken = credentials.idToken;
52-
accessToken = credentials.accessToken;
53-
type = credentials.type;
54-
refreshToken = credentials.refreshToken;
55-
}
49+
@SerializedName("id_token")
50+
private String idToken;
5651

57-
protected Credentials() {
58-
}
52+
53+
@SerializedName("refresh_token")
54+
private String refreshToken;
5955

6056
public Credentials(String idToken, String accessToken, String type, String refreshToken) {
6157
this.idToken = idToken;

auth0/src/main/java/com/auth0/authentication/result/DatabaseUser.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
package com.auth0.authentication.result;
2626

27+
import com.auth0.util.JsonRequired;
2728
import com.google.gson.annotations.SerializedName;
2829

2930
/**

0 commit comments

Comments
 (0)