Light, fluffy, and always free - The AWS Local Emulator alternative
fix(cognito): align AdminGetUser lookup with pool sign-in settings (#1571)
shrimptails-fe6448eec7d48
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.javaindex cd48b5fc12..d84baeeee6 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@@ -158,13 +158,14 @@ public class CognitoService {public UserPool updateUserPool(Map<String, Object> request, String region) {String id = (String) request.get("UserPoolId");UserPool pool = describeUserPool(id);+ UserPool updatedPool = MAPPER.convertValue(pool, UserPool.class);- populateUserPool(pool, request);+ populateUserPool(updatedPool, request);- pool.setLastModifiedDate(System.currentTimeMillis() / 1000L);- poolStore.put(id, pool);+ updatedPool.setLastModifiedDate(System.currentTimeMillis() / 1000L);+ poolStore.put(id, updatedPool);LOG.infov("Updated User Pool: {0}", id);- return pool;+ return updatedPool;}public void addCustomAttributes(String userPoolId, List<Map<String, Object>> customAttributes) {@@ -818,17 +819,21 @@ public class CognitoService {}public CognitoUser adminGetUser(String userPoolId, String username) {- Optional<CognitoUser> byKey = userStore.get(userKey(userPoolId, username));- if (byKey.isPresent()) {- return byKey.get();- }- // Fallback: resolve by sub UUID or email alias+ UserPool pool = poolStore.get(userPoolId).orElseThrow(+ () -> new AwsException("ResourceNotFoundException", "User pool not found", 400));+ LinkedHashSet<CognitoUser> matches = new LinkedHashSet<>();+ userStore.get(userKey(userPoolId, username)).ifPresent(matches::add);String prefix = userPoolId + "::";- return userStore.scan(k -> k.startsWith(prefix)).stream()- .filter(u -> username.equals(u.getAttributes().get("sub"))- || username.equals(u.getAttributes().get("email")))- .findFirst()- .orElseThrow(() -> new AwsException("UserNotFoundException", "User not found", 404));+ matches.addAll(userStore.scan(k -> k.startsWith(prefix)).stream()+ .filter(u -> matchesAliasOrUsernameAttribute(pool, u, username)).toList());+ if (matches.isEmpty()) {+ throw new AwsException("UserNotFoundException", "User not found", 400);+ }+ if (matches.size() > 1) {+ throw new AwsException("InvalidParameterException",+ "Multiple users found for the supplied username", 400);+ }+ return matches.getFirst();}public void adminDeleteUser(String userPoolId, String username) {@@ -2389,6 +2394,38 @@ public class CognitoService {};}+ private boolean matchesAliasOrUsernameAttribute(UserPool pool, CognitoUser user,+ String username) {+ if (username.equals(user.getAttributes().get("sub"))) {+ return true;+ }++ for (String attribute : pool.getAliasAttributes()) {+ if (username.equals(user.getAttributes().get(attribute))+ && isActiveAliasAttribute(user, attribute)) {+ return true;+ }+ }++ for (String attribute : pool.getUsernameAttributes()) {+ if (username.equals(user.getAttributes().get(attribute))) {+ return true;+ }+ }++ return false;+ }++ private boolean isActiveAliasAttribute(CognitoUser user, String attribute) {+ return switch (attribute) {+ case "email" -> Boolean+ .parseBoolean(user.getAttributes().getOrDefault("email_verified", "false"));+ case "phone_number" -> Boolean.parseBoolean(+ user.getAttributes().getOrDefault("phone_number_verified", "false"));+ default -> true;+ };+ }+private List<String> accountRecoveryMechanisms(UserPool pool) {Map<String, Object> setting = pool.getAccountRecoverySetting();if (setting == null) {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.javaindex 150f004733..06ea588f43 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@@ -33,18 +33,20 @@ import static org.mockito.Mockito.when;class CognitoServiceTest {private CognitoService service;+ private InMemoryStorage<String, CognitoUser> userStore;private InMemoryStorage<String, CognitoGroup> groupStore;private RegionResolver regionResolver;@BeforeEachvoid setUp() {+ userStore = new InMemoryStorage<>();groupStore = new InMemoryStorage<>();regionResolver = new RegionResolver("us-east-1", "000000000000");service = new CognitoService(new InMemoryStorage<>(),new InMemoryStorage<>(),new InMemoryStorage<>(),- new InMemoryStorage<>(),+ userStore,groupStore,new InMemoryStorage<>(), // revokedTokenStore"http://localhost:4566",@@ -1353,7 +1355,7 @@ class CognitoServiceTest {}// =========================================================================- // Issue #220 — adminGetUser resolves sub UUID and email aliases+ // AdminGetUser resolves configured identifiers// =========================================================================@Test@@ -1370,13 +1372,132 @@ class CognitoServiceTest {@Testvoid adminGetUserByEmailAlias() {- UserPool pool = service.createUserPool(Map.of("PoolName", "TestPool"), "us-east-1");+ UserPool pool = service.createUserPool(+ Map.of("PoolName", "TestPool", "AliasAttributes", List.of("email")),+ "us-east-1"+ );+ service.adminCreateUser(pool.getId(), "bob",+ Map.of("email", "[email protected]", "email_verified", "true"), null);++ CognitoUser found = service.adminGetUser(pool.getId(), "[email protected]");+ assertEquals("bob", found.getUsername());+ }++ @Test+ void adminGetUserByPhoneNumberAlias() {+ UserPool pool = service.createUserPool(+ Map.of("PoolName", "TestPool", "AliasAttributes", List.of("phone_number")),+ "us-east-1"+ );+ service.adminCreateUser(pool.getId(), "bob",+ Map.of("phone_number", "+15551234567", "phone_number_verified", "true"), null);++ CognitoUser found = service.adminGetUser(pool.getId(), "+15551234567");+ assertEquals("bob", found.getUsername());+ }++ @Test+ void adminGetUserByPreferredUsernameAlias() {+ UserPool pool = service.createUserPool(+ Map.of("PoolName", "TestPool", "AliasAttributes", List.of("preferred_username")),+ "us-east-1"+ );+ service.adminCreateUser(pool.getId(), "bob", Map.of("preferred_username", "bobby"), null);++ CognitoUser found = service.adminGetUser(pool.getId(), "bobby");+ assertEquals("bob", found.getUsername());+ }++ @Test+ void adminGetUserByEmailUsernameAttribute() {+ UserPool pool = service.createUserPool(+ Map.of("PoolName", "TestPool", "UsernameAttributes", List.of("email")),+ "us-east-1"+ );service.adminCreateUser(pool.getId(), "bob", Map.of("email", "[email protected]"), null);CognitoUser found = service.adminGetUser(pool.getId(), "[email protected]");assertEquals("bob", found.getUsername());}+ @Test+ void adminGetUserByPhoneNumberUsernameAttribute() {+ UserPool pool = service.createUserPool(+ Map.of("PoolName", "TestPool", "UsernameAttributes", List.of("phone_number")),+ "us-east-1"+ );+ service.adminCreateUser(pool.getId(), "bob", Map.of("phone_number", "+15551234567"), null);++ CognitoUser found = service.adminGetUser(pool.getId(), "+15551234567");+ assertEquals("bob", found.getUsername());+ }++ @Test+ void adminGetUserRejectsEmailWithoutConfiguredAlias() {+ UserPool pool = service.createUserPool(Map.of("PoolName", "TestPool"), "us-east-1");+ service.adminCreateUser(pool.getId(), "bob", Map.of("email", "[email protected]"), null);++ AwsException ex = assertThrows(AwsException.class,+ () -> service.adminGetUser(pool.getId(), "[email protected]"));+ assertEquals("UserNotFoundException", ex.getErrorCode());+ assertEquals(400, ex.getHttpStatus());+ }++ @Test+ void adminGetUserRejectsUnverifiedEmailAlias() {+ UserPool pool = service.createUserPool(+ Map.of("PoolName", "TestPool", "AliasAttributes", List.of("email")),+ "us-east-1"+ );+ service.adminCreateUser(pool.getId(), "bob", Map.of("email", "[email protected]"), null);++ AwsException ex = assertThrows(AwsException.class,+ () -> service.adminGetUser(pool.getId(), "[email protected]"));+ assertEquals("UserNotFoundException", ex.getErrorCode());+ assertEquals(400, ex.getHttpStatus());+ }++ @Test+ void adminGetUserRejectsUnknownPoolWithResourceNotFound() {+ AwsException ex = assertThrows(AwsException.class,+ () -> service.adminGetUser("us-east-1_missing", "bob"));+ assertEquals("ResourceNotFoundException", ex.getErrorCode());+ assertEquals(400, ex.getHttpStatus());+ }++ @Test+ void adminGetUserRejectsAmbiguousLookupValueCreatedViaAdminCreateUser() {+ UserPool pool = service.createUserPool(+ Map.of("PoolName", "TestPool", "AliasAttributes", List.of("email")),+ "us-east-1"+ );+ service.adminCreateUser(pool.getId(), "shared-lookup", Map.of("email", "[email protected]"), null);+ service.adminCreateUser(pool.getId(), "alice",+ Map.of("email", "shared-lookup", "email_verified", "true"), null);++ AwsException ex = assertThrows(AwsException.class,+ () -> service.adminGetUser(pool.getId(), "shared-lookup"));+ assertEquals("InvalidParameterException", ex.getErrorCode());+ assertEquals(400, ex.getHttpStatus());+ }++ @Test+ void adminGetUserRejectsAmbiguousLookupValueCreatedViaAdminUpdateUserAttributes() {+ UserPool pool = service.createUserPool(+ Map.of("PoolName", "TestPool", "AliasAttributes", List.of("email")),+ "us-east-1"+ );+ service.adminCreateUser(pool.getId(), "shared-lookup", Map.of("email", "[email protected]"), null);+ service.adminCreateUser(pool.getId(), "alice", Map.of("email", "[email protected]"), null);+ service.adminUpdateUserAttributes(pool.getId(), "alice",+ Map.of("email", "shared-lookup", "email_verified", "true"));++ AwsException ex = assertThrows(AwsException.class,+ () -> service.adminGetUser(pool.getId(), "shared-lookup"));+ assertEquals("InvalidParameterException", ex.getErrorCode());+ assertEquals(400, ex.getHttpStatus());+ }+// =========================================================================// Issue #233 — listUsers Filter// =========================================================================