فهرست منبع

use AuthorizeMiddleware

Yuki Takei 5 سال پیش
والد
کامیت
ac658cc30a

+ 14 - 14
packages/slackbot-proxy/src/controllers/slack.ts

@@ -1,5 +1,5 @@
 import {
 import {
-  BodyParams, Controller, Get, Inject, Post, Req, Res,
+  BodyParams, Controller, Get, Inject, Post, Req, Res, UseBefore,
 } from '@tsed/common';
 } from '@tsed/common';
 import { parseSlashCommand } from '@growi/slack';
 import { parseSlashCommand } from '@growi/slack';
 import { Installation } from '~/entities/installation';
 import { Installation } from '~/entities/installation';
@@ -11,6 +11,8 @@ 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 { AuthedReq } from '~/interfaces/authorized-req';
 
 
 const logger = loggerFactory('slackbot-proxy:controllers:slack');
 const logger = loggerFactory('slackbot-proxy:controllers:slack');
 
 
@@ -32,10 +34,6 @@ export class SlackCtrl {
   @Inject()
   @Inject()
   registerService: RegisterService;
   registerService: RegisterService;
 
 
-  growiCommandsMappings = {
-    register: async(body:{[key:string]:string}):Promise<void> => this.registerService.execSlashCommand(body),
-  };
-
   @Get('/testsave')
   @Get('/testsave')
   testsave(): void {
   testsave(): void {
     const installation = new Installation();
     const installation = new Installation();
@@ -76,23 +74,25 @@ export class SlackCtrl {
   }
   }
 
 
   @Post('/commands')
   @Post('/commands')
-  async handleCommand(@BodyParams() body:{[key:string]:string}, @Res() res: Res): Promise<void|string> {
+  @UseBefore(AuthorizeMiddleware)
+  async handleCommand(@Req() req: AuthedReq, @Res() res: Res): Promise<void|string> {
+    const { body, authorizeResult } = req;
+
     if (body.text == null) {
     if (body.text == null) {
       return 'No text.';
       return 'No text.';
     }
     }
 
 
-    const parsedBody = parseSlashCommand(body);
-    const executeGrowiCommand = this.growiCommandsMappings[parsedBody.growiCommandType];
-
-    if (executeGrowiCommand == null) {
-      return 'No executeGrowiCommand';
-    }
-
     // Send response immediately to avoid opelation_timeout error
     // Send response immediately to avoid opelation_timeout error
     // See https://api.slack.com/apis/connections/events-api#the-events-api__responding-to-events
     // See https://api.slack.com/apis/connections/events-api#the-events-api__responding-to-events
     res.send();
     res.send();
 
 
-    await executeGrowiCommand(body);
+    const growiCommand = parseSlashCommand(body);
+
+    // register
+    if (growiCommand.growiCommandType === 'register') {
+      await this.registerService.process(growiCommand, authorizeResult, body as {[key:string]:string});
+      return;
+    }
 
 
     const installation = await this.installationRepository.findByID('1');
     const installation = await this.installationRepository.findByID('1');
     if (installation == null) {
     if (installation == null) {

+ 6 - 0
packages/slackbot-proxy/src/interfaces/growi-command-processor.ts

@@ -0,0 +1,6 @@
+import { AuthorizeResult } from '@slack/oauth';
+import { GrowiCommand } from '@growi/slack';
+
+export interface GrowiCommandProcessor {
+  process(growiCommand: GrowiCommand, authorizeResult: AuthorizeResult, body: {[key:string]:string}): Promise<void>
+}

+ 0 - 3
packages/slackbot-proxy/src/interfaces/growi-commands-mappings.ts

@@ -1,3 +0,0 @@
-export interface GrowiCommandsMappings{
-  execSlashCommand(body:{[key:string]:string}):Promise<void>
-}

+ 8 - 19
packages/slackbot-proxy/src/services/RegisterService.ts

@@ -1,27 +1,16 @@
-import { Inject, Service } from '@tsed/di';
+import { Service } from '@tsed/di';
 import { WebClient, LogLevel } from '@slack/web-api';
 import { WebClient, LogLevel } from '@slack/web-api';
-import { generateInputSectionBlock } from '@growi/slack';
-import { InstallationQuery } from '@slack/oauth';
-import { GrowiCommandsMappings } from '../interfaces/growi-commands-mappings';
-import { InstallerService } from './InstallerService';
+import { generateInputSectionBlock, GrowiCommand } from '@growi/slack';
+import { AuthorizeResult } from '@slack/oauth';
 
 
-@Service()
-export class RegisterService implements GrowiCommandsMappings {
-
-  @Inject()
-  private readonly installerService: InstallerService;
+import { GrowiCommandProcessor } from '~/interfaces/growi-command-processor';
 
 
-  async execSlashCommand(body:{[key:string]:string}):Promise<void> {
+@Service()
+export class RegisterService implements GrowiCommandProcessor {
 
 
-    // create query from body
-    const query: InstallationQuery<boolean> = {
-      teamId: body.team_id,
-      enterpriseId: body.enterprize_id,
-      isEnterpriseInstall: body.is_enterprise_install === 'true',
-    };
+  async process(growiCommand: GrowiCommand, authorizeResult: AuthorizeResult, body: {[key:string]:string}): Promise<void> {
 
 
-    const result = await this.installerService.installer.authorize(query);
-    const { botToken } = result;
+    const { botToken } = authorizeResult;
 
 
     // tmp use process.env
     // tmp use process.env
     const client = new WebClient(botToken, { logLevel: LogLevel.DEBUG });
     const client = new WebClient(botToken, { logLevel: LogLevel.DEBUG });