From 5ca9fc9b44f1e9fd817cfb3b08eb15c2039c8a3e Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Sat, 26 Apr 2025 17:25:04 -0400 Subject: [PATCH] Add processor to revoke external tokens when OAuthClient is unpublished --- .../OAuthClientUnpublishedProcessor.ts | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 server/queues/processors/OAuthClientUnpublishedProcessor.ts diff --git a/server/queues/processors/OAuthClientUnpublishedProcessor.ts b/server/queues/processors/OAuthClientUnpublishedProcessor.ts new file mode 100644 index 0000000000..7a6a2fd1fd --- /dev/null +++ b/server/queues/processors/OAuthClientUnpublishedProcessor.ts @@ -0,0 +1,36 @@ +import { Op } from "sequelize"; +import { OAuthAuthentication, OAuthClient, User } from "@server/models"; +import { OAuthClientEvent, Event as TEvent } from "@server/types"; +import BaseProcessor from "./BaseProcessor"; + +export default class OAuthClientUnpublishedProcessor extends BaseProcessor { + static applicableEvents: TEvent["name"][] = ["oauthClients.update"]; + + async perform(event: OAuthClientEvent) { + if ( + event.changes?.previous.published === true && + event.changes.attributes.published === false + ) { + const oauthClient = await OAuthClient.findByPk(event.modelId, { + rejectOnEmpty: true, + }); + const users = await User.findAll({ + attributes: ["id"], + where: { + teamId: oauthClient.teamId, + }, + }); + const userIds = users.map((user) => user.id); + + // Revoke access for all users except any that are in the same team + await OAuthAuthentication.destroy({ + where: { + oauthClientId: event.modelId, + userId: { + [Op.notIn]: userIds, + }, + }, + }); + } + } +}