Преглед изворни кода

impl AuthorizeInteractionMiddleware

Yuki Takei пре 5 година
родитељ
комит
47bed8e46b

+ 6 - 4
packages/slackbot-proxy/src/controllers/slack.ts

@@ -14,7 +14,7 @@ import { InstallerService } from '~/services/InstallerService';
 import { RegisterService } from '~/services/RegisterService';
 import { RegisterService } from '~/services/RegisterService';
 
 
 import loggerFactory from '~/utils/logger';
 import loggerFactory from '~/utils/logger';
-import { AuthorizeMiddleware } from '~/middlewares/authorizer';
+import { AuthorizeCommandMiddleware, AuthorizeInteractionMiddleware } from '~/middlewares/authorizer';
 import { AuthedReq } from '~/interfaces/authorized-req';
 import { AuthedReq } from '~/interfaces/authorized-req';
 import { Relation } from '~/entities/relation';
 import { Relation } from '~/entities/relation';
 
 
@@ -78,7 +78,7 @@ export class SlackCtrl {
   }
   }
 
 
   @Post('/commands')
   @Post('/commands')
-  @UseBefore(AuthorizeMiddleware)
+  @UseBefore(AuthorizeCommandMiddleware)
   async handleCommand(@Req() req: AuthedReq, @Res() res: Res): Promise<void|string> {
   async handleCommand(@Req() req: AuthedReq, @Res() res: Res): Promise<void|string> {
     const { body, authorizeResult } = req;
     const { body, authorizeResult } = req;
 
 
@@ -118,8 +118,10 @@ export class SlackCtrl {
   }
   }
 
 
   @Post('/interactions')
   @Post('/interactions')
-  async handleInteraction(@BodyParams() body:{[key:string]:string}, @Res() res: Res): Promise<void|string> {
-    logger.info('receive interaction', body);
+  @UseBefore(AuthorizeInteractionMiddleware)
+  async handleInteraction(@Req() req: AuthedReq, @Res() res: Res): Promise<void|string> {
+    logger.info('receive interaction', req.body);
+    logger.info('receive interaction', req.authorizeResult);
     return;
     return;
   }
   }
 
 

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

@@ -8,7 +8,7 @@ import { InstallationRepository } from '~/repositories/installation';
 import { InstallerService } from '~/services/InstallerService';
 import { InstallerService } from '~/services/InstallerService';
 
 
 @Middleware()
 @Middleware()
-export class AuthorizeMiddleware implements IMiddleware {
+export class AuthorizeCommandMiddleware implements IMiddleware {
 
 
   @Inject()
   @Inject()
   installerService: InstallerService;
   installerService: InstallerService;
@@ -47,3 +47,47 @@ export class AuthorizeMiddleware implements IMiddleware {
   }
   }
 
 
 }
 }
+
+
+@Middleware()
+export class AuthorizeInteractionMiddleware implements IMiddleware {
+
+  @Inject()
+  installerService: InstallerService;
+
+  @Inject()
+  installationRepository: InstallationRepository;
+
+  async use(@Req() req: AuthedReq, @Res() res: Res): Promise<void> {
+    const { body } = req;
+
+    const payload = JSON.parse(body.payload);
+
+    // extract id from body
+    const teamId = payload.team?.id;
+    const enterpriseId = body.enterprise?.id;
+
+    if (teamId == null && enterpriseId == null) {
+      res.writeHead(400);
+      return res.end();
+    }
+
+    // create query from body
+    const query: InstallationQuery<boolean> = {
+      teamId,
+      enterpriseId,
+      isEnterpriseInstall: body.is_enterprise_install === 'true',
+    };
+
+    const result = await this.installerService.installer.authorize(query);
+
+    if (result.botToken == null) {
+      res.writeHead(403);
+      return res.end();
+    }
+
+    // set authorized data
+    req.authorizeResult = result;
+  }
+
+}