trilium/src/services/session_secret.js

29 lines
762 B
JavaScript
Raw Normal View History

2017-10-24 11:38:52 +08:00
"use strict";
const fs = require('fs');
const crypto = require('crypto');
const dataDir = require('./data_dir');
2017-10-25 10:17:48 +08:00
const log = require('./log');
2017-10-24 11:38:52 +08:00
const sessionSecretPath = dataDir.TRILIUM_DATA_DIR + "/session_secret.txt";
let sessionSecret;
function randomValueHex(len) {
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-25 10:17:48 +08:00
log.info("Generated session secret");
2017-10-24 11:38:52 +08:00
fs.writeFileSync(sessionSecretPath, sessionSecret, 'ASCII');
}
else {
sessionSecret = fs.readFileSync(sessionSecretPath, 'ASCII');
}
module.exports = sessionSecret;