Files
hobbypj-contents/server/set-admin-password.mjs
T
Bkk1234 a629e67ca4 feat(admin): verify the backoffice password with PBKDF2, not plaintext compare
- ui/auth.js: derive PBKDF2-HMAC-SHA256(name:password) with a per-deploy salt
  (250k iterations) and compare against the verifier in constant time
- config/admin-credentials.json holds salt/iterations/verifier only — no password
- server/set-admin-password.mjs generates/rotates that file (CLI or hidden prompt)
- login is async now: button shows a pending state; config load errors surface in the form
- SW precaches the credentials file; version bumped to 2026.11.19.4
- docs: README/AGENTS explain rotation and the residual limits of a client-side gate
2026-11-19 11:32:48 +07:00

94 lines
3.8 KiB
JavaScript

#!/usr/bin/env node
/**
* server/set-admin-password.mjs — สร้าง/เปลี่ยนรหัสเข้าหลังบ้าน
*
* node server/set-admin-password.mjs admin "รหัสใหม่ที่ตั้ง"
* node server/set-admin-password.mjs # ถามทางคีย์บอร์ด (ซ่อนรหัส)
* ADMIN_PASSWORD=xxx node server/set-admin-password.mjs admin
*
* เขียนผลลัพธ์ลง config/admin-credentials.json (salt + verifier เท่านั้น ไม่มีรหัสจริง)
* ฝั่งเว็บ (ui/auth.js) จะ PBKDF2 จากสิ่งที่ผู้ใช้พิมพ์ด้วยค่า salt/iterations ชุดเดียวกันแล้วเทียบ
*/
import { pbkdf2Sync, randomBytes } from "node:crypto";
import { mkdir, writeFile } from "node:fs/promises";
import { stdin, stdout } from "node:process";
import path from "node:path";
import { fileURLToPath } from "node:url";
import readline from "node:readline";
const ITERATIONS = Number(process.env.PBKDF2_ITERATIONS || 250_000);
const KEY_LENGTH = 32;
const HERE = path.dirname(fileURLToPath(import.meta.url));
const ROOT = path.resolve(HERE, "..");
const OUT_FILE = path.join(ROOT, "config", "admin-credentials.json");
function ask(question, { hidden = false } = {}) {
return new Promise((resolve) => {
const rl = readline.createInterface({ input: stdin, output: stdout, terminal: true });
if (!hidden) {
rl.question(question, (answer) => {
rl.close();
resolve(answer);
});
return;
}
const onData = (char) => {
const text = String(char);
if (text === "\n" || text === "\r" || text === "\u0004") return;
stdout.write("*");
};
rl._writeToOutput = (text) => {
if (text.includes(question)) stdout.write(question);
};
stdin.on("data", onData);
rl.question(question, (answer) => {
stdin.off("data", onData);
stdout.write("\n");
rl.close();
resolve(answer);
});
});
}
function derive(name, password, salt, iterations) {
return pbkdf2Sync(`${name}:${password}`, salt, iterations, KEY_LENGTH, "sha256");
}
async function main() {
const name = (process.argv[2] || (await ask("ชื่อผู้ใช้ [admin]: ")) || "admin").trim();
let password = process.argv[3] || process.env.ADMIN_PASSWORD || "";
if (!password) password = await ask("รหัสผ่านใหม่: ", { hidden: true });
if (!password || password.length < 6) {
console.error("รหัสต้องยาวอย่างน้อย 6 ตัวอักษร");
process.exit(1);
}
const salt = randomBytes(16);
const verifier = derive(name, password, salt, ITERATIONS);
const payload = {
note: "สร้างด้วย node server/set-admin-password.mjs — อย่าแก้ด้วยมือ",
algorithm: "PBKDF2-HMAC-SHA256",
iterations: ITERATIONS,
key_length: KEY_LENGTH,
salt: salt.toString("base64"),
verifier: verifier.toString("base64"),
created_at: new Date().toISOString(),
};
await mkdir(path.dirname(OUT_FILE), { recursive: true });
await writeFile(OUT_FILE, `${JSON.stringify(payload, null, 2)}\n`, "utf8");
console.log(`เขียนแล้ว: ${path.relative(ROOT, OUT_FILE)}`);
console.log(` algorithm = ${payload.algorithm} · iterations = ${ITERATIONS}`);
console.log(` salt = ${payload.salt}`);
console.log(` verifier = ${payload.verifier.slice(0, 16)}…`);
console.log("รหัสจริงไม่ถูกบันทึกที่ไหน — ถ้าลืมให้รันสคริปต์นี้ใหม่");
}
main().catch((error) => {
console.error(error);
process.exit(1);
});