Просмотр исходного кода

Merge pull request #3722 from weseek/feat/GW-5859-generate-access-token

generate access token
Yuki Takei 4 лет назад
Родитель
Сommit
1e6f292880
1 измененных файлов с 21 добавлено и 7 удалено
  1. 21 7
      src/server/models/slack-app-integration.js

+ 21 - 7
src/server/models/slack-app-integration.js

@@ -1,10 +1,24 @@
-module.exports = function(crowi) {
-  const mongoose = require('mongoose');
+const crypto = require('crypto');
+const mongoose = require('mongoose');
+
+const schema = new mongoose.Schema({
+  tokenGtoP: { type: String, required: true, unique: true },
+  tokenPtoG: { type: String, required: true, unique: true },
+});
+class SlackAppIntegration {
 
-  const slackAppIntegrationSchema = new mongoose.Schema({
-    tokenGtoP: { type: String, required: true, unique: true },
-    tokenPtoG: { type: String, required: true, unique: true },
-  });
+  static generateAccessToken(user) {
+    const hasher1 = crypto.createHash('sha512');
+    const hasher2 = crypto.createHash('sha512');
+    const tokenGtoP = hasher1.update(new Date().getTime() + user._id).digest('base64');
+    const tokenPtoG = hasher2.update(new Date().getTime() + user.username).digest('base64');
+    return [tokenGtoP, tokenPtoG];
+  }
 
-  return mongoose.model('SlackAppIntegration', slackAppIntegrationSchema);
+}
+
+module.exports = function(crowi) {
+  SlackAppIntegration.crowi = crowi;
+  schema.loadClass(SlackAppIntegration);
+  return mongoose.model('SlackAppIntegration', schema);
 };