fix(cognito): run missing post-confirmation triggers (#5207)

* fix(cognito): run missing post-confirmation triggers

* fix(cognito): forward confirmation client metadata
This commit is contained in:
Aniket Shukla authored and GitHub committed 2026-10-09 10:23:06 -04:00
1 parent 0ec64add2d
commit 5dfb45d71f
3 files changed
+108 -17

No files matched your search

@@ -983,10 +983,13 @@ public class CognitoJsonHandler {
}
private Response handleConfirmSignUp(JsonNode request) {
Map<String, String> clientMetadata = new HashMap<>();
request.path("ClientMetadata").fields().forEachRemaining(e -> clientMetadata.put(e.getKey(), e.getValue().asText()));
service.confirmSignUp(
request.path("ClientId").asText(),
request.path("Username").asText(),
request.path("ConfirmationCode").asText()
request.path("ConfirmationCode").asText(),
clientMetadata
);
return Response.ok(objectMapper.createObjectNode()).build();
}
@@ -1005,9 +1008,12 @@ public class CognitoJsonHandler {
}
private Response handleAdminConfirmSignUp(JsonNode request) {
Map<String, String> clientMetadata = new HashMap<>();
request.path("ClientMetadata").fields().forEachRemaining(e -> clientMetadata.put(e.getKey(), e.getValue().asText()));
service.adminConfirmSignUp(
request.path("UserPoolId").asText(),
request.path("Username").asText()
request.path("Username").asText(),
clientMetadata
);
return Response.ok(objectMapper.createObjectNode()).build();
}
@@ -1035,11 +1041,14 @@ public class CognitoJsonHandler {
}
private Response handleConfirmForgotPassword(JsonNode request) {
Map<String, String> clientMetadata = new HashMap<>();
request.path("ClientMetadata").fields().forEachRemaining(e -> clientMetadata.put(e.getKey(), e.getValue().asText()));
service.confirmForgotPassword(
request.path("ClientId").asText(),
request.path("Username").asText(),
request.path("ConfirmationCode").asText(),
request.path("Password").asText()
request.path("Password").asText(),
clientMetadata
);
return Response.ok(objectMapper.createObjectNode()).build();
}
@@ -3073,21 +3073,26 @@ public class CognitoService implements ResourceProvider {
}
public void confirmSignUp(String clientId, String username) {
confirmSignUp(clientId, username, null);
confirmSignUp(clientId, username, null, Map.of());
}
public void confirmSignUp(String clientId, String username, String confirmationCode) {
confirmSignUp(clientId, username, confirmationCode, Map.of());
}
public void confirmSignUp(String clientId, String username, String confirmationCode,
Map<String, String> clientMetadata) {
UserPoolClient client = clientStore.get(clientId)
.orElseThrow(() -> new AwsException("ResourceNotFoundException", "Client not found",
400));
String userPoolId = client.getUserPoolId();
synchronized (userLock(userPoolId, username)) {
confirmSignUpUnderUserLock(client, userPoolId, username, confirmationCode);
confirmSignUpUnderUserLock(client, userPoolId, username, confirmationCode, clientMetadata);
}
}
private void confirmSignUpUnderUserLock(UserPoolClient client, String userPoolId, String username,
String confirmationCode) {
String confirmationCode, Map<String, String> clientMetadata) {
UserPool pool = poolStore.get(userPoolId)
.orElseThrow(() -> userPoolNotFound(userPoolId));
CognitoUser user = adminGetUser(client.getUserPoolId(), username);
@@ -3113,7 +3118,7 @@ public class CognitoService implements ResourceProvider {
user.setUserStatus("CONFIRMED");
user.setLastModifiedDate(System.currentTimeMillis() / 1000L);
userStore.put(userKey(client.getUserPoolId(), user.getUsername()), user);
authFlowHandler.firePostConfirmation(pool, client, user, Map.of(), "PostConfirmation_ConfirmSignUp");
authFlowHandler.firePostConfirmation(pool, client, user, clientMetadata, "PostConfirmation_ConfirmSignUp");
}
Map<String, String> signUpCodeDeliveryDetails(CognitoUser user) {
@@ -3177,18 +3182,26 @@ public class CognitoService implements ResourceProvider {
}
public void adminConfirmSignUp(String userPoolId, String username) {
CognitoUser resolvedUser = adminGetUser(userPoolId, username);
synchronized (userLock(userPoolId, resolvedUser.getUsername())) {
adminConfirmSignUpUnderUserLock(userPoolId, resolvedUser.getUsername());
}
adminConfirmSignUp(userPoolId, username, Map.of());
}
private void adminConfirmSignUpUnderUserLock(String userPoolId, String username) {
public void adminConfirmSignUp(String userPoolId, String username, Map<String, String> clientMetadata) {
CognitoUser resolvedUser = adminGetUser(userPoolId, username);
CognitoUser user;
synchronized (userLock(userPoolId, resolvedUser.getUsername())) {
user = adminConfirmSignUpUnderUserLock(userPoolId, resolvedUser.getUsername());
}
authFlowHandler.firePostConfirmation(describeUserPool(userPoolId), null, user,
clientMetadata, "PostConfirmation_ConfirmSignUp");
}
private CognitoUser adminConfirmSignUpUnderUserLock(String userPoolId, String username) {
CognitoUser user = adminGetUser(userPoolId, username);
user.setUserStatus("CONFIRMED");
user.setLastModifiedDate(System.currentTimeMillis() / 1000L);
userStore.put(userKey(userPoolId, user.getUsername()), user);
LOG.infov("Admin confirmed sign up for user {0} in pool {1}", username, userPoolId);
return user;
}
// ──────────────────────────── Auth ────────────────────────────
@@ -3362,6 +3375,11 @@ public class CognitoService implements ResourceProvider {
}
public void confirmForgotPassword(String clientId, String username, String confirmationCode, String newPassword) {
confirmForgotPassword(clientId, username, confirmationCode, newPassword, Map.of());
}
public void confirmForgotPassword(String clientId, String username, String confirmationCode, String newPassword,
Map<String, String> clientMetadata) {
UserPoolClient client = clientStore.get(clientId)
.orElseThrow(() -> new AwsException("ResourceNotFoundException", "Client not found", 400));
CognitoUser user = adminGetUser(client.getUserPoolId(), username);
@@ -3373,6 +3391,9 @@ public class CognitoService implements ResourceProvider {
throw mapVerificationCodeException(e);
}
adminSetUserPassword(client.getUserPoolId(), user.getUsername(), newPassword, true);
authFlowHandler.firePostConfirmation(describeUserPool(client.getUserPoolId()), client,
adminGetUser(client.getUserPoolId(), user.getUsername()), clientMetadata,
"PostConfirmation_ConfirmForgotPassword");
}
public Map<String, Object> getUser(String accessToken) {
@@ -416,7 +416,7 @@ class CognitoLambdaTriggersTest {
// =========================================================================
@Test
void postConfirmationFiresOnConfirmSignUp() {
void postConfirmationFiresOnConfirmSignUp() throws Exception {
UserPool pool = createPoolWithLambdaConfig(Map.of("PostConfirmation", "arn:aws:lambda:::post-confirm"));
UserPoolClient client = createClient(pool);
@@ -428,11 +428,72 @@ class CognitoLambdaTriggersTest {
any(byte[].class), eq(InvocationType.RequestResponse)))
.thenReturn(ok(Map.of()));
service.confirmSignUp(client.getClientId(), "alice");
ObjectNode request = MAPPER.createObjectNode();
request.put("ClientId", client.getClientId());
request.put("Username", "alice");
request.put("ConfirmationCode", "123456");
request.putObject("ClientMetadata").put("source", "confirm-sign-up");
new CognitoJsonHandler(service, MAPPER).handle("ConfirmSignUp", request, "us-east-1");
verify(lambdaService, atLeastOnce())
.invoke(anyString(), eq("arn:aws:lambda:::post-confirm"),
any(byte[].class), eq(InvocationType.RequestResponse));
ArgumentCaptor<byte[]> payload = ArgumentCaptor.forClass(byte[].class);
verify(lambdaService).invoke(anyString(), eq("arn:aws:lambda:::post-confirm"),
payload.capture(), eq(InvocationType.RequestResponse));
JsonNode event = MAPPER.readTree(payload.getValue());
assertEquals("PostConfirmation_ConfirmSignUp", event.path("triggerSource").asText());
assertEquals("confirm-sign-up", event.path("request").path("clientMetadata").path("source").asText());
}
@Test
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"));
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()));
ObjectNode request = MAPPER.createObjectNode();
request.put("UserPoolId", pool.getId());
request.put("Username", "alice");
request.putObject("ClientMetadata").put("source", "admin-confirm");
new CognitoJsonHandler(service, MAPPER).handle("AdminConfirmSignUp", request, "us-east-1");
verify(lambdaService).invoke(anyString(), eq("arn:aws:lambda:::post-confirm"),
payload.capture(), eq(InvocationType.RequestResponse));
JsonNode event = MAPPER.readTree(payload.getValue());
assertEquals("PostConfirmation_ConfirmSignUp", event.path("triggerSource").asText());
assertEquals("admin-confirm", event.path("request").path("clientMetadata").path("source").asText());
}
@Test
void postConfirmationFiresOnConfirmForgotPassword() throws Exception {
VerificationCodeService verificationCodes = mock(VerificationCodeService.class);
CognitoService svc = createServiceWithMessaging(
mock(SesService.class), mock(SnsService.class), verificationCodes);
UserPool pool = svc.createUserPool(Map.of(
"PoolName", "trigger-pool",
"LambdaConfig", Map.of("PostConfirmation", "arn:aws:lambda:::post-confirm")), "us-east-1");
UserPoolClient client = svc.createUserPoolClient(
pool.getId(), "c", false, false, List.of(), List.of());
svc.adminCreateUser(pool.getId(), "alice", Map.of("email", "alice@example.com"), null);
svc.adminSetUserPassword(pool.getId(), "alice", "Perm1234!", true);
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()));
ObjectNode request = MAPPER.createObjectNode();
request.put("ClientId", client.getClientId());
request.put("Username", "alice");
request.put("ConfirmationCode", "123456");
request.put("Password", "NewPass1234!");
request.putObject("ClientMetadata").put("source", "forgot-password");
new CognitoJsonHandler(svc, MAPPER).handle("ConfirmForgotPassword", request, "us-east-1");
verify(lambdaService).invoke(anyString(), eq("arn:aws:lambda:::post-confirm"),
payload.capture(), eq(InvocationType.RequestResponse));
JsonNode event = MAPPER.readTree(payload.getValue());
assertEquals("PostConfirmation_ConfirmForgotPassword", event.path("triggerSource").asText());
assertEquals("forgot-password", event.path("request").path("clientMetadata").path("source").asText());
}
@Test