|
@@ -1,21 +1,25 @@
|
|
|
import { AuthorizeResult, InstallationQuery } from '@slack/oauth';
|
|
import { AuthorizeResult, InstallationQuery } from '@slack/oauth';
|
|
|
import {
|
|
import {
|
|
|
- IMiddleware, Inject, Middleware, Req, Res,
|
|
|
|
|
|
|
+ IMiddleware, Inject, Middleware, Next, Req, Res,
|
|
|
} from '@tsed/common';
|
|
} from '@tsed/common';
|
|
|
|
|
|
|
|
import Logger from 'bunyan';
|
|
import Logger from 'bunyan';
|
|
|
|
|
|
|
|
|
|
+import createError from 'http-errors';
|
|
|
|
|
+
|
|
|
import { SlackOauthReq } from '~/interfaces/slack-to-growi/slack-oauth-req';
|
|
import { SlackOauthReq } from '~/interfaces/slack-to-growi/slack-oauth-req';
|
|
|
import { InstallerService } from '~/services/InstallerService';
|
|
import { InstallerService } from '~/services/InstallerService';
|
|
|
import loggerFactory from '~/utils/logger';
|
|
import loggerFactory from '~/utils/logger';
|
|
|
|
|
|
|
|
|
|
+// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
|
|
|
+const logger = loggerFactory('@growi/slackbot-proxy:middlewares:authorizer');
|
|
|
|
|
+
|
|
|
|
|
|
|
|
const getCommonMiddleware = (query:InstallationQuery<boolean>, installerService:InstallerService, logger:Logger) => {
|
|
const getCommonMiddleware = (query:InstallationQuery<boolean>, installerService:InstallerService, logger:Logger) => {
|
|
|
- return async(req: SlackOauthReq, res: Res): Promise<void|Res> => {
|
|
|
|
|
|
|
+ return async(req: SlackOauthReq, res: Res, next: Next): Promise<void|Res> => {
|
|
|
|
|
|
|
|
if (query.teamId == null && query.enterpriseId == null) {
|
|
if (query.teamId == null && query.enterpriseId == null) {
|
|
|
- res.writeHead(400, 'No installation found');
|
|
|
|
|
- return res.end();
|
|
|
|
|
|
|
+ return next(createError(400, 'No installation found'));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
let result: AuthorizeResult;
|
|
let result: AuthorizeResult;
|
|
@@ -23,19 +27,18 @@ const getCommonMiddleware = (query:InstallationQuery<boolean>, installerService:
|
|
|
result = await installerService.installer.authorize(query);
|
|
result = await installerService.installer.authorize(query);
|
|
|
|
|
|
|
|
if (result.botToken == null) {
|
|
if (result.botToken == null) {
|
|
|
- res.writeHead(403, `The installation for the team(${query.teamId || query.enterpriseId}) has no botToken`);
|
|
|
|
|
- return res.end();
|
|
|
|
|
|
|
+ return next(createError(403, `The installation for the team(${query.teamId || query.enterpriseId}) has no botToken`));
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
catch (e) {
|
|
catch (e) {
|
|
|
logger.error(e.message);
|
|
logger.error(e.message);
|
|
|
|
|
|
|
|
- res.writeHead(500, e.message);
|
|
|
|
|
- return res.end();
|
|
|
|
|
|
|
+ return next(createError(500, e.message));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// set authorized data
|
|
// set authorized data
|
|
|
req.authorizeResult = result;
|
|
req.authorizeResult = result;
|
|
|
|
|
+ next();
|
|
|
};
|
|
};
|
|
|
};
|
|
};
|
|
|
@Middleware()
|
|
@Middleware()
|
|
@@ -50,7 +53,7 @@ export class AuthorizeCommandMiddleware implements IMiddleware {
|
|
|
@Inject()
|
|
@Inject()
|
|
|
installerService: InstallerService;
|
|
installerService: InstallerService;
|
|
|
|
|
|
|
|
- async use(@Req() req: SlackOauthReq, @Res() res: Res): Promise<void|Res> {
|
|
|
|
|
|
|
+ async use(@Req() req: SlackOauthReq, @Res() res: Res, @Next() next: Next): Promise<void|Res> {
|
|
|
const { body } = req;
|
|
const { body } = req;
|
|
|
const teamId = body.team_id;
|
|
const teamId = body.team_id;
|
|
|
const enterpriseId = body.enterprise_id;
|
|
const enterpriseId = body.enterprise_id;
|
|
@@ -62,7 +65,7 @@ export class AuthorizeCommandMiddleware implements IMiddleware {
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
const commonMiddleware = getCommonMiddleware(query, this.installerService, this.logger);
|
|
const commonMiddleware = getCommonMiddleware(query, this.installerService, this.logger);
|
|
|
- await commonMiddleware(req, res);
|
|
|
|
|
|
|
+ await commonMiddleware(req, res, next);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|
|
}
|
|
@@ -79,9 +82,13 @@ export class AuthorizeInteractionMiddleware implements IMiddleware {
|
|
|
@Inject()
|
|
@Inject()
|
|
|
installerService: InstallerService;
|
|
installerService: InstallerService;
|
|
|
|
|
|
|
|
- async use(@Req() req: SlackOauthReq, @Res() res:Res): Promise<void|Res> {
|
|
|
|
|
|
|
+ async use(@Req() req: SlackOauthReq, @Res() res:Res, @Next() next: Next): Promise<void|Res> {
|
|
|
const { body } = req;
|
|
const { body } = req;
|
|
|
|
|
|
|
|
|
|
+ if (body.payload == null) {
|
|
|
|
|
+ return next(createError(400, 'The request has no payload.'));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
const payload = JSON.parse(body.payload);
|
|
const payload = JSON.parse(body.payload);
|
|
|
|
|
|
|
|
// extract id from body.payload
|
|
// extract id from body.payload
|
|
@@ -89,12 +96,6 @@ export class AuthorizeInteractionMiddleware implements IMiddleware {
|
|
|
const enterpriseId = payload.enterprise?.id;
|
|
const enterpriseId = payload.enterprise?.id;
|
|
|
const isEnterpriseInstall = payload.is_enterprise_install === 'true';
|
|
const isEnterpriseInstall = payload.is_enterprise_install === 'true';
|
|
|
|
|
|
|
|
- if (body.payload == null) {
|
|
|
|
|
- // do nothing
|
|
|
|
|
- this.logger.info('body does not have payload');
|
|
|
|
|
- return;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
const query: InstallationQuery<boolean> = {
|
|
const query: InstallationQuery<boolean> = {
|
|
|
teamId,
|
|
teamId,
|
|
|
enterpriseId,
|
|
enterpriseId,
|
|
@@ -102,7 +103,7 @@ export class AuthorizeInteractionMiddleware implements IMiddleware {
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
const commonMiddleware = getCommonMiddleware(query, this.installerService, this.logger);
|
|
const commonMiddleware = getCommonMiddleware(query, this.installerService, this.logger);
|
|
|
- await commonMiddleware(req, res);
|
|
|
|
|
|
|
+ await commonMiddleware(req, res, next);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|
|
}
|
|
@@ -118,7 +119,7 @@ export class AuthorizeEventsMiddleware implements IMiddleware {
|
|
|
@Inject()
|
|
@Inject()
|
|
|
installerService: InstallerService;
|
|
installerService: InstallerService;
|
|
|
|
|
|
|
|
- async use(@Req() req: SlackOauthReq, @Res() res: Res): Promise<void|Res> {
|
|
|
|
|
|
|
+ async use(@Req() req: SlackOauthReq, @Res() res: Res, @Next() next: Next): Promise<void|Res> {
|
|
|
const { body } = req;
|
|
const { body } = req;
|
|
|
const teamId = body.team_id;
|
|
const teamId = body.team_id;
|
|
|
const enterpriseId = body.enterprise_id;
|
|
const enterpriseId = body.enterprise_id;
|
|
@@ -130,7 +131,7 @@ export class AuthorizeEventsMiddleware implements IMiddleware {
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
const commonMiddleware = getCommonMiddleware(query, this.installerService, this.logger);
|
|
const commonMiddleware = getCommonMiddleware(query, this.installerService, this.logger);
|
|
|
- await commonMiddleware(req, res);
|
|
|
|
|
|
|
+ await commonMiddleware(req, res, next);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|
|
}
|