Skip to content

Commit

Permalink
added phonenumber identifier in Auth API
Browse files Browse the repository at this point in the history
  • Loading branch information
tanya732 committed Nov 25, 2024
1 parent bc505ca commit ebc882a
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 2 deletions.
36 changes: 36 additions & 0 deletions src/main/java/com/auth0/client/auth/AuthAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public class AuthAPI {
private static final String PATH_PASSWORDLESS = "passwordless";
private static final String PATH_START = "start";
private static final String KEY_ORGANIZATION = "organization";
private static final String KEY_PHONE_NUMBER = "phone_number";

private final Auth0HttpClient client;
private final String clientId;
Expand Down Expand Up @@ -485,6 +486,41 @@ public Request<Void> resetPassword(String clientId, String email, String connect
return request;
}

/**
* Creates a sign up request with the given credentials, phone number and database connection.
* "Requires Username" option must be turned on in the Connection's configuration first.
* i.e.:
* <pre>
* {@code
* try {
* Map<String, String> fields = new HashMap<String, String>();
* fields.put("age", "25);
* fields.put("city", "Buenos Aires");
* authAPI.signUp("[email protected]", "myself", new char[]{'s','e','c','r','e','t'}, "db-connection", "1234567890")
* .setCustomFields(fields)
* .execute();
* } catch (Auth0Exception e) {
* //Something happened
* }
* }
* </pre>
*
* @see <a href="https://auth0.com/docs/api/authentication#signup">Signup API docs</a>
* @param email the desired user's email.
* @param username the desired user's username.
* @param password the desired user's password.
* @param connection the database connection where the user is going to be created.
* @param phoneNumber the desired users's phone number.
* @return a Request to configure and execute.
*/
public SignUpRequest signUp(String email, String username, char[] password, String connection, String phoneNumber) {
Asserts.assertNotNull(phoneNumber, "phone number");

SignUpRequest request = this.signUp(email, username, password, connection);
request.addParameter(KEY_PHONE_NUMBER, phoneNumber);
return request;
}

/**
* Creates a sign up request with the given credentials and database connection.
* "Requires Username" option must be turned on in the Connection's configuration first.
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/auth0/json/auth/CreatedUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public class CreatedUser {
private String username;
@JsonProperty("email_verified")
private Boolean emailVerified;
@JsonProperty("phone_number")
private String phoneNumber;

@JsonProperty("_id")
@JsonAlias({"_id", "id", "user_id"})
Expand All @@ -44,4 +46,8 @@ public Boolean isEmailVerified() {
return emailVerified;
}

@JsonProperty("phone_number")
public String getPhoneNumber() {
return phoneNumber;
}
}
35 changes: 35 additions & 0 deletions src/test/java/com/auth0/client/auth/AuthAPITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,13 @@ public void shouldThrowOnUsernameSignUpWithNullConnection() {
"'connection' cannot be null!");
}

@Test
public void shouldThrowOnUsernameAndPhoneNumberSignUpWithNullPhoneNumber() {
verifyThrows(IllegalArgumentException.class,
() -> api.signUp("[email protected]", "me", new char[]{'p','4','5','5','w','0','r','d'}, "my-connection", null),
"'phone number' cannot be null!");
}

@Test
public void shouldHaveNotStrongPasswordWithDetailedDescription() throws Exception {
ObjectMapper mapper = new ObjectMapper();
Expand All @@ -424,6 +431,34 @@ public void shouldHaveNotStrongPasswordWithShortDetailedDescription() throws Exc
assertThat(ex.getDescription(), is(expectedDescription));
}

@Test
public void shouldCreateSignUpRequestWithUsernameAndPhoneNumber() throws Exception {
SignUpRequest request = api.signUp("[email protected]", "me", new char[]{'p','4','5','5','w','0','r','d'}, "db-connection", "1234567890");
assertThat(request, is(notNullValue()));

server.jsonResponse(AUTH_SIGN_UP_USERNAME, 200);
CreatedUser response = request.execute().getBody();
RecordedRequest recordedRequest = server.takeRequest();

assertThat(recordedRequest, hasMethodAndPath(HttpMethod.POST, "/dbconnections/signup"));
assertThat(recordedRequest, hasHeader("Content-Type", "application/json"));

Map<String, Object> body = bodyFromRequest(recordedRequest);
assertThat(body, hasEntry("email", "[email protected]"));
assertThat(body, hasEntry("username", "me"));
assertThat(body, hasEntry("password", "p455w0rd"));
assertThat(body, hasEntry("connection", "db-connection"));
assertThat(body, hasEntry("client_id", CLIENT_ID));
assertThat(body, hasEntry("phone_number", "1234567890"));

assertThat(response, is(notNullValue()));
assertThat(response.getUserId(), is("58457fe6b27"));
assertThat(response.getEmail(), is("[email protected]"));
assertThat(response.isEmailVerified(), is(false));
assertThat(response.getUsername(), is("me"));
assertThat(response.getPhoneNumber(), is("1234567890"));
}

@Test
public void shouldCreateSignUpRequestWithUsername() throws Exception {
@SuppressWarnings("deprecation")
Expand Down
5 changes: 3 additions & 2 deletions src/test/resources/auth/sign_up_username.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"_id": "58457fe6b27",
"email_verified": false,
"email": "[email protected]",
"username": "me"
}
"username": "me",
"phone_number": "1234567890"
}

0 comments on commit ebc882a

Please sign in to comment.