fix(cognito): pass the groups' IAM roles to the pre token generation trigger (#8134)

The pre token generation event always carried
groupConfiguration.iamRolesToOverride = [] and preferredRole = null, so a
trigger that reads the role Cognito computed for the user (to copy it into
the access token, for instance) got nothing. AWS fills both from the
user's groups: every group role, and the role of the lowest-precedence
group that has one, none when groups tied at that rank name different
roles.

Build groupConfiguration after checking that the pool has a pre token
generation trigger, read the pool's groups once instead of once per
membership, sort the test imports, and pin that a group without a role
does not outrank one with a role.

Closes #8222
This commit is contained in:
Giovanni Brethenoux authored and GitHub committed 2026-11-13 16:34:20 -04:00
1 parent f1a6260a94
commit 63b8da91f5
2 files changed
+94 -5

No files matched your search

@@ -9,6 +9,7 @@ import io.floci.aws.core.common.AwsException;
import io.floci.aws.core.common.RegionResolver;
import io.floci.aws.core.storage.InMemoryStorage;
import io.floci.aws.services.acm.AcmService;
import io.floci.aws.services.cognito.model.CognitoGroup;
import io.floci.aws.services.cognito.model.CognitoUser;
import io.floci.aws.services.cognito.model.ResourceServerScope;
import io.floci.aws.services.cognito.model.UserPool;
@@ -1356,6 +1357,61 @@ class CognitoLambdaTriggersTest {
assertNotNull(req.get("userAttributes"));
}
@Test
@SuppressWarnings("unchecked")
void preTokenGenerationGroupConfigurationCarriesTheGroupsRoles() throws Exception {
UserPool pool = createPoolWithLambdaConfig(Map.of("PreTokenGeneration", "arn:aws:lambda:::pre-token"));
seedUser(pool, "alice", "Perm1234!");
UserPoolClient client = createClient(pool);
String admin = "arn:aws:iam::000000000000:role/admin";
String reader = "arn:aws:iam::000000000000:role/reader";
service.createGroup(pool.getId(), "admins", null, 1, admin);
service.createGroup(pool.getId(), "readers", null, 5, reader);
service.createGroup(pool.getId(), "no-role", null, 0, null);
service.adminAddUserToGroup(pool.getId(), "admins", "alice");
service.adminAddUserToGroup(pool.getId(), "readers", "alice");
service.adminAddUserToGroup(pool.getId(), "no-role", "alice");
ArgumentCaptor<byte[]> payloadCap = ArgumentCaptor.forClass(byte[].class);
when(lambdaService.invoke(anyString(), eq("arn:aws:lambda:::pre-token"), payloadCap.capture(), any()))
.thenReturn(ok(Map.of()));
service.initiateAuth(client.getClientId(), "USER_PASSWORD_AUTH",
Map.of("USERNAME", "alice", "PASSWORD", "Perm1234!"));
Map<String, Object> event = MAPPER.readValue(payloadCap.getValue(), new TypeReference<>() {});
Map<String, Object> groups = (Map<String, Object>) ((Map<String, Object>) event.get("request"))
.get("groupConfiguration");
assertEquals(Set.of("admins", "readers", "no-role"), Set.copyOf((List<String>) groups.get("groupsToOverride")));
assertEquals(Set.of(admin, reader), Set.copyOf((List<String>) groups.get("iamRolesToOverride")));
assertEquals(admin, groups.get("preferredRole"),
"the role of the lowest-precedence group that has one, as on AWS");
}
@Test
void preferredRoleFollowsAwsPrecedenceRules() {
String a = "arn:aws:iam::000000000000:role/a";
String b = "arn:aws:iam::000000000000:role/b";
assertEquals(a, CognitoAuthFlowHandler.preferredRole(List.of(group(1, a), group(2, b))));
assertEquals(a, CognitoAuthFlowHandler.preferredRole(List.of(group(null, b), group(3, a))),
"a group without precedence ranks after every group with one");
assertEquals(a, CognitoAuthFlowHandler.preferredRole(List.of(group(1, a), group(1, a))),
"a tie on the same role keeps that role");
assertNull(CognitoAuthFlowHandler.preferredRole(List.of(group(1, a), group(1, b))),
"a tie between different roles sets none");
assertEquals(b, CognitoAuthFlowHandler.preferredRole(List.of(group(null, b))));
assertNull(CognitoAuthFlowHandler.preferredRole(List.of(group(0, null))));
assertEquals(b, CognitoAuthFlowHandler.preferredRole(List.of(group(0, null), group(5, b))),
"a group without a role does not outrank one with a role (CreateGroup: the lowest "
+ "precedence group whose role ARN is given)");
}
private static CognitoGroup group(Integer precedence, String roleArn) {
CognitoGroup group = new CognitoGroup();
group.setPrecedence(precedence);
group.setRoleArn(roleArn);
return group;
}
// =========================================================================
// PreTokenGeneration for client credentials (M2M)
// =========================================================================