refactor(session_secret): use existing randomSecureToken function

this also improves secret security,
as it uses a larger charset size
than the UPPERCASED HEX secret before
This commit is contained in:
Panagiotis Papadopoulos 2024-12-31 11:22:18 +01:00
parent 8959b96f28
commit 79887d1ce0

View File

@ -1,9 +1,9 @@
"use strict";
import fs from "fs";
import crypto from "crypto";
import dataDir from "./data_dir.js";
import log from "./log.js";
import utils from "./utils.js"
const sessionSecretPath = `${dataDir.TRILIUM_DATA_DIR}/session_secret.txt`;
@ -11,14 +11,8 @@ let sessionSecret: string;
const ENCODING = "ascii";
function randomValueHex(len: number) {
return crypto.randomBytes(Math.ceil(len / 2))
.toString('hex') // convert to hexadecimal format
.slice(0, len).toUpperCase(); // return required number of characters
}
if (!fs.existsSync(sessionSecretPath)) {
sessionSecret = randomValueHex(64);
sessionSecret = utils.randomSecureToken(64).slice(0, 64);
log.info("Generated session secret");