import { randomBytes, scryptSync } from 'node:crypto'; import { mkdir, open, stat } from 'node:fs/promises'; import { dirname, resolve } from 'node:path'; const file = resolve(process.env.WF_LOCAL_ACCESS_FILE || '.wishforge/local-access.json'); await mkdir(dirname(file), { recursive: true, mode: 0o700 }); try { const existing = await stat(file); if (!existing.isFile() || (existing.mode & 0o077) !== 0 || existing.uid !== process.getuid()) throw new Error('Existing credentials must be operator-owned with mode 0600.'); console.log(`Existing local access credentials retained: ${file}`); } catch (error) { if (error.code !== 'ENOENT') throw error; const password = randomBytes(32).toString('base64url'); const salt = randomBytes(32).toString('hex'); const handle = await open(file, 'wx', 0o600); try { await handle.writeFile( `${JSON.stringify( { username: 'admin', password, salt, passwordHash: scryptSync(password, salt, 64).toString('hex'), sessionSecret: randomBytes(48).toString('base64url'), }, null, 2, )}\n`, ); } finally { await handle.close(); } console.log(`Created private local access credentials (0600): ${file}`); } console.log( 'Set WF_LOCAL_ACCESS=true, WF_LOCAL_ACCESS_FILE to this absolute path, and WF_LOCAL_ALLOWED_ORIGINS to the exact browser origins. Credentials are never printed.', );