fix(cognito): auto-generate sub, fix JWT sub claim, add AdminUserGlobalSignOut (#68) (#183)

This commit is contained in:
Hector Ventura authored and GitHub committed 2026-04-03 18:20:50 -05:00
1 parent 86b50d46d9
commit 9d6181c547
4 files changed
+169 -2

No files matched your search

@@ -2,6 +2,7 @@ package io.github.hectorvent.floci.core.common;
import io.github.hectorvent.floci.services.cloudformation.CloudFormationQueryHandler;
import io.github.hectorvent.floci.services.cloudwatch.metrics.CloudWatchMetricsQueryHandler;
import io.github.hectorvent.floci.services.cognito.CognitoJsonHandler;
import io.github.hectorvent.floci.services.elasticache.ElastiCacheQueryHandler;
import io.github.hectorvent.floci.services.iam.IamQueryHandler;
import io.github.hectorvent.floci.services.iam.StsQueryHandler;
@@ -109,6 +110,7 @@ public class AwsQueryController {
private final IamQueryHandler iamQueryHandler;
private final StsQueryHandler stsQueryHandler;
private final CloudWatchMetricsQueryHandler cloudWatchMetricsQueryHandler;
private final CognitoJsonHandler cognitoJsonHandler;
private final RegionResolver regionResolver;
@Inject
@@ -119,6 +121,7 @@ public class AwsQueryController {
SesQueryHandler sesQueryHandler,
IamQueryHandler iamQueryHandler, StsQueryHandler stsQueryHandler,
CloudWatchMetricsQueryHandler cloudWatchMetricsQueryHandler,
CognitoJsonHandler cognitoJsonHandler,
RegionResolver regionResolver) {
this.cloudFormationQueryHandler = cloudFormationQueryHandler;
this.elastiCacheQueryHandler = elastiCacheQueryHandler;
@@ -129,6 +132,7 @@ public class AwsQueryController {
this.iamQueryHandler = iamQueryHandler;
this.stsQueryHandler = stsQueryHandler;
this.cloudWatchMetricsQueryHandler = cloudWatchMetricsQueryHandler;
this.cognitoJsonHandler = cognitoJsonHandler;
this.regionResolver = regionResolver;
}
@@ -160,11 +164,29 @@ public class AwsQueryController {
case "email" -> sesQueryHandler.handle(action, formParams, region);
case "monitoring" -> cloudWatchMetricsQueryHandler.handle(action, formParams, region);
case "cloudformation" -> cloudFormationQueryHandler.handle(action, formParams, region);
case "cognito-idp" -> handleCognitoQuery(action, formParams, region);
default -> xmlErrorResponse("UnknownService",
"Unknown or unsupported service: " + service, 400);
};
}
private Response handleCognitoQuery(String action, MultivaluedMap<String, String> formParams, String region) {
// Cognito is primarily JSON 1.1, but we provide a bridge for Query protocol if hit.
// Convert MultivaluedMap to JsonNode if needed, but for now just return UnsupportedOperation
// with Cognito namespace.
String xml = new XmlBuilder()
.start("ErrorResponse")
.start("Error")
.elem("Type", "Sender")
.elem("Code", "UnsupportedOperation")
.elem("Message", "Operation " + action + " is not supported by Cognito via Query protocol.")
.end("Error")
.elem("RequestId", UUID.randomUUID().toString())
.end("ErrorResponse")
.build();
return Response.status(400).entity(xml).type(MediaType.APPLICATION_XML).build();
}
/**
* Determines the target AWS service. Prefers the service name embedded in the
* Authorization header credential scope. Falls back to action-name lookup when
@@ -205,7 +227,19 @@ public class AwsQueryController {
"GetIdentityDkimAttributes"
);
private static final Set<String> QUERY_PROTOCOL_SERVICES = Set.of("sqs", "sns", "iam", "sts", "elasticache", "rds", "monitoring", "cloudformation", "email");
private static final Set<String> COGNITO_ACTIONS = Set.of(
"AdminCreateUser", "AdminGetUser", "AdminDeleteUser", "AdminSetUserPassword",
"AdminUpdateUserAttributes", "AdminUserGlobalSignOut", "ListUsers",
"InitiateAuth", "AdminInitiateAuth", "RespondToAuthChallenge",
"SignUp", "ConfirmSignUp", "ChangePassword", "ForgotPassword",
"ConfirmForgotPassword", "GetUser", "UpdateUserAttributes",
"CreateUserPool", "DescribeUserPool", "ListUserPools", "DeleteUserPool",
"CreateUserPoolClient", "DescribeUserPoolClient", "ListUserPoolClients", "DeleteUserPoolClient",
"CreateGroup", "GetGroup", "ListGroups", "DeleteGroup",
"AdminAddUserToGroup", "AdminRemoveUserFromGroup", "AdminListGroupsForUser"
);
private static final Set<String> QUERY_PROTOCOL_SERVICES = Set.of("sqs", "sns", "iam", "sts", "elasticache", "rds", "monitoring", "cloudformation", "email", "cognito-idp");
private String resolveService(String authorization, String action) {
if (authorization != null && !authorization.isEmpty()) {
@@ -245,6 +279,9 @@ public class AwsQueryController {
if (SES_ACTIONS.contains(action)) {
return "email";
}
if (COGNITO_ACTIONS.contains(action)) {
return "cognito-idp";
}
// SQS actions are numerous and not enumerated — fall back to sqs only for
// requests that arrived without an Authorization header (raw/test clients)
return "sqs";
@@ -51,6 +51,7 @@ public class CognitoJsonHandler {
case "AdminDeleteUser" -> handleAdminDeleteUser(request);
case "AdminSetUserPassword" -> handleAdminSetUserPassword(request);
case "AdminUpdateUserAttributes" -> handleAdminUpdateUserAttributes(request);
case "AdminUserGlobalSignOut" -> handleAdminUserGlobalSignOut(request);
case "ListUsers" -> handleListUsers(request);
case "InitiateAuth" -> handleInitiateAuth(request);
case "AdminInitiateAuth" -> handleAdminInitiateAuth(request);
@@ -256,6 +257,14 @@ public class CognitoJsonHandler {
return Response.ok(objectMapper.createObjectNode()).build();
}
private Response handleAdminUserGlobalSignOut(JsonNode request) {
service.adminUserGlobalSignOut(
request.path("UserPoolId").asText(),
request.path("Username").asText()
);
return Response.ok(objectMapper.createObjectNode()).build();
}
private Response handleListUsers(JsonNode request) {
List<CognitoUser> users = service.listUsers(request.path("UserPoolId").asText());
ObjectNode response = objectMapper.createObjectNode();
@@ -225,6 +225,11 @@ public class CognitoService {
user.getAttributes().putAll(attributes);
}
// Ensure sub attribute is present
if (!user.getAttributes().containsKey("sub")) {
user.getAttributes().put("sub", UUID.randomUUID().toString());
}
if (temporaryPassword != null && !temporaryPassword.isEmpty()) {
user.setPasswordHash(hashPassword(temporaryPassword));
user.setTemporaryPassword(true);
@@ -236,6 +241,11 @@ public class CognitoService {
return user;
}
public void adminUserGlobalSignOut(String userPoolId, String username) {
adminGetUser(userPoolId, username);
LOG.infov("AdminUserGlobalSignOut stub: user {0} in pool {1} signed out globally", username, userPoolId);
}
public CognitoUser adminGetUser(String userPoolId, String username) {
return userStore.get(userKey(userPoolId, username))
.orElseThrow(() -> new AwsException("UserNotFoundException", "User not found", 404));
@@ -386,6 +396,11 @@ public class CognitoService {
user.getAttributes().putAll(attributes);
}
// Ensure sub attribute is present
if (!user.getAttributes().containsKey("sub")) {
user.getAttributes().put("sub", UUID.randomUUID().toString());
}
userStore.put(key, user);
LOG.infov("Signed up user {0} in pool {1}", username, userPoolId);
return user;
@@ -639,11 +654,12 @@ public class CognitoService {
.collect(Collectors.joining(",", "[", "]"));
groupsFragment = ",\"cognito:groups\":" + groupsJson;
}
String sub = user.getAttributes().getOrDefault("sub", user.getUsername());
String payloadJson = String.format(
"{\"sub\":\"%s\",\"event_id\":\"%s\",\"token_use\":\"%s\",\"auth_time\":%d," +
"\"iss\":\"%s\",\"exp\":%d,\"iat\":%d," +
"\"username\":\"%s\",\"email\":\"%s\",\"cognito:username\":\"%s\"%s}",
UUID.randomUUID(), UUID.randomUUID(), type, now,
escapeJson(sub), UUID.randomUUID(), type, now,
escapeJson(getIssuer(pool.getId())), now + 3600, now,
user.getUsername(), email, user.getUsername(), groupsFragment
);