fix(cognito): answer UsernameExistsException when AdminCreateUser's username is taken in a UsernameAttributes pool (#9286)

This commit is contained in:
jwoa95 authored and GitHub committed 2026-11-23 13:48:58 -04:00
1 parent 5807e901a4
commit 374b5f5c8e
4 files changed
+55 -190

No files matched your search

@@ -831,30 +831,6 @@ class CognitoLambdaTriggersTest {
assertNull(user.getAttributes().get("phone_number_verified"));
}
@Test
void preSignUpLambdaErrorOnForcedAliasCreationKeepsTheExistingAlias() {
Map<String, Object> poolRequest = new HashMap<>();
poolRequest.put("PoolName", "alias-trigger-pool");
poolRequest.put("UsernameAttributes", List.of("email"));
poolRequest.put("LambdaConfig", Map.of("PreSignUp", "arn:aws:lambda:::pre-signup"));
UserPool pool = service.createUserPool(poolRequest, "us-east-1");
when(lambdaService.invoke(anyString(), eq("arn:aws:lambda:::pre-signup"),
any(byte[].class), any()))
.thenReturn(ok(Map.of()))
.thenReturn(lambdaError("Unhandled", "Refused"));
CognitoUser holder = service.adminCreateUser(pool.getId(), "alice@example.com",
Map.of("email_verified", "true"), null, "SUPPRESS");
AwsException ex = assertThrows(AwsException.class, () ->
service.adminCreateUser(pool.getId(), "alice@example.com",
Map.of("email_verified", "true"), null, "SUPPRESS", true));
assertEquals("UserLambdaValidationException", ex.getErrorCode());
CognitoUser after = service.adminGetUser(pool.getId(), holder.getUsername());
assertEquals("alice@example.com", after.getAttributes().get("email"));
assertEquals("true", after.getAttributes().get("email_verified"));
}
@Test
void preSignUpFunctionCallingBackForTheSameUsernameIsNotBlocked() {
UserPool pool = createPoolWithLambdaConfig(Map.of("PreSignUp", "arn:aws:lambda:::pre-signup"));
@@ -914,7 +890,7 @@ class CognitoLambdaTriggersTest {
AwsException second = assertThrows(AwsException.class, () -> service.adminCreateUser(pool.getId(),
"alice", Map.of("name", "second"), null, "SUPPRESS"));
assertEquals("UsernameExistsException", second.getErrorCode());
assertEquals("User already exists", second.getMessage());
assertEquals("User account already exists", second.getMessage());
releaseFirst.countDown();
assertEquals("alice", first.get(10, TimeUnit.SECONDS).getUsername());
@@ -973,66 +949,6 @@ class CognitoLambdaTriggersTest {
assertEquals("retry", service.adminGetUser(pool.getId(), "alice").getAttributes().get("name"));
}
@Test
void forcedAliasCreationDoesNotRestoreAHolderThePreSignUpFunctionDeleted() {
UserPool pool = createEmailUsernamePoolWithPreSignUp();
when(lambdaService.invoke(anyString(), eq("arn:aws:lambda:::pre-signup"),
any(byte[].class), any()))
.thenReturn(ok(Map.of()))
.thenAnswer(invocation -> {
callFromFunction(() -> {
service.adminDeleteUser(pool.getId(), "alice@example.com");
return null;
});
return ok(Map.of());
});
CognitoUser holder = service.adminCreateUser(pool.getId(), "alice@example.com",
Map.of("email_verified", "true"), null, "SUPPRESS");
CognitoUser created = service.adminCreateUser(pool.getId(), "alice@example.com",
Map.of("email_verified", "true"), null, "SUPPRESS", true);
AwsException lookup = assertThrows(AwsException.class, () ->
service.adminGetUser(pool.getId(), holder.getUsername()));
assertEquals("UserNotFoundException", lookup.getErrorCode());
assertEquals(created.getUsername(),
service.adminGetUser(pool.getId(), "alice@example.com").getUsername());
}
@Test
void forcedAliasCreationKeepsTheEmailThePreSignUpFunctionGaveTheHolder() {
UserPool pool = createEmailUsernamePoolWithPreSignUp();
when(lambdaService.invoke(anyString(), eq("arn:aws:lambda:::pre-signup"),
any(byte[].class), any()))
.thenReturn(ok(Map.of()))
.thenAnswer(invocation -> {
callFromFunction(() -> {
service.adminUpdateUserAttributes(pool.getId(), "alice@example.com",
Map.of("email", "alice.new@example.com"));
return null;
});
return ok(Map.of());
});
CognitoUser holder = service.adminCreateUser(pool.getId(), "alice@example.com",
Map.of("email_verified", "true"), null, "SUPPRESS");
CognitoUser created = service.adminCreateUser(pool.getId(), "alice@example.com",
Map.of("email_verified", "true"), null, "SUPPRESS", true);
assertEquals("alice.new@example.com",
service.adminGetUser(pool.getId(), holder.getUsername()).getAttributes().get("email"));
assertEquals(created.getUsername(),
service.adminGetUser(pool.getId(), "alice@example.com").getUsername());
}
private UserPool createEmailUsernamePoolWithPreSignUp() {
Map<String, Object> poolRequest = new HashMap<>();
poolRequest.put("PoolName", "alias-trigger-pool");
poolRequest.put("UsernameAttributes", List.of("email"));
poolRequest.put("LambdaConfig", Map.of("PreSignUp", "arn:aws:lambda:::pre-signup"));
return service.createUserPool(poolRequest, "us-east-1");
}
/**
* Makes {@code call} the way a trigger function's SDK call reaches Floci: from another thread,
* while the request that invoked the function waits for it to return.
@@ -6229,7 +6229,7 @@ class CognitoServiceTest {
Map.of("email_verified", "true"), null);
AwsException duplicate = assertThrows(AwsException.class,
() -> service.adminCreateUser(pool.getId(), "Fixture@Example.com", Map.of(), null));
assertEquals("AliasExistsException", duplicate.getErrorCode());
assertEquals("UsernameExistsException", duplicate.getErrorCode());
UserPoolClient client = service.createUserPoolClient(pool.getId(), "client", false, false, List.of(), List.of());
AwsException signup = assertThrows(AwsException.class, () -> service.signUp(client.getClientId(),
"Fixture@Example.com", "Valid-password123!", Map.of(), Map.of(), Map.of()));
@@ -6641,7 +6641,7 @@ class CognitoServiceTest {
}
@Test
void emailAliasPoolVerifiedDuplicateThrowsAliasExists() {
void emailAliasPoolVerifiedDuplicateWithVerifiedFlagThrowsUsernameExists() {
UserPool pool = createEmailAliasPool();
service.adminCreateUser(pool.getId(), "dupe@b.com",
new HashMap<>(Map.of("email", "dupe@b.com", "email_verified", "true")), null);
@@ -6649,33 +6649,46 @@ class CognitoServiceTest {
AwsException ex = assertThrows(AwsException.class,
() -> service.adminCreateUser(pool.getId(), "dupe@b.com",
new HashMap<>(Map.of("email", "dupe@b.com", "email_verified", "true")), null));
assertEquals("AliasExistsException", ex.getErrorCode());
assertEquals("UsernameExistsException", ex.getErrorCode());
assertEquals("User account already exists", ex.getMessage());
}
@Test
void emailAliasPoolAliasExistsUsesExistingVerificationNotIncoming() {
void emailAliasPoolVerifiedDuplicateWithoutVerifiedFlagThrowsUsernameExists() {
UserPool pool = createEmailAliasPool();
// Existing owner holds a *verified* alias.
service.adminCreateUser(pool.getId(), "owner@b.com",
new HashMap<>(Map.of("email", "owner@b.com", "email_verified", "true")), null);
// The second create omits email_verified from its own payload. The exception
// type must be driven by the existing owner's verified alias -> AliasExistsException.
AwsException ex = assertThrows(AwsException.class,
() -> service.adminCreateUser(pool.getId(), "owner@b.com",
new HashMap<>(Map.of("email", "owner@b.com")), null));
assertEquals("AliasExistsException", ex.getErrorCode());
assertEquals("UsernameExistsException", ex.getErrorCode());
}
@Test
void emailAliasPoolIncomingVerifiedFlagCannotForceAliasExists() {
void emailAliasPoolForceAliasCreationDoesNotTakeAVerifiedUsername() {
UserPool pool = createEmailAliasPool();
CognitoUser first = service.adminCreateUser(pool.getId(), "move@b.com",
new HashMap<>(Map.of("email", "move@b.com", "email_verified", "true")), null);
AwsException ex = assertThrows(AwsException.class,
() -> service.adminCreateUser(pool.getId(), "move@b.com",
new HashMap<>(Map.of("email", "move@b.com", "email_verified", "true")), null, null, true));
assertEquals("UsernameExistsException", ex.getErrorCode());
List<CognitoUser> users = service.listUsers(pool.getId(), null);
assertEquals(1, users.size());
assertEquals(first.getUsername(), users.get(0).getUsername());
assertEquals("move@b.com", users.get(0).getAttributes().get("email"));
assertEquals("true", users.get(0).getAttributes().get("email_verified"));
}
@Test
void emailAliasPoolUnverifiedDuplicateWithVerifiedFlagThrowsUsernameExists() {
UserPool pool = createEmailAliasPool();
// Existing owner's alias is *unverified*, so the alias is not reserved.
service.adminCreateUser(pool.getId(), "unv@b.com",
new HashMap<>(Map.of("email", "unv@b.com")), null);
// A caller-supplied email_verified=true must not upgrade the decision:
// the existing owner is unverified -> UsernameExistsException, not AliasExistsException.
AwsException ex = assertThrows(AwsException.class,
() -> service.adminCreateUser(pool.getId(), "unv@b.com",
new HashMap<>(Map.of("email", "unv@b.com", "email_verified", "true")), null));
@@ -6683,34 +6696,15 @@ class CognitoServiceTest {
}
@Test
void emailAliasPoolForceAliasCreationReclaimsWithoutIncomingVerifiedFlag() {
void emailAliasPoolUnverifiedDuplicateWithoutVerifiedFlagThrowsUsernameExists() {
UserPool pool = createEmailAliasPool();
CognitoUser first = service.adminCreateUser(pool.getId(), "reclaim@b.com",
new HashMap<>(Map.of("email", "reclaim@b.com", "email_verified", "true")), null);
service.adminCreateUser(pool.getId(), "unv@b.com",
new HashMap<>(Map.of("email", "unv@b.com")), null);
// ForceAliasCreation reclaim keys off the existing owner's verified alias and
// must not require the caller to re-send email_verified in the new payload.
CognitoUser second = service.adminCreateUser(pool.getId(), "reclaim@b.com",
new HashMap<>(Map.of("email", "reclaim@b.com")), null, null, true);
assertNotEquals(first.getUsername(), second.getUsername());
assertEquals(second.getUsername(), service.adminGetUser(pool.getId(), "reclaim@b.com").getUsername());
assertNull(service.adminGetUser(pool.getId(), first.getUsername()).getAttributes().get("email"));
}
@Test
void emailAliasPoolForceAliasCreationMigratesVerifiedAlias() {
UserPool pool = createEmailAliasPool();
CognitoUser first = service.adminCreateUser(pool.getId(), "move@b.com",
new HashMap<>(Map.of("email", "move@b.com", "email_verified", "true")), null);
CognitoUser second = service.adminCreateUser(pool.getId(), "move@b.com",
new HashMap<>(Map.of("email", "move@b.com", "email_verified", "true")), null, null, true);
assertNotEquals(first.getUsername(), second.getUsername());
// The alias now resolves to the new user; the previous user lost it.
assertEquals(second.getUsername(), service.adminGetUser(pool.getId(), "move@b.com").getUsername());
assertNull(service.adminGetUser(pool.getId(), first.getUsername()).getAttributes().get("email"));
AwsException ex = assertThrows(AwsException.class,
() -> service.adminCreateUser(pool.getId(), "unv@b.com",
new HashMap<>(Map.of("email", "unv@b.com")), null));
assertEquals("UsernameExistsException", ex.getErrorCode());
}
@Test
@@ -7473,26 +7467,6 @@ class CognitoServiceTest {
AwsException lookup = assertThrows(AwsException.class, () -> svc.adminGetUser(pool.getId(), "alice"));
assertEquals("UserNotFoundException", lookup.getErrorCode());
}
@Test
void failedInvitationDelivery_withForceAliasCreation_leavesTheAliasWithItsHolder() {
Map<String, Object> poolRequest = Map.of("PoolName", "AliasInvitePool",
"UsernameAttributes", List.of("email"));
UserPool pool = svc.createUserPool(poolRequest, "us-east-1");
CognitoUser holder = create(pool, "alice@example.com", Map.of("email_verified", "true"), "Temp1234!",
"SUPPRESS", List.of("EMAIL"));
failDelivery();
AwsException failure = assertThrows(AwsException.class, () -> svc.adminCreateUser(pool.getId(),
"alice@example.com", Map.of("email_verified", "true"), null, null, true, List.of("EMAIL"),
Map.of(), Map.of()));
assertEquals("CodeDeliveryFailureException", failure.getErrorCode());
CognitoUser after = svc.adminGetUser(pool.getId(), holder.getUsername());
assertEquals("alice@example.com", after.getAttributes().get("email"));
assertEquals("true", after.getAttributes().get("email_verified"));
assertEquals(holder.getUsername(), svc.adminGetUser(pool.getId(), "alice@example.com").getUsername());
}
}
// --- replaceGroupDescription: a service method that does not exist on main ---