Skip to content

[DRAFT] [WIP] Add in builtin listener for token revoked and app uninstalled events #2337

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions src/listener/builtin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { InstallationStore, InstallationQuery } from "@slack/oauth";
import { ConsoleLogger, Logger, LogLevel } from '@slack/logger';
import { Context } from "../types";

export class TokenRevocationListeners {
private installationStore: InstallationStore;
private logger: Logger;

public constructor(installationStore: InstallationStore, logger: Logger, logLevel = LogLevel.INFO,) {
this.installationStore = installationStore;
this.logger =
logger ??
(() => {
const defaultLogger = new ConsoleLogger();
defaultLogger.setLevel(logLevel);
return defaultLogger;
})();
}

public handleTokensRevokedEvents(context: Context) {
const isEnterpriseInstall = context.isEnterpriseInstall;

const installQuery: InstallationQuery<typeof isEnterpriseInstall> = {
isEnterpriseInstall: isEnterpriseInstall,
teamId: context.teamId,
enterpriseId: context.enterpriseId,
userId: context.userId,
};

if (this.installationStore.deleteInstallation) {
this.installationStore.deleteInstallation(installQuery, this.logger);
/**
* deleteInstallation with enterprise/team_id + user_id -> delete only user tokens
deleteBot with enterprise/team_id -> delete only bot tokens
deleteAll with enterprise/team_id -> delete both bot and user tokens
*/
} else {
throw new Error(
'Custom InstallationStore must have deleteInstallation method implemented',
);
}
}

public handleAppUninstalledEvents(context: Context) {
const isEnterpriseInstall = context.isEnterpriseInstall;

const installQuery: InstallationQuery<typeof isEnterpriseInstall> = {
isEnterpriseInstall: isEnterpriseInstall,
teamId: context.teamId,
enterpriseId: context.enterpriseId,
userId: context.userId,
};

if (this.installationStore.deleteInstallation) {
this.installationStore.deleteInstallation(installQuery, this.logger);
/**
* deleteInstallation with enterprise/team_id + user_id -> delete only user tokens
deleteBot with enterprise/team_id -> delete only bot tokens
deleteAll with enterprise/team_id -> delete both bot and user tokens
*/
} else {
throw new Error(
'Custom InstallationStore must have deleteInstallation method implemented',
);
}
}
}