mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
feat(cognito): implement DeleteUser self-service API action (#4493)
This commit is contained in:
1 parent
6a3bc12769
commit
8ea033d9fd
8 files changed
+414
-8
No files matched your search
@@ -111,6 +111,7 @@ public class CognitoJsonHandler {
|
||||
case "UpdateUserAttributes" -> handleUpdateUserAttributes(request);
|
||||
case "DeleteUserAttributes" -> handleDeleteUserAttributes(request);
|
||||
case "GlobalSignOut" -> handleGlobalSignOut(request);
|
||||
case "DeleteUser" -> handleDeleteUser(request);
|
||||
case "CreateGroup" -> handleCreateGroup(request);
|
||||
case "GetGroup" -> handleGetGroup(request);
|
||||
case "ListGroups" -> handleListGroups(request);
|
||||
@@ -1069,7 +1070,12 @@ public class CognitoJsonHandler {
|
||||
}
|
||||
|
||||
private Response handleGlobalSignOut(JsonNode request) {
|
||||
service.globalSignOut(request.path("AccessToken").asText());
|
||||
service.globalSignOut(request.path("AccessToken").asText(null));
|
||||
return Response.ok(objectMapper.createObjectNode()).build();
|
||||
}
|
||||
|
||||
private Response handleDeleteUser(JsonNode request) {
|
||||
service.deleteUser(request.path("AccessToken").asText(null));
|
||||
return Response.ok(objectMapper.createObjectNode()).build();
|
||||
}
|
||||
|
||||
|
||||
@@ -2146,6 +2146,34 @@ public class CognitoService implements ResourceProvider {
|
||||
LOG.infov("GlobalSignOut: revoked all tokens for user {0} in pool {1}", user.getUsername(), poolId);
|
||||
}
|
||||
|
||||
/**
|
||||
* DeleteUser: the self-service counterpart to AdminDeleteUser, authenticated with the caller's
|
||||
* access token instead of admin credentials. Deletes the user profile and removes them from all
|
||||
* groups, matching AWS behavior.
|
||||
*/
|
||||
public void deleteUser(String accessToken) {
|
||||
if (accessToken == null || accessToken.isEmpty()) {
|
||||
throw new AwsException("InvalidParameterException",
|
||||
"1 validation error detected: Value at 'accessToken' failed to satisfy constraint: Member must not be null", 400);
|
||||
}
|
||||
|
||||
VerifiedAccessToken token = verifyAccessToken(accessToken);
|
||||
requireScope(accessToken, USER_ADMIN_SCOPE);
|
||||
String username = token.username();
|
||||
String poolId = token.poolId();
|
||||
try {
|
||||
adminDeleteUser(poolId, username);
|
||||
} catch (AwsException e) {
|
||||
if ("UserNotFoundException".equals(e.getErrorCode())
|
||||
|| "ResourceNotFoundException".equals(e.getErrorCode())) {
|
||||
throw new AwsException("NotAuthorizedException", INVALID_ACCESS_TOKEN_MESSAGE, 400);
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
|
||||
LOG.infov("DeleteUser: deleted user {0} in pool {1}", username, poolId);
|
||||
}
|
||||
|
||||
public CognitoUser adminGetUser(String userPoolId, String username) {
|
||||
UserPool pool = poolStore.get(userPoolId).orElseThrow(
|
||||
() -> userPoolNotFound(userPoolId));
|
||||
@@ -3887,8 +3915,18 @@ public class CognitoService implements ResourceProvider {
|
||||
boolean isAccess = "access".equals(tokenType);
|
||||
List<String> suppress = isAccess ? override.accessClaimsToSuppress() : override.idClaimsToSuppress();
|
||||
Map<String, Object> addOrOverride = isAccess ? override.accessClaimsToAddOrOverride() : override.idClaimsToAddOrOverride();
|
||||
if (suppress != null) suppress.forEach(claims::remove);
|
||||
if (addOrOverride != null) claims.putAll(addOrOverride);
|
||||
if (suppress != null) {
|
||||
suppress.stream()
|
||||
.filter(claim -> !isTriggerProtectedIdentityClaim(claim, isAccess))
|
||||
.forEach(claims::remove);
|
||||
}
|
||||
if (addOrOverride != null) {
|
||||
addOrOverride.forEach((claim, value) -> {
|
||||
if (!isTriggerProtectedIdentityClaim(claim, isAccess)) {
|
||||
claims.put(claim, value);
|
||||
}
|
||||
});
|
||||
}
|
||||
if (override.groupsToOverride() != null) {
|
||||
claims.put("cognito:groups", override.groupsToOverride());
|
||||
}
|
||||
@@ -3917,6 +3955,15 @@ public class CognitoService implements ResourceProvider {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* AWS lets no pre token generation trigger add, modify or suppress the claims that identify
|
||||
* the user: {@code sub} in either token, {@code username} in the access token and
|
||||
* {@code cognito:username} in the ID token.
|
||||
*/
|
||||
private static boolean isTriggerProtectedIdentityClaim(String claim, boolean isAccess) {
|
||||
return "sub".equals(claim) || (isAccess ? "username" : "cognito:username").equals(claim);
|
||||
}
|
||||
|
||||
private String encodeJwtHeader(UserPool pool) {
|
||||
String headerJson = String.format(
|
||||
"{\"alg\":\"RS256\",\"typ\":\"JWT\",\"kid\":\"%s\"}",
|
||||
@@ -4652,6 +4699,10 @@ public class CognitoService implements ResourceProvider {
|
||||
validateUserNotGloballySignedOut(username, poolId, tokenUse,
|
||||
requiredNumericClaim(claims, "iat"));
|
||||
}
|
||||
boolean clientCredentialsToken = "access".equals(tokenUse) && subject.equals(clientId);
|
||||
if (!clientCredentialsToken) {
|
||||
requireTokenUserExists(poolId, username, subject);
|
||||
}
|
||||
Map<String, Object> mapped = MAPPER.convertValue(claims, new TypeReference<Map<String, Object>>() {});
|
||||
return new VerifiedApiGatewayToken(poolId, tokenUse, Map.copyOf(mapped));
|
||||
} catch (AwsException e) {
|
||||
@@ -4662,6 +4713,19 @@ public class CognitoService implements ResourceProvider {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A deleted user's tokens keep a valid signature until they expire, so the authorizer also
|
||||
* requires the user to still be in the pool. Every user token names its user, since no pre
|
||||
* token generation trigger can remove the username claim, and matching {@code sub} keeps a user
|
||||
* re-created under the same name from inheriting the deleted user's tokens.
|
||||
*/
|
||||
private void requireTokenUserExists(String poolId, String username, String subject) {
|
||||
CognitoUser user = username == null ? null : userStore.get(userKey(poolId, username)).orElse(null);
|
||||
if (user == null || !subject.equals(user.getAttributes().getOrDefault("sub", user.getUsername()))) {
|
||||
throw new AwsException("NotAuthorizedException", INVALID_ACCESS_TOKEN_MESSAGE, 400);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies the Cognito access-token contract before any self-service operation uses its claims.
|
||||
* The pool's persisted public key is the trust anchor; claims are never trusted before the
|
||||
|
||||
Reference in new issue
Block a user