mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(cognito): add group management support (#149)
* feat(cognito): add group management support * fix(cognito): handle null precedence in createGroup request * test(cognito): add tests for JWT escaping special characters in group names and user pool deletion cascading groups * feat(cognito): enhance group listing by sorting and improve group name validation * test(cognito): enhance group deletion test to verify group absence after user pool deletion * feat(cognito): update group and user models to set last modified date on changes * feat(cognito): update deleteGroup method to set last modified date for users when removing from group
This commit is contained in:
1 parent
b762becaea
commit
75bf3c3bdb
7 files changed
+780
-2
No files matched your search
+164
@@ -173,6 +173,170 @@ class CognitoIntegrationTest {
|
||||
assertEquals("RS256", document.path("id_token_signing_alg_values_supported").get(0).asText());
|
||||
}
|
||||
|
||||
// ── Groups ────────────────────────────────────────────────────────
|
||||
|
||||
@Test
|
||||
@Order(10)
|
||||
void createGroup() throws Exception {
|
||||
JsonNode resp = cognitoJson("CreateGroup", """
|
||||
{
|
||||
"UserPoolId": "%s",
|
||||
"GroupName": "admin",
|
||||
"Description": "Admin group",
|
||||
"Precedence": 1
|
||||
}
|
||||
""".formatted(poolId));
|
||||
assertEquals("admin", resp.path("Group").path("GroupName").asText());
|
||||
assertEquals(poolId, resp.path("Group").path("UserPoolId").asText());
|
||||
assertEquals("Admin group", resp.path("Group").path("Description").asText());
|
||||
assertEquals(1, resp.path("Group").path("Precedence").asInt());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(11)
|
||||
void createGroupDuplicate() {
|
||||
cognitoAction("CreateGroup", """
|
||||
{
|
||||
"UserPoolId": "%s",
|
||||
"GroupName": "admin",
|
||||
"Description": "Admin group",
|
||||
"Precedence": 1
|
||||
}
|
||||
""".formatted(poolId))
|
||||
.then()
|
||||
.statusCode(400);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(12)
|
||||
void getGroup() throws Exception {
|
||||
JsonNode resp = cognitoJson("GetGroup", """
|
||||
{
|
||||
"UserPoolId": "%s",
|
||||
"GroupName": "admin"
|
||||
}
|
||||
""".formatted(poolId));
|
||||
assertEquals("admin", resp.path("Group").path("GroupName").asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(13)
|
||||
void listGroups() throws Exception {
|
||||
JsonNode resp = cognitoJson("ListGroups", """
|
||||
{
|
||||
"UserPoolId": "%s"
|
||||
}
|
||||
""".formatted(poolId));
|
||||
assertEquals(1, resp.path("Groups").size());
|
||||
assertEquals("admin", resp.path("Groups").get(0).path("GroupName").asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(14)
|
||||
void adminAddUserToGroup() {
|
||||
cognitoAction("AdminAddUserToGroup", """
|
||||
{
|
||||
"UserPoolId": "%s",
|
||||
"GroupName": "admin",
|
||||
"Username": "%s"
|
||||
}
|
||||
""".formatted(poolId, username))
|
||||
.then()
|
||||
.statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(15)
|
||||
void adminListGroupsForUser() throws Exception {
|
||||
JsonNode resp = cognitoJson("AdminListGroupsForUser", """
|
||||
{
|
||||
"UserPoolId": "%s",
|
||||
"Username": "%s"
|
||||
}
|
||||
""".formatted(poolId, username));
|
||||
assertEquals(1, resp.path("Groups").size());
|
||||
assertEquals("admin", resp.path("Groups").get(0).path("GroupName").asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(16)
|
||||
void authenticateAndVerifyGroupsInToken() throws Exception {
|
||||
Response authResponse = cognitoAction("InitiateAuth", """
|
||||
{
|
||||
"ClientId": "%s",
|
||||
"AuthFlow": "USER_PASSWORD_AUTH",
|
||||
"AuthParameters": {
|
||||
"USERNAME": "%s",
|
||||
"PASSWORD": "%s"
|
||||
}
|
||||
}
|
||||
""".formatted(clientId, username, password));
|
||||
|
||||
authResponse.then().statusCode(200);
|
||||
|
||||
String accessToken = authResponse.jsonPath().getString("AuthenticationResult.AccessToken");
|
||||
JsonNode payload = decodeJwtPayload(accessToken);
|
||||
|
||||
assertTrue(payload.has("cognito:groups"),
|
||||
"JWT payload should contain cognito:groups claim");
|
||||
assertTrue(payload.path("cognito:groups").toString().contains("\"admin\""),
|
||||
"JWT payload should contain admin group");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(17)
|
||||
void adminRemoveUserFromGroup() {
|
||||
cognitoAction("AdminRemoveUserFromGroup", """
|
||||
{
|
||||
"UserPoolId": "%s",
|
||||
"GroupName": "admin",
|
||||
"Username": "%s"
|
||||
}
|
||||
""".formatted(poolId, username))
|
||||
.then()
|
||||
.statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(18)
|
||||
void adminListGroupsForUserEmpty() throws Exception {
|
||||
JsonNode resp = cognitoJson("AdminListGroupsForUser", """
|
||||
{
|
||||
"UserPoolId": "%s",
|
||||
"Username": "%s"
|
||||
}
|
||||
""".formatted(poolId, username));
|
||||
assertEquals(0, resp.path("Groups").size());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(19)
|
||||
void deleteGroup() {
|
||||
cognitoAction("DeleteGroup", """
|
||||
{
|
||||
"UserPoolId": "%s",
|
||||
"GroupName": "admin"
|
||||
}
|
||||
""".formatted(poolId))
|
||||
.then()
|
||||
.statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(20)
|
||||
void getGroupNotFound() {
|
||||
cognitoAction("GetGroup", """
|
||||
{
|
||||
"UserPoolId": "%s",
|
||||
"GroupName": "admin"
|
||||
}
|
||||
""".formatted(poolId))
|
||||
.then()
|
||||
.statusCode(404);
|
||||
}
|
||||
|
||||
// ── Helpers ───────────────────────────────────────────────────────
|
||||
|
||||
private static Response cognitoAction(String action, String body) {
|
||||
return given()
|
||||
.header("X-Amz-Target", "AWSCognitoIdentityProviderService." + action)
|
||||
|
||||
@@ -0,0 +1,282 @@
|
||||
package io.github.hectorvent.floci.services.cognito;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
|
||||
import io.github.hectorvent.floci.services.cognito.model.CognitoGroup;
|
||||
import io.github.hectorvent.floci.services.cognito.model.CognitoUser;
|
||||
import io.github.hectorvent.floci.services.cognito.model.UserPool;
|
||||
import io.github.hectorvent.floci.services.cognito.model.UserPoolClient;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class CognitoServiceTest {
|
||||
|
||||
private CognitoService service;
|
||||
private InMemoryStorage<String, CognitoGroup> groupStore;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
groupStore = new InMemoryStorage<>();
|
||||
service = new CognitoService(
|
||||
new InMemoryStorage<>(),
|
||||
new InMemoryStorage<>(),
|
||||
new InMemoryStorage<>(),
|
||||
new InMemoryStorage<>(),
|
||||
groupStore,
|
||||
"http://localhost:4566"
|
||||
);
|
||||
}
|
||||
|
||||
private UserPool createPoolAndUser() {
|
||||
UserPool pool = service.createUserPool("TestPool", "us-east-1");
|
||||
service.adminCreateUser(pool.getId(), "alice", Map.of("email", "alice@example.com"), "TempPass1!");
|
||||
service.adminSetUserPassword(pool.getId(), "alice", "Perm1234!", true);
|
||||
return pool;
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// Groups
|
||||
// =========================================================================
|
||||
|
||||
@Test
|
||||
void createGroup() {
|
||||
UserPool pool = service.createUserPool("TestPool", "us-east-1");
|
||||
CognitoGroup group = service.createGroup(pool.getId(), "admins", "Admin group", 1, null);
|
||||
|
||||
assertEquals("admins", group.getGroupName());
|
||||
assertEquals(pool.getId(), group.getUserPoolId());
|
||||
assertEquals("Admin group", group.getDescription());
|
||||
assertEquals(1, group.getPrecedence());
|
||||
assertNull(group.getRoleArn());
|
||||
assertTrue(group.getCreationDate() > 0);
|
||||
assertTrue(group.getLastModifiedDate() > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void createGroupDuplicateThrows() {
|
||||
UserPool pool = service.createUserPool("TestPool", "us-east-1");
|
||||
service.createGroup(pool.getId(), "admins", "Admin group", 1, null);
|
||||
|
||||
assertThrows(AwsException.class, () ->
|
||||
service.createGroup(pool.getId(), "admins", "Another desc", 2, null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getGroup() {
|
||||
UserPool pool = service.createUserPool("TestPool", "us-east-1");
|
||||
service.createGroup(pool.getId(), "admins", "Admin group", 1, null);
|
||||
|
||||
CognitoGroup fetched = service.getGroup(pool.getId(), "admins");
|
||||
assertEquals("admins", fetched.getGroupName());
|
||||
assertEquals(pool.getId(), fetched.getUserPoolId());
|
||||
assertEquals("Admin group", fetched.getDescription());
|
||||
assertEquals(1, fetched.getPrecedence());
|
||||
}
|
||||
|
||||
@Test
|
||||
void getGroupNotFoundThrows() {
|
||||
UserPool pool = service.createUserPool("TestPool", "us-east-1");
|
||||
|
||||
assertThrows(AwsException.class, () ->
|
||||
service.getGroup(pool.getId(), "nonexistent"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void listGroups() {
|
||||
UserPool pool = service.createUserPool("TestPool", "us-east-1");
|
||||
service.createGroup(pool.getId(), "admins", "Admin group", 1, null);
|
||||
service.createGroup(pool.getId(), "editors", "Editor group", 2, null);
|
||||
|
||||
List<CognitoGroup> groups = service.listGroups(pool.getId());
|
||||
assertEquals(2, groups.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteGroup() {
|
||||
UserPool pool = service.createUserPool("TestPool", "us-east-1");
|
||||
service.createGroup(pool.getId(), "admins", "Admin group", 1, null);
|
||||
|
||||
service.deleteGroup(pool.getId(), "admins");
|
||||
|
||||
assertThrows(AwsException.class, () ->
|
||||
service.getGroup(pool.getId(), "admins"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteGroupCleansUpUserMembership() {
|
||||
UserPool pool = createPoolAndUser();
|
||||
service.createGroup(pool.getId(), "admins", "Admin group", 1, null);
|
||||
service.adminAddUserToGroup(pool.getId(), "admins", "alice");
|
||||
|
||||
service.deleteGroup(pool.getId(), "admins");
|
||||
|
||||
CognitoUser user = service.adminGetUser(pool.getId(), "alice");
|
||||
assertTrue(user.getGroupNames().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void adminDeleteUserCleansUpGroupMembership() {
|
||||
UserPool pool = createPoolAndUser();
|
||||
service.createGroup(pool.getId(), "admins", "Admin group", 1, null);
|
||||
service.adminAddUserToGroup(pool.getId(), "admins", "alice");
|
||||
|
||||
service.adminDeleteUser(pool.getId(), "alice");
|
||||
|
||||
CognitoGroup group = service.getGroup(pool.getId(), "admins");
|
||||
assertFalse(group.getUserNames().contains("alice"));
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// Group membership
|
||||
// =========================================================================
|
||||
|
||||
@Test
|
||||
void adminAddUserToGroup() {
|
||||
UserPool pool = createPoolAndUser();
|
||||
service.createGroup(pool.getId(), "admins", "Admin group", 1, null);
|
||||
|
||||
service.adminAddUserToGroup(pool.getId(), "admins", "alice");
|
||||
|
||||
CognitoGroup group = service.getGroup(pool.getId(), "admins");
|
||||
assertTrue(group.getUserNames().contains("alice"));
|
||||
|
||||
CognitoUser user = service.adminGetUser(pool.getId(), "alice");
|
||||
assertTrue(user.getGroupNames().contains("admins"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void adminAddUserToGroupIdempotent() {
|
||||
UserPool pool = createPoolAndUser();
|
||||
service.createGroup(pool.getId(), "admins", "Admin group", 1, null);
|
||||
|
||||
service.adminAddUserToGroup(pool.getId(), "admins", "alice");
|
||||
service.adminAddUserToGroup(pool.getId(), "admins", "alice");
|
||||
|
||||
CognitoGroup group = service.getGroup(pool.getId(), "admins");
|
||||
assertEquals(1, group.getUserNames().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void adminRemoveUserFromGroup() {
|
||||
UserPool pool = createPoolAndUser();
|
||||
service.createGroup(pool.getId(), "admins", "Admin group", 1, null);
|
||||
service.adminAddUserToGroup(pool.getId(), "admins", "alice");
|
||||
|
||||
service.adminRemoveUserFromGroup(pool.getId(), "admins", "alice");
|
||||
|
||||
CognitoGroup group = service.getGroup(pool.getId(), "admins");
|
||||
assertFalse(group.getUserNames().contains("alice"));
|
||||
|
||||
CognitoUser user = service.adminGetUser(pool.getId(), "alice");
|
||||
assertFalse(user.getGroupNames().contains("admins"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void adminListGroupsForUser() {
|
||||
UserPool pool = createPoolAndUser();
|
||||
service.createGroup(pool.getId(), "admins", "Admin group", 1, null);
|
||||
service.createGroup(pool.getId(), "editors", "Editor group", 2, null);
|
||||
service.adminAddUserToGroup(pool.getId(), "admins", "alice");
|
||||
service.adminAddUserToGroup(pool.getId(), "editors", "alice");
|
||||
|
||||
List<CognitoGroup> groups = service.adminListGroupsForUser(pool.getId(), "alice");
|
||||
assertEquals(2, groups.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void adminAddUserToGroupNonexistentGroupThrows() {
|
||||
UserPool pool = createPoolAndUser();
|
||||
|
||||
assertThrows(AwsException.class, () ->
|
||||
service.adminAddUserToGroup(pool.getId(), "nonexistent", "alice"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void adminAddUserToGroupNonexistentUserThrows() {
|
||||
UserPool pool = service.createUserPool("TestPool", "us-east-1");
|
||||
service.createGroup(pool.getId(), "admins", "Admin group", 1, null);
|
||||
|
||||
assertThrows(AwsException.class, () ->
|
||||
service.adminAddUserToGroup(pool.getId(), "admins", "nonexistent"));
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// JWT groups claim
|
||||
// =========================================================================
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
void jwtContainsGroupsClaim() {
|
||||
UserPool pool = createPoolAndUser();
|
||||
UserPoolClient client = service.createUserPoolClient(pool.getId(), "test-client", false, false, List.of(), List.of());
|
||||
String clientId = client.getClientId();
|
||||
|
||||
service.createGroup(pool.getId(), "admins", "Admin group", 1, null);
|
||||
service.adminAddUserToGroup(pool.getId(), "admins", "alice");
|
||||
|
||||
Map<String, Object> authResult = service.initiateAuth(
|
||||
clientId, "USER_PASSWORD_AUTH",
|
||||
Map.of("USERNAME", "alice", "PASSWORD", "Perm1234!"));
|
||||
|
||||
Map<String, Object> authenticationResult = (Map<String, Object>) authResult.get("AuthenticationResult");
|
||||
String accessToken = (String) authenticationResult.get("AccessToken");
|
||||
|
||||
// Decode the JWT payload (second segment)
|
||||
String[] parts = accessToken.split("\\.");
|
||||
String payloadJson = new String(
|
||||
Base64.getUrlDecoder().decode(parts[1]), StandardCharsets.UTF_8);
|
||||
|
||||
assertTrue(payloadJson.contains("\"cognito:groups\":[\"admins\"]"),
|
||||
"JWT payload should contain cognito:groups claim with the group name");
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
void jwtEscapesSpecialCharsInGroupName() {
|
||||
UserPool pool = createPoolAndUser();
|
||||
UserPoolClient client = service.createUserPoolClient(pool.getId(), "test-client", false, false, List.of(), List.of());
|
||||
|
||||
String specialGroup = "group\"with\\special\nchars";
|
||||
service.createGroup(pool.getId(), specialGroup, null, null, null);
|
||||
service.adminAddUserToGroup(pool.getId(), specialGroup, "alice");
|
||||
|
||||
Map<String, Object> authResult = service.initiateAuth(
|
||||
client.getClientId(), "USER_PASSWORD_AUTH",
|
||||
Map.of("USERNAME", "alice", "PASSWORD", "Perm1234!"));
|
||||
|
||||
Map<String, Object> auth = (Map<String, Object>) authResult.get("AuthenticationResult");
|
||||
String token = (String) auth.get("AccessToken");
|
||||
String payloadJson = new String(
|
||||
Base64.getUrlDecoder().decode(token.split("\\.")[1]), StandardCharsets.UTF_8);
|
||||
|
||||
assertTrue(payloadJson.contains("cognito:groups"),
|
||||
"JWT should contain cognito:groups claim");
|
||||
assertTrue(payloadJson.contains("group\\\"with\\\\special\\nchars"),
|
||||
"Group name should be properly JSON-escaped in JWT payload");
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// deleteUserPool cascades groups
|
||||
// =========================================================================
|
||||
|
||||
@Test
|
||||
void deleteUserPoolCascadesGroups() {
|
||||
UserPool pool = service.createUserPool("TestPool", "us-east-1");
|
||||
service.createGroup(pool.getId(), "admins", "Admin group", 1, null);
|
||||
service.createGroup(pool.getId(), "editors", "Editor group", 2, null);
|
||||
|
||||
String prefix = pool.getId() + "::";
|
||||
assertEquals(2, groupStore.scan(k -> k.startsWith(prefix)).size());
|
||||
|
||||
service.deleteUserPool(pool.getId());
|
||||
|
||||
assertEquals(0, groupStore.scan(k -> k.startsWith(prefix)).size());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user