feat(server): add persistent authentication and production build (#6)
Protect HTTP and WebSocket API routes with scrypt password login and signed persistent sessions. Restrict forwarded identity and HTTPS detection to configured proxies, and serve optional web assets with SPA fallback. Add @fastify/static for Fastify-compatible static file handling and esbuild to bundle TypeScript core source while retaining native dependencies externally.
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
import { createInterface } from "node:readline/promises";
|
||||
import { resolve } from "node:path";
|
||||
import { setPassword } from "../auth/credentials";
|
||||
|
||||
// TTY input is hidden; piped input is a single line (never a command-line argument).
|
||||
async function readPassword(): Promise<string> {
|
||||
if (!process.stdin.isTTY) {
|
||||
const reader = createInterface({ input: process.stdin });
|
||||
try { for await (const line of reader) return line; return ""; }
|
||||
finally { reader.close(); }
|
||||
}
|
||||
process.stderr.write("New ShellBook password: ");
|
||||
process.stdin.setRawMode(true);
|
||||
process.stdin.resume();
|
||||
process.stdin.setEncoding("utf8");
|
||||
return new Promise((resolvePassword, reject) => {
|
||||
let password = "";
|
||||
const onData = (chunk: string) => {
|
||||
for (const char of chunk) {
|
||||
if (char === "\r" || char === "\n" || char === "\u0003") {
|
||||
process.stdin.off("data", onData);
|
||||
process.stdin.setRawMode(false);
|
||||
process.stdin.pause();
|
||||
process.stderr.write("\n");
|
||||
if (char === "\u0003") reject(new Error("Cancelled")); else resolvePassword(password);
|
||||
return;
|
||||
}
|
||||
if (char === "\u007f" || char === "\b") password = password.slice(0, -1);
|
||||
else password += char;
|
||||
}
|
||||
};
|
||||
process.stdin.on("data", onData);
|
||||
});
|
||||
}
|
||||
await setPassword(resolve(process.env.SHELLBOOK_DATA_DIR ?? "data"), await readPassword());
|
||||
console.log("Password saved. Existing sessions remain valid until expiry.");
|
||||
Reference in New Issue
Block a user