27 lines
1.5 KiB
JavaScript
27 lines
1.5 KiB
JavaScript
// Explicit operator action only. Prints a bcrypt hash; never writes .env or a database.
|
||
const readline = require('node:readline');
|
||
const { Writable } = require('node:stream');
|
||
const bcrypt = require('bcrypt');
|
||
async function main() {
|
||
if (!process.stdin.isTTY) throw new Error('Run interactively in a terminal; passwords are never accepted as command arguments.');
|
||
const muted = new Writable({ write(_chunk, _encoding, callback) { callback(); } });
|
||
const reader = readline.createInterface({ input: process.stdin, output: muted, terminal: true });
|
||
const ask = (prompt) => new Promise((resolve) => {
|
||
process.stdout.write(prompt);
|
||
reader.question('', (value) => { process.stdout.write('\n'); resolve(value); });
|
||
});
|
||
reader.on('SIGINT', () => { reader.close(); process.exit(130); });
|
||
let password;
|
||
try {
|
||
password = await ask('Existing/chosen password (hidden): ');
|
||
const confirmation = await ask('Confirm password (hidden): ');
|
||
if (password !== confirmation) throw new Error('Confirmation differs; no hash generated.');
|
||
if (password.length < 8 || password.length > 128 || Buffer.byteLength(password, 'utf8') > 72) {
|
||
throw new Error('Password must be 8–128 characters and at most 72 UTF-8 bytes for bcrypt.');
|
||
}
|
||
} finally { reader.close(); }
|
||
console.log(`SUPERADMIN_PASSWORD_HASH=${await bcrypt.hash(password, 12)}`);
|
||
console.log('Set this value securely on the intended backend. Nothing was changed automatically.');
|
||
}
|
||
main().catch((error) => { console.error(error.message); process.exitCode = 1; });
|