Light, fluffy, and always free - The AWS Local Emulator alternative
fix(cognito): omit absent clientMetadata in custom message events (#6195)
Jimmy Liu Jiae38980b6c1b1
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.javaindex 69d33796d6..5fc5d54db7 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@@ -1881,7 +1881,10 @@ final class CognitoAuthFlowHandler {Map<String, Object> req = new HashMap<>();req.put("codeParameter", CUSTOM_MESSAGE_CODE_PARAMETER);req.put("usernameParameter", user.getUsername());- req.put("clientMetadata", requestClientMetadata(clientMetadata));+ Map<String, String> metadata = requestClientMetadata(clientMetadata);+ if (metadata != null || "CustomMessage_Authentication".equals(triggerSource)) {+ req.put("clientMetadata", metadata);+ }return customMessageResponse(pool, client, user, triggerSource, req);}@@ -1895,13 +1898,16 @@ final class CognitoAuthFlowHandler {Map<String, Object> req = new HashMap<>();req.put("codeParameter", CUSTOM_MESSAGE_CODE_PARAMETER);req.put("usernameParameter", CUSTOM_MESSAGE_USERNAME_PARAMETER);- req.put("clientMetadata", requestClientMetadata(clientMetadata));+ Map<String, String> metadata = requestClientMetadata(clientMetadata);+ if (metadata != null) {+ req.put("clientMetadata", metadata);+ }return customMessageResponse(pool, null, user, "CustomMessage_AdminCreateUser", req);}/**- * A custom message or sender event's {@code clientMetadata}: the request's, or null when it- * carries none, as Cognito sends it (measured, eu-west-1, 2026-10-16 and 2026-10-20).+ * Normalizes absent or empty ClientMetadata for each trigger's event shape. CustomMessage+ * omits the member for measured sources; CustomEmailSender includes it with null.*/private static Map<String, String> requestClientMetadata(Map<String, String> clientMetadata) {return clientMetadata == null || clientMetadata.isEmpty() ? null : clientMetadata;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.javaindex 381b95214a..3907947298 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@@ -2357,20 +2357,64 @@ class CognitoLambdaTriggersTest {assertEquals(Map.of("source", "forgot"), ((Map<String, Object>) event.get("request")).get("clientMetadata"));}- /** Measured (eu-west-1, 2026-10-20): a request without ClientMetadata gives the event a null one, not {}. */+ /** Measured (eu-west-1, 2026-10-21): no ClientMetadata omits the key, including an empty map. */@Test- void customMessageWithoutClientMetadataCarriesANullOne() throws Exception {+ void customMessageSignUpOmitsMissingOrEmptyClientMetadata() throws Exception {CognitoService svc = customMessageService(mock(SesService.class),VerificationCode.Purpose.SIGNUP_CONFIRMATION, "246962");UserPoolClient client = customMessageClient(svc,customMessagePool(svc, Map.of("AutoVerifiedAttributes", List.of("email"))));ArgumentCaptor<byte[]> events = customMessageAnswers(Map.of());- svc.signUp(client.getClientId(), "spike", "Spike@Test1", Map.of("email", "[email protected]"), Map.of(), Map.of());+ svc.signUp(client.getClientId(), "spike", "Spike@Test1", Map.of("email", "[email protected]"), Map.of(), null);JsonNode request = MAPPER.readTree(events.getValue()).path("request");- assertTrue(request.has("clientMetadata"));- assertTrue(request.path("clientMetadata").isNull());+ assertFalse(request.has("clientMetadata"));++ svc.signUp(client.getClientId(), "spike2", "Spike@Test1", Map.of("email", "[email protected]"),+ Map.of(), Map.of());+ request = MAPPER.readTree(events.getValue()).path("request");+ assertFalse(request.has("clientMetadata"));+ }++ @Test+ void customMessageResendAndForgotPasswordOmitMissingClientMetadata() throws Exception {+ VerificationCodeService codes = inlineMock(VerificationCodeService.class);+ when(codes.issue(any(), any(), eq(VerificationCode.Purpose.SIGNUP_CONFIRMATION), any()))+ .thenReturn("111222");+ when(codes.issue(any(), any(), eq(VerificationCode.Purpose.PASSWORD_RESET), any()))+ .thenReturn("333444");+ CognitoService svc = createServiceWithMessaging(mock(SesService.class), mock(SnsService.class), codes);+ UserPoolClient client = customMessageClient(svc,+ customMessagePool(svc, Map.of("AutoVerifiedAttributes", List.of("email"))));+ ArgumentCaptor<byte[]> events = customMessageAnswers(Map.of());+ svc.signUp(client.getClientId(), "spike", "Spike@Test1", Map.of("email", "[email protected]"),+ Map.of(), Map.of());++ svc.resendConfirmationCode(client.getClientId(), "spike");+ JsonNode event = MAPPER.readTree(events.getValue());+ assertEquals("CustomMessage_ResendCode", event.path("triggerSource").asText());+ assertFalse(event.path("request").has("clientMetadata"));++ svc.confirmSignUp(client.getClientId(), "spike", "111222");+ svc.forgotPassword(client.getClientId(), "spike");+ event = MAPPER.readTree(events.getValue());+ assertEquals("CustomMessage_ForgotPassword", event.path("triggerSource").asText());+ assertFalse(event.path("request").has("clientMetadata"));+ }++ @Test+ void customMessageAdminCreateUserOmitsMissingClientMetadata() throws Exception {+ CognitoService svc = customMessageService(mock(SesService.class), null, null);+ UserPool pool = customMessagePool(svc, Map.of());+ ArgumentCaptor<byte[]> events = customMessageAnswers(Map.of());++ svc.adminCreateUser(pool.getId(), "spike", Map.of("email", "[email protected]"), "Spike@Test1", null,+ false, List.of("EMAIL"), Map.of(), null);++ JsonNode event = MAPPER.readTree(events.getValue());+ assertEquals("CustomMessage_AdminCreateUser", event.path("triggerSource").asText());+ assertFalse(event.path("request").has("clientMetadata"));}/** Measured (eu-west-1, 2026-10-20): a sign-in code's event carries no ClientMetadata, though InitiateAuth sends one. */