Skip to content

armgit / floci

Public Imported from GitHub
0

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

fix(cognito): match custom message trigger event fields (#6445)

Aniket Shukla381ad1f00b4e
diff --git a/docs/services/cognito.md b/docs/services/cognito.md
index 135765c71c..5d0a0dc8e8 100644
--- a/docs/services/cognito.md
+++ b/docs/services/cognito.md
@@ -526,6 +526,12 @@ the stored or returned value. A client that signs in with `USER_PASSWORD_AUTH`,
## Custom Message Trigger
+`CustomMessage` requests include the `{####}` code placeholder, the `{##Click Here##}`
+link placeholder, and the user's current `cognito:user_status` in `userAttributes`.
+`usernameParameter` is `null` except for an AdminCreateUser invitation, where it is
+`{username}`. The initial `response` contains `emailMessage`, `emailSubject`, and
+`smsMessage`, each set to `null` for the function to customize.
+
`CustomMessage` events omit `request.clientMetadata` when metadata is absent or empty,
including `CustomMessage_Authentication`, which does not forward `InitiateAuth`
metadata. Custom Email Sender events retain the key with `null` when
diff --git a/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoAuthFlowHandler.java b/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoAuthFlowHandler.java
index bba576e05e..91c09c9470 100644
--- a/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoAuthFlowHandler.java
+++ b/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoAuthFlowHandler.java
@@ -1727,13 +1727,21 @@ final class CognitoAuthFlowHandler {
if (user.getAttributes() != null) {
userAttributes.putAll(user.getAttributes());
}
- if ("PreAuthentication".equals(triggerKey) || "CustomEmailSender".equals(triggerKey)) {
+ if ("PreAuthentication".equals(triggerKey) || "CustomEmailSender".equals(triggerKey)
+ || "CustomMessage".equals(triggerKey)) {
userAttributes.put("cognito:user_status", user.getUserStatus());
}
req.put("userAttributes", userAttributes);
}
event.put("request", req);
- event.put("response", new HashMap<>());
+ Map<String, Object> response = new HashMap<>();
+ if ("CustomMessage".equals(triggerKey)) {
+ req.put("linkParameter", "{##Click Here##}");
+ response.put("emailMessage", null);
+ response.put("emailSubject", null);
+ response.put("smsMessage", null);
+ }
+ event.put("response", response);
return event;
}
@@ -1884,7 +1892,7 @@ final class CognitoAuthFlowHandler {
String triggerSource, Map<String, String> clientMetadata) {
Map<String, Object> req = new HashMap<>();
req.put("codeParameter", CUSTOM_MESSAGE_CODE_PARAMETER);
- req.put("usernameParameter", user.getUsername());
+ req.put("usernameParameter", null);
putCustomMessageClientMetadata(req, clientMetadata);
return customMessageResponse(pool, client, user, triggerSource, req);
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoLambdaTriggersTest.java b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoLambdaTriggersTest.java
index 59e440e0fd..7e71238655 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoLambdaTriggersTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoLambdaTriggersTest.java
@@ -55,6 +55,7 @@ import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import static io.github.hectorvent.floci.testing.InlineMocks.inlineMock;
+import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
@@ -2409,7 +2410,7 @@ class CognitoLambdaTriggersTest {
@Test
@SuppressWarnings("unchecked")
- void customMessageSignUpTriggerReceivesCodeParameterAndUsername() throws Exception {
+ void customMessageSignUpTriggerReceivesCodeParameterAndNullUsername() throws Exception {
CognitoService svc = customMessageService(mock(SesService.class),
VerificationCode.Purpose.SIGNUP_CONFIRMATION, "246962");
UserPoolClient client = customMessageClient(svc,
@@ -2422,7 +2423,7 @@ class CognitoLambdaTriggersTest {
assertEquals("CustomMessage_SignUp", event.get("triggerSource"));
Map<String, Object> request = (Map<String, Object>) event.get("request");
assertEquals("{####}", request.get("codeParameter"));
- assertEquals("spike", request.get("usernameParameter"));
+ assertCustomMessageShape(events.getValue(), "UNCONFIRMED", null);
}
@Test
@@ -2457,6 +2458,27 @@ class CognitoLambdaTriggersTest {
"Your verification code is 246962.");
}
+ @Test
+ void customMessageEchoKeepsTheDefaultSignUpMessage() throws Exception {
+ SesService ses = mock(SesService.class);
+ CognitoService svc = customMessageService(ses, VerificationCode.Purpose.SIGNUP_CONFIRMATION, "246962");
+ UserPoolClient client = customMessageClient(svc,
+ customMessagePool(svc, Map.of("AutoVerifiedAttributes", List.of("email"))));
+ when(lambdaService.invoke(anyString(), eq(CUSTOM_MESSAGE), any(byte[].class),
+ eq(InvocationType.RequestResponse))).thenAnswer(invocation -> {
+ byte[] payload = invocation.getArgument(2);
+ assertCustomMessageShape(payload, "UNCONFIRMED", null);
+ return new InvokeResult(200, null, payload, null, "req-id");
+ });
+
+ svc.signUp(client.getClientId(), "spike", "Spike@Test1", Map.of("email", "[email protected]"), Map.of(), Map.of());
+
+ assertVerificationEmail(ses, "[email protected]", "Your verification code",
+ "Your verification code is 246962.");
+ verify(lambdaService).invoke(anyString(), eq(CUSTOM_MESSAGE), any(byte[].class),
+ eq(InvocationType.RequestResponse));
+ }
+
@Test
void customMessageDoesNotFireWhenNotConfigured() {
SesService ses = mock(SesService.class);
@@ -2488,6 +2510,7 @@ class CognitoLambdaTriggersTest {
Map<String, Object> event = MAPPER.readValue(events.getValue(), new TypeReference<>() {});
assertEquals("CustomMessage_ResendCode", event.get("triggerSource"));
assertEquals(Map.of("source", "resend"), ((Map<String, Object>) event.get("request")).get("clientMetadata"));
+ assertCustomMessageShape(events.getValue(), "UNCONFIRMED", null);
}
@Test
@@ -2507,6 +2530,7 @@ class CognitoLambdaTriggersTest {
Map<String, Object> event = MAPPER.readValue(events.getValue(), new TypeReference<>() {});
assertEquals("CustomMessage_ForgotPassword", event.get("triggerSource"));
assertEquals(Map.of("source", "forgot"), ((Map<String, Object>) event.get("request")).get("clientMetadata"));
+ assertCustomMessageShape(events.getValue(), "CONFIRMED", null);
}
/** Measured (eu-west-1, 2026-10-21): the event carries the user as the whole update leaves them. */
@@ -2525,6 +2549,7 @@ class CognitoLambdaTriggersTest {
JsonNode event = MAPPER.readTree(events.getValue());
assertEquals("CustomMessage_UpdateUserAttribute", event.path("triggerSource").asText());
+ assertCustomMessageShape(events.getValue(), "CONFIRMED", null);
assertEquals(client.getClientId(), event.path("callerContext").path("clientId").asText());
JsonNode request = event.path("request");
assertEquals("{####}", request.path("codeParameter").asText());
@@ -2550,6 +2575,7 @@ class CognitoLambdaTriggersTest {
JsonNode event = MAPPER.readTree(events.getValue());
assertEquals("CustomMessage_VerifyUserAttribute", event.path("triggerSource").asText());
+ assertCustomMessageShape(events.getValue(), "CONFIRMED", null);
assertEquals(client.getClientId(), event.path("callerContext").path("clientId").asText());
JsonNode request = event.path("request");
assertEquals("{####}", request.path("codeParameter").asText());
@@ -2829,6 +2855,7 @@ class CognitoLambdaTriggersTest {
JsonNode event = MAPPER.readTree(events.getValue());
assertEquals("CustomMessage_Authentication", event.path("triggerSource").asText());
assertFalse(event.path("request").has("clientMetadata"));
+ assertCustomMessageShape(events.getValue(), "CONFIRMED", null);
}
@Test
@@ -2863,6 +2890,22 @@ class CognitoLambdaTriggersTest {
assertEquals("{####}", request.get("codeParameter"));
assertEquals("{username}", request.get("usernameParameter"));
assertEquals(Map.of("source", "admin"), request.get("clientMetadata"));
+ assertCustomMessageShape(events.getValue(), "FORCE_CHANGE_PASSWORD", "{username}");
+ }
+
+ private static void assertCustomMessageShape(byte[] payload, String status, String usernameParameter)
+ throws Exception {
+ JsonNode event = MAPPER.readTree(payload);
+ JsonNode request = event.path("request");
+ JsonNode expectedUsername = usernameParameter == null ? MAPPER.nullNode()
+ : MAPPER.getNodeFactory().textNode(usernameParameter);
+ ObjectNode response = MAPPER.createObjectNode().putNull("emailMessage")
+ .putNull("emailSubject").putNull("smsMessage");
+ assertAll(event.path("triggerSource").asText(),
+ () -> assertEquals(expectedUsername, request.get("usernameParameter")),
+ () -> assertEquals("{##Click Here##}", request.path("linkParameter").asText()),
+ () -> assertEquals(status, request.path("userAttributes").path("cognito:user_status").asText()),
+ () -> assertEquals(response, event.path("response")));
}
@Test