mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 06:00:46 -05:00
fix(cognito): pass ClientMetadata to the CustomMessage trigger for ResendCode and ForgotPassword (#6108)
* fix(cognito): pass ClientMetadata to the CustomMessage trigger for ResendCode and ForgotPassword
The developer guide lists ResendConfirmationCode and ForgotPassword among the
calls whose ClientMetadata reaches the CustomMessage trigger, and Floci passed
an empty map there. Measured on Cognito, the event's clientMetadata is also
null rather than {} when the request carries none, and for a sign-in code
(CustomMessage_Authentication) even when InitiateAuth sends one. The
CustomMessage event now follows both through one helper, shared with the
CustomEmailSender event, which applied the same rule inline, so the two cannot
drift.
* test(cognito): share one CustomMessage setup across the trigger tests
The CustomMessage tests each built the same fixture by hand: the verification
code mock, a pool whose CustomMessage trigger is the test function, its
client, and the captured invocation. They now share customMessageService,
customMessagePool, customMessageClient and customMessageAnswers, the four new
tests of this change included, as the review asked.
This commit is contained in:
1 parent
1108bc75dc
commit
538a81d871
3 files changed
+138
-138
No files matched your search
+15
-6
@@ -1017,7 +1017,7 @@ final class CognitoAuthFlowHandler {
|
||||
VerificationCode.Purpose purpose = "EMAIL_OTP".equals(challengeName)
|
||||
? VerificationCode.Purpose.EMAIL_OTP : VerificationCode.Purpose.SMS_OTP;
|
||||
Map<String, Object> customMessage = customEmailSenderTakesOver(pool, deliveryMedium) ? Map.of()
|
||||
: fireCustomMessage(pool, client, user, "CustomMessage_Authentication", Map.of());
|
||||
: fireCustomMessage(pool, client, user, "CustomMessage_Authentication", null);
|
||||
Map<String, String> challengeParams = new HashMap<>(
|
||||
service.issueSignInOtp(pool, client, user, purpose, attributeName, deliveryMedium, customMessage));
|
||||
challengeParams.put("USERNAME", user.getUsername());
|
||||
@@ -1873,14 +1873,15 @@ final class CognitoAuthFlowHandler {
|
||||
* Fires the CustomMessage trigger, if configured, so the function can override the
|
||||
* subject/body Cognito would otherwise deliver. Non-blocking: an unconfigured or
|
||||
* failing trigger falls back to the pool's default templated message, mirroring how
|
||||
* {@link #firePostConfirmation} tolerates errors.
|
||||
* {@link #firePostConfirmation} tolerates errors. {@code clientMetadata} is the request's
|
||||
* ClientMetadata; a sign-in code's event carries none (measured, eu-west-1, 2026-10-20).
|
||||
*/
|
||||
Map<String, Object> fireCustomMessage(UserPool pool, UserPoolClient client, CognitoUser user,
|
||||
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("clientMetadata", clientMetadata == null ? Map.of() : clientMetadata);
|
||||
req.put("clientMetadata", requestClientMetadata(clientMetadata));
|
||||
return customMessageResponse(pool, client, user, triggerSource, req);
|
||||
}
|
||||
|
||||
@@ -1894,10 +1895,18 @@ 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", clientMetadata == null ? Map.of() : clientMetadata);
|
||||
req.put("clientMetadata", requestClientMetadata(clientMetadata));
|
||||
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).
|
||||
*/
|
||||
private static Map<String, String> requestClientMetadata(Map<String, String> clientMetadata) {
|
||||
return clientMetadata == null || clientMetadata.isEmpty() ? null : clientMetadata;
|
||||
}
|
||||
|
||||
private Map<String, Object> customMessageResponse(UserPool pool, UserPoolClient client, CognitoUser user,
|
||||
String triggerSource, Map<String, Object> request) {
|
||||
TriggerResult result = invokeTrigger(pool, client, user, "CustomMessage", triggerSource, request);
|
||||
@@ -1932,7 +1941,7 @@ final class CognitoAuthFlowHandler {
|
||||
Map<String, Object> req = new HashMap<>();
|
||||
req.put("type", "customEmailSenderRequestV1");
|
||||
req.put("code", encryptedCode);
|
||||
req.put("clientMetadata", clientMetadata == null || clientMetadata.isEmpty() ? null : clientMetadata);
|
||||
req.put("clientMetadata", requestClientMetadata(clientMetadata));
|
||||
byte[] payload;
|
||||
try {
|
||||
payload = MAPPER.writeValueAsBytes(
|
||||
@@ -2186,7 +2195,7 @@ final class CognitoAuthFlowHandler {
|
||||
}
|
||||
boolean sms = "SMS_MFA".equals(factor);
|
||||
Map<String, Object> customMessage = customEmailSenderTakesOver(pool, sms ? "SMS" : "EMAIL") ? Map.of()
|
||||
: fireCustomMessage(pool, client, user, "CustomMessage_Authentication", Map.of());
|
||||
: fireCustomMessage(pool, client, user, "CustomMessage_Authentication", null);
|
||||
Map<String, String> parameters = new HashMap<>(service.issueMfaCode(pool, client, user,
|
||||
sms ? VerificationCode.Purpose.SMS_MFA : VerificationCode.Purpose.EMAIL_MFA,
|
||||
sms ? "phone_number" : "email", sms ? "SMS" : "EMAIL", customMessage));
|
||||
|
||||
@@ -3561,7 +3561,7 @@ public class CognitoService implements ResourceProvider {
|
||||
deliveryTarget.deliveryMedium(), "CustomEmailSender_ResendCode",
|
||||
VerificationCode.Purpose.SIGNUP_CONFIRMATION, code, clientMetadata)) {
|
||||
Map<String, Object> customMessage = authFlowHandler.fireCustomMessage(
|
||||
pool, client, user, "CustomMessage_ResendCode", Map.of());
|
||||
pool, client, user, "CustomMessage_ResendCode", clientMetadata);
|
||||
messageDispatcher.dispatch(pool, user, VerificationCode.Purpose.SIGNUP_CONFIRMATION,
|
||||
code, List.of(deliveryTarget.deliveryMedium()), customMessage);
|
||||
}
|
||||
@@ -3979,7 +3979,7 @@ public class CognitoService implements ResourceProvider {
|
||||
deliveryTarget.deliveryMedium(), "CustomEmailSender_ForgotPassword",
|
||||
VerificationCode.Purpose.PASSWORD_RESET, code, clientMetadata)) {
|
||||
Map<String, Object> customMessage = authFlowHandler.fireCustomMessage(
|
||||
pool, client, user, "CustomMessage_ForgotPassword", Map.of());
|
||||
pool, client, user, "CustomMessage_ForgotPassword", clientMetadata);
|
||||
messageDispatcher.dispatch(pool, user, VerificationCode.Purpose.PASSWORD_RESET, code,
|
||||
List.of(deliveryTarget.deliveryMedium()), customMessage);
|
||||
}
|
||||
|
||||
+121
-130
@@ -2209,25 +2209,46 @@ class CognitoLambdaTriggersTest {
|
||||
// CustomMessage
|
||||
// =========================================================================
|
||||
|
||||
private static final String CUSTOM_MESSAGE = "arn:aws:lambda:::custom-message";
|
||||
|
||||
/** A service that delivers through {@code ses} and issues {@code code} for every {@code purpose} code. */
|
||||
private CognitoService customMessageService(SesService ses, VerificationCode.Purpose purpose, String code) {
|
||||
VerificationCodeService verificationCodeService = inlineMock(VerificationCodeService.class);
|
||||
if (purpose != null) {
|
||||
when(verificationCodeService.issue(any(), any(), eq(purpose), any())).thenReturn(code);
|
||||
}
|
||||
return createServiceWithMessaging(ses, mock(SnsService.class), verificationCodeService);
|
||||
}
|
||||
|
||||
/** A pool with {@code settings} whose CustomMessage trigger is {@link #CUSTOM_MESSAGE}. */
|
||||
private static UserPool customMessagePool(CognitoService svc, Map<String, Object> settings) {
|
||||
Map<String, Object> request = new HashMap<>(settings);
|
||||
request.put("PoolName", "trigger-pool");
|
||||
request.put("LambdaConfig", Map.of("CustomMessage", CUSTOM_MESSAGE));
|
||||
return svc.createUserPool(request, "us-east-1");
|
||||
}
|
||||
|
||||
private static UserPoolClient customMessageClient(CognitoService svc, UserPool pool) {
|
||||
return svc.createUserPoolClient(pool.getId(), "c", false, false, List.of(), List.of());
|
||||
}
|
||||
|
||||
/** Answers the CustomMessage function with {@code response} and captures the events it is sent. */
|
||||
private ArgumentCaptor<byte[]> customMessageAnswers(Map<String, Object> response) {
|
||||
ArgumentCaptor<byte[]> events = ArgumentCaptor.forClass(byte[].class);
|
||||
when(lambdaService.invoke(anyString(), eq(CUSTOM_MESSAGE), events.capture(),
|
||||
eq(InvocationType.RequestResponse))).thenReturn(ok(response));
|
||||
return events;
|
||||
}
|
||||
|
||||
@Test
|
||||
void customMessageOverridesSignUpVerificationEmail() {
|
||||
SesService ses = mock(SesService.class);
|
||||
VerificationCodeService verificationCodeService = inlineMock(VerificationCodeService.class);
|
||||
when(verificationCodeService.issue(any(), any(), eq(VerificationCode.Purpose.SIGNUP_CONFIRMATION), any()))
|
||||
.thenReturn("246962");
|
||||
CognitoService svc = createServiceWithMessaging(ses, mock(SnsService.class), verificationCodeService);
|
||||
|
||||
UserPool pool = svc.createUserPool(Map.of(
|
||||
"PoolName", "trigger-pool",
|
||||
"AutoVerifiedAttributes", List.of("email"),
|
||||
"LambdaConfig", Map.of("CustomMessage", "arn:aws:lambda:::custom-message")), "us-east-1");
|
||||
UserPoolClient client = svc.createUserPoolClient(pool.getId(), "c", false, false, List.of(), List.of());
|
||||
|
||||
when(lambdaService.invoke(anyString(), eq("arn:aws:lambda:::custom-message"),
|
||||
any(byte[].class), eq(InvocationType.RequestResponse)))
|
||||
.thenReturn(ok(Map.of(
|
||||
"emailSubject", "TRIGGER-FIRED",
|
||||
"emailMessage", "TRIGGER-FIRED code={####}")));
|
||||
CognitoService svc = customMessageService(ses, VerificationCode.Purpose.SIGNUP_CONFIRMATION, "246962");
|
||||
UserPoolClient client = customMessageClient(svc,
|
||||
customMessagePool(svc, Map.of("AutoVerifiedAttributes", List.of("email"))));
|
||||
customMessageAnswers(Map.of(
|
||||
"emailSubject", "TRIGGER-FIRED",
|
||||
"emailMessage", "TRIGGER-FIRED code={####}"));
|
||||
|
||||
svc.signUp(client.getClientId(), "spike", "Spike@Test1", Map.of("email", "spike@example.com"), Map.of(), Map.of());
|
||||
|
||||
@@ -2237,26 +2258,15 @@ class CognitoLambdaTriggersTest {
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
void customMessageSignUpTriggerReceivesCodeParameterAndUsername() throws Exception {
|
||||
VerificationCodeService verificationCodeService = inlineMock(VerificationCodeService.class);
|
||||
when(verificationCodeService.issue(any(), any(), eq(VerificationCode.Purpose.SIGNUP_CONFIRMATION), any()))
|
||||
.thenReturn("246962");
|
||||
CognitoService svc = createServiceWithMessaging(mock(SesService.class), mock(SnsService.class),
|
||||
verificationCodeService);
|
||||
|
||||
UserPool pool = svc.createUserPool(Map.of(
|
||||
"PoolName", "trigger-pool",
|
||||
"AutoVerifiedAttributes", List.of("email"),
|
||||
"LambdaConfig", Map.of("CustomMessage", "arn:aws:lambda:::custom-message")), "us-east-1");
|
||||
UserPoolClient client = svc.createUserPoolClient(pool.getId(), "c", false, false, List.of(), List.of());
|
||||
|
||||
ArgumentCaptor<byte[]> payloadCaptor = ArgumentCaptor.forClass(byte[].class);
|
||||
when(lambdaService.invoke(anyString(), eq("arn:aws:lambda:::custom-message"),
|
||||
payloadCaptor.capture(), eq(InvocationType.RequestResponse)))
|
||||
.thenReturn(ok(Map.of()));
|
||||
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", "spike@example.com"), Map.of(), Map.of());
|
||||
|
||||
Map<String, Object> event = MAPPER.readValue(payloadCaptor.getValue(), new TypeReference<>() {});
|
||||
Map<String, Object> event = MAPPER.readValue(events.getValue(), new TypeReference<>() {});
|
||||
assertEquals("CustomMessage_SignUp", event.get("triggerSource"));
|
||||
Map<String, Object> request = (Map<String, Object>) event.get("request");
|
||||
assertEquals("{####}", request.get("codeParameter"));
|
||||
@@ -2265,27 +2275,16 @@ class CognitoLambdaTriggersTest {
|
||||
|
||||
@Test
|
||||
void customMessageSignUpTriggerCarriesClientMetadata() throws Exception {
|
||||
VerificationCodeService verificationCodeService = inlineMock(VerificationCodeService.class);
|
||||
when(verificationCodeService.issue(any(), any(), eq(VerificationCode.Purpose.SIGNUP_CONFIRMATION), any()))
|
||||
.thenReturn("246962");
|
||||
CognitoService svc = createServiceWithMessaging(mock(SesService.class), mock(SnsService.class),
|
||||
verificationCodeService);
|
||||
|
||||
UserPool pool = svc.createUserPool(Map.of(
|
||||
"PoolName", "trigger-pool",
|
||||
"AutoVerifiedAttributes", List.of("email"),
|
||||
"LambdaConfig", Map.of("CustomMessage", "arn:aws:lambda:::custom-message")), "us-east-1");
|
||||
UserPoolClient client = svc.createUserPoolClient(pool.getId(), "c", false, false, List.of(), List.of());
|
||||
|
||||
ArgumentCaptor<byte[]> payloadCaptor = ArgumentCaptor.forClass(byte[].class);
|
||||
when(lambdaService.invoke(anyString(), eq("arn:aws:lambda:::custom-message"),
|
||||
payloadCaptor.capture(), eq(InvocationType.RequestResponse)))
|
||||
.thenReturn(ok(Map.of()));
|
||||
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", "spike@example.com"),
|
||||
Map.of(), Map.of("source", "signup-form"));
|
||||
|
||||
JsonNode event = MAPPER.readTree(payloadCaptor.getValue());
|
||||
JsonNode event = MAPPER.readTree(events.getValue());
|
||||
assertEquals("CustomMessage_SignUp", event.path("triggerSource").asText());
|
||||
assertEquals("signup-form", event.path("request").path("clientMetadata").path("source").asText());
|
||||
}
|
||||
@@ -2293,18 +2292,10 @@ class CognitoLambdaTriggersTest {
|
||||
@Test
|
||||
void customMessageLambdaErrorFallsBackToDefaultSignUpMessage() {
|
||||
SesService ses = mock(SesService.class);
|
||||
VerificationCodeService verificationCodeService = inlineMock(VerificationCodeService.class);
|
||||
when(verificationCodeService.issue(any(), any(), eq(VerificationCode.Purpose.SIGNUP_CONFIRMATION), any()))
|
||||
.thenReturn("246962");
|
||||
CognitoService svc = createServiceWithMessaging(ses, mock(SnsService.class), verificationCodeService);
|
||||
|
||||
UserPool pool = svc.createUserPool(Map.of(
|
||||
"PoolName", "trigger-pool",
|
||||
"AutoVerifiedAttributes", List.of("email"),
|
||||
"LambdaConfig", Map.of("CustomMessage", "arn:aws:lambda:::custom-message")), "us-east-1");
|
||||
UserPoolClient client = svc.createUserPoolClient(pool.getId(), "c", false, false, List.of(), List.of());
|
||||
|
||||
when(lambdaService.invoke(anyString(), eq("arn:aws:lambda:::custom-message"), any(byte[].class), any()))
|
||||
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), any()))
|
||||
.thenReturn(lambdaError("Unhandled"));
|
||||
|
||||
// SignUp must still succeed and deliver the default message when CustomMessage errors.
|
||||
@@ -2317,15 +2308,11 @@ class CognitoLambdaTriggersTest {
|
||||
@Test
|
||||
void customMessageDoesNotFireWhenNotConfigured() {
|
||||
SesService ses = mock(SesService.class);
|
||||
VerificationCodeService verificationCodeService = inlineMock(VerificationCodeService.class);
|
||||
when(verificationCodeService.issue(any(), any(), eq(VerificationCode.Purpose.SIGNUP_CONFIRMATION), any()))
|
||||
.thenReturn("246962");
|
||||
CognitoService svc = createServiceWithMessaging(ses, mock(SnsService.class), verificationCodeService);
|
||||
|
||||
CognitoService svc = customMessageService(ses, VerificationCode.Purpose.SIGNUP_CONFIRMATION, "246962");
|
||||
UserPool pool = svc.createUserPool(Map.of(
|
||||
"PoolName", "trigger-pool",
|
||||
"AutoVerifiedAttributes", List.of("email")), "us-east-1");
|
||||
UserPoolClient client = svc.createUserPoolClient(pool.getId(), "c", false, false, List.of(), List.of());
|
||||
UserPoolClient client = customMessageClient(svc, pool);
|
||||
|
||||
svc.signUp(client.getClientId(), "spike", "Spike@Test1", Map.of("email", "spike@example.com"), Map.of(), Map.of());
|
||||
|
||||
@@ -2336,72 +2323,85 @@ class CognitoLambdaTriggersTest {
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
void customMessageFiresWithResendCodeTriggerSourceOnResendConfirmationCode() throws Exception {
|
||||
VerificationCodeService verificationCodeService = inlineMock(VerificationCodeService.class);
|
||||
when(verificationCodeService.issue(any(), any(), eq(VerificationCode.Purpose.SIGNUP_CONFIRMATION), any()))
|
||||
.thenReturn("111222");
|
||||
CognitoService svc = createServiceWithMessaging(mock(SesService.class), mock(SnsService.class),
|
||||
verificationCodeService);
|
||||
|
||||
UserPool pool = svc.createUserPool(Map.of(
|
||||
"PoolName", "trigger-pool",
|
||||
"AutoVerifiedAttributes", List.of("email"),
|
||||
"LambdaConfig", Map.of("CustomMessage", "arn:aws:lambda:::custom-message")), "us-east-1");
|
||||
UserPoolClient client = svc.createUserPoolClient(pool.getId(), "c", false, false, List.of(), List.of());
|
||||
void customMessageFiresWithResendCodeTriggerSourceAndClientMetadataOnResendConfirmationCode() throws Exception {
|
||||
CognitoService svc = customMessageService(mock(SesService.class),
|
||||
VerificationCode.Purpose.SIGNUP_CONFIRMATION, "111222");
|
||||
UserPoolClient client = customMessageClient(svc,
|
||||
customMessagePool(svc, Map.of("AutoVerifiedAttributes", List.of("email"))));
|
||||
svc.signUp(client.getClientId(), "spike", "Spike@Test1", Map.of("email", "spike@example.com"), Map.of(), Map.of());
|
||||
ArgumentCaptor<byte[]> events = customMessageAnswers(Map.of());
|
||||
|
||||
ArgumentCaptor<byte[]> payloadCaptor = ArgumentCaptor.forClass(byte[].class);
|
||||
when(lambdaService.invoke(anyString(), eq("arn:aws:lambda:::custom-message"),
|
||||
payloadCaptor.capture(), eq(InvocationType.RequestResponse)))
|
||||
.thenReturn(ok(Map.of()));
|
||||
svc.resendConfirmationCode(client.getClientId(), "spike", Map.of("source", "resend"));
|
||||
|
||||
svc.resendConfirmationCode(client.getClientId(), "spike");
|
||||
|
||||
Map<String, Object> event = MAPPER.readValue(payloadCaptor.getValue(), new TypeReference<>() {});
|
||||
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"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
void customMessageFiresWithForgotPasswordTriggerSourceOnForgotPassword() throws Exception {
|
||||
VerificationCodeService verificationCodeService = inlineMock(VerificationCodeService.class);
|
||||
when(verificationCodeService.issue(any(), any(), eq(VerificationCode.Purpose.PASSWORD_RESET), any()))
|
||||
.thenReturn("333444");
|
||||
CognitoService svc = createServiceWithMessaging(mock(SesService.class), mock(SnsService.class),
|
||||
verificationCodeService);
|
||||
|
||||
UserPool pool = svc.createUserPool(Map.of(
|
||||
"PoolName", "trigger-pool",
|
||||
"LambdaConfig", Map.of("CustomMessage", "arn:aws:lambda:::custom-message")), "us-east-1");
|
||||
UserPoolClient client = svc.createUserPoolClient(pool.getId(), "c", false, false, List.of(), List.of());
|
||||
void customMessageFiresWithForgotPasswordTriggerSourceAndClientMetadataOnForgotPassword() throws Exception {
|
||||
CognitoService svc = customMessageService(mock(SesService.class),
|
||||
VerificationCode.Purpose.PASSWORD_RESET, "333444");
|
||||
UserPool pool = customMessagePool(svc, Map.of());
|
||||
UserPoolClient client = customMessageClient(svc, pool);
|
||||
svc.adminCreateUser(pool.getId(), "spike",
|
||||
Map.of("email", "spike@example.com", "email_verified", "true"), null);
|
||||
svc.adminSetUserPassword(pool.getId(), "spike", "Spike@Test1", true);
|
||||
ArgumentCaptor<byte[]> events = customMessageAnswers(Map.of());
|
||||
|
||||
ArgumentCaptor<byte[]> payloadCaptor = ArgumentCaptor.forClass(byte[].class);
|
||||
when(lambdaService.invoke(anyString(), eq("arn:aws:lambda:::custom-message"),
|
||||
payloadCaptor.capture(), eq(InvocationType.RequestResponse)))
|
||||
.thenReturn(ok(Map.of()));
|
||||
svc.forgotPassword(client.getClientId(), "spike", Map.of("source", "forgot"));
|
||||
|
||||
svc.forgotPassword(client.getClientId(), "spike");
|
||||
|
||||
Map<String, Object> event = MAPPER.readValue(payloadCaptor.getValue(), new TypeReference<>() {});
|
||||
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"));
|
||||
}
|
||||
|
||||
/** Measured (eu-west-1, 2026-10-20): a request without ClientMetadata gives the event a null one, not {}. */
|
||||
@Test
|
||||
void customMessageWithoutClientMetadataCarriesANullOne() 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", "spike@example.com"), Map.of(), Map.of());
|
||||
|
||||
JsonNode request = MAPPER.readTree(events.getValue()).path("request");
|
||||
assertTrue(request.has("clientMetadata"));
|
||||
assertTrue(request.path("clientMetadata").isNull());
|
||||
}
|
||||
|
||||
/** Measured (eu-west-1, 2026-10-20): a sign-in code's event carries no ClientMetadata, though InitiateAuth sends one. */
|
||||
@Test
|
||||
void customMessageForASignInCodeCarriesANullClientMetadata() throws Exception {
|
||||
CognitoService svc = customMessageService(mock(SesService.class), VerificationCode.Purpose.EMAIL_OTP, "555666");
|
||||
UserPool pool = customMessagePool(svc, Map.of("Policies", Map.of("SignInPolicy",
|
||||
Map.of("AllowedFirstAuthFactors", List.of("PASSWORD", "EMAIL_OTP")))));
|
||||
UserPoolClient client = svc.createUserPoolClient(pool.getId(), "c", false, false, List.of(), List.of(),
|
||||
null, List.of(), null, List.of("ALLOW_USER_AUTH"), null, null, List.of(), null, List.of(), null,
|
||||
null, null, List.of(), null, null);
|
||||
svc.adminCreateUser(pool.getId(), "spike", Map.of("email", "spike@example.com", "email_verified", "true"), null);
|
||||
svc.adminSetUserPassword(pool.getId(), "spike", "Spike@Test1", true);
|
||||
ArgumentCaptor<byte[]> events = customMessageAnswers(Map.of());
|
||||
|
||||
svc.initiateAuth(client.getClientId(), "USER_AUTH",
|
||||
Map.of("USERNAME", "spike", "PREFERRED_CHALLENGE", "EMAIL_OTP"), Map.of("app", "web"));
|
||||
|
||||
JsonNode event = MAPPER.readTree(events.getValue());
|
||||
assertEquals("CustomMessage_Authentication", event.path("triggerSource").asText());
|
||||
assertTrue(event.path("request").path("clientMetadata").isNull());
|
||||
}
|
||||
|
||||
@Test
|
||||
void customMessageOverridesTheAdminCreateUserInvitation() {
|
||||
SesService ses = mock(SesService.class);
|
||||
CognitoService svc = createServiceWithMessaging(ses, mock(SnsService.class),
|
||||
inlineMock(VerificationCodeService.class));
|
||||
UserPool pool = svc.createUserPool(Map.of(
|
||||
"PoolName", "trigger-pool",
|
||||
"LambdaConfig", Map.of("CustomMessage", "arn:aws:lambda:::custom-message")), "us-east-1");
|
||||
when(lambdaService.invoke(anyString(), eq("arn:aws:lambda:::custom-message"),
|
||||
any(byte[].class), eq(InvocationType.RequestResponse)))
|
||||
.thenReturn(ok(Map.of(
|
||||
"emailSubject", "Welcome",
|
||||
"emailMessage", "Sign in as {username} with {####}")));
|
||||
CognitoService svc = customMessageService(ses, null, null);
|
||||
UserPool pool = customMessagePool(svc, Map.of());
|
||||
customMessageAnswers(Map.of(
|
||||
"emailSubject", "Welcome",
|
||||
"emailMessage", "Sign in as {username} with {####}"));
|
||||
|
||||
svc.adminCreateUser(pool.getId(), "spike", Map.of("email", "spike@example.com"), "Spike@Test1", null,
|
||||
false, List.of("EMAIL"), Map.of(), Map.of());
|
||||
@@ -2412,20 +2412,14 @@ class CognitoLambdaTriggersTest {
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
void customMessageAdminCreateUserTriggerReceivesPlaceholdersAndClientMetadata() throws Exception {
|
||||
CognitoService svc = createServiceWithMessaging(mock(SesService.class), mock(SnsService.class),
|
||||
inlineMock(VerificationCodeService.class));
|
||||
UserPool pool = svc.createUserPool(Map.of(
|
||||
"PoolName", "trigger-pool",
|
||||
"LambdaConfig", Map.of("CustomMessage", "arn:aws:lambda:::custom-message")), "us-east-1");
|
||||
ArgumentCaptor<byte[]> payloadCaptor = ArgumentCaptor.forClass(byte[].class);
|
||||
when(lambdaService.invoke(anyString(), eq("arn:aws:lambda:::custom-message"),
|
||||
payloadCaptor.capture(), eq(InvocationType.RequestResponse)))
|
||||
.thenReturn(ok(Map.of()));
|
||||
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", "spike@example.com"), "Spike@Test1", null,
|
||||
false, List.of("EMAIL"), Map.of(), Map.of("source", "admin"));
|
||||
|
||||
Map<String, Object> event = MAPPER.readValue(payloadCaptor.getValue(), new TypeReference<>() {});
|
||||
Map<String, Object> event = MAPPER.readValue(events.getValue(), new TypeReference<>() {});
|
||||
assertEquals("CustomMessage_AdminCreateUser", event.get("triggerSource"));
|
||||
assertEquals("CLIENT_ID_NOT_APPLICABLE", ((Map<String, Object>) event.get("callerContext")).get("clientId"));
|
||||
Map<String, Object> request = (Map<String, Object>) event.get("request");
|
||||
@@ -2436,11 +2430,8 @@ class CognitoLambdaTriggersTest {
|
||||
|
||||
@Test
|
||||
void customMessageDoesNotFireForAnInvitationWithNowhereToGo() {
|
||||
CognitoService svc = createServiceWithMessaging(mock(SesService.class), mock(SnsService.class),
|
||||
inlineMock(VerificationCodeService.class));
|
||||
UserPool pool = svc.createUserPool(Map.of(
|
||||
"PoolName", "trigger-pool",
|
||||
"LambdaConfig", Map.of("CustomMessage", "arn:aws:lambda:::custom-message")), "us-east-1");
|
||||
CognitoService svc = customMessageService(mock(SesService.class), null, null);
|
||||
UserPool pool = customMessagePool(svc, Map.of());
|
||||
|
||||
// SMS, the default medium, and no phone number.
|
||||
svc.adminCreateUser(pool.getId(), "spike", Map.of("email", "spike@example.com"), "Spike@Test1");
|
||||
|
||||
Reference in new issue
Block a user