Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(cognito): clear stale pending admin updates (#2533)

Daniel Zwickera704f731aa15
diff --git a/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoService.java b/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoService.java
index 72994b6942..4203b8654f 100644
--- a/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoService.java
@@ -1791,6 +1791,8 @@ public class CognitoService implements ResourceProvider {
}
}
}
+ attributes.keySet().forEach(attributeName ->
+ clearPendingAttributeVerification(userPoolId, user, attributeName));
user.getAttributes().putAll(attributes);
user.setLastModifiedDate(System.currentTimeMillis() / 1000L);
userStore.put(userKey(userPoolId, user.getUsername()), user);
@@ -1800,6 +1802,7 @@ public class CognitoService implements ResourceProvider {
CognitoUser user = adminGetUser(userPoolId, username);
for (String attrName : attributeNames) {
user.getAttributes().remove(attrName);
+ clearPendingAttributeVerification(userPoolId, user, attrName);
}
user.setLastModifiedDate(System.currentTimeMillis() / 1000L);
userStore.put(userKey(userPoolId, user.getUsername()), user);
@@ -4026,6 +4029,18 @@ public class CognitoService implements ResourceProvider {
return "email".equals(attributeName) || "phone_number".equals(attributeName);
}
+ private void clearPendingAttributeVerification(String userPoolId, CognitoUser user,
+ String attributeName) {
+ if (!isVerifiableContactAttribute(attributeName)) {
+ return;
+ }
+ user.getPendingAttributes().remove(attributeName);
+ if (verificationCodeService != null) {
+ verificationCodeService.invalidatePrevious(
+ userPoolId, user.getUsername(), verificationPurpose(attributeName));
+ }
+ }
+
private VerificationCode.Purpose verificationPurpose(String attributeName) {
return "email".equals(attributeName)
? VerificationCode.Purpose.EMAIL_ATTRIBUTE_VERIFICATION
diff --git a/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoServiceTest.java
index 19bd311770..f832de46e7 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoServiceTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoServiceTest.java
@@ -1717,6 +1717,110 @@ class CognitoServiceTest {
serviceWithVerification.adminGetUser(pool.getId(), "alice").getAttributes().get("email"));
}
+ @Test
+ void adminContactUpdateClearsOnlyMatchingPendingValueAndVerificationCode() {
+ VerificationCodeService verificationCodeService = mock(VerificationCodeService.class);
+ CognitoMessageDispatcher messageDispatcher = mock(CognitoMessageDispatcher.class);
+ PendingContactFixture fixture = createPendingContactFixture(
+ verificationCodeService, messageDispatcher);
+ clearInvocations(verificationCodeService);
+
+ fixture.service().adminUpdateUserAttributes(fixture.pool().getId(), "alice", Map.of(
+ "email", "[email protected]",
+ "custom:note", "updated"));
+ fixture.service().adminUpdateUserAttributes(fixture.pool().getId(), "alice", Map.of(
+ "email", "[email protected]"));
+
+ CognitoUser updated = fixture.service().adminGetUser(fixture.pool().getId(), "alice");
+ assertEquals("[email protected]", updated.getAttributes().get("email"));
+ assertEquals("updated", updated.getAttributes().get("custom:note"));
+ assertFalse(updated.getPendingAttributes().containsKey("email"));
+ assertEquals("+12025550101", updated.getPendingAttributes().get("phone_number"));
+ verify(verificationCodeService, times(2)).invalidatePrevious(
+ fixture.pool().getId(), "alice",
+ VerificationCode.Purpose.EMAIL_ATTRIBUTE_VERIFICATION);
+ verify(verificationCodeService, never()).invalidatePrevious(
+ fixture.pool().getId(), "alice",
+ VerificationCode.Purpose.PHONE_ATTRIBUTE_VERIFICATION);
+ }
+
+ @Test
+ void adminContactDeleteClearsOnlyMatchingPendingValueAndVerificationCode() {
+ VerificationCodeService verificationCodeService = mock(VerificationCodeService.class);
+ CognitoMessageDispatcher messageDispatcher = mock(CognitoMessageDispatcher.class);
+ PendingContactFixture fixture = createPendingContactFixture(
+ verificationCodeService, messageDispatcher);
+ clearInvocations(verificationCodeService);
+
+ fixture.service().adminDeleteUserAttributes(
+ fixture.pool().getId(), "alice", List.of("phone_number", "custom:note"));
+ fixture.service().adminDeleteUserAttributes(
+ fixture.pool().getId(), "alice", List.of("phone_number"));
+
+ CognitoUser updated = fixture.service().adminGetUser(fixture.pool().getId(), "alice");
+ assertFalse(updated.getAttributes().containsKey("phone_number"));
+ assertFalse(updated.getAttributes().containsKey("custom:note"));
+ assertFalse(updated.getPendingAttributes().containsKey("phone_number"));
+ assertEquals("[email protected]", updated.getPendingAttributes().get("email"));
+ verify(verificationCodeService, times(2)).invalidatePrevious(
+ fixture.pool().getId(), "alice",
+ VerificationCode.Purpose.PHONE_ATTRIBUTE_VERIFICATION);
+ verify(verificationCodeService, never()).invalidatePrevious(
+ fixture.pool().getId(), "alice",
+ VerificationCode.Purpose.EMAIL_ATTRIBUTE_VERIFICATION);
+ }
+
+ @SuppressWarnings("unchecked")
+ private PendingContactFixture createPendingContactFixture(
+ VerificationCodeService verificationCodeService,
+ CognitoMessageDispatcher messageDispatcher) {
+ when(verificationCodeService.issue(any(), any(), any(), any())).thenReturn("123456");
+ CognitoService serviceWithVerification = new CognitoService(
+ new InMemoryStorage<>(),
+ new InMemoryStorage<>(),
+ new InMemoryStorage<>(),
+ new InMemoryStorage<>(),
+ new InMemoryStorage<>(),
+ new InMemoryStorage<>(),
+ new InMemoryStorage<>(),
+ new InMemoryStorage<>(),
+ "http://localhost:4566",
+ regionResolver,
+ null,
+ acmService,
+ verificationCodeService,
+ messageDispatcher,
+ mock(TlsCertificateManager.class));
+ UserPool pool = serviceWithVerification.createUserPool(Map.of(
+ "PoolName", "AdminPendingContactPool",
+ "AutoVerifiedAttributes", List.of("email", "phone_number"),
+ "UserAttributeUpdateSettings", Map.of(
+ "AttributesRequireVerificationBeforeUpdate", List.of("email", "phone_number"))),
+ "us-east-1");
+ UserPoolClient client = serviceWithVerification.createUserPoolClient(
+ pool.getId(), "admin-pending-contact-client", false, false, List.of(), List.of());
+ serviceWithVerification.adminCreateUser(pool.getId(), "alice", Map.of(
+ "email", "[email protected]",
+ "email_verified", "true",
+ "phone_number", "+12025550100",
+ "phone_number_verified", "true",
+ "custom:note", "original"), "TempPass1!");
+ serviceWithVerification.adminSetUserPassword(
+ pool.getId(), "alice", "Permanent1!", true);
+ Map<String, Object> authResult = serviceWithVerification.initiateAuth(
+ client.getClientId(), "USER_PASSWORD_AUTH",
+ Map.of("USERNAME", "alice", "PASSWORD", "Permanent1!"));
+ String accessToken = (String) ((Map<String, Object>) authResult.get("AuthenticationResult"))
+ .get("AccessToken");
+ serviceWithVerification.updateUserAttributes(accessToken, Map.of(
+ "email", "[email protected]",
+ "phone_number", "+12025550101"));
+ return new PendingContactFixture(serviceWithVerification, pool);
+ }
+
+ private record PendingContactFixture(CognitoService service, UserPool pool) {
+ }
+
@Test
@SuppressWarnings("unchecked")
void jwtSubMatchesStoredSubAttribute() {