zahmis 4 年之前
父節點
當前提交
cea7e46c9e
共有 1 個文件被更改,包括 30 次插入32 次删除
  1. 30 32
      packages/slackbot-proxy/src/middlewares/slack-to-growi/authorizer.ts

+ 30 - 32
packages/slackbot-proxy/src/middlewares/slack-to-growi/authorizer.ts

@@ -1,6 +1,6 @@
 import { AuthorizeResult, InstallationQuery } from '@slack/oauth';
 import { AuthorizeResult, InstallationQuery } from '@slack/oauth';
 import {
 import {
-  IMiddleware, Inject, Middleware, Req, Res, BodyParams,
+  IMiddleware, Inject, Middleware, Req, Res,
 } from '@tsed/common';
 } from '@tsed/common';
 
 
 import Logger from 'bunyan';
 import Logger from 'bunyan';
@@ -10,6 +10,27 @@ import { InstallationRepository } from '~/repositories/installation';
 import { InstallerService } from '~/services/InstallerService';
 import { InstallerService } from '~/services/InstallerService';
 import loggerFactory from '~/utils/logger';
 import loggerFactory from '~/utils/logger';
 
 
+const createInstallationQuery = async(req:Req, res:Res):Promise<InstallationQuery<boolean>|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) {
+    res.writeHead(400, 'No installation found');
+    return res.end();
+  }
+
+  // create query from body
+  const query: InstallationQuery<boolean> = {
+    teamId,
+    enterpriseId,
+    isEnterpriseInstall,
+  };
+  return query;
+};
+
 @Middleware()
 @Middleware()
 export class AuthorizeCommandMiddleware implements IMiddleware {
 export class AuthorizeCommandMiddleware implements IMiddleware {
 
 
@@ -26,30 +47,18 @@ export class AuthorizeCommandMiddleware implements IMiddleware {
   }
   }
 
 
   async use(@Req() req: SlackOauthReq, @Res() res: Res): Promise<void> {
   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';
+    const query = await createInstallationQuery(req, res);
 
 
-    if (teamId == null && enterpriseId == null) {
-      res.writeHead(400, 'No installation found');
-      return res.end();
+    if (query == null) {
+      return;
     }
     }
 
 
-    // create query from body
-    const query: InstallationQuery<boolean> = {
-      teamId,
-      enterpriseId,
-      isEnterpriseInstall,
-    };
-
     let result: AuthorizeResult;
     let result: AuthorizeResult;
     try {
     try {
       result = await this.installerService.installer.authorize(query);
       result = await this.installerService.installer.authorize(query);
 
 
       if (result.botToken == null) {
       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();
         return res.end();
       }
       }
     }
     }
@@ -155,29 +164,18 @@ export class AuthorizeEventsMiddleware implements IMiddleware {
       return;
       return;
     }
     }
 
 
-    // extract id from body
-    const teamId = req.body.team_id;
-    const enterpriseId = req.body.enterprise_id;
-    const isEnterpriseInstall = req.body.is_enterprise_install === 'true';
+    const query = await createInstallationQuery(req, res);
 
 
-    if (teamId == null && enterpriseId == null) {
-      res.writeHead(400, 'No installation found');
-      return res.end();
+    if (query == null) {
+      return;
     }
     }
 
 
-    // create query from body
-    const query: InstallationQuery<boolean> = {
-      teamId,
-      enterpriseId,
-      isEnterpriseInstall,
-    };
-
     let result: AuthorizeResult;
     let result: AuthorizeResult;
     try {
     try {
       result = await this.installerService.installer.authorize(query);
       result = await this.installerService.installer.authorize(query);
 
 
       if (result.botToken == null) {
       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();
         return res.end();
       }
       }
     }
     }