Przeglądaj źródła

set token Gtop to header

itizawa 4 lat temu
rodzic
commit
4523ace6ae

+ 5 - 3
src/server/routes/apiv3/slack-integration.js

@@ -17,25 +17,27 @@ module.exports = (crowi) => {
   // Check if the access token is correct
   // Check if the access token is correct
   async function verifyAccessTokenFromProxy(req, res, next) {
   async function verifyAccessTokenFromProxy(req, res, next) {
     const tokenPtoG = req.headers['x-growi-ptog-tokens'];
     const tokenPtoG = req.headers['x-growi-ptog-tokens'];
-
     if (tokenPtoG == null) {
     if (tokenPtoG == null) {
       const message = 'The value of header \'x-growi-ptog-tokens\' must not be empty.';
       const message = 'The value of header \'x-growi-ptog-tokens\' must not be empty.';
       logger.warn(message, { body: req.body });
       logger.warn(message, { body: req.body });
       return res.status(400).send({ message });
       return res.status(400).send({ message });
     }
     }
 
 
-    const slackAppIntegration = await SlackAppIntegration.estimatedDocumentCount({ tokenPtoG });
+    const slackAppIntegration = await SlackAppIntegration.findOne({ tokenPtoG });
 
 
     logger.debug('verifyAccessTokenFromProxy', {
     logger.debug('verifyAccessTokenFromProxy', {
       tokenPtoG,
       tokenPtoG,
     });
     });
 
 
-    if (slackAppIntegration === 0) {
+    if (slackAppIntegration == null) {
       return res.status(403).send({
       return res.status(403).send({
         message: 'The access token that identifies the request source is slackbot-proxy is invalid. Did you setup with `/growi register`?',
         message: 'The access token that identifies the request source is slackbot-proxy is invalid. Did you setup with `/growi register`?',
       });
       });
     }
     }
 
 
+    // set tokenGtoP for repsponse
+    req.body.tokenGtoP = slackAppIntegration.tokenGtoP;
+
     next();
     next();
   }
   }
 
 

+ 28 - 28
src/server/service/slackbot.js

@@ -26,7 +26,7 @@ class SlackBotService extends S2sMessageHandlable {
     this.lastLoadedAt = new Date();
     this.lastLoadedAt = new Date();
   }
   }
 
 
