2017-10-23 23:38:52 -04:00
|
|
|
"use strict";
|
|
|
|
|
2024-02-18 13:13:16 +02:00
|
|
|
import fs = require('fs');
|
|
|
|
import crypto = require('crypto');
|
2024-07-18 21:35:17 +03:00
|
|
|
import dataDir from "./data_dir.js";
|
|
|
|
import log from "./log.js";
|
2017-10-23 23:38:52 -04:00
|
|
|
|
2022-12-21 15:19:05 +01:00
|
|
|
const sessionSecretPath = `${dataDir.TRILIUM_DATA_DIR}/session_secret.txt`;
|
2017-10-23 23:38:52 -04:00
|
|
|
|
2024-04-07 14:32:08 +03:00
|
|
|
let sessionSecret: string;
|
2017-10-23 23:38:52 -04:00
|
|
|
|
2024-02-18 13:13:16 +02:00
|
|
|
const ENCODING = "ascii";
|
|
|
|
|
|
|
|
function randomValueHex(len: number) {
|
2017-10-23 23:38:52 -04:00
|
|
|
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);
|
|
|
|
|
2017-10-24 22:17:48 -04:00
|
|
|
log.info("Generated session secret");
|
|
|
|
|
2024-02-18 13:13:16 +02:00
|
|
|
fs.writeFileSync(sessionSecretPath, sessionSecret, ENCODING);
|
2017-10-23 23:38:52 -04:00
|
|
|
}
|
|
|
|
else {
|
2024-02-18 13:13:16 +02:00
|
|
|
sessionSecret = fs.readFileSync(sessionSecretPath, ENCODING);
|
2017-10-23 23:38:52 -04:00
|
|
|
}
|
|
|
|
|
2024-02-18 13:13:16 +02:00
|
|
|
export = sessionSecret;
|