diff --git a/src/main/java/io/github/hectorvent/floci/services/apigateway/CognitoUserPoolAuthorizer.java b/src/main/java/io/github/hectorvent/floci/services/apigateway/CognitoUserPoolAuthorizer.java index e92da59219..cc440d1d5d 100644 --- a/src/main/java/io/github/hectorvent/floci/services/apigateway/CognitoUserPoolAuthorizer.java +++ b/src/main/java/io/github/hectorvent/floci/services/apigateway/CognitoUserPoolAuthorizer.java @@ -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 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); } diff --git a/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayHttpContextMappingIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayHttpContextMappingIntegrationTest.java index 4e10f252d4..ffe0da39dc 100644 --- a/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayHttpContextMappingIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayHttpContextMappingIntegrationTest.java @@ -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 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 {