diff --git a/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoService.java b/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoService.java index 5b36e76a43..c896757152 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoService.java +++ b/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoService.java @@ -2488,19 +2488,15 @@ public class CognitoService { List autoVerifiedAttributes = pool.getAutoVerifiedAttributes() != null ? pool.getAutoVerifiedAttributes() : List.of(); - for (String attribute : autoVerifiedAttributes) { - if ("email".equals(attribute)) { - String email = blankToNull(attributes.get("email")); - if (email != null) { - return new DeliveryTarget("email", "EMAIL", maskEmail(email)); - } - } - if ("phone_number".equals(attribute)) { - String phoneNumber = blankToNull(attributes.get("phone_number")); - if (phoneNumber != null) { - return new DeliveryTarget("phone_number", "SMS", maskPhoneNumber(phoneNumber)); - } - } + + String phoneNumber = blankToNull(attributes.get("phone_number")); + if (autoVerifiedAttributes.contains("phone_number") && phoneNumber != null) { + return new DeliveryTarget("phone_number", "SMS", maskPhoneNumber(phoneNumber)); + } + + String email = blankToNull(attributes.get("email")); + if (autoVerifiedAttributes.contains("email") && email != null) { + return new DeliveryTarget("email", "EMAIL", maskEmail(email)); } return null; diff --git a/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoIntegrationTest.java index b9c4e5e555..f79f8ee675 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoIntegrationTest.java @@ -37,6 +37,9 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.notNullValue; @QuarkusTest @TestMethodOrder(MethodOrderer.OrderAnnotation.class) @@ -293,6 +296,51 @@ class CognitoIntegrationTest { .body("message", org.hamcrest.Matchers.equalTo("Invalid verification code provided, please try again.")); } + @Test + void signUpPrefersPhoneDeliveryWhenEmailAndPhoneAreAutoVerified() throws Exception { + Response poolResponse = cognitoAction("CreateUserPool", """ + { + "PoolName": "SignUpPhonePreferredPool", + "AutoVerifiedAttributes": ["email", "phone_number"] + } + """); + + poolResponse.then().statusCode(200); + String userPoolId = poolResponse.jsonPath().getString("UserPool.Id"); + + Response clientResponse = cognitoAction("CreateUserPoolClient", """ + { + "UserPoolId": "%s", + "ClientName": "signup-phone-preferred-client" + } + """.formatted(userPoolId)); + + clientResponse.then().statusCode(200); + String clientId = clientResponse.jsonPath().getString("UserPoolClient.ClientId"); + + Response signUpResponse = cognitoAction("SignUp", """ + { + "ClientId": "%s", + "Username": "phone-preferred-user", + "Password": "Password123!", + "UserAttributes": [ + { "Name": "email", "Value": "user@example.com" }, + { "Name": "phone_number", "Value": "+15551234567" } + ] + } + """.formatted(clientId)); + + signUpResponse.then().statusCode(200); + + assertEquals("phone_number", + signUpResponse.jsonPath().getString("CodeDeliveryDetails.AttributeName")); + assertEquals("SMS", + signUpResponse.jsonPath().getString("CodeDeliveryDetails.DeliveryMedium")); + String destination = signUpResponse.jsonPath().getString("CodeDeliveryDetails.Destination"); + assertThat(destination, notNullValue()); + assertThat(destination, containsString("4567")); + } + @Test @Order(7) void confirmSignUpRequiresValidConfirmationCode() throws Exception {