refactor: update jwt secret generator more safe for user

This commit is contained in:
moonrailgun 2024-05-09 00:58:05 +08:00
parent ebb6c51f81
commit 6e8c28026e
2 changed files with 9 additions and 2 deletions

View File

@ -9,6 +9,7 @@ import _ from 'lodash';
import { getWorkspaceWebsiteDateRange } from '../model/workspace'; import { getWorkspaceWebsiteDateRange } from '../model/workspace';
import { isCuid } from '@paralleldrive/cuid2'; import { isCuid } from '@paralleldrive/cuid2';
import { getMinimumUnit } from '@tianji/shared'; import { getMinimumUnit } from '@tianji/shared';
import { env } from './env';
export { isCuid }; export { isCuid };
@ -151,8 +152,7 @@ function getDataType(value: any): string {
/** /**
* Secret for auth and cacheTokenGenerate * Secret for auth and cacheTokenGenerate
*/ */
export const jwtSecret = export const jwtSecret = env.jwtSecret;
process.env.JWT_SECRET || hashUuid(dayjs().format('YYYYMMDD'));
export function createToken(payload: any, secret = jwtSecret, options?: any) { export function createToken(payload: any, secret = jwtSecret, options?: any) {
return jwt.sign(payload, secret, options); return jwt.sign(payload, secret, options);

View File

@ -1,6 +1,13 @@
import { v1 as uuid } from 'uuid';
export const env = { export const env = {
isProd: process.env.NODE_ENV === 'production', isProd: process.env.NODE_ENV === 'production',
isTest: process.env.NODE_ENV === 'test', isTest: process.env.NODE_ENV === 'test',
jwtSecret:
!process.env.JWT_SECRET ||
process.env.JWT_SECRET === 'replace-me-with-a-random-string'
? uuid()
: process.env.JWT_SECRET,
port: Number(process.env.PORT || 12345), port: Number(process.env.PORT || 12345),
allowRegister: checkEnvTrusty(process.env.ALLOW_REGISTER), allowRegister: checkEnvTrusty(process.env.ALLOW_REGISTER),
allowOpenapi: checkEnvTrusty(process.env.ALLOW_OPENAPI), allowOpenapi: checkEnvTrusty(process.env.ALLOW_OPENAPI),