fix(apigateway): map Cognito list claims in REST integrations (#6348)

* fix(apigateway): map Cognito list claims in REST integrations

* fix(apigateway): serialize Cognito groups for mappings
This commit is contained in:
Jimmy Liu Jia authored and GitHub committed 2026-10-23 15:00:56 -04:00
1 parent 38c641ec7e
commit 2cda0110a6
2 files changed
+59 -1

No files matched your search

@@ -10,9 +10,11 @@ import jakarta.ws.rs.core.HttpHeaders;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
/** Verifies Cognito user-pool tokens for REST execute-api methods. */
@ApplicationScoped
@@ -75,7 +77,10 @@ public class CognitoUserPoolAuthorizer {
}
}
String principalId = String.valueOf(verified.claims().getOrDefault("sub", "unknown"));
return new Result(null, principalId, Map.of("claims", verified.claims()));
Map<String, Object> claims = new LinkedHashMap<>(verified.claims());
claims.replaceAll((key, value) -> value instanceof List<?> items
? items.stream().map(String::valueOf).collect(Collectors.joining(",")) : value);
return new Result(null, principalId, Map.of("claims", claims));
} catch (AwsException e) {
return new Result(Failure.UNAUTHORIZED, null, null);
}
@@ -4,6 +4,9 @@ import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpServer;
import io.github.hectorvent.floci.services.cognito.CognitoService;
import io.github.hectorvent.floci.services.cognito.model.UserPool;
import io.github.hectorvent.floci.services.cognito.model.UserPoolClient;
import io.github.hectorvent.floci.services.lambda.LambdaService;
import io.github.hectorvent.floci.services.lambda.model.InvocationType;
import io.github.hectorvent.floci.services.lambda.model.InvokeResult;
@@ -14,6 +17,7 @@ import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import jakarta.inject.Inject;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
@@ -56,6 +60,9 @@ class ApiGatewayHttpContextMappingIntegrationTest {
@InjectMock
LambdaService lambdaService;
@Inject
CognitoService cognitoService;
private String apiId;
private String resourceId;
private String methodPath;
@@ -142,6 +149,52 @@ class ApiGatewayHttpContextMappingIntegrationTest {
}
}
@Test
void mapsCognitoListClaimsIntoHttpProxyHeaders() throws Exception {
UserPool pool = cognitoService.createUserPool(Map.of("PoolName", "groups-mapping"), "us-east-1");
String username = "group-user-" + UUID.randomUUID();
cognitoService.adminCreateUser(pool.getId(), username,
Map.of("email", username + "@example.com"), "TempPass1!");
cognitoService.adminSetUserPassword(pool.getId(), username, "Perm1234!", true);
cognitoService.createGroup(pool.getId(), "admins", "Admin group", 1, null);
cognitoService.createGroup(pool.getId(), "auditors", "Audit group", 2, null);
cognitoService.adminAddUserToGroup(pool.getId(), "admins", username);
cognitoService.adminAddUserToGroup(pool.getId(), "auditors", username);
UserPoolClient client = cognitoService.createUserPoolClient(pool.getId(), "groups-client", false, false,
List.of(), List.of(), null, List.of(), null,
List.of("ALLOW_USER_PASSWORD_AUTH"), null, null, List.of(), null, List.of(), null,
null, null, List.of(), null, null);
Map<String, Object> auth = cognitoService.initiateAuth(client.getClientId(),
"USER_PASSWORD_AUTH", Map.of("USERNAME", username, "PASSWORD", "Perm1234!"));
String token = (String) ((Map<?, ?>) auth.get("AuthenticationResult")).get("AccessToken");
String authorizerId = given().contentType(ContentType.JSON).body(Map.of(
"name", "groups-authorizer", "type", "COGNITO_USER_POOLS",
"providerARNs", List.of(pool.getArn()),
"identitySource", "method.request.header.Authorization"))
.post("/restapis/" + apiId + "/authorizers")
.then().statusCode(201).extract().path("id");
given().contentType(ContentType.JSON).body(Map.of(
"authorizationType", "COGNITO_USER_POOLS", "authorizerId", authorizerId))
.put(methodPath + "GET").then().statusCode(201);
configureIntegration("HTTP_PROXY", "GET", Map.of(
"integration.request.header.X-Auth-Groups", "context.authorizer.claims.cognito:groups",
"integration.request.header.X-Auth-Sub", "context.authorizer.claims.sub",
"integration.request.path.principal", "context.authorizer.claims.sub",
"integration.request.path.proxy", "method.request.path.proxy"));
deploy();
JsonNode response = MAPPER.readTree(given().header("Authorization", "Bearer " + token)
.header("X-Auth-Groups", "forged")
.get("/execute-api/" + apiId + "/test/orders/42")
.then().statusCode(200).extract().asByteArray());
JsonNode groups = response.path("headers").path("x-auth-groups");
assertEquals(1, groups.size());
assertEquals("admins,auditors", groups.get(0).asText());
assertFalse(groups.get(0).asText().contains("forged"));
assertTrue(response.path("headers").has("x-auth-sub"));
}
@ParameterizedTest
@ValueSource(strings = {"HTTP_PROXY", "HTTP"})
void rejectsObjectAuthorizerContextBeforeReachingBackend(String type) throws Exception {