Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

feat(cognito): implement ResendConfirmationCode (#2071)

Daniel Zwickerbb7c45ce1bd1
diff --git a/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoJsonHandler.java
index 3f3ade3bb3..9cdd5df0c7 100644
--- a/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoJsonHandler.java
+++ b/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoJsonHandler.java
@@ -74,6 +74,7 @@ public class CognitoJsonHandler {
case "AdminRespondToAuthChallenge" -> handleAdminRespondToAuthChallenge(request);
case "SignUp" -> handleSignUp(request);
case "ConfirmSignUp" -> handleConfirmSignUp(request);
+ case "ResendConfirmationCode" -> handleResendConfirmationCode(request);
case "AdminConfirmSignUp" -> handleAdminConfirmSignUp(request);
case "ChangePassword" -> handleChangePassword(request);
case "ForgotPassword" -> handleForgotPassword(request);
@@ -559,6 +560,19 @@ public class CognitoJsonHandler {
return Response.ok(objectMapper.createObjectNode()).build();
}
+ private Response handleResendConfirmationCode(JsonNode request) {
+ Map<String, String> deliveryDetails = service.resendConfirmationCode(
+ request.path("ClientId").asText(),
+ request.path("Username").asText()
+ );
+ ObjectNode response = objectMapper.createObjectNode();
+ ObjectNode delivery = response.putObject("CodeDeliveryDetails");
+ delivery.put("AttributeName", deliveryDetails.get("AttributeName"));
+ delivery.put("DeliveryMedium", deliveryDetails.get("DeliveryMedium"));
+ delivery.put("Destination", deliveryDetails.get("Destination"));
+ return Response.ok(response).build();
+ }
+
private Response handleAdminConfirmSignUp(JsonNode request) {
service.adminConfirmSignUp(
request.path("UserPoolId").asText(),
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 c896757152..9433a0bbf4 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
@@ -1251,6 +1251,51 @@ public class CognitoService {
deliveryTarget.deliveryMedium(), "Destination", deliveryTarget.destination());
}
+ public Map<String, String> resendConfirmationCode(String clientId, String username) {
+ UserPoolClient client = clientStore.get(clientId)
+ .orElseThrow(() -> new AwsException("ResourceNotFoundException", "Client not found",
+ 400));
+ UserPool pool = describeUserPool(client.getUserPoolId());
+ CognitoUser user = adminGetUser(client.getUserPoolId(), username);
+ if (!"UNCONFIRMED".equals(user.getUserStatus())) {
+ throw new AwsException("NotAuthorizedException",
+ "User cannot be confirmed. Current status is " + user.getUserStatus(), 400);
+ }
+ if (!isSignUpConfirmationEnabled(pool)) {
+ throw new AwsException("InvalidParameterException",
+ "User pool does not have sign-up confirmation delivery configured", 400);
+ }
+
+ DeliveryTarget deliveryTarget = resolveSignUpDeliveryTarget(pool, user);
+ if (deliveryTarget == null) {
+ throw new AwsException("InvalidParameterException",
+ "Cannot confirm user because email or phone_number is missing", 400);
+ }
+
+ ensureVerificationWiring();
+ verificationCodeService.invalidatePrevious(pool.getId(), user.getUsername(),
+ VerificationCode.Purpose.SIGNUP_CONFIRMATION);
+ try {
+ String code = verificationCodeService.issue(pool.getId(), user.getUsername(),
+ VerificationCode.Purpose.SIGNUP_CONFIRMATION, Duration.ofHours(24));
+ messageDispatcher.dispatch(pool, user, VerificationCode.Purpose.SIGNUP_CONFIRMATION,
+ code, List.of(deliveryTarget.deliveryMedium()));
+ } catch (VerificationCodeException e) {
+ throw mapVerificationCodeException(e);
+ } catch (RuntimeException e) {
+ verificationCodeService.invalidatePrevious(pool.getId(), user.getUsername(),
+ VerificationCode.Purpose.SIGNUP_CONFIRMATION);
+ throw new AwsException("CodeDeliveryFailureException",
+ "Failed to deliver the message.", 400);
+ }
+
+ return Map.of(
+ "AttributeName", deliveryTarget.attributeName(),
+ "DeliveryMedium", deliveryTarget.deliveryMedium(),
+ "Destination", deliveryTarget.destination()
+ );
+ }
+
public void adminConfirmSignUp(String userPoolId, String username) {
CognitoUser user = adminGetUser(userPoolId, username);
user.setUserStatus("CONFIRMED");
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 f79f8ee675..cdf68885c8 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
@@ -461,6 +461,92 @@ class CognitoIntegrationTest {
assertEquals("CONFIRMED", confirmedUser.path("UserStatus").asText());
}
+ @Test
+ void resendConfirmationCodeReplacesTheSignUpCode() throws Exception {
+ given().delete("/_aws/ses").then().statusCode(200);
+
+ JsonNode poolResponse = cognitoJson("CreateUserPool", """
+ {
+ "PoolName": "ResendConfirmationCodePool",
+ "AutoVerifiedAttributes": ["email"]
+ }
+ """);
+ String resendPoolId = poolResponse.path("UserPool").path("Id").asText();
+
+ JsonNode clientResponse = cognitoJson("CreateUserPoolClient", """
+ {
+ "UserPoolId": "%s",
+ "ClientName": "resend-confirmation-code-client"
+ }
+ """.formatted(resendPoolId));
+ String resendClientId = clientResponse.path("UserPoolClient").path("ClientId").asText();
+
+ String resendUsername = "resend+" + UUID.randomUUID() + "@example.com";
+ cognitoAction("SignUp", """
+ {
+ "ClientId": "%s",
+ "Username": "%s",
+ "Password": "Passw0rd!",
+ "UserAttributes": [
+ { "Name": "email", "Value": "%s" }
+ ]
+ }
+ """.formatted(resendClientId, resendUsername, resendUsername))
+ .then()
+ .statusCode(200);
+
+ String originalCode = fetchLatestSesVerificationCode(resendUsername);
+ given().delete("/_aws/ses").then().statusCode(200);
+
+ Response resendResponse = cognitoAction("ResendConfirmationCode", """
+ {
+ "ClientId": "%s",
+ "Username": "%s"
+ }
+ """.formatted(resendClientId, resendUsername));
+ resendResponse.then().statusCode(200);
+ assertEquals("email",
+ resendResponse.jsonPath().getString("CodeDeliveryDetails.AttributeName"));
+ assertEquals("EMAIL",
+ resendResponse.jsonPath().getString("CodeDeliveryDetails.DeliveryMedium"));
+ assertThat(resendResponse.jsonPath().getString("CodeDeliveryDetails.Destination"),
+ containsString("@"));
+
+ String replacementCode = fetchLatestSesVerificationCode(resendUsername);
+ assertNotEquals(originalCode, replacementCode);
+
+ cognitoAction("ConfirmSignUp", """
+ {
+ "ClientId": "%s",
+ "Username": "%s",
+ "ConfirmationCode": "%s"
+ }
+ """.formatted(resendClientId, resendUsername, originalCode))
+ .then()
+ .statusCode(400)
+ .body("__type", org.hamcrest.Matchers.equalTo("CodeMismatchException"));
+
+ cognitoAction("ConfirmSignUp", """
+ {
+ "ClientId": "%s",
+ "Username": "%s",
+ "ConfirmationCode": "%s"
+ }
+ """.formatted(resendClientId, resendUsername, replacementCode))
+ .then()
+ .statusCode(200);
+
+ cognitoAction("ResendConfirmationCode", """
+ {
+ "ClientId": "%s",
+ "Username": "%s"
+ }
+ """.formatted(resendClientId, resendUsername))
+ .then()
+ .statusCode(400)
+ .body("__type", org.hamcrest.Matchers.equalTo("NotAuthorizedException"));
+ }
+
@Test
@Order(8)
void forgotPasswordRequiresVerifiedRecoveryDestination() throws Exception {