2
0
Yuki Takei 5 жил өмнө
parent
commit
bffac8f2a8

+ 45 - 12
packages/slackbot-proxy/src/middlewares/authorizer.ts

@@ -1,11 +1,14 @@
-import { InstallationQuery } from '@slack/oauth';
+import { AuthorizeResult, InstallationQuery } from '@slack/oauth';
 import {
 import {
   IMiddleware, Inject, Middleware, Req, Res,
   IMiddleware, Inject, Middleware, Req, Res,
 } from '@tsed/common';
 } from '@tsed/common';
 
 
+import Logger from 'bunyan';
+
 import { AuthedReq } from '~/interfaces/authorized-req';
 import { AuthedReq } from '~/interfaces/authorized-req';
 import { InstallationRepository } from '~/repositories/installation';
 import { InstallationRepository } from '~/repositories/installation';
 import { InstallerService } from '~/services/InstallerService';
 import { InstallerService } from '~/services/InstallerService';
+import loggerFactory from '~/utils/logger';
 
 
 @Middleware()
 @Middleware()
 export class AuthorizeCommandMiddleware implements IMiddleware {
 export class AuthorizeCommandMiddleware implements IMiddleware {
@@ -16,15 +19,22 @@ export class AuthorizeCommandMiddleware implements IMiddleware {
   @Inject()
   @Inject()
   installationRepository: InstallationRepository;
   installationRepository: InstallationRepository;
 
 
+  private logger: Logger;
+
+  constructor() {
+    this.logger = loggerFactory('slackbot-proxy:middlewares:AuthorizeCommandMiddleware');
+  }
+
   async use(@Req() req: AuthedReq, @Res() res: Res): Promise<void> {
   async use(@Req() req: AuthedReq, @Res() res: Res): Promise<void> {
     const { body } = req;
     const { body } = req;
 
 
     // extract id from body
     // extract id from body
     const teamId = body.team_id;
     const teamId = body.team_id;
     const enterpriseId = body.enterprize_id;
     const enterpriseId = body.enterprize_id;
+    const isEnterpriseInstall = body.is_enterprise_install === 'true';
 
 
     if (teamId == null && enterpriseId == null) {
     if (teamId == null && enterpriseId == null) {
-      res.writeHead(400);
+      res.writeHead(400, 'No installation found');
       return res.end();
       return res.end();
     }
     }
 
 
@@ -32,13 +42,21 @@ export class AuthorizeCommandMiddleware implements IMiddleware {
     const query: InstallationQuery<boolean> = {
     const query: InstallationQuery<boolean> = {
       teamId,
       teamId,
       enterpriseId,
       enterpriseId,
-      isEnterpriseInstall: body.is_enterprise_install === 'true',
+      isEnterpriseInstall,
     };
     };
 
 
-    const result = await this.installerService.installer.authorize(query);
+    let result: AuthorizeResult;
+    try {
+      result = await this.installerService.installer.authorize(query);
+
+      if (result.botToken == null) {
+        throw new Error(`The installation for the team(${teamId || enterpriseId}) has no botToken`);
+      }
+    }
+    catch (e) {
+      this.logger.error(e.message);
 
 
-    if (result.botToken == null) {
-      res.writeHead(403);
+      res.writeHead(403, e.message);
       return res.end();
       return res.end();
     }
     }
 
 
@@ -58,6 +76,12 @@ export class AuthorizeInteractionMiddleware implements IMiddleware {
   @Inject()
   @Inject()
   installationRepository: InstallationRepository;
   installationRepository: InstallationRepository;
 
 
+  private logger: Logger;
+
+  constructor() {
+    this.logger = loggerFactory('slackbot-proxy:middlewares:AuthorizeInteractionMiddleware');
+  }
+
   async use(@Req() req: AuthedReq, @Res() res: Res): Promise<void> {
   async use(@Req() req: AuthedReq, @Res() res: Res): Promise<void> {
     const { body } = req;
     const { body } = req;
 
 
@@ -65,10 +89,11 @@ export class AuthorizeInteractionMiddleware implements IMiddleware {
 
 
     // extract id from body
     // extract id from body
     const teamId = payload.team?.id;
     const teamId = payload.team?.id;
-    const enterpriseId = body.enterprise?.id;
+    const enterpriseId = payload.enterprise?.id;
+    const isEnterpriseInstall = payload.is_enterprise_install === 'true';
 
 
     if (teamId == null && enterpriseId == null) {
     if (teamId == null && enterpriseId == null) {
-      res.writeHead(400);
+      res.writeHead(400, 'No installation found');
       return res.end();
       return res.end();
     }
     }
 
 
@@ -76,13 +101,21 @@ export class AuthorizeInteractionMiddleware implements IMiddleware {
     const query: InstallationQuery<boolean> = {
     const query: InstallationQuery<boolean> = {
       teamId,
       teamId,
       enterpriseId,
       enterpriseId,
-      isEnterpriseInstall: body.is_enterprise_install === 'true',
+      isEnterpriseInstall,
     };
     };
 
 
-    const result = await this.installerService.installer.authorize(query);
+    let result: AuthorizeResult;
+    try {
+      result = await this.installerService.installer.authorize(query);
+
+      if (result.botToken == null) {
+        throw new Error(`The installation for the team(${teamId || enterpriseId}) has no botToken`);
+      }
+    }
+    catch (e) {
+      this.logger.error(e.message);
 
 
-    if (result.botToken == null) {
-      res.writeHead(403);
+      res.writeHead(403, e.message);
       return res.end();
       return res.end();
     }
     }