feat: generate secure secret key and prevent default

This commit is contained in:
Lin, Hung-Shin authored and Annie Huang committed 2023-11-12 21:23:12 +08:00
1 parent 55b61dbcab
commit f1a50067c5
11 files changed
+45 -5

No files matched your search

+1 -1
View File
@@ -3,7 +3,7 @@ import config from '../config.js'
const jwtOptions = {
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
secretOrKey: config.jwtSecret,
secretOrKey: config.secret,
}
function verifyJwt(jwtPayload, done) {
+9
View File
@@ -0,0 +1,9 @@
import { genSecret } from '../utils/env.js'
function setup() {
console.log('Setting up .env file...')
const secret = genSecret()
console.log(`SECRET_KEY=${secret}`)
}
setup()
+9 -2
View File
@@ -1,10 +1,10 @@
import { get } from './utils/env.js'
import { get, genSecret } from './utils/env.js'
const config = {
host: get('HOST', 'localhost'),
port: get('PORT', 3000),
db: get('MONGO_URL', 'mongodb://localhost:27017/odyssey-test'),
jwtSecret: get('JWT_SECRET', 'secret'),
secret: get('SECRET_KEY', ''),
dockerNetwork: get('DOCKER_NETWORK', 'linux-odyssey-players'),
isProduction: process.env.NODE_ENV === 'production',
hostPwd: get('HOST_PWD', ''),
@@ -22,4 +22,11 @@ const config = {
config.baseUrl = get('BASE_URL', `http://${config.host}:${config.port}`)
if (!config.secret) {
config.secret = genSecret()
console.warn(
'No SECRET_KEY found in .env! A temporary secret key has been generated. To set up a persistent key, please run the setup script.'
)
}
export default config
+1 -1
View File
@@ -4,7 +4,7 @@ import MongoStore from 'connect-mongo'
import config from '../config.js'
export default session({
secret: config.jwtSecret,
secret: config.secret,
resave: false,
saveUninitialized: false,
rolling: true,
+6 -1
View File
@@ -1,4 +1,5 @@
/* eslint-disable import/prefer-default-export */
import crypto from 'crypto'
export function get(key, defaultValue) {
const value = process.env[key]
if (value != null) {
@@ -6,3 +7,7 @@ export function get(key, defaultValue) {
}
return defaultValue
}
export function genSecret() {
return crypto.randomUUID()
}