From bc06258a1d1160dde4687e8cab045a20c7c2a5ca Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Tue, 5 Sep 2023 15:43:29 +0800 Subject: [PATCH] feat: add session cache --- src/server/middleware/auth.ts | 5 +---- src/server/model/website.ts | 13 ++++++++++++- src/server/router/website.ts | 5 ++++- src/server/utils/common.ts | 19 +++++++++++++++++++ 4 files changed, 36 insertions(+), 6 deletions(-) diff --git a/src/server/middleware/auth.ts b/src/server/middleware/auth.ts index 447661b..1cfa2ff 100644 --- a/src/server/middleware/auth.ts +++ b/src/server/middleware/auth.ts @@ -3,11 +3,8 @@ import passport from 'passport'; import { Handler } from 'express'; import { Strategy as JwtStrategy, ExtractJwt } from 'passport-jwt'; import jwt from 'jsonwebtoken'; -import { hashUuid } from '../utils/common'; -import dayjs from 'dayjs'; +import { jwtSecret } from '../utils/common'; -export const jwtSecret = - process.env.JWT_SECRET || hashUuid(dayjs().format('YYYYMMDD')); export const jwtIssuer = process.env.JWT_ISSUER || 'tianji.msgbyte.com'; export const jwtAudience = process.env.JWT_AUDIENCE || 'msgbyte.com'; diff --git a/src/server/model/website.ts b/src/server/model/website.ts index 0d09bed..b0939eb 100644 --- a/src/server/model/website.ts +++ b/src/server/model/website.ts @@ -1,5 +1,5 @@ import { Website, WebsiteSession } from '@prisma/client'; -import { flattenJSON, hashUuid, isUuid } from '../utils/common'; +import { flattenJSON, hashUuid, isUuid, parseToken } from '../utils/common'; import { prisma } from './_client'; import { Request } from 'express'; import { getClientInfo } from '../utils/detect'; @@ -41,6 +41,17 @@ export async function findSession(req: Request): Promise<{ // Verify payload const { payload } = req.body; + // Check if cache token is passed + const cacheToken = req.headers['x-tianji-cache'] as string; + + if (cacheToken) { + const result = parseToken(cacheToken); + + if (result) { + return result as any; + } + } + const { website: websiteId, hostname, diff --git a/src/server/router/website.ts b/src/server/router/website.ts index 6e45040..a8c041c 100644 --- a/src/server/router/website.ts +++ b/src/server/router/website.ts @@ -7,6 +7,7 @@ import { saveWebsiteEvent, saveWebsiteSessionData, } from '../model/website'; +import { createToken } from '../utils/common'; export const websiteRouter = Router(); @@ -101,6 +102,8 @@ websiteRouter.post( }); } - res.send(); + const token = createToken(session); + + res.send(token); } ); diff --git a/src/server/utils/common.ts b/src/server/utils/common.ts index 61d352d..0d360a0 100644 --- a/src/server/utils/common.ts +++ b/src/server/utils/common.ts @@ -3,6 +3,7 @@ import crypto from 'crypto'; import { DATA_TYPE } from './const'; import { DynamicDataType } from './types'; import dayjs from 'dayjs'; +import jwt from 'jsonwebtoken'; export function isUuid(value: string) { return validate(value); @@ -129,3 +130,21 @@ function getDataType(value: any): string { return type; } + +/** + * Secret for auth and cacheTokenGenerate + */ +export const jwtSecret = + process.env.JWT_SECRET || hashUuid(dayjs().format('YYYYMMDD')); + +export function createToken(payload: any, secret = jwtSecret, options?: any) { + return jwt.sign(payload, secret, options); +} + +export function parseToken(token: string, secret = jwtSecret) { + try { + return jwt.verify(token, secret); + } catch { + return null; + } +}