Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(cognito): return sub UUID as UserSub in SignUp response (#351)

TiMa067db011728
diff --git a/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoJsonHandler.java
index 12577731c1..fcc4c74afd 100644
--- a/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoJsonHandler.java
+++ b/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoJsonHandler.java
@@ -352,7 +352,7 @@ public class CognitoJsonHandler {
);
ObjectNode response = objectMapper.createObjectNode();
response.put("UserConfirmed", "CONFIRMED".equals(user.getUserStatus()));
- response.put("UserSub", user.getUsername());
+ response.put("UserSub", user.getAttributes().get("sub"));
ObjectNode delivery = response.putObject("CodeDeliveryDetails");
delivery.put("AttributeName", "email");
delivery.put("DeliveryMedium", "EMAIL");
diff --git a/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoJsonHandlerTest.java b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoJsonHandlerTest.java
index c8baec928e..5788026dd5 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoJsonHandlerTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoJsonHandlerTest.java
@@ -36,6 +36,39 @@ class CognitoJsonHandlerTest {
handler = new CognitoJsonHandler(service, mapper);
}
+ @Test
+ void signUpReturnsGeneratedSubAsUserSub() {
+ ObjectNode poolReq = mapper.createObjectNode();
+ poolReq.put("PoolName", "signup-pool");
+ JsonNode poolBody = (JsonNode) handler.handle("CreateUserPool", poolReq, "us-east-1").getEntity();
+ String poolId = poolBody.get("UserPool").get("Id").asText();
+
+ ObjectNode clientReq = mapper.createObjectNode();
+ clientReq.put("UserPoolId", poolId);
+ clientReq.put("ClientName", "signup-client");
+ JsonNode clientBody = (JsonNode) handler.handle("CreateUserPoolClient", clientReq, "us-east-1").getEntity();
+ String clientId = clientBody.get("UserPoolClient").get("ClientId").asText();
+
+ ObjectNode signUpReq = mapper.createObjectNode();
+ signUpReq.put("ClientId", clientId);
+ signUpReq.put("Username", "[email protected]");
+ signUpReq.put("Password", "Password123!");
+ ArrayNode attrs = signUpReq.putArray("UserAttributes");
+ ObjectNode emailAttr = attrs.addObject();
+ emailAttr.put("Name", "email");
+ emailAttr.put("Value", "[email protected]");
+
+ Response response = handler.handle("SignUp", signUpReq, "us-east-1");
+ assertEquals(200, response.getStatus());
+
+ JsonNode body = (JsonNode) response.getEntity();
+ String userSub = body.get("UserSub").asText();
+ assertNotEquals("[email protected]", userSub,
+ "UserSub must be the generated UUID, not the username");
+ assertTrue(userSub.matches("[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"),
+ "UserSub should be a UUID, got: " + userSub);
+ }
+
@Test
void createUserPoolReturnsRichResponse() {
ObjectNode request = mapper.createObjectNode();