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/main/java/com/auth0/json/mgmt/organizations/EnabledConnection.java b/src/main/java/com/auth0/json/mgmt/organizations/EnabledConnection.java index af9d0588..ba144f46 100644 --- a/src/main/java/com/auth0/json/mgmt/organizations/EnabledConnection.java +++ b/src/main/java/com/auth0/json/mgmt/organizations/EnabledConnection.java @@ -20,6 +20,8 @@ public class EnabledConnection { private String connectionId; @JsonProperty("show_as_button") private Boolean showAsButton; + @JsonProperty("is_signup_enabled") + private Boolean isSignupEnabled; public EnabledConnection() {} @@ -95,4 +97,19 @@ public Boolean getShowAsButton() { public void setShowAsButton(Boolean showAsButton) { this.showAsButton = showAsButton; } + + /** + * @return whether signup is enabled for this connection. + */ + public Boolean getSignupEnabled() { + return isSignupEnabled; + } + + /** + * Sets whether signup is enabled for this connection. + * @param signupEnabled {@code true} to enable signup, {@code false} to disable it. + */ + public void setSignupEnabled(Boolean signupEnabled) { + isSignupEnabled = signupEnabled; + } } 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/java/com/auth0/json/mgmt/organizations/EnabledConnectionsPageTest.java b/src/test/java/com/auth0/json/mgmt/organizations/EnabledConnectionsPageTest.java index d52e0112..8bfe7529 100644 --- a/src/test/java/com/auth0/json/mgmt/organizations/EnabledConnectionsPageTest.java +++ b/src/test/java/com/auth0/json/mgmt/organizations/EnabledConnectionsPageTest.java @@ -12,6 +12,7 @@ public class EnabledConnectionsPageTest extends JsonTest " {\n" + " \"connection_id\": \"con_1\",\n" + " \"assign_membership_on_login\": false,\n" + + " \"is_signup_enabled\": true,\n" + " \"connection\": {\n" + " \"name\": \"google-oauth2\",\n" + " \"strategy\": \"google-oauth2\"\n" + @@ -20,6 +21,7 @@ public class EnabledConnectionsPageTest extends JsonTest " {\n" + " \"connection_id\": \"con_2\",\n" + " \"assign_membership_on_login\": true,\n" + + " \"is_signup_enabled\": true,\n" + " \"connection\": {\n" + " \"name\": \"Username-Password-Authentication\",\n" + " \"strategy\": \"auth0\"\n" + @@ -32,6 +34,7 @@ public class EnabledConnectionsPageTest extends JsonTest " {\n" + " \"connection_id\": \"con_1\",\n" + " \"assign_membership_on_login\": false,\n" + + " \"is_signup_enabled\": true,\n" + " \"connection\": {\n" + " \"name\": \"google-oauth2\",\n" + " \"strategy\": \"google-oauth2\"\n" + @@ -40,6 +43,7 @@ public class EnabledConnectionsPageTest extends JsonTest " {\n" + " \"connection_id\": \"con_2\",\n" + " \"assign_membership_on_login\": true,\n" + + " \"is_signup_enabled\": true,\n" + " \"connection\": {\n" + " \"name\": \"Username-Password-Authentication\",\n" + " \"strategy\": \"auth0\"\n" + diff --git a/src/test/java/com/auth0/json/mgmt/organizations/EnabledConnectionsTest.java b/src/test/java/com/auth0/json/mgmt/organizations/EnabledConnectionsTest.java index b232c627..49017692 100644 --- a/src/test/java/com/auth0/json/mgmt/organizations/EnabledConnectionsTest.java +++ b/src/test/java/com/auth0/json/mgmt/organizations/EnabledConnectionsTest.java @@ -22,6 +22,7 @@ public void shouldSerialize() throws Exception { EnabledConnection enabledConnection = new EnabledConnection(); enabledConnection.setAssignMembershipOnLogin(true); enabledConnection.setConnection(connection); + enabledConnection.setSignupEnabled(true); String serialized = toJSON(enabledConnection); assertThat(serialized, is(notNullValue())); @@ -39,6 +40,7 @@ public void shouldDeserialize() throws Exception { String json = "{\n" + " \"connection_id\": \"con_id\",\n" + " \"assign_membership_on_login\": false,\n" + + " \"is_signup_enabled\": true,\n" + " \"connection\": {\n" + " \"name\": \"google-oauth2\",\n" + " \"strategy\": \"google-oauth2\"\n" + @@ -49,6 +51,7 @@ public void shouldDeserialize() throws Exception { assertThat(enabledConnection, is(notNullValue())); assertThat(enabledConnection.getConnectionId(), is("con_id")); assertThat(enabledConnection.isAssignMembershipOnLogin(), is(false)); + assertThat(enabledConnection.getSignupEnabled(), is(true)); assertThat(enabledConnection.getConnection(), is(notNullValue())); assertThat(enabledConnection.getConnection().getName(), is("google-oauth2")); assertThat(enabledConnection.getConnection().getStrategy(), is("google-oauth2")); 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" +}