mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -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);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user