From ebc882a36a4bacfef022478583e46e327bf5dbbb Mon Sep 17 00:00:00 2001 From: tanya732 Date: Mon, 25 Nov 2024 13:06:47 +0530 Subject: [PATCH] added phonenumber identifier in Auth API --- .../java/com/auth0/client/auth/AuthAPI.java | 36 +++++++++++++++++++ .../java/com/auth0/json/auth/CreatedUser.java | 6 ++++ .../com/auth0/client/auth/AuthAPITest.java | 35 ++++++++++++++++++ src/test/resources/auth/sign_up_username.json | 5 +-- 4 files changed, 80 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/auth0/client/auth/AuthAPI.java b/src/main/java/com/auth0/client/auth/AuthAPI.java index a9f7a9e9..ad353a32 100644 --- a/src/main/java/com/auth0/client/auth/AuthAPI.java +++ b/src/main/java/com/auth0/client/auth/AuthAPI.java @@ -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; @@ -485,6 +486,41 @@ public Request 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.: + *
+     * {@code
+     * try {
+     *      Map fields = new HashMap();
+     *      fields.put("age", "25);
+     *      fields.put("city", "Buenos Aires");
+     *      authAPI.signUp("me@auth0.com", "myself", new char[]{'s','e','c','r','e','t'}, "db-connection", "1234567890")
+     *          .setCustomFields(fields)
+     *          .execute();
+     * } catch (Auth0Exception e) {
+     *      //Something happened
+     * }
+     * }
+     * 
+ * + * @see Signup API docs + * @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. diff --git a/src/main/java/com/auth0/json/auth/CreatedUser.java b/src/main/java/com/auth0/json/auth/CreatedUser.java index 79d05378..c2d0b6ef 100644 --- a/src/main/java/com/auth0/json/auth/CreatedUser.java +++ b/src/main/java/com/auth0/json/auth/CreatedUser.java @@ -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"}) @@ -44,4 +46,8 @@ public Boolean isEmailVerified() { return emailVerified; } + @JsonProperty("phone_number") + public String getPhoneNumber() { + return phoneNumber; + } } diff --git a/src/test/java/com/auth0/client/auth/AuthAPITest.java b/src/test/java/com/auth0/client/auth/AuthAPITest.java index 6ae92f09..535fec03 100644 --- a/src/test/java/com/auth0/client/auth/AuthAPITest.java +++ b/src/test/java/com/auth0/client/auth/AuthAPITest.java @@ -400,6 +400,13 @@ public void shouldThrowOnUsernameSignUpWithNullConnection() { "'connection' cannot be null!"); } + @Test + public void shouldThrowOnUsernameAndPhoneNumberSignUpWithNullPhoneNumber() { + verifyThrows(IllegalArgumentException.class, + () -> api.signUp("me@auth0.com", "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(); @@ -424,6 +431,34 @@ public void shouldHaveNotStrongPasswordWithShortDetailedDescription() throws Exc assertThat(ex.getDescription(), is(expectedDescription)); } + @Test + public void shouldCreateSignUpRequestWithUsernameAndPhoneNumber() throws Exception { + SignUpRequest request = api.signUp("me@auth0.com", "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 body = bodyFromRequest(recordedRequest); + assertThat(body, hasEntry("email", "me@auth0.com")); + 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("me@auth0.com")); + assertThat(response.isEmailVerified(), is(false)); + assertThat(response.getUsername(), is("me")); + assertThat(response.getPhoneNumber(), is("1234567890")); + } + @Test public void shouldCreateSignUpRequestWithUsername() throws Exception { @SuppressWarnings("deprecation") diff --git a/src/test/resources/auth/sign_up_username.json b/src/test/resources/auth/sign_up_username.json index 6baee703..83315e25 100644 --- a/src/test/resources/auth/sign_up_username.json +++ b/src/test/resources/auth/sign_up_username.json @@ -2,5 +2,6 @@ "_id": "58457fe6b27", "email_verified": false, "email": "me@auth0.com", - "username": "me" -} \ No newline at end of file + "username": "me", + "phone_number": "1234567890" +}