feat(cognito): implement AdminRespondToAuthChallenge operation (#666)

Adds the admin variant of RespondToAuthChallenge, enabling server-side
challenge response handling with explicit UserPoolId validation.
Extracts shared challenge logic into processChallenge to avoid duplication.
This commit is contained in:
jsoizo authored and GitHub committed 2026-04-30 20:29:36 -05:00
1 parent 832cdf94db
commit 13203954b3
6 files changed
+157 -1

No files matched your search

@@ -287,7 +287,7 @@ public class AwsQueryController {
private static final Set<String> COGNITO_ACTIONS = Set.of(
"AdminCreateUser", "AdminGetUser", "AdminDeleteUser", "AdminSetUserPassword",
"AdminUpdateUserAttributes", "AdminUserGlobalSignOut", "ListUsers",
"InitiateAuth", "AdminInitiateAuth", "RespondToAuthChallenge",
"InitiateAuth", "AdminInitiateAuth", "RespondToAuthChallenge", "AdminRespondToAuthChallenge",
"SignUp", "ConfirmSignUp", "ChangePassword", "ForgotPassword",
"ConfirmForgotPassword", "GetUser", "UpdateUserAttributes",
"CreateUserPool", "DescribeUserPool", "ListUserPools", "UpdateUserPool", "DeleteUserPool",
@@ -137,7 +137,20 @@ final class CognitoAuthFlowHandler {
Map<String, String> responses, Map<String, String> clientMetadata) {
UserPoolClient client = service.findClientById(clientId);
UserPool pool = service.describeUserPool(client.getUserPoolId());
return processChallenge(pool, client, challengeName, session, responses, clientMetadata);
}
Map<String, Object> adminRespondToAuthChallenge(String userPoolId, String clientId, String challengeName,
String session, Map<String, String> responses,
Map<String, String> clientMetadata) {
UserPoolClient client = service.describeUserPoolClient(userPoolId, clientId);
UserPool pool = service.describeUserPool(userPoolId);
return processChallenge(pool, client, challengeName, session, responses, clientMetadata);
}
private Map<String, Object> processChallenge(UserPool pool, UserPoolClient client, String challengeName,
String session, Map<String, String> responses,
Map<String, String> clientMetadata) {
if ("PASSWORD_VERIFIER".equals(challengeName)) {
return handlePasswordVerifierChallenge(pool, client, session, responses, clientMetadata);
}
@@ -66,6 +66,7 @@ public class CognitoJsonHandler {
case "InitiateAuth" -> handleInitiateAuth(request);
case "AdminInitiateAuth" -> handleAdminInitiateAuth(request);
case "RespondToAuthChallenge" -> handleRespondToAuthChallenge(request);
case "AdminRespondToAuthChallenge" -> handleAdminRespondToAuthChallenge(request);
case "SignUp" -> handleSignUp(request);
case "ConfirmSignUp" -> handleConfirmSignUp(request);
case "ChangePassword" -> handleChangePassword(request);
@@ -407,6 +408,23 @@ public class CognitoJsonHandler {
return Response.ok(objectMapper.valueToTree(result)).build();
}
private Response handleAdminRespondToAuthChallenge(JsonNode request) {
Map<String, String> responses = new HashMap<>();
request.path("ChallengeResponses").fields().forEachRemaining(e -> responses.put(e.getKey(), e.getValue().asText()));
Map<String, String> clientMetadata = new HashMap<>();
request.path("ClientMetadata").fields().forEachRemaining(e -> clientMetadata.put(e.getKey(), e.getValue().asText()));
Map<String, Object> result = service.adminRespondToAuthChallenge(
request.path("UserPoolId").asText(),
request.path("ClientId").asText(),
request.path("ChallengeName").asText(),
request.path("Session").asText(null),
responses,
clientMetadata
);
return Response.ok(objectMapper.valueToTree(result)).build();
}
private Response handleSignUp(JsonNode request) {
Map<String, String> attrs = new HashMap<>();
request.path("UserAttributes").forEach(a -> attrs.put(a.path("Name").asText(), a.path("Value").asText()));
@@ -770,6 +770,19 @@ public class CognitoService {
return authFlowHandler.respondToAuthChallenge(clientId, challengeName, session, responses, clientMetadata);
}
public Map<String, Object> adminRespondToAuthChallenge(String userPoolId, String clientId,
String challengeName, String session,
Map<String, String> responses) {
return authFlowHandler.adminRespondToAuthChallenge(userPoolId, clientId, challengeName, session, responses, Map.of());
}
public Map<String, Object> adminRespondToAuthChallenge(String userPoolId, String clientId,
String challengeName, String session,
Map<String, String> responses,
Map<String, String> clientMetadata) {
return authFlowHandler.adminRespondToAuthChallenge(userPoolId, clientId, challengeName, session, responses, clientMetadata);
}
public void changePassword(String accessToken, String previousPassword, String proposedPassword) {
String username = extractUsernameFromToken(accessToken);
String poolId = extractPoolIdFromToken(accessToken);