mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
feat: Implement Cognito GlobalSignOut (#1701)
* feat: Implement Cognito GlobalSignOut * fix(cognito): validate user existence and jti in GlobalSignOut Address PR #1701 review: - Add adminGetUser existence check before revoking tokens so a crafted token referencing a nonexistent pool/user returns UserNotFoundException/ ResourceNotFoundException instead of silently writing revocation records and returning 200, matching AdminUserGlobalSignOut and getUser. - Include jti in the invalid-token guard so legacy tokens without a jti claim cannot be used as a sign-out credential, consistent with getUser. - Cover the nonexistent-user path with an integration test. * fix(cognito): error reporting * fix: map ResourceNotFoundException -> NotAuthorizedException
This commit is contained in:
1 parent
742bb8cf03
commit
001e530aca
3 files changed
+311
-2
No files matched your search
@@ -81,6 +81,7 @@ public class CognitoJsonHandler {
|
||||
case "GetUser" -> handleGetUser(request);
|
||||
case "UpdateUserAttributes" -> handleUpdateUserAttributes(request);
|
||||
case "DeleteUserAttributes" -> handleDeleteUserAttributes(request);
|
||||
case "GlobalSignOut" -> handleGlobalSignOut(request);
|
||||
case "CreateGroup" -> handleCreateGroup(request);
|
||||
case "GetGroup" -> handleGetGroup(request);
|
||||
case "ListGroups" -> handleListGroups(request);
|
||||
@@ -609,6 +610,11 @@ public class CognitoJsonHandler {
|
||||
return Response.ok(objectMapper.createObjectNode()).build();
|
||||
}
|
||||
|
||||
private Response handleGlobalSignOut(JsonNode request) {
|
||||
service.globalSignOut(request.path("AccessToken").asText());
|
||||
return Response.ok(objectMapper.createObjectNode()).build();
|
||||
}
|
||||
|
||||
private ObjectNode userPoolToDescriptionNode(UserPool p) {
|
||||
ObjectNode node = objectMapper.createObjectNode();
|
||||
node.put("Id", p.getId());
|
||||
|
||||
@@ -847,13 +847,56 @@ public class CognitoService {
|
||||
public void adminUserGlobalSignOut(String userPoolId, String username) {
|
||||
// Validate user exists
|
||||
CognitoUser user = adminGetUser(userPoolId, username);
|
||||
|
||||
|
||||
// Revoke all tokens for this user
|
||||
revokeAllUserTokens(userPoolId, username);
|
||||
|
||||
|
||||
LOG.infov("AdminUserGlobalSignOut: revoked all tokens for user {0} in pool {1}", username, userPoolId);
|
||||
}
|
||||
|
||||
/**
|
||||
* GlobalSignOut — the self-service counterpart to AdminUserGlobalSignOut, authenticated
|
||||
* with the caller's access token instead of admin credentials. Invalidates the access,
|
||||
* ID, and refresh tokens Cognito issued to the user, matching AWS behavior.
|
||||
*/
|
||||
public void globalSignOut(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);
|
||||
}
|
||||
|
||||
String username = extractUsernameFromToken(accessToken);
|
||||
String poolId = extractPoolIdFromToken(accessToken);
|
||||
String jti = extractJtiFromToken(accessToken);
|
||||
|
||||
if (username == null || poolId == null || jti == null) {
|
||||
throw new AwsException("NotAuthorizedException", "Invalid access token", 400);
|
||||
}
|
||||
|
||||
// A token that was already revoked (or issued before an earlier sign-out) cannot
|
||||
// authorize a fresh sign-out.
|
||||
validateTokenNotRevoked(jti, poolId, "access");
|
||||
Long iat = extractIatFromToken(accessToken);
|
||||
validateUserNotGloballySignedOut(username, poolId, "access", iat != null ? iat : 0L);
|
||||
|
||||
// Confirm the pool and user actually exist before mutating the revocation store,
|
||||
// matching the implicit validation AWS performs and the adminUserGlobalSignOut counterpart.
|
||||
try {
|
||||
adminGetUser(poolId, username);
|
||||
} catch (AwsException e) {
|
||||
if ("UserNotFoundException".equals(e.getErrorCode())
|
||||
|| "ResourceNotFoundException".equals(e.getErrorCode())) {
|
||||
throw new AwsException("NotAuthorizedException", "Invalid access token", 400);
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
|
||||
// Revoke all of the user's issued tokens (access, ID, refresh).
|
||||
revokeAllUserTokens(poolId, username);
|
||||
|
||||
LOG.infov("GlobalSignOut: revoked all tokens for user {0} in pool {1}", username, poolId);
|
||||
}
|
||||
|
||||
public CognitoUser adminGetUser(String userPoolId, String username) {
|
||||
UserPool pool = poolStore.get(userPoolId).orElseThrow(
|
||||
() -> new AwsException("ResourceNotFoundException", "User pool not found", 400));
|
||||
|
||||
Reference in new issue
Block a user