mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 06:00:46 -05:00
fix(cognito): forward SignUp ValidationData and ClientMetadata to the pre-signup trigger (#5825)
* fix(cognito): forward SignUp ValidationData and ClientMetadata to the pre-signup trigger Real Amazon Cognito delivers the SignUp request's ValidationData and ClientMetadata to the pre-signup Lambda trigger as request.validationData and request.clientMetadata (see the pre sign-up trigger docs). The SignUp path parsed only UserAttributes and called signUp without them, so firePreSignUp always received empty maps — unlike AdminCreateUser, which already parses and forwards both fields. The auto-confirm PostConfirmation firing dropped ClientMetadata the same way. Parse both fields in handleSignUp and thread them through signUp → signUpWithReservedUsername → firePreSignUp, mirroring the AdminCreateUser plumbing, pass ClientMetadata on to PostConfirmation when PreSignUp auto-confirms, and cover both with JSON-handler-level regression tests. Adapted to #5783's signup restructure (callers of signUp in its new tests pass explicit empty maps). * refactor(cognito): extract name/value pair parsing helper The Name/Value forEach loop was copied six times across AdminCreateUser, AdminUpdateUserAttributes, SignUp and UpdateUserAttributes. One readNameValuePairs helper next to readStringList now serves all six. * fix(cognito): forward ClientMetadata to CustomMessage trigger sources fireCustomMessage hardcoded an empty clientMetadata map. It now takes the calling action's metadata so SignUp can pass the request's ClientMetadata to CustomMessage_SignUp, as the developer guide lists for that source. The other sources keep an empty map: ResendConfirmationCode and ForgotPassword do not accept ClientMetadata at all yet, and the guide excludes InitiateAuth-family metadata from custom message requests, so the MFA and OTP CustomMessage_Authentication paths deliberately pass none. Regression test asserts CustomMessage_SignUp receives the signup metadata.
This commit is contained in:
1 parent
de4f91f990
commit
9705b2584d
7 files changed
+167
-68
No files matched your search
+6
-4
@@ -1004,7 +1004,8 @@ final class CognitoAuthFlowHandler {
|
||||
firePreAuthentication(pool, client, user, null, clientMetadata, false);
|
||||
VerificationCode.Purpose purpose = "EMAIL_OTP".equals(challengeName)
|
||||
? VerificationCode.Purpose.EMAIL_OTP : VerificationCode.Purpose.SMS_OTP;
|
||||
Map<String, Object> customMessage = fireCustomMessage(pool, client, user, "CustomMessage_Authentication");
|
||||
Map<String, Object> customMessage = fireCustomMessage(pool, client, user, "CustomMessage_Authentication",
|
||||
Map.of());
|
||||
Map<String, String> challengeParams = new HashMap<>(
|
||||
service.issueSignInOtp(pool, user, purpose, attributeName, deliveryMedium, customMessage));
|
||||
challengeParams.put("USERNAME", user.getUsername());
|
||||
@@ -1856,11 +1857,11 @@ final class CognitoAuthFlowHandler {
|
||||
* {@link #firePostConfirmation} tolerates errors.
|
||||
*/
|
||||
Map<String, Object> fireCustomMessage(UserPool pool, UserPoolClient client, CognitoUser user,
|
||||
String triggerSource) {
|
||||
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", Map.of());
|
||||
req.put("clientMetadata", clientMetadata == null ? Map.of() : clientMetadata);
|
||||
return customMessageResponse(pool, client, user, triggerSource, req);
|
||||
}
|
||||
|
||||
@@ -2131,7 +2132,8 @@ final class CognitoAuthFlowHandler {
|
||||
return totpChallenge(pool, client, user, TotpPhase.CODE_REQUIRED, triggerSource, metadata);
|
||||
}
|
||||
boolean sms = "SMS_MFA".equals(factor);
|
||||
Map<String, Object> customMessage = fireCustomMessage(pool, client, user, "CustomMessage_Authentication");
|
||||
Map<String, Object> customMessage = fireCustomMessage(pool, client, user, "CustomMessage_Authentication",
|
||||
Map.of());
|
||||
Map<String, String> parameters = new HashMap<>(service.issueMfaCode(pool, user,
|
||||
sms ? VerificationCode.Purpose.SMS_MFA : VerificationCode.Purpose.EMAIL_MFA,
|
||||
sms ? "phone_number" : "email", sms ? "SMS" : "EMAIL", customMessage));
|
||||
|
||||
@@ -648,14 +648,12 @@ public class CognitoJsonHandler {
|
||||
}
|
||||
|
||||
private Response handleAdminCreateUser(JsonNode request) {
|
||||
Map<String, String> attrs = new HashMap<>();
|
||||
request.path("UserAttributes").forEach(a -> attrs.put(a.path("Name").asText(), a.path("Value").asText()));
|
||||
Map<String, String> attrs = readNameValuePairs(request, "UserAttributes");
|
||||
String tempPassword = request.path("TemporaryPassword").isMissingNode() ? null
|
||||
: request.path("TemporaryPassword").asText(null);
|
||||
String messageAction = request.path("MessageAction").isMissingNode() ? null
|
||||
: request.path("MessageAction").asText(null);
|
||||
Map<String, String> validationData = new HashMap<>();
|
||||
request.path("ValidationData").forEach(a -> validationData.put(a.path("Name").asText(), a.path("Value").asText()));
|
||||
Map<String, String> validationData = readNameValuePairs(request, "ValidationData");
|
||||
Map<String, String> clientMetadata = clientMetadata(request);
|
||||
List<String> desiredDeliveryMediums = new ArrayList<>();
|
||||
request.path("DesiredDeliveryMediums").forEach(m -> desiredDeliveryMediums.add(m.asText()));
|
||||
@@ -720,8 +718,7 @@ public class CognitoJsonHandler {
|
||||
}
|
||||
|
||||
private Response handleAdminUpdateUserAttributes(JsonNode request) {
|
||||
Map<String, String> attrs = new HashMap<>();
|
||||
request.path("UserAttributes").forEach(a -> attrs.put(a.path("Name").asText(), a.path("Value").asText()));
|
||||
Map<String, String> attrs = readNameValuePairs(request, "UserAttributes");
|
||||
service.adminUpdateUserAttributes(
|
||||
request.path("UserPoolId").asText(),
|
||||
request.path("Username").asText(),
|
||||
@@ -999,14 +996,17 @@ public class CognitoJsonHandler {
|
||||
}
|
||||
|
||||
private Response handleSignUp(JsonNode request) {
|
||||
Map<String, String> attrs = new HashMap<>();
|
||||
request.path("UserAttributes").forEach(a -> attrs.put(a.path("Name").asText(), a.path("Value").asText()));
|
||||
Map<String, String> attrs = readNameValuePairs(request, "UserAttributes");
|
||||
Map<String, String> validationData = readNameValuePairs(request, "ValidationData");
|
||||
Map<String, String> clientMetadata = clientMetadata(request);
|
||||
|
||||
CognitoUser user = service.signUp(
|
||||
request.path("ClientId").asText(),
|
||||
request.path("Username").asText(),
|
||||
request.path("Password").asText(),
|
||||
attrs
|
||||
attrs,
|
||||
validationData,
|
||||
clientMetadata
|
||||
);
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
response.put("UserConfirmed", "CONFIRMED".equals(user.getUserStatus()));
|
||||
@@ -1124,8 +1124,7 @@ public class CognitoJsonHandler {
|
||||
}
|
||||
|
||||
private Response handleUpdateUserAttributes(JsonNode request) {
|
||||
Map<String, String> attrs = new HashMap<>();
|
||||
request.path("UserAttributes").forEach(a -> attrs.put(a.path("Name").asText(), a.path("Value").asText()));
|
||||
Map<String, String> attrs = readNameValuePairs(request, "UserAttributes");
|
||||
List<Map<String, Object>> deliveryDetails = service.updateUserAttributes(
|
||||
request.path("AccessToken").asText(), attrs);
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
@@ -1375,6 +1374,12 @@ public class CognitoJsonHandler {
|
||||
return values;
|
||||
}
|
||||
|
||||
private Map<String, String> readNameValuePairs(JsonNode request, String member) {
|
||||
Map<String, String> pairs = new HashMap<>();
|
||||
request.path(member).forEach(a -> pairs.put(a.path("Name").asText(), a.path("Value").asText()));
|
||||
return pairs;
|
||||
}
|
||||
|
||||
private List<String> readStringList(JsonNode request, String member) {
|
||||
JsonNode node = request.get(member);
|
||||
if (node == null || !node.isArray()) {
|
||||
|
||||
@@ -3282,7 +3282,8 @@ public class CognitoService implements ResourceProvider {
|
||||
|
||||
// ──────────────────────────── Self-Service Registration ────────────────────────────
|
||||
|
||||
public CognitoUser signUp(String clientId, String username, String password, Map<String, String> attributes) {
|
||||
public CognitoUser signUp(String clientId, String username, String password, Map<String, String> attributes,
|
||||
Map<String, String> validationData, Map<String, String> clientMetadata) {
|
||||
UserPoolClient client = clientStore.get(clientId)
|
||||
.orElseThrow(() -> new AwsException("ResourceNotFoundException", "Client not found",
|
||||
400));
|
||||
@@ -3292,14 +3293,16 @@ public class CognitoService implements ResourceProvider {
|
||||
throw new AwsException("UsernameExistsException", "User already exists", 400);
|
||||
}
|
||||
try {
|
||||
return signUpWithReservedUsername(client, userPoolId, username, password, attributes);
|
||||
return signUpWithReservedUsername(client, userPoolId, username, password, attributes,
|
||||
validationData, clientMetadata);
|
||||
} finally {
|
||||
userCreatesInFlight.remove(inFlightKey);
|
||||
}
|
||||
}
|
||||
|
||||
private CognitoUser signUpWithReservedUsername(UserPoolClient client, String userPoolId,
|
||||
String username, String password, Map<String, String> attributes) {
|
||||
String username, String password, Map<String, String> attributes,
|
||||
Map<String, String> validationData, Map<String, String> clientMetadata) {
|
||||
UserPool pool = describeUserPool(userPoolId);
|
||||
requireAvailableSignUpUsername(pool, username);
|
||||
|
||||
@@ -3333,7 +3336,7 @@ public class CognitoService implements ResourceProvider {
|
||||
// Fire PreSignUp BEFORE persisting: allows the trigger to block signup
|
||||
// (via lambda error) or auto-confirm/auto-verify the user (via response).
|
||||
CognitoAuthFlowHandler.PreSignUpResponse preSignUp = authFlowHandler.firePreSignUp(
|
||||
pool, client, user, Map.of(), Map.of(), "PreSignUp_SignUp");
|
||||
pool, client, user, validationData, clientMetadata, "PreSignUp_SignUp");
|
||||
if (preSignUp.autoConfirmUser()) {
|
||||
user.setUserStatus("CONFIRMED");
|
||||
}
|
||||
@@ -3374,7 +3377,7 @@ public class CognitoService implements ResourceProvider {
|
||||
String code = verificationCodeService.issue(pool.getId(), user.getUsername(),
|
||||
VerificationCode.Purpose.SIGNUP_CONFIRMATION, Duration.ofHours(24));
|
||||
Map<String, Object> customMessage = authFlowHandler.fireCustomMessage(
|
||||
pool, client, triggerUser, "CustomMessage_SignUp");
|
||||
pool, client, triggerUser, "CustomMessage_SignUp", clientMetadata);
|
||||
messageDispatcher.dispatch(pool, triggerUser, VerificationCode.Purpose.SIGNUP_CONFIRMATION,
|
||||
code, List.of(deliveryTarget.deliveryMedium()), customMessage);
|
||||
} catch (VerificationCodeException e) {
|
||||
@@ -3390,7 +3393,7 @@ public class CognitoService implements ResourceProvider {
|
||||
// When PreSignUp auto-confirms, AWS Cognito also fires PostConfirmation.
|
||||
// See: docs.aws.amazon.com/cognito/latest/developerguide/user-pool-lambda-pre-sign-up.html
|
||||
if (preSignUp.autoConfirmUser()) {
|
||||
authFlowHandler.firePostConfirmation(pool, client, triggerUser, Map.of(), "PostConfirmation_ConfirmSignUp");
|
||||
authFlowHandler.firePostConfirmation(pool, client, triggerUser, clientMetadata, "PostConfirmation_ConfirmSignUp");
|
||||
}
|
||||
synchronized (userLock(userPoolId, user.getUsername())) {
|
||||
triggerUser.setUserStatus(user.getUserStatus());
|
||||
@@ -3501,7 +3504,7 @@ public class CognitoService implements ResourceProvider {
|
||||
String code = verificationCodeService.issue(pool.getId(), user.getUsername(),
|
||||
VerificationCode.Purpose.SIGNUP_CONFIRMATION, Duration.ofHours(24));
|
||||
Map<String, Object> customMessage = authFlowHandler.fireCustomMessage(
|
||||
pool, client, user, "CustomMessage_ResendCode");
|
||||
pool, client, user, "CustomMessage_ResendCode", Map.of());
|
||||
messageDispatcher.dispatch(pool, user, VerificationCode.Purpose.SIGNUP_CONFIRMATION,
|
||||
code, List.of(deliveryTarget.deliveryMedium()), customMessage);
|
||||
} catch (VerificationCodeException e) {
|
||||
@@ -3902,7 +3905,7 @@ public class CognitoService implements ResourceProvider {
|
||||
String code = verificationCodeService.issue(pool.getId(), user.getUsername(),
|
||||
VerificationCode.Purpose.PASSWORD_RESET, Duration.ofHours(1));
|
||||
Map<String, Object> customMessage = authFlowHandler.fireCustomMessage(
|
||||
pool, client, user, "CustomMessage_ForgotPassword");
|
||||
pool, client, user, "CustomMessage_ForgotPassword", Map.of());
|
||||
messageDispatcher.dispatch(pool, user, VerificationCode.Purpose.PASSWORD_RESET, code,
|
||||
List.of(deliveryTarget.deliveryMedium()), customMessage);
|
||||
} catch (VerificationCodeException e) {
|
||||
|
||||
+106
-17
@@ -67,6 +67,7 @@ import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.atLeastOnce;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@@ -423,7 +424,7 @@ class CognitoLambdaTriggersTest {
|
||||
|
||||
// SignUp creates an UNCONFIRMED user
|
||||
service.signUp(client.getClientId(), "alice", "Perm1234!",
|
||||
Map.of("email", "alice@example.com"));
|
||||
Map.of("email", "alice@example.com"), Map.of(), Map.of());
|
||||
|
||||
when(lambdaService.invoke(anyString(), eq("arn:aws:lambda:::post-confirm"),
|
||||
any(byte[].class), eq(InvocationType.RequestResponse)))
|
||||
@@ -448,7 +449,7 @@ class CognitoLambdaTriggersTest {
|
||||
void postConfirmationFiresOnAdminConfirmSignUp() throws Exception {
|
||||
UserPool pool = createPoolWithLambdaConfig(Map.of("PostConfirmation", "arn:aws:lambda:::post-confirm"));
|
||||
UserPoolClient client = createClient(pool);
|
||||
service.signUp(client.getClientId(), "alice", "Perm1234!", Map.of("email", "alice@example.com"));
|
||||
service.signUp(client.getClientId(), "alice", "Perm1234!", Map.of("email", "alice@example.com"), Map.of(), Map.of());
|
||||
ArgumentCaptor<byte[]> payload = ArgumentCaptor.forClass(byte[].class);
|
||||
when(lambdaService.invoke(anyString(), eq("arn:aws:lambda:::post-confirm"),
|
||||
any(byte[].class), eq(InvocationType.RequestResponse))).thenReturn(ok(Map.of()));
|
||||
@@ -503,7 +504,7 @@ class CognitoLambdaTriggersTest {
|
||||
UserPoolClient client = createClient(pool);
|
||||
|
||||
service.signUp(client.getClientId(), "alice", "Perm1234!",
|
||||
Map.of("email", "alice@example.com"));
|
||||
Map.of("email", "alice@example.com"), Map.of(), Map.of());
|
||||
|
||||
when(lambdaService.invoke(anyString(), eq("arn:aws:lambda:::post-confirm"),
|
||||
any(byte[].class), any()))
|
||||
@@ -520,7 +521,7 @@ class CognitoLambdaTriggersTest {
|
||||
UserPoolClient client = createClient(pool);
|
||||
|
||||
service.signUp(client.getClientId(), "alice", "Perm1234!",
|
||||
Map.of("email", "alice@example.com"));
|
||||
Map.of("email", "alice@example.com"), Map.of(), Map.of());
|
||||
service.confirmSignUp(client.getClientId(), "alice");
|
||||
|
||||
verify(lambdaService, never())
|
||||
@@ -541,7 +542,7 @@ class CognitoLambdaTriggersTest {
|
||||
.thenReturn(ok(Map.of()));
|
||||
|
||||
service.signUp(client.getClientId(), "alice", "Perm1234!",
|
||||
Map.of("email", "alice@example.com"));
|
||||
Map.of("email", "alice@example.com"), Map.of(), Map.of());
|
||||
|
||||
ArgumentCaptor<byte[]> payloadCaptor = ArgumentCaptor.forClass(byte[].class);
|
||||
verify(lambdaService).invoke(anyString(), eq("arn:aws:lambda:::pre-signup"),
|
||||
@@ -550,6 +551,67 @@ class CognitoLambdaTriggersTest {
|
||||
.path("userAttributes").has("cognito:user_status"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void preSignUpFiresOnSignUpWithValidationDataAndClientMetadata() throws Exception {
|
||||
UserPool pool = createPoolWithLambdaConfig(Map.of("PreSignUp", "arn:aws:lambda:::pre-signup"));
|
||||
UserPoolClient client = createClient(pool);
|
||||
|
||||
when(lambdaService.invoke(anyString(), eq("arn:aws:lambda:::pre-signup"),
|
||||
any(byte[].class), eq(InvocationType.RequestResponse)))
|
||||
.thenReturn(ok(Map.of()));
|
||||
|
||||
ObjectNode request = MAPPER.createObjectNode();
|
||||
request.put("ClientId", client.getClientId());
|
||||
request.put("Username", "alice");
|
||||
request.put("Password", "Perm1234!");
|
||||
request.putArray("UserAttributes").addObject().put("Name", "email").put("Value", "alice@example.com");
|
||||
request.putArray("ValidationData").addObject().put("Name", "inviteCode").put("Value", "abc123");
|
||||
request.putObject("ClientMetadata").put("source", "signup-form");
|
||||
new CognitoJsonHandler(service, MAPPER).handle("SignUp", request, "us-east-1");
|
||||
|
||||
ArgumentCaptor<byte[]> payloadCaptor = ArgumentCaptor.forClass(byte[].class);
|
||||
verify(lambdaService).invoke(anyString(), eq("arn:aws:lambda:::pre-signup"),
|
||||
payloadCaptor.capture(), eq(InvocationType.RequestResponse));
|
||||
JsonNode event = MAPPER.readTree(payloadCaptor.getValue());
|
||||
assertEquals("PreSignUp_SignUp", event.path("triggerSource").asText());
|
||||
assertEquals(client.getClientId(), event.path("callerContext").path("clientId").asText());
|
||||
assertEquals("alice@example.com", event.path("request").path("userAttributes").path("email").asText());
|
||||
assertEquals("abc123", event.path("request").path("validationData").path("inviteCode").asText());
|
||||
assertEquals("signup-form", event.path("request").path("clientMetadata").path("source").asText());
|
||||
assertFalse(service.adminGetUser(pool.getId(), "alice").getAttributes().containsKey("inviteCode"),
|
||||
"ValidationData must not be stored as a user attribute");
|
||||
}
|
||||
|
||||
@Test
|
||||
void postConfirmationFiresWithSignUpClientMetadataWhenAutoConfirmed() throws Exception {
|
||||
UserPool pool = createPoolWithLambdaConfig(Map.of(
|
||||
"PreSignUp", "arn:aws:lambda:::pre-signup",
|
||||
"PostConfirmation", "arn:aws:lambda:::post-confirm"));
|
||||
UserPoolClient client = createClient(pool);
|
||||
|
||||
when(lambdaService.invoke(anyString(), eq("arn:aws:lambda:::pre-signup"),
|
||||
any(byte[].class), eq(InvocationType.RequestResponse)))
|
||||
.thenReturn(ok(Map.of("autoConfirmUser", true)));
|
||||
when(lambdaService.invoke(anyString(), eq("arn:aws:lambda:::post-confirm"),
|
||||
any(byte[].class), eq(InvocationType.RequestResponse)))
|
||||
.thenReturn(ok(Map.of()));
|
||||
|
||||
ObjectNode request = MAPPER.createObjectNode();
|
||||
request.put("ClientId", client.getClientId());
|
||||
request.put("Username", "alice");
|
||||
request.put("Password", "Perm1234!");
|
||||
request.putArray("UserAttributes").addObject().put("Name", "email").put("Value", "alice@example.com");
|
||||
request.putObject("ClientMetadata").put("source", "signup-form");
|
||||
new CognitoJsonHandler(service, MAPPER).handle("SignUp", request, "us-east-1");
|
||||
|
||||
ArgumentCaptor<byte[]> payloadCaptor = ArgumentCaptor.forClass(byte[].class);
|
||||
verify(lambdaService, times(2)).invoke(anyString(), anyString(),
|
||||
payloadCaptor.capture(), eq(InvocationType.RequestResponse));
|
||||
JsonNode postConfirmation = MAPPER.readTree(payloadCaptor.getAllValues().get(1));
|
||||
assertEquals("PostConfirmation_ConfirmSignUp", postConfirmation.path("triggerSource").asText());
|
||||
assertEquals("signup-form", postConfirmation.path("request").path("clientMetadata").path("source").asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void preSignUpLambdaErrorBlocksSignUp() {
|
||||
UserPool pool = createPoolWithLambdaConfig(Map.of("PreSignUp", "arn:aws:lambda:::pre-signup"));
|
||||
@@ -561,7 +623,7 @@ class CognitoLambdaTriggersTest {
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class, () ->
|
||||
service.signUp(client.getClientId(), "alice", "Perm1234!",
|
||||
Map.of("email", "alice@example.com")));
|
||||
Map.of("email", "alice@example.com"), Map.of(), Map.of()));
|
||||
assertEquals("UserLambdaValidationException", ex.getErrorCode());
|
||||
assertEquals("PreSignUp failed with error Invitations are closed.", ex.getMessage());
|
||||
}
|
||||
@@ -578,7 +640,7 @@ class CognitoLambdaTriggersTest {
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class, () ->
|
||||
service.signUp(client.getClientId(), "alice", "Perm1234!",
|
||||
Map.of("email", "alice@example.com")));
|
||||
Map.of("email", "alice@example.com"), Map.of(), Map.of()));
|
||||
|
||||
assertEquals("UnexpectedLambdaException", ex.getErrorCode());
|
||||
assertEquals(400, ex.getHttpStatus());
|
||||
@@ -600,7 +662,7 @@ class CognitoLambdaTriggersTest {
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class, () ->
|
||||
service.signUp(client.getClientId(), "alice", "Perm1234!",
|
||||
Map.of("email", "alice@example.com")));
|
||||
Map.of("email", "alice@example.com"), Map.of(), Map.of()));
|
||||
|
||||
assertEquals("InvalidLambdaResponseException", ex.getErrorCode());
|
||||
assertEquals(400, ex.getHttpStatus());
|
||||
@@ -622,7 +684,7 @@ class CognitoLambdaTriggersTest {
|
||||
"autoVerifyEmail", true)));
|
||||
|
||||
var user = service.signUp(client.getClientId(), "alice", "Perm1234!",
|
||||
Map.of("email", "alice@example.com"));
|
||||
Map.of("email", "alice@example.com"), Map.of(), Map.of());
|
||||
|
||||
assertEquals("CONFIRMED", user.getUserStatus(),
|
||||
"autoConfirmUser=true should set status to CONFIRMED at signup");
|
||||
@@ -640,7 +702,7 @@ class CognitoLambdaTriggersTest {
|
||||
.thenReturn(ok(Map.of("autoVerifyPhone", true)));
|
||||
|
||||
var user = service.signUp(client.getClientId(), "alice", "Perm1234!",
|
||||
Map.of("phone_number", "+15551234567"));
|
||||
Map.of("phone_number", "+15551234567"), Map.of(), Map.of());
|
||||
|
||||
assertEquals("true", user.getAttributes().get("phone_number_verified"),
|
||||
"autoVerifyPhone=true should set phone_number_verified attribute");
|
||||
@@ -662,7 +724,7 @@ class CognitoLambdaTriggersTest {
|
||||
.thenReturn(ok(Map.of()));
|
||||
|
||||
service.signUp(client.getClientId(), "alice", "Perm1234!",
|
||||
Map.of("email", "alice@example.com"));
|
||||
Map.of("email", "alice@example.com"), Map.of(), Map.of());
|
||||
|
||||
verify(lambdaService, atLeastOnce())
|
||||
.invoke(anyString(), eq("arn:aws:lambda:::post-confirm"),
|
||||
@@ -675,7 +737,7 @@ class CognitoLambdaTriggersTest {
|
||||
UserPoolClient client = createClient(pool);
|
||||
|
||||
service.signUp(client.getClientId(), "alice", "Perm1234!",
|
||||
Map.of("email", "alice@example.com"));
|
||||
Map.of("email", "alice@example.com"), Map.of(), Map.of());
|
||||
|
||||
verify(lambdaService, never())
|
||||
.invoke(anyString(), anyString(), any(byte[].class), any());
|
||||
@@ -2163,7 +2225,7 @@ class CognitoLambdaTriggersTest {
|
||||
"emailSubject", "TRIGGER-FIRED",
|
||||
"emailMessage", "TRIGGER-FIRED code={####}")));
|
||||
|
||||
svc.signUp(client.getClientId(), "spike", "Spike@Test1", Map.of("email", "spike@example.com"));
|
||||
svc.signUp(client.getClientId(), "spike", "Spike@Test1", Map.of("email", "spike@example.com"), Map.of(), Map.of());
|
||||
|
||||
assertVerificationEmail(ses, "spike@example.com", "TRIGGER-FIRED", "TRIGGER-FIRED code=246962");
|
||||
}
|
||||
@@ -2188,7 +2250,7 @@ class CognitoLambdaTriggersTest {
|
||||
payloadCaptor.capture(), eq(InvocationType.RequestResponse)))
|
||||
.thenReturn(ok(Map.of()));
|
||||
|
||||
svc.signUp(client.getClientId(), "spike", "Spike@Test1", Map.of("email", "spike@example.com"));
|
||||
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<>() {});
|
||||
assertEquals("CustomMessage_SignUp", event.get("triggerSource"));
|
||||
@@ -2197,6 +2259,33 @@ class CognitoLambdaTriggersTest {
|
||||
assertEquals("spike", request.get("usernameParameter"));
|
||||
}
|
||||
|
||||
@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()));
|
||||
|
||||
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());
|
||||
assertEquals("CustomMessage_SignUp", event.path("triggerSource").asText());
|
||||
assertEquals("signup-form", event.path("request").path("clientMetadata").path("source").asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void customMessageLambdaErrorFallsBackToDefaultSignUpMessage() {
|
||||
SesService ses = mock(SesService.class);
|
||||
@@ -2215,7 +2304,7 @@ class CognitoLambdaTriggersTest {
|
||||
.thenReturn(lambdaError("Unhandled"));
|
||||
|
||||
// SignUp must still succeed and deliver the default message when CustomMessage errors.
|
||||
svc.signUp(client.getClientId(), "spike", "Spike@Test1", Map.of("email", "spike@example.com"));
|
||||
svc.signUp(client.getClientId(), "spike", "Spike@Test1", Map.of("email", "spike@example.com"), Map.of(), Map.of());
|
||||
|
||||
assertVerificationEmail(ses, "spike@example.com", "Your verification code",
|
||||
"Your verification code is 246962.");
|
||||
@@ -2234,7 +2323,7 @@ class CognitoLambdaTriggersTest {
|
||||
"AutoVerifiedAttributes", List.of("email")), "us-east-1");
|
||||
UserPoolClient client = svc.createUserPoolClient(pool.getId(), "c", false, false, List.of(), List.of());
|
||||
|
||||
svc.signUp(client.getClientId(), "spike", "Spike@Test1", Map.of("email", "spike@example.com"));
|
||||
svc.signUp(client.getClientId(), "spike", "Spike@Test1", Map.of("email", "spike@example.com"), Map.of(), Map.of());
|
||||
|
||||
verify(lambdaService, never()).invoke(anyString(), anyString(), any(byte[].class), any());
|
||||
assertVerificationEmail(ses, "spike@example.com", "Your verification code",
|
||||
@@ -2255,7 +2344,7 @@ class CognitoLambdaTriggersTest {
|
||||
"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());
|
||||
svc.signUp(client.getClientId(), "spike", "Spike@Test1", Map.of("email", "spike@example.com"));
|
||||
svc.signUp(client.getClientId(), "spike", "Spike@Test1", Map.of("email", "spike@example.com"), Map.of(), Map.of());
|
||||
|
||||
ArgumentCaptor<byte[]> payloadCaptor = ArgumentCaptor.forClass(byte[].class);
|
||||
when(lambdaService.invoke(anyString(), eq("arn:aws:lambda:::custom-message"),
|
||||
|
||||
@@ -257,7 +257,7 @@ class CognitoServiceTest {
|
||||
Map<String, String> attributes = Map.of("email", email, "name", "must-not-be-saved");
|
||||
for (Runnable operation : List.<Runnable>of(
|
||||
() -> service.adminCreateUser(pool.getId(), "invalid-admin", attributes, "TempPass1!"),
|
||||
() -> service.signUp(client.getClientId(), "invalid-signup", "Pass1234!", attributes),
|
||||
() -> service.signUp(client.getClientId(), "invalid-signup", "Pass1234!", attributes, Map.of(), Map.of()),
|
||||
() -> service.adminUpdateUserAttributes(pool.getId(), "alice", attributes),
|
||||
() -> service.updateUserAttributes(token, attributes))) {
|
||||
AwsException error = assertThrows(AwsException.class, operation::run);
|
||||
@@ -649,7 +649,7 @@ class CognitoServiceTest {
|
||||
service.signUp(client.getClientId(), "alice@example.com", password, Map.of(
|
||||
"email", "alice@example.com",
|
||||
"phone_number", "+4915112345678"
|
||||
)));
|
||||
), Map.of(), Map.of()));
|
||||
|
||||
assertEquals("InvalidPasswordException", exception.getErrorCode());
|
||||
}
|
||||
@@ -665,7 +665,7 @@ class CognitoServiceTest {
|
||||
"alice@example.com",
|
||||
"ValidPassword1!",
|
||||
Map.of("email", "alice@example.com", "phone_number", "+4915112345678")
|
||||
);
|
||||
, Map.of(), Map.of());
|
||||
|
||||
assertEquals("alice@example.com", user.getUsername());
|
||||
}
|
||||
@@ -908,7 +908,7 @@ class CognitoServiceTest {
|
||||
pool.getId(), "no-requirements-client", false, false, List.of(), List.of());
|
||||
assertDoesNotThrow(() -> service.signUp(
|
||||
client.getClientId(), "alice@example.com", "abcdefg", Map.of(
|
||||
"email", "alice@example.com", "phone_number", "+4915112345678")));
|
||||
"email", "alice@example.com", "phone_number", "+4915112345678"), Map.of(), Map.of()));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -2291,7 +2291,7 @@ class CognitoServiceTest {
|
||||
false, false, List.of(), List.of());
|
||||
|
||||
CognitoUser user = service.signUp(client.getClientId(),
|
||||
"carol", "Pass1234!", Map.of("email", "carol@example.com"));
|
||||
"carol", "Pass1234!", Map.of("email", "carol@example.com"), Map.of(), Map.of());
|
||||
|
||||
assertTrue(user.getAttributes().containsKey("sub"),
|
||||
"signUp should auto-generate a sub attribute");
|
||||
@@ -2326,7 +2326,7 @@ class CognitoServiceTest {
|
||||
UserPoolClient client = openClient(serviceWithVerification, pool.getId(), "test-client", false);
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class, () ->
|
||||
serviceWithVerification.signUp(client.getClientId(), "carol", "Pass1234!", Map.of()));
|
||||
serviceWithVerification.signUp(client.getClientId(), "carol", "Pass1234!", Map.of(), Map.of(), Map.of()));
|
||||
assertEquals("InvalidParameterException", ex.getErrorCode());
|
||||
|
||||
AwsException lookupEx = assertThrows(AwsException.class, () ->
|
||||
@@ -2368,7 +2368,7 @@ class CognitoServiceTest {
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class, () ->
|
||||
serviceWithVerification.signUp(client.getClientId(), "carol", "Pass1234!",
|
||||
Map.of("email", "carol@example.com")));
|
||||
Map.of("email", "carol@example.com"), Map.of(), Map.of()));
|
||||
assertEquals("CodeDeliveryFailureException", ex.getErrorCode());
|
||||
assertEquals(400, ex.getHttpStatus());
|
||||
assertEquals("Failed to deliver the message.", ex.getMessage());
|
||||
@@ -2383,7 +2383,7 @@ class CognitoServiceTest {
|
||||
@Test
|
||||
void signUpReturnsResourceNotFoundAsBadRequestWhenClientMissing() {
|
||||
AwsException ex = assertThrows(AwsException.class, () ->
|
||||
service.signUp("missing-client", "carol", "Pass1234!", Map.of("email", "carol@example.com")));
|
||||
service.signUp("missing-client", "carol", "Pass1234!", Map.of("email", "carol@example.com"), Map.of(), Map.of()));
|
||||
assertEquals("ResourceNotFoundException", ex.getErrorCode());
|
||||
assertEquals(400, ex.getHttpStatus());
|
||||
}
|
||||
@@ -2858,7 +2858,7 @@ class CognitoServiceTest {
|
||||
void initiateAuthWithUserSrpAuthRejectsUnconfirmedUser() {
|
||||
UserPool pool = service.createUserPool(Map.of("PoolName", "TestPool"), "us-east-1");
|
||||
UserPoolClient client = service.createUserPoolClient(pool.getId(), "c", false, false, List.of(), List.of());
|
||||
service.signUp(client.getClientId(), "bob", "Password123!", Map.of("email", "bob@example.com"));
|
||||
service.signUp(client.getClientId(), "bob", "Password123!", Map.of("email", "bob@example.com"), Map.of(), Map.of());
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class, () ->
|
||||
service.initiateAuth(client.getClientId(), "USER_SRP_AUTH",
|
||||
@@ -5586,7 +5586,7 @@ class CognitoServiceTest {
|
||||
pool.getId(), "c", false, false, List.of(), List.of());
|
||||
|
||||
svc.signUp(client.getClientId(), username, password,
|
||||
Map.of("email", username));
|
||||
Map.of("email", username), Map.of(), Map.of());
|
||||
svc.confirmSignUp(client.getClientId(), username, "123456");
|
||||
|
||||
CognitoUser user = svc.adminGetUser(pool.getId(), username);
|
||||
@@ -5613,7 +5613,7 @@ class CognitoServiceTest {
|
||||
pool.getId(), "c", false, false, List.of(), List.of());
|
||||
|
||||
svc.signUp(client.getClientId(), username, password,
|
||||
Map.of("phone_number", phoneNumber));
|
||||
Map.of("phone_number", phoneNumber), Map.of(), Map.of());
|
||||
svc.confirmSignUp(client.getClientId(), username, "123456");
|
||||
|
||||
CognitoUser user = svc.adminGetUser(pool.getId(), username);
|
||||
@@ -5640,7 +5640,7 @@ class CognitoServiceTest {
|
||||
pool.getId(), "c", false, false, List.of(), List.of());
|
||||
|
||||
svc.signUp(client.getClientId(), email, password,
|
||||
Map.of("email", email, "phone_number", phoneNumber));
|
||||
Map.of("email", email, "phone_number", phoneNumber), Map.of(), Map.of());
|
||||
svc.confirmSignUp(client.getClientId(), email, "123456");
|
||||
|
||||
CognitoUser user = svc.adminGetUser(pool.getId(), email);
|
||||
@@ -5667,7 +5667,7 @@ class CognitoServiceTest {
|
||||
pool.getId(), "c", false, false, List.of(), List.of());
|
||||
|
||||
svc.signUp(client.getClientId(), email, password,
|
||||
Map.of("email", email, "phone_number", phoneNumber));
|
||||
Map.of("email", email, "phone_number", phoneNumber), Map.of(), Map.of());
|
||||
svc.confirmSignUp(client.getClientId(), email, "123456");
|
||||
|
||||
CognitoUser user = svc.adminGetUser(pool.getId(), email);
|
||||
@@ -5690,7 +5690,7 @@ class CognitoServiceTest {
|
||||
pool.getId(), "c", false, false, List.of(), List.of());
|
||||
|
||||
svc.signUp(client.getClientId(), username, password,
|
||||
Map.of("email", username));
|
||||
Map.of("email", username), Map.of(), Map.of());
|
||||
svc.confirmSignUp(client.getClientId(), username, "123456");
|
||||
|
||||
CognitoUser user = svc.adminGetUser(pool.getId(), username);
|
||||
@@ -5717,7 +5717,7 @@ class CognitoServiceTest {
|
||||
pool.getId(), "c", false, false, List.of(), List.of());
|
||||
|
||||
service.signUp(client.getClientId(), username, password,
|
||||
Map.of("email", username));
|
||||
Map.of("email", username), Map.of(), Map.of());
|
||||
|
||||
service.confirmSignUp(client.getClientId(), username);
|
||||
|
||||
@@ -5744,7 +5744,7 @@ class CognitoServiceTest {
|
||||
pool.getId(), "c", false, false, List.of(), List.of());
|
||||
|
||||
svc.signUp(client.getClientId(), username, password,
|
||||
Map.of("email", username));
|
||||
Map.of("email", username), Map.of(), Map.of());
|
||||
svc.updateUserPool(Map.of(
|
||||
"UserPoolId", pool.getId(),
|
||||
"AutoVerifiedAttributes", List.of("phone_number")
|
||||
|
||||
+2
-2
@@ -98,13 +98,13 @@ class CognitoSignUpCreationScopeTest {
|
||||
? service.adminCreateUser(POOL_ID, "alice", Map.of("name", "first"),
|
||||
"Perm1234!", "SUPPRESS")
|
||||
: service.signUp(firstClient.getClientId(), "alice", "Perm1234!",
|
||||
Map.of("name", "first"))));
|
||||
Map.of("name", "first"), Map.of(), Map.of())));
|
||||
pending.add(first);
|
||||
assertTrue(firstTriggerEntered.await(5, TimeUnit.SECONDS), "first request must enter PreSignUp");
|
||||
|
||||
Future<CognitoUser> overlapping = requests.submit(() -> inAccount(overlappingAccount, () ->
|
||||
service.signUp(overlappingClient.getClientId(), "alice", "Perm1234!",
|
||||
Map.of("name", "overlapping"))));
|
||||
Map.of("name", "overlapping"), Map.of(), Map.of())));
|
||||
pending.add(overlapping);
|
||||
if (differentAccount) {
|
||||
assertEquals("overlapping", overlapping.get(5, TimeUnit.SECONDS).getAttributes().get("name"));
|
||||
|
||||
+10
-10
@@ -102,7 +102,7 @@ class CognitoSignUpTriggerReentrancyTest {
|
||||
UserPool pool = pool(Map.of("PostConfirmation", FUNCTION));
|
||||
UserPoolClient client = client(pool);
|
||||
CognitoUser user = service.signUp(client.getClientId(), "alice", "Perm1234!",
|
||||
Map.of("phone_number", PHONE));
|
||||
Map.of("phone_number", PHONE), Map.of(), Map.of());
|
||||
String code = sns.getSentMessages().getFirst().getMessage().replaceAll("\\D", "");
|
||||
AtomicReference<Throwable> callbackFailure = new AtomicReference<>();
|
||||
when(lambdaService.invoke(anyString(), eq(FUNCTION), any(byte[].class),
|
||||
@@ -158,7 +158,7 @@ class CognitoSignUpTriggerReentrancyTest {
|
||||
new CognitoMessageDispatcher(null, sns, "us-east-1"), null);
|
||||
UserPool pool = pool(Map.of("PostConfirmation", FUNCTION));
|
||||
UserPoolClient client = client(pool);
|
||||
service.signUp(client.getClientId(), "alice", "Perm1234!", Map.of("phone_number", PHONE));
|
||||
service.signUp(client.getClientId(), "alice", "Perm1234!", Map.of("phone_number", PHONE), Map.of(), Map.of());
|
||||
confirmationCode.set(sns.getSentMessages().getFirst().getMessage().replaceAll("\\D", ""));
|
||||
AtomicReference<JsonNode> event = new AtomicReference<>();
|
||||
when(lambdaService.invoke(anyString(), eq(FUNCTION), any(byte[].class),
|
||||
@@ -196,7 +196,7 @@ class CognitoSignUpTriggerReentrancyTest {
|
||||
});
|
||||
|
||||
CognitoUser user = service.signUp(client.getClientId(), "alice", "Perm1234!",
|
||||
Map.of("phone_number", PHONE));
|
||||
Map.of("phone_number", PHONE), Map.of(), Map.of());
|
||||
|
||||
assertNull(callbackFailure.get(), () -> "auto-confirm PostConfirmation callback failed before returning: "
|
||||
+ callbackFailure.get());
|
||||
@@ -221,7 +221,7 @@ class CognitoSignUpTriggerReentrancyTest {
|
||||
});
|
||||
|
||||
CognitoUser user = service.signUp(client.getClientId(), "alice", "Perm1234!",
|
||||
Map.of("phone_number", PHONE));
|
||||
Map.of("phone_number", PHONE), Map.of(), Map.of());
|
||||
|
||||
assertNull(callbackFailure.get(), () -> "CustomMessage callback failed before returning: "
|
||||
+ callbackFailure.get());
|
||||
@@ -254,7 +254,7 @@ class CognitoSignUpTriggerReentrancyTest {
|
||||
});
|
||||
|
||||
service.signUp(client.getClientId(), "alice", "Perm1234!",
|
||||
Map.of("phone_number", PHONE, "name", "created by signup"));
|
||||
Map.of("phone_number", PHONE, "name", "created by signup"), Map.of(), Map.of());
|
||||
|
||||
assertNull(callbackFailure.get(), () -> "PreSignUp callback failed before returning: "
|
||||
+ callbackFailure.get());
|
||||
@@ -277,7 +277,7 @@ class CognitoSignUpTriggerReentrancyTest {
|
||||
return response(Map.of());
|
||||
});
|
||||
|
||||
service.signUp(client.getClientId(), "Aa", "Perm1234!", Map.of("phone_number", PHONE));
|
||||
service.signUp(client.getClientId(), "Aa", "Perm1234!", Map.of("phone_number", PHONE), Map.of(), Map.of());
|
||||
|
||||
assertNull(callbackFailure.get());
|
||||
assertEquals("updated by PreSignUp", service.adminGetUser(pool.getId(), "BB").getAttributes().get("name"));
|
||||
@@ -302,7 +302,7 @@ class CognitoSignUpTriggerReentrancyTest {
|
||||
return response(Map.of());
|
||||
});
|
||||
|
||||
CognitoUser result = service.signUp(client.getClientId(), "alice", "Perm1234!", Map.of("phone_number", PHONE));
|
||||
CognitoUser result = service.signUp(client.getClientId(), "alice", "Perm1234!", Map.of("phone_number", PHONE), Map.of(), Map.of());
|
||||
|
||||
assertNull(callbackFailure.get());
|
||||
assertEquals(1, sns.getSentMessages().size());
|
||||
@@ -333,7 +333,7 @@ class CognitoSignUpTriggerReentrancyTest {
|
||||
});
|
||||
|
||||
AwsException failure = assertThrows(AwsException.class, () -> service.signUp(client.getClientId(),
|
||||
"alice", "Perm1234!", Map.of("phone_number", PHONE)));
|
||||
"alice", "Perm1234!", Map.of("phone_number", PHONE), Map.of(), Map.of()));
|
||||
|
||||
assertEquals("CodeDeliveryFailureException", failure.getErrorCode());
|
||||
assertNull(callbackFailure.get());
|
||||
@@ -366,7 +366,7 @@ class CognitoSignUpTriggerReentrancyTest {
|
||||
});
|
||||
|
||||
AwsException failure = assertThrows(AwsException.class, () -> service.signUp(client.getClientId(),
|
||||
"alice", "Perm1234!", Map.of("phone_number", PHONE)));
|
||||
"alice", "Perm1234!", Map.of("phone_number", PHONE), Map.of(), Map.of()));
|
||||
|
||||
assertEquals("CodeDeliveryFailureException", failure.getErrorCode());
|
||||
assertNull(callbackFailure.get());
|
||||
@@ -380,7 +380,7 @@ class CognitoSignUpTriggerReentrancyTest {
|
||||
UserPool pool = pool(Map.of("PostConfirmation", FUNCTION));
|
||||
UserPoolClient client = client(pool);
|
||||
service.signUp(client.getClientId(), "alice", "Perm1234!",
|
||||
Map.of("phone_number", PHONE, "name", "at confirmation"));
|
||||
Map.of("phone_number", PHONE, "name", "at confirmation"), Map.of(), Map.of());
|
||||
CognitoUser user = service.adminGetUser(pool.getId(), "alice");
|
||||
String code = sns.getSentMessages().getFirst().getMessage().replaceAll("\\D", "");
|
||||
AtomicBoolean captureStarted = new AtomicBoolean();
|
||||
|
||||
Reference in new issue
Block a user