-  get client() {
+  async generateClient(body) {
     const currentBotType = this.crowi.configManager.getConfig('crowi', 'slackbot:currentBotType');
     const currentBotType = this.crowi.configManager.getConfig('crowi', 'slackbot:currentBotType');
 
 
     if (currentBotType == null) {
     if (currentBotType == null) {
@@ -37,16 +37,18 @@ class SlackBotService extends S2sMessageHandlable {
     let token;
     let token;
 
 
     // connect to proxy
     // connect to proxy
-    if (currentBotType !== 'customBotWithoutProxy') {
-      const proxyServerUri = this.crowi.configManager.getConfig('crowi', 'slackbot:proxyServerUri');
-      serverUri = urljoin(proxyServerUri, '/g2s');
-    }
-    // connect directly
-    else {
+    if (currentBotType === 'customBotWithoutProxy') {
+      // connect directly
       token = this.crowi.configManager.getConfig('crowi', 'slackbot:token');
       token = this.crowi.configManager.getConfig('crowi', 'slackbot:token');
+      return generateWebClient(token, serverUri);
     }
     }
 
 
-    return generateWebClient(token, serverUri);
+    const proxyServerUri = this.crowi.configManager.getConfig('crowi', 'slackbot:proxyServerUri');
+    serverUri = urljoin(proxyServerUri, '/g2s');
+    const headers = {
+      'x-growi-gtop-tokens': body.tokenGtoP,
+    };
+    return generateWebClient(token, serverUri, headers);
   }
   }
 
 
   /**
   /**
@@ -88,13 +90,10 @@ class SlackBotService extends S2sMessageHandlable {
     }
     }
   }
   }
 
 
-  async sendAuthTest() {
-    await this.client.api.test();
-  }
-
-  notCommand(body) {
+  async notCommand(body) {
     logger.error('Invalid first argument');
     logger.error('Invalid first argument');
-    this.client.chat.postEphemeral({
+    const client = await this.generateClient(body);
+    client.chat.postEphemeral({
       channel: body.channel_id,
       channel: body.channel_id,
       user: body.user_id,
       user: body.user_id,
       blocks: [
       blocks: [
@@ -111,9 +110,10 @@ class SlackBotService extends S2sMessageHandlable {
   }
   }
 
 
   async getSearchResultPaths(body, args, offset = 0) {
   async getSearchResultPaths(body, args, offset = 0) {
+    const client = this.generateClient(body);
     const firstKeyword = args[1];
     const firstKeyword = args[1];
     if (firstKeyword == null) {
     if (firstKeyword == null) {
-      this.client.chat.postEphemeral({
+      client.chat.postEphemeral({
         channel: body.channel_id,
         channel: body.channel_id,
         user: body.user_id,
         user: body.user_id,
         blocks: [
         blocks: [
@@ -133,7 +133,7 @@ class SlackBotService extends S2sMessageHandlable {
     // no search results
     // no search results
     if (results.data.length === 0) {
     if (results.data.length === 0) {
       logger.info(`No page found with "${keywords}"`);
       logger.info(`No page found with "${keywords}"`);
-      this.client.chat.postEphemeral({
+      client.chat.postEphemeral({
         channel: body.channel_id,
         channel: body.channel_id,
         user: body.user_id,
         user: body.user_id,
         blocks: [
         blocks: [
@@ -167,13 +167,9 @@ class SlackBotService extends S2sMessageHandlable {
     };
     };
   }
   }
 
 
-  async getSlackChannelName() {
-    const slackTeamInfo = await this.client.team.info();
-    return slackTeamInfo.team.name;
-  }
-
-  shareSearchResults(payload) {
-    this.client.chat.postMessage({
+  async shareSearchResults(payload) {
+    const client = await this.generateClient();
+    client.chat.postMessage({
       channel: payload.channel.id,
       channel: payload.channel.id,
       text: payload.actions[0].value,
       text: payload.actions[0].value,
     });
     });
@@ -183,6 +179,7 @@ class SlackBotService extends S2sMessageHandlable {
     const {
     const {
       resultPaths, offset, resultsTotal,
       resultPaths, offset, resultsTotal,
     } = await this.getSearchResultPaths(body, args, offsetNum);
     } = await this.getSearchResultPaths(body, args, offsetNum);
+    const client = await this.generateClient(body);
 
 
     const keywords = this.getKeywords(args);
     const keywords = this.getKeywords(args);
 
 
@@ -247,7 +244,7 @@ class SlackBotService extends S2sMessageHandlable {
           },
           },
         );
         );
       }
       }
-      await this.client.chat.postEphemeral({
+      await client.chat.postEphemeral({
         channel: body.channel_id,
         channel: body.channel_id,
         user: body.user_id,
         user: body.user_id,
         blocks: [
         blocks: [
@@ -259,7 +256,7 @@ class SlackBotService extends S2sMessageHandlable {
     }
     }
     catch {
     catch {
       logger.error('Failed to get search results.');
       logger.error('Failed to get search results.');
-      await this.client.chat.postEphemeral({
+      await client.chat.postEphemeral({
         channel: body.channel_id,
         channel: body.channel_id,
         user: body.user_id,
         user: body.user_id,
         blocks: [
         blocks: [
@@ -271,8 +268,10 @@ class SlackBotService extends S2sMessageHandlable {
   }
   }
 
 
   async createModal(body) {
   async createModal(body) {
+    const client = await this.generateClient(body);
+
     try {
     try {
-      await this.client.views.open({
+      await client.views.open({
         trigger_id: body.trigger_id,
         trigger_id: body.trigger_id,
 
 
         view: {
         view: {
@@ -300,7 +299,7 @@ class SlackBotService extends S2sMessageHandlable {
     }
     }
     catch (err) {
     catch (err) {
       logger.error('Failed to create a page.');
       logger.error('Failed to create a page.');
-      await this.client.chat.postEphemeral({
+      await client.chat.postEphemeral({
         channel: body.channel_id,
         channel: body.channel_id,
         user: body.user_id,
         user: body.user_id,
         blocks: [
         blocks: [
@@ -317,6 +316,7 @@ class SlackBotService extends S2sMessageHandlable {
     const pathUtils = require('growi-commons').pathUtils;
     const pathUtils = require('growi-commons').pathUtils;
 
 
     const contentsBody = payload.view.state.values.contents.contents_input.value;
     const contentsBody = payload.view.state.values.contents.contents_input.value;
+    const client = await this.generateClient();
 
 
     try {
     try {
       let path = payload.view.state.values.path.path_input.value;
       let path = payload.view.state.values.path.path_input.value;
@@ -329,7 +329,7 @@ class SlackBotService extends S2sMessageHandlable {
       await Page.create(path, contentsBody, dummyObjectIdOfUser, {});
       await Page.create(path, contentsBody, dummyObjectIdOfUser, {});
     }
     }
     catch (err) {
     catch (err) {
-      this.client.chat.postMessage({
+      client.chat.postMessage({
         channel: payload.user.id,
         channel: payload.user.id,
         blocks: [
         blocks: [
           this.generateMarkdownSectionBlock(`Cannot create new page to existed path\n *Contents* :memo:\n ${contentsBody}`)],
           this.generateMarkdownSectionBlock(`Cannot create new page to existed path\n *Contents* :memo:\n ${contentsBody}`)],