mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
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:
1 parent
010bbd15cd
commit
e1c3f7fcf6
3 files changed
+222
-3
No files matched your search
+161
@@ -166,6 +166,167 @@ class CognitoLambdaTriggersTest {
|
||||
assertNotNull(auth.get("AccessToken"));
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// PostConfirmation
|
||||
// =========================================================================
|
||||
|
||||
@Test
|
||||
void postConfirmationFiresOnConfirmSignUp() {
|
||||
UserPool pool = createPoolWithLambdaConfig(Map.of("PostConfirmation", "arn:aws:lambda:::post-confirm"));
|
||||
UserPoolClient client = createClient(pool);
|
||||
|
||||
// SignUp creates an UNCONFIRMED user
|
||||
service.signUp(client.getClientId(), "alice", "Perm1234!",
|
||||
Map.of("email", "alice@example.com"));
|
||||
|
||||
when(lambdaService.invoke(anyString(), eq("arn:aws:lambda:::post-confirm"),
|
||||
any(byte[].class), eq(InvocationType.RequestResponse)))
|
||||
.thenReturn(ok(Map.of()));
|
||||
|
||||
service.confirmSignUp(client.getClientId(), "alice");
|
||||
|
||||
verify(lambdaService, atLeastOnce())
|
||||
.invoke(anyString(), eq("arn:aws:lambda:::post-confirm"),
|
||||
any(byte[].class), eq(InvocationType.RequestResponse));
|
||||
}
|
||||
|
||||
@Test
|
||||
void postConfirmationLambdaErrorDoesNotBlockConfirm() {
|
||||
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"));
|
||||
|
||||
when(lambdaService.invoke(anyString(), eq("arn:aws:lambda:::post-confirm"),
|
||||
any(byte[].class), any()))
|
||||
.thenReturn(lambdaError("Unhandled"));
|
||||
|
||||
// confirmSignUp must succeed even if the trigger errors (Cognito semantics)
|
||||
service.confirmSignUp(client.getClientId(), "alice");
|
||||
// Test passes if no exception was thrown.
|
||||
}
|
||||
|
||||
@Test
|
||||
void postConfirmationDoesNotFireWhenNotConfigured() {
|
||||
UserPool pool = createPoolWithLambdaConfig(Map.of());
|
||||
UserPoolClient client = createClient(pool);
|
||||
|
||||
service.signUp(client.getClientId(), "alice", "Perm1234!",
|
||||
Map.of("email", "alice@example.com"));
|
||||
service.confirmSignUp(client.getClientId(), "alice");
|
||||
|
||||
verify(lambdaService, never())
|
||||
.invoke(anyString(), anyString(), any(byte[].class), any());
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// PreSignUp
|
||||
// =========================================================================
|
||||
|
||||
@Test
|
||||
void preSignUpFiresOnSignUp() {
|
||||
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()));
|
||||
|
||||
service.signUp(client.getClientId(), "alice", "Perm1234!",
|
||||
Map.of("email", "alice@example.com"));
|
||||
|
||||
verify(lambdaService, atLeastOnce())
|
||||
.invoke(anyString(), eq("arn:aws:lambda:::pre-signup"),
|
||||
any(byte[].class), eq(InvocationType.RequestResponse));
|
||||
}
|
||||
|
||||
@Test
|
||||
void preSignUpLambdaErrorBlocksSignUp() {
|
||||
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), any()))
|
||||
.thenReturn(lambdaError("Unhandled"));
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class, () ->
|
||||
service.signUp(client.getClientId(), "alice", "Perm1234!",
|
||||
Map.of("email", "alice@example.com")));
|
||||
assertEquals("NotAuthorizedException", ex.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void preSignUpAutoConfirmUserResponseSkipsConfirmStep() {
|
||||
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), any()))
|
||||
.thenReturn(ok(Map.of(
|
||||
"autoConfirmUser", true,
|
||||
"autoVerifyEmail", true)));
|
||||
|
||||
var user = service.signUp(client.getClientId(), "alice", "Perm1234!",
|
||||
Map.of("email", "alice@example.com"));
|
||||
|
||||
assertEquals("CONFIRMED", user.getUserStatus(),
|
||||
"autoConfirmUser=true should set status to CONFIRMED at signup");
|
||||
assertEquals("true", user.getAttributes().get("email_verified"),
|
||||
"autoVerifyEmail=true should set email_verified attribute");
|
||||
}
|
||||
|
||||
@Test
|
||||
void preSignUpAutoVerifyPhoneSetsPhoneVerifiedAttribute() {
|
||||
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), any()))
|
||||
.thenReturn(ok(Map.of("autoVerifyPhone", true)));
|
||||
|
||||
var user = service.signUp(client.getClientId(), "alice", "Perm1234!",
|
||||
Map.of("phone_number", "+15551234567"));
|
||||
|
||||
assertEquals("true", user.getAttributes().get("phone_number_verified"),
|
||||
"autoVerifyPhone=true should set phone_number_verified attribute");
|
||||
}
|
||||
|
||||
@Test
|
||||
void preSignUpAutoConfirmAlsoFiresPostConfirmation() {
|
||||
// AWS docs: when PreSignUp returns autoConfirmUser=true, PostConfirmation is also invoked.
|
||||
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), any()))
|
||||
.thenReturn(ok(Map.of("autoConfirmUser", true)));
|
||||
when(lambdaService.invoke(anyString(), eq("arn:aws:lambda:::post-confirm"),
|
||||
any(byte[].class), any()))
|
||||
.thenReturn(ok(Map.of()));
|
||||
|
||||
service.signUp(client.getClientId(), "alice", "Perm1234!",
|
||||
Map.of("email", "alice@example.com"));
|
||||
|
||||
verify(lambdaService, atLeastOnce())
|
||||
.invoke(anyString(), eq("arn:aws:lambda:::post-confirm"),
|
||||
any(byte[].class), eq(InvocationType.RequestResponse));
|
||||
}
|
||||
|
||||
@Test
|
||||
void preSignUpDoesNotFireWhenNotConfigured() {
|
||||
UserPool pool = createPoolWithLambdaConfig(Map.of());
|
||||
UserPoolClient client = createClient(pool);
|
||||
|
||||
service.signUp(client.getClientId(), "alice", "Perm1234!",
|
||||
Map.of("email", "alice@example.com"));
|
||||
|
||||
verify(lambdaService, never())
|
||||
.invoke(anyString(), anyString(), any(byte[].class), any());
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// PreTokenGeneration
|
||||
// =========================================================================
|
||||
|
||||
Reference in new issue
Block a user