feat(cognito): fire PostConfirmation and PreSignUp triggers (#859)

Adds support for two Cognito user-pool Lambda triggers that were not yet
wired in floci:

PostConfirmation:
  Fires on ConfirmSignUp with triggerSource=PostConfirmation_ConfirmSignUp.
  Errors are logged but do not block the confirm operation, matching
  AWS Cognito fire-and-forget semantics for post-* triggers.

PreSignUp:
  Fires on SignUp with triggerSource=PreSignUp_SignUp BEFORE the user is
  persisted. Lambda errors block the signup (NotAuthorizedException). The
  trigger response can auto-confirm the user and auto-verify email/phone
  via the documented response fields:
    response.autoConfirmUser
    response.autoVerifyEmail
    response.autoVerifyPhone

When PreSignUp returns autoConfirmUser=true, PostConfirmation is also
invoked, matching AWS Cognito's documented chain behavior:
  https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-lambda-pre-sign-up.html

Tests cover happy path, error handling, response application, the
auto-confirm → post-confirmation chain, and not-configured no-op behavior
for both triggers.
This commit is contained in:
Ricardo Medina authored and GitHub committed 2026-05-16 13:55:44 -05:00
1 parent 010bbd15cd
commit e1c3f7fcf6
3 files changed
+222 -3

No files matched your search

@@ -732,6 +732,39 @@ final class CognitoAuthFlowHandler {
invokeTrigger(pool, client, user, "PostAuthentication", "PostAuthentication_Authentication", req);
}
void firePostConfirmation(UserPool pool, UserPoolClient client, CognitoUser user,
Map<String, String> clientMetadata, String triggerSource) {
Map<String, Object> req = new HashMap<>();
req.put("clientMetadata", clientMetadata == null ? Map.of() : clientMetadata);
// PostConfirmation is fire-and-forget per AWS Cognito semantics:
// trigger errors are logged but never block the confirm operation.
invokeTrigger(pool, client, user, "PostConfirmation", triggerSource, req);
}
record PreSignUpResponse(boolean autoConfirmUser, boolean autoVerifyEmail, boolean autoVerifyPhone) {
static PreSignUpResponse empty() { return new PreSignUpResponse(false, false, false); }
}
PreSignUpResponse firePreSignUp(UserPool pool, UserPoolClient client, CognitoUser user,
Map<String, String> validationData,
Map<String, String> clientMetadata,
String triggerSource) {
Map<String, Object> req = new HashMap<>();
req.put("validationData", validationData == null ? Map.of() : validationData);
req.put("clientMetadata", clientMetadata == null ? Map.of() : clientMetadata);
TriggerResult result = invokeTrigger(pool, client, user, "PreSignUp", triggerSource, req);
if (result.errored()) {
throw new AwsException("NotAuthorizedException",
"PreSignUp trigger denied signup: " + result.errorMessage(), 400);
}
if (!result.configured() || result.response() == null) return PreSignUpResponse.empty();
Map<String, Object> resp = result.response();
return new PreSignUpResponse(
Boolean.TRUE.equals(resp.get("autoConfirmUser")),
Boolean.TRUE.equals(resp.get("autoVerifyEmail")),
Boolean.TRUE.equals(resp.get("autoVerifyPhone")));
}
@SuppressWarnings("unchecked")
private CognitoService.ClaimsOverride firePreTokenGeneration(UserPool pool, UserPoolClient client, CognitoUser user,
Map<String, String> clientMetadata, String triggerSource) {
@@ -754,7 +754,7 @@ public class CognitoService {
UserPoolClient client = clientStore.get(clientId)
.orElseThrow(() -> new AwsException("ResourceNotFoundException", "Client not found", 404));
String userPoolId = client.getUserPoolId();
describeUserPool(userPoolId);
UserPool pool = describeUserPool(userPoolId);
String key = userKey(userPoolId, username);
if (userStore.get(key).isPresent()) {
@@ -770,13 +770,34 @@ public class CognitoService {
user.getAttributes().putAll(attributes);
}
// Ensure sub attribute is present
// Ensure sub attribute is present (required by PreSignUp event)
if (!user.getAttributes().containsKey("sub")) {
user.getAttributes().put("sub", UUID.randomUUID().toString());
}
// 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");
if (preSignUp.autoConfirmUser()) {
user.setUserStatus("CONFIRMED");
}
if (preSignUp.autoVerifyEmail()) {
user.getAttributes().put("email_verified", "true");
}
if (preSignUp.autoVerifyPhone()) {
user.getAttributes().put("phone_number_verified", "true");
}
userStore.put(key, user);
LOG.infov("Signed up user {0} in pool {1}", username, userPoolId);
LOG.infov("Signed up user {0} in pool {1} (status={2})",
username, userPoolId, user.getUserStatus());
// 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, user, Map.of(), "PostConfirmation_ConfirmSignUp");
}
return user;
}
@@ -787,6 +808,10 @@ public class CognitoService {
user.setUserStatus("CONFIRMED");
user.setLastModifiedDate(System.currentTimeMillis() / 1000L);
userStore.put(userKey(client.getUserPoolId(), user.getUsername()), user);
UserPool pool = poolStore.get(client.getUserPoolId())
.orElseThrow(() -> new AwsException("ResourceNotFoundException", "User pool not found", 404));
authFlowHandler.firePostConfirmation(pool, client, user, Map.of(), "PostConfirmation_ConfirmSignUp");
}
// ──────────────────────────── Auth ────────────────────────────