From 435431feff739ccabfe5fa55b05b9e3405956bcc Mon Sep 17 00:00:00 2001 From: ZhangYuan Date: Wed, 29 Nov 2023 15:23:28 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81aes=E4=BC=A0=E8=BE=93?= =?UTF-8?q?=20(#10)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/package.json | 8 ++-- server/src/apps/surveyPublish/config/index.ts | 7 +++ server/src/apps/surveyPublish/index.ts | 10 +++++ .../service/surveySubmitService.ts | 44 ++++++++++++++++++- server/src/apps/surveyPublish/utils/index.ts | 8 ++++ 5 files changed, 73 insertions(+), 4 deletions(-) diff --git a/server/package.json b/server/package.json index cbe440ee..576a619e 100644 --- a/server/package.json +++ b/server/package.json @@ -16,13 +16,15 @@ "dev": "npm run copy && nodemon -e js,mjs,json,ts --exec 'npm run launch:dev' --watch ./src" }, "devDependencies": { + "@types/crypto-js": "^4.2.1", + "@types/koa": "^2.13.8", + "@types/koa-bodyparser": "^4.3.10", + "@types/koa-router": "^7.4.4", "nodemon": "^2.0.20", "typescript": "^4.8.4" }, "dependencies": { - "@types/koa": "^2.13.8", - "@types/koa-bodyparser": "^4.3.10", - "@types/koa-router": "^7.4.4", + "crypto-js": "^4.2.0", "joi": "^17.9.2", "jsonwebtoken": "^9.0.1", "koa": "^2.14.2", diff --git a/server/src/apps/surveyPublish/config/index.ts b/server/src/apps/surveyPublish/config/index.ts index de16e99f..a57641a5 100644 --- a/server/src/apps/surveyPublish/config/index.ts +++ b/server/src/apps/surveyPublish/config/index.ts @@ -2,6 +2,13 @@ const config = { mongo:{ url: process.env.xiaojuSurveyMongoUrl ||'mongodb://localhost:27017', dbName:'xiaojuSurvey', + }, + session:{ + expireTime: parseInt(process.env.xiaojuSurveySessionExpireTime) || 8*3600*1000 + }, + encrypt:{ + type: process.env.xiaojuSurveyEncryptType ||'aes', + aesCodelength: parseInt(process.env.xiaojuSurveyAesCodelength) || 10 //aes密钥长度 } } diff --git a/server/src/apps/surveyPublish/index.ts b/server/src/apps/surveyPublish/index.ts index 25687b66..7b79cbff 100644 --- a/server/src/apps/surveyPublish/index.ts +++ b/server/src/apps/surveyPublish/index.ts @@ -33,6 +33,15 @@ export default class SurveyPublish { data: data, } } + + @SurveyServer({ type: 'http', method: 'get', routerName: '/getEncryptInfo' }) + async getEncryptInfo({ req, res }: { req: Request, res: Response }) { + const data = await surveySubmitService.getEncryptInfo() + return { + code: 200, + data: data, + } + } // 提交问卷 @SurveyServer({ type: 'http', method: 'post', routerName: '/submit' }) async submit({ req, res }: { req: Request, res: Response }) { @@ -43,6 +52,7 @@ export default class SurveyPublish { surveyPath: Joi.string().required(), data: Joi.string().required(), encryptType: Joi.string(), + sessionId: Joi.string(), }).validate(req.body, { allowUnknown: true })); await surveySubmitService.submit({ surveySubmitData }) return { diff --git a/server/src/apps/surveyPublish/service/surveySubmitService.ts b/server/src/apps/surveyPublish/service/surveySubmitService.ts index dcb0b354..ffe1c585 100644 --- a/server/src/apps/surveyPublish/service/surveySubmitService.ts +++ b/server/src/apps/surveyPublish/service/surveySubmitService.ts @@ -1,10 +1,49 @@ import { mongo } from '../db/mongo' -import { getStatusObject, getMapByKey } from '../utils/index' +import { getStatusObject, getMapByKey, randomCode } from '../utils/index' import { SURVEY_STATUS, CommonError } from '../../../types/index' import { surveyKeyStoreService } from './surveyKeyStoreService' +import { getConfig } from '../config/index' +import * as CryptoJS from 'crypto-js' +import * as aes from 'crypto-js/aes'; import * as moment from 'moment' +const config = getConfig() class SurveySubmitService { + + async addSessionData(data) { + const surveySession = await mongo.getCollection({ collectionName: 'surveySession' }); + const surveySessionRes = await surveySession.insertOne({ + data, + expireDate: Date.now() + config.session.expireTime + }) + return { + sessionId: surveySessionRes.insertedId.toString(), + ...data + } + } + + async getSessionData(sessionId) { + const surveySession = await mongo.getCollection({ collectionName: 'surveySession' }); + const sessionObjectId = mongo.getObjectIdByStr(sessionId) + const surveySessionRes = await surveySession.findOne({ _id: sessionObjectId }) + await surveySession.deleteMany({ expireDate: { $lt: Date.now() } }) + return { sessionId, data: surveySessionRes.data } + } + + async getEncryptInfo() { + const encryptType = config.encrypt.type + let data = {} + if (encryptType === 'aes') { + data = await this.addSessionData({ + code: randomCode(config.encrypt.aesCodelength) + }) + } + return { + encryptType, + data + } + } + async submit({ surveySubmitData }: { surveySubmitData: any }) { const surveyMeta = await mongo.getCollection({ collectionName: 'surveyMeta' }); const surveyMetaRes = mongo.convertId2StringByDoc( @@ -19,6 +58,9 @@ class SurveySubmitService { const surveySubmit = await mongo.getCollection({ collectionName: 'surveySubmit' }); if (surveySubmitData.encryptType === 'base64') { surveySubmitData.data = JSON.parse(decodeURIComponent(Buffer.from(surveySubmitData.data, "base64").toString())) + } else if (surveySubmitData.encryptType === 'aes') { + const sessionData = await this.getSessionData(surveySubmitData.sessionId) + surveySubmitData.data = JSON.parse(decodeURIComponent(aes.decrypt(surveySubmitData.data, sessionData.data.code).toString(CryptoJS.enc.Utf8))) } else { surveySubmitData.data = JSON.parse(surveySubmitData.data) } diff --git a/server/src/apps/surveyPublish/utils/index.ts b/server/src/apps/surveyPublish/utils/index.ts index a20ba839..b071279d 100644 --- a/server/src/apps/surveyPublish/utils/index.ts +++ b/server/src/apps/surveyPublish/utils/index.ts @@ -21,4 +21,12 @@ export function getValidateValue(validationResult:Joi.ValidationResult throw new CommonError(validationResult.error.details.map(e=>e.message).join()) } return validationResult.value; +} + +export function randomCode(length) { + let charList:Array = [] + for(let i=0;i