feat(cognito): support pending user attribute verification (#2531)

* feat(cognito): support pending attribute verification

* fix(cognito): omit UserAttributeUpdateSettings when a pool never configured it

DescribeUserPool/CreateUserPool/UpdateUserPool always serialized
UserAttributeUpdateSettings as an empty object for pools that never set it,
where AWS omits absent optional members entirely. UserPoolAddOns right above
it already handles this correctly (an unconfigured pool omits the key rather
than returning {} or null, confirmed by Terraform re-planning the block into
and out of existence); UserAttributeUpdateSettings now follows the same
pattern.

* test(cognito): replace var with explicit AWS SDK response types

AGENTS.md's Types and names rule bans var in local declarations; the compat
test used it for four locals whose concrete AWS SDK type is the thing under
review here (CodeDeliveryDetailsType, UpdateUserAttributesResponse).

---------

Co-authored-by: Priamo Germosen <6233424+pgermosen@users.noreply.github.com>
This commit is contained in:
Daniel ZwickerandPriamo Germosen authored and GitHub committed 2026-09-14 11:50:16 -04:00
1 parent 52bafb7e5c
commit a03c4d6160
8 files changed
+545 -37

No files matched your search

@@ -7,12 +7,17 @@ import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import software.amazon.awssdk.services.cognitoidentityprovider.CognitoIdentityProviderClient;
import software.amazon.awssdk.services.cognitoidentityprovider.model.AliasAttributeType;
import software.amazon.awssdk.services.cognitoidentityprovider.model.AttributeType;
import software.amazon.awssdk.services.cognitoidentityprovider.model.AuthFlowType;
import software.amazon.awssdk.services.cognitoidentityprovider.model.CodeDeliveryDetailsType;
import software.amazon.awssdk.services.cognitoidentityprovider.model.CodeMismatchException;
import software.amazon.awssdk.services.cognitoidentityprovider.model.ExpiredCodeException;
import software.amazon.awssdk.services.cognitoidentityprovider.model.ExplicitAuthFlowsType;
import software.amazon.awssdk.services.cognitoidentityprovider.model.MessageActionType;
import software.amazon.awssdk.services.cognitoidentityprovider.model.UpdateUserAttributesResponse;
import software.amazon.awssdk.services.cognitoidentityprovider.model.UserNotFoundException;
import software.amazon.awssdk.services.cognitoidentityprovider.model.VerifiedAttributeType;
import java.net.URI;
import java.net.URLEncoder;
@@ -68,41 +73,22 @@ class CognitoVerifyUserAttributeTest {
poolId = cognito.createUserPool(b -> b.poolName("verify-attribute-" + UUID.randomUUID()))
.userPool()
.id();
String clientId = cognito.createUserPoolClient(b -> b
.userPoolId(poolId)
.clientName("verify-attribute-client")
.explicitAuthFlows(ExplicitAuthFlowsType.ALLOW_USER_PASSWORD_AUTH))
.userPoolClient()
.clientId();
String clientId = createClient();
String username = "verify-" + UUID.randomUUID();
String email = username + "@example.com";
String phoneNumber = "+1555" + ThreadLocalRandom.current().nextLong(1_000_000, 10_000_000);
cognito.adminCreateUser(b -> b
.userPoolId(poolId)
.username(username)
.messageAction(MessageActionType.SUPPRESS)
.userAttributes(
AttributeType.builder().name("email").value(email).build(),
AttributeType.builder().name("phone_number").value(phoneNumber).build()));
cognito.adminSetUserPassword(b -> b
.userPoolId(poolId)
.username(username)
.password(PASSWORD)
.permanent(true));
createConfirmedUser(username,
AttributeType.builder().name("email").value(email).build(),
AttributeType.builder().name("phone_number").value(phoneNumber).build());
String accessToken = cognito.initiateAuth(b -> b
.clientId(clientId)
.authFlow(AuthFlowType.USER_PASSWORD_AUTH)
.authParameters(Map.of("USERNAME", username, "PASSWORD", PASSWORD)))
.authenticationResult()
.accessToken();
String accessToken = authenticate(clientId, username);
clearInspectionEndpoint("/_aws/ses");
clearInspectionEndpoint("/_aws/sns");
var emailDelivery = cognito.getUserAttributeVerificationCode(b -> b
CodeDeliveryDetailsType emailDelivery = cognito.getUserAttributeVerificationCode(b -> b
.accessToken(accessToken)
.attributeName("email"))
.codeDeliveryDetails();
@@ -111,7 +97,7 @@ class CognitoVerifyUserAttributeTest {
assertThat(emailDelivery.destination()).isEqualTo("v***@e***");
String emailCode = fetchLatestSesVerificationCode(email);
var phoneDelivery = cognito.getUserAttributeVerificationCode(b -> b
CodeDeliveryDetailsType phoneDelivery = cognito.getUserAttributeVerificationCode(b -> b
.accessToken(accessToken)
.attributeName("phone_number"))
.codeDeliveryDetails();
@@ -173,6 +159,141 @@ class CognitoVerifyUserAttributeTest {
assertThat(userAttribute(username, "phone_number_verified")).isEqualTo("true");
}
@Test
void requiredEmailUpdateStaysPendingUntilVerification() throws Exception {
poolId = cognito.createUserPool(b -> b
.poolName("pending-email-update-" + UUID.randomUUID())
.aliasAttributes(AliasAttributeType.EMAIL)
.autoVerifiedAttributes(VerifiedAttributeType.EMAIL)
.userAttributeUpdateSettings(s -> s
.attributesRequireVerificationBeforeUpdate(VerifiedAttributeType.EMAIL)))
.userPool()
.id();
String clientId = createClient();
String username = "pending-email-" + UUID.randomUUID();
String oldEmail = "old+" + UUID.randomUUID() + "@example.com";
String newEmail = "new+" + UUID.randomUUID() + "@example.com";
createConfirmedUser(username,
AttributeType.builder().name("email").value(oldEmail).build(),
AttributeType.builder().name("email_verified").value("true").build());
String accessToken = authenticate(clientId, oldEmail);
clearInspectionEndpoint("/_aws/ses");
UpdateUserAttributesResponse update = cognito.updateUserAttributes(b -> b
.accessToken(accessToken)
.userAttributes(AttributeType.builder().name("email").value(newEmail).build()));
assertThat(update.codeDeliveryDetailsList()).singleElement().satisfies(delivery -> {
assertThat(delivery.attributeName()).isEqualTo("email");
assertThat(delivery.deliveryMediumAsString()).isEqualTo("EMAIL");
assertThat(delivery.destination()).isEqualTo(maskedEmail(newEmail));
});
String code = fetchLatestSesVerificationCode(newEmail);
assertThat(userAttribute(username, "email")).isEqualTo(oldEmail);
assertThat(userAttribute(username, "email_verified")).isEqualTo("true");
assertThat(authenticate(clientId, oldEmail)).isNotBlank();
assertAliasRejected(clientId, newEmail);
assertThatThrownBy(() -> cognito.verifyUserAttribute(b -> b
.accessToken(accessToken)
.attributeName("email")
.code(differentCode(code))))
.isInstanceOf(CodeMismatchException.class);
assertThat(userAttribute(username, "email")).isEqualTo(oldEmail);
assertThat(userAttribute(username, "email_verified")).isEqualTo("true");
assertThat(authenticate(clientId, oldEmail)).isNotBlank();
assertAliasRejected(clientId, newEmail);
cognito.verifyUserAttribute(b -> b
.accessToken(accessToken)
.attributeName("email")
.code(code));
assertThat(userAttribute(username, "email")).isEqualTo(newEmail);
assertThat(userAttribute(username, "email_verified")).isEqualTo("true");
assertAliasRejected(clientId, oldEmail);
assertThat(authenticate(clientId, newEmail)).isNotBlank();
}
@Test
void nonRequiredPhoneUpdateIsImmediateAndUnverifiedUntilVerification() throws Exception {
poolId = cognito.createUserPool(b -> b
.poolName("immediate-phone-update-" + UUID.randomUUID())
.aliasAttributes(AliasAttributeType.PHONE_NUMBER)
.autoVerifiedAttributes(VerifiedAttributeType.PHONE_NUMBER))
.userPool()
.id();
String clientId = createClient();
String username = "immediate-phone-" + UUID.randomUUID();
String oldPhone = "+15550001111";
String newPhone = "+15550002222";
createConfirmedUser(username,
AttributeType.builder().name("phone_number").value(oldPhone).build(),
AttributeType.builder().name("phone_number_verified").value("true").build());
String accessToken = authenticate(clientId, oldPhone);
clearInspectionEndpoint("/_aws/sns");
UpdateUserAttributesResponse update = cognito.updateUserAttributes(b -> b
.accessToken(accessToken)
.userAttributes(AttributeType.builder().name("phone_number").value(newPhone).build()));
assertThat(update.codeDeliveryDetailsList()).singleElement().satisfies(delivery -> {
assertThat(delivery.attributeName()).isEqualTo("phone_number");
assertThat(delivery.deliveryMediumAsString()).isEqualTo("SMS");
assertThat(delivery.destination()).isEqualTo(maskedPhoneNumber(newPhone));
});
String code = fetchLatestSnsVerificationCode(newPhone);
assertThat(userAttribute(username, "phone_number")).isEqualTo(newPhone);
assertThat(userAttribute(username, "phone_number_verified")).isEqualTo("false");
assertAliasRejected(clientId, oldPhone);
assertAliasRejected(clientId, newPhone);
cognito.verifyUserAttribute(b -> b
.accessToken(accessToken)
.attributeName("phone_number")
.code(code));
assertThat(userAttribute(username, "phone_number_verified")).isEqualTo("true");
assertAliasRejected(clientId, oldPhone);
assertThat(authenticate(clientId, newPhone)).isNotBlank();
}
private String createClient() {
return cognito.createUserPoolClient(b -> b
.userPoolId(poolId)
.clientName("verify-attribute-client")
.explicitAuthFlows(ExplicitAuthFlowsType.ALLOW_USER_PASSWORD_AUTH))
.userPoolClient()
.clientId();
}
private void createConfirmedUser(String username, AttributeType... attributes) {
cognito.adminCreateUser(b -> b
.userPoolId(poolId)
.username(username)
.messageAction(MessageActionType.SUPPRESS)
.userAttributes(attributes));
cognito.adminSetUserPassword(b -> b
.userPoolId(poolId)
.username(username)
.password(PASSWORD)
.permanent(true));
}
private String authenticate(String clientId, String username) {
return cognito.initiateAuth(b -> b
.clientId(clientId)
.authFlow(AuthFlowType.USER_PASSWORD_AUTH)
.authParameters(Map.of("USERNAME", username, "PASSWORD", PASSWORD)))
.authenticationResult()
.accessToken();
}
private void assertAliasRejected(String clientId, String alias) {
assertThatThrownBy(() -> authenticate(clientId, alias))
.isInstanceOf(UserNotFoundException.class);
}
private String userAttribute(String username, String attributeName) {
return cognito.adminGetUser(b -> b.userPoolId(poolId).username(username))
.userAttributes()
@@ -232,6 +353,11 @@ class CognitoVerifyUserAttributeTest {
return "000000".equals(code) ? "000001" : "000000";
}
private static String maskedEmail(String email) {
int at = email.indexOf('@');
return email.charAt(0) + "***@" + email.charAt(at + 1) + "***";
}
private static String maskedPhoneNumber(String phoneNumber) {
return "+" + "*".repeat(phoneNumber.length() - 5)
+ phoneNumber.substring(phoneNumber.length() - 4);