Просмотр исходного кода

fix(slackbot-proxy): Call next in connect-styled middleware

Yuki Takei 4 лет назад
Родитель
Сommit
86e863b658
1 измененных файлов с 22 добавлено и 20 удалено
  1. 22 20
      packages/slackbot-proxy/src/middlewares/slack-to-growi/authorizer.ts

+ 22 - 20
packages/slackbot-proxy/src/middlewares/slack-to-growi/authorizer.ts

@@ -1,21 +1,24 @@
 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';
 
 
+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 +26,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 +52,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 +64,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 +81,15 @@ 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) {
+        const message = 'The request has no payload.';
+        logger.warn(message, { body: req.body });
+        return next(createError(400, message));
+      }
+
       const payload = JSON.parse(body.payload);
       const payload = JSON.parse(body.payload);
 
 
       // extract id from body.payload
       // extract id from body.payload
@@ -89,12 +97,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 +104,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 +120,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 +132,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);
   }
   }
 
 
 }
 }