|
|
@@ -6,56 +6,29 @@ import {
|
|
|
import Logger from 'bunyan';
|
|
|
|
|
|
import { SlackOauthReq } from '~/interfaces/slack-to-growi/slack-oauth-req';
|
|
|
-import { InstallationRepository } from '~/repositories/installation';
|
|
|
import { InstallerService } from '~/services/InstallerService';
|
|
|
import loggerFactory from '~/utils/logger';
|
|
|
|
|
|
-@Middleware()
|
|
|
-export class AuthorizeCommandMiddleware implements IMiddleware {
|
|
|
-
|
|
|
- @Inject()
|
|
|
- installerService: InstallerService;
|
|
|
-
|
|
|
- @Inject()
|
|
|
- installationRepository: InstallationRepository;
|
|
|
-
|
|
|
- private logger: Logger;
|
|
|
|
|
|
- constructor() {
|
|
|
- this.logger = loggerFactory('slackbot-proxy:middlewares:AuthorizeCommandMiddleware');
|
|
|
- }
|
|
|
+const getCommonMiddleware = (query:InstallationQuery<boolean>, installerService:InstallerService, logger:Logger) => {
|
|
|
+ return async(req: SlackOauthReq, res: Res): Promise<void|Res> => {
|
|
|
|
|
|
- async use(@Req() req: SlackOauthReq, @Res() res: Res): Promise<void> {
|
|
|
- const { body } = req;
|
|
|
-
|
|
|
- // extract id from body
|
|
|
- const teamId = body.team_id;
|
|
|
- const enterpriseId = body.enterprise_id;
|
|
|
- const isEnterpriseInstall = body.is_enterprise_install === 'true';
|
|
|
-
|
|
|
- if (teamId == null && enterpriseId == null) {
|
|
|
+ if (query.teamId == null && query.enterpriseId == null) {
|
|
|
res.writeHead(400, 'No installation found');
|
|
|
return res.end();
|
|
|
}
|
|
|
|
|
|
- // create query from body
|
|
|
- const query: InstallationQuery<boolean> = {
|
|
|
- teamId,
|
|
|
- enterpriseId,
|
|
|
- isEnterpriseInstall,
|
|
|
- };
|
|
|
-
|
|
|
let result: AuthorizeResult;
|
|
|
try {
|
|
|
- result = await this.installerService.installer.authorize(query);
|
|
|
+ result = await installerService.installer.authorize(query);
|
|
|
|
|
|
if (result.botToken == null) {
|
|
|
- res.writeHead(403, `The installation for the team(${teamId || enterpriseId}) has no botToken`);
|
|
|
+ res.writeHead(403, `The installation for the team(${query.teamId || query.enterpriseId}) has no botToken`);
|
|
|
return res.end();
|
|
|
}
|
|
|
}
|
|
|
catch (e) {
|
|
|
- this.logger.error(e.message);
|
|
|
+ logger.error(e.message);
|
|
|
|
|
|
res.writeHead(500, e.message);
|
|
|
return res.end();
|
|
|
@@ -63,19 +36,39 @@ export class AuthorizeCommandMiddleware implements IMiddleware {
|
|
|
|
|
|
// set authorized data
|
|
|
req.authorizeResult = result;
|
|
|
- }
|
|
|
-
|
|
|
-}
|
|
|
+ };
|
|
|
+};
|
|
|
+@Middleware()
|
|
|
+export class AuthorizeCommandMiddleware implements IMiddleware {
|
|
|
|
|
|
+ private logger: Logger;
|
|
|
|
|
|
-@Middleware()
|
|
|
-export class AuthorizeInteractionMiddleware implements IMiddleware {
|
|
|
+ constructor() {
|
|
|
+ this.logger = loggerFactory('slackbot-proxy:middlewares:AuthorizeCommandMiddleware');
|
|
|
+ }
|
|
|
|
|
|
@Inject()
|
|
|
installerService: InstallerService;
|
|
|
|
|
|
- @Inject()
|
|
|
- installationRepository: InstallationRepository;
|
|
|
+ async use(@Req() req: SlackOauthReq, @Res() res: Res): Promise<void|Res> {
|
|
|
+ const { body } = req;
|
|
|
+ const teamId = body.team_id;
|
|
|
+ const enterpriseId = body.enterprise_id;
|
|
|
+ const isEnterpriseInstall = body.is_enterprise_install === 'true';
|
|
|
+ const query: InstallationQuery<boolean> = {
|
|
|
+ teamId,
|
|
|
+ enterpriseId,
|
|
|
+ isEnterpriseInstall,
|
|
|
+ };
|
|
|
+
|
|
|
+ const commonMiddleware = getCommonMiddleware(query, this.installerService, this.logger);
|
|
|
+ await commonMiddleware(req, res);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+@Middleware()
|
|
|
+export class AuthorizeInteractionMiddleware implements IMiddleware {
|
|
|
|
|
|
private logger: Logger;
|
|
|
|
|
|
@@ -83,52 +76,61 @@ export class AuthorizeInteractionMiddleware implements IMiddleware {
|
|
|
this.logger = loggerFactory('slackbot-proxy:middlewares:AuthorizeInteractionMiddleware');
|
|
|
}
|
|
|
|
|
|
- async use(@Req() req: SlackOauthReq, @Res() res: Res): Promise<void> {
|
|
|
- const { body } = req;
|
|
|
+ @Inject()
|
|
|
+ installerService: InstallerService;
|
|
|
+
|
|
|
+ async use(@Req() req: SlackOauthReq, @Res() res:Res): Promise<void|Res> {
|
|
|
+ const { body } = req;
|
|
|
+
|
|
|
+ const payload = JSON.parse(body.payload);
|
|
|
+
|
|
|
+ // extract id from body.payload
|
|
|
+ const teamId = payload.team?.id;
|
|
|
+ const enterpriseId = payload.enterprise?.id;
|
|
|
+ const isEnterpriseInstall = payload.is_enterprise_install === 'true';
|
|
|
|
|
|
- if (body.payload == null) {
|
|
|
+ if (body.payload == null) {
|
|
|
// do nothing
|
|
|
- this.logger.info('body does not have payload');
|
|
|
- return;
|
|
|
+ this.logger.info('body does not have payload');
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ const query: InstallationQuery<boolean> = {
|
|
|
+ teamId,
|
|
|
+ enterpriseId,
|
|
|
+ isEnterpriseInstall,
|
|
|
+ };
|
|
|
+
|
|
|
+ const commonMiddleware = getCommonMiddleware(query, this.installerService, this.logger);
|
|
|
+ await commonMiddleware(req, res);
|
|
|
}
|
|
|
|
|
|
- const payload = JSON.parse(body.payload);
|
|
|
+}
|
|
|
+@Middleware()
|
|
|
+export class AuthorizeEventsMiddleware implements IMiddleware {
|
|
|
|
|
|
- // extract id from body
|
|
|
- const teamId = payload.team?.id;
|
|
|
- const enterpriseId = payload.enterprise?.id;
|
|
|
- const isEnterpriseInstall = payload.is_enterprise_install === 'true';
|
|
|
+ private logger: Logger;
|
|
|
|
|
|
- if (teamId == null && enterpriseId == null) {
|
|
|
- res.writeHead(400, 'No installation found');
|
|
|
- return res.end();
|
|
|
- }
|
|
|
+ constructor() {
|
|
|
+ this.logger = loggerFactory('slackbot-proxy:middlewares:AuthorizeEventsMiddleware');
|
|
|
+ }
|
|
|
|
|
|
- // create query from body
|
|
|
+ @Inject()
|
|
|
+ installerService: InstallerService;
|
|
|
+
|
|
|
+ async use(@Req() req: SlackOauthReq, @Res() res: Res): Promise<void|Res> {
|
|
|
+ const { body } = req;
|
|
|
+ const teamId = body.team_id;
|
|
|
+ const enterpriseId = body.enterprise_id;
|
|
|
+ const isEnterpriseInstall = body.is_enterprise_install === 'true';
|
|
|
const query: InstallationQuery<boolean> = {
|
|
|
teamId,
|
|
|
enterpriseId,
|
|
|
isEnterpriseInstall,
|
|
|
};
|
|
|
|
|
|
- let result: AuthorizeResult;
|
|
|
- try {
|
|
|
- result = await this.installerService.installer.authorize(query);
|
|
|
-
|
|
|
- if (result.botToken == null) {
|
|
|
- res.writeHead(403, `The installation for the team(${teamId || enterpriseId}) has no botToken`);
|
|
|
- return res.end();
|
|
|
- }
|
|
|
- }
|
|
|
- catch (e) {
|
|
|
- this.logger.error(e.message);
|
|
|
-
|
|
|
- res.writeHead(500, e.message);
|
|
|
- return res.end();
|
|
|
- }
|
|
|
-
|
|
|
- // set authorized data
|
|
|
- req.authorizeResult = result;
|
|
|
+ const commonMiddleware = getCommonMiddleware(query, this.installerService, this.logger);
|
|
|
+ await commonMiddleware(req, res);
|
|
|
}
|
|
|
|
|
|
}
|