badmfck-api-server 6.0.7 → 6.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -295,7 +295,8 @@ monitorAuthDir: "/var/lib/my-service", // where 2FA enrollments are kept
295
295
  for 30 minutes.
296
296
  - **Change access without a restart.** Instead of an inline object, `access` can be a path to a JSON
297
297
  file with the same shape: `access: "/etc/my-service/access.json"`. `npx badmfck access-config` creates
298
- and edits this file from a menu. Passwords are typed hidden and stored as hashes, and the result is
298
+ and edits this file from a menu. Run in the project folder, it finds the path the code assigns to
299
+ `access` (a string literal in `src/` or the compiled `bin/`/`dist/`), or takes it as an argument. Passwords are typed hidden and stored as hashes, and the result is
299
300
  checked with the service's own validator. The file is watched, and changes apply
300
301
  within a second, without a restart or a redeploy:
301
302
 
@@ -711,7 +712,7 @@ git: it holds credentials.
711
712
  | `badmfck hash-password [--deployer]` | hash a password for the monitor or `setup.json` |
712
713
  | `badmfck project-token [--hash]` | generate a project token and its hash, or hash an existing token |
713
714
  | `badmfck project-config` | build a project config for the deployer step by step (plus a `deploy.json` skeleton) |
714
- | `badmfck access-config` | create or edit `access.json`: monitor and documentation users, roles, passwords, allowed addresses |
715
+ | `badmfck access-config [file]` | create or edit `access.json`: monitor and documentation users, roles, passwords, allowed addresses |
715
716
 
716
717
  To pass a config path through npm, use `--`: `npm run deploy -- deploy/prod.json`.
717
718
 
@@ -1 +1,5 @@
1
- export declare function AccessConfigWizard(): Promise<void>;
1
+ export declare function findAccessPaths(root: string): {
2
+ path: string;
3
+ where: string;
4
+ }[];
5
+ export declare function AccessConfigWizard(fileArg?: string): Promise<void>;
@@ -3,6 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.findAccessPaths = findAccessPaths;
6
7
  exports.AccessConfigWizard = AccessConfigWizard;
7
8
  const fs_1 = __importDefault(require("fs"));
8
9
  const path_1 = __importDefault(require("path"));
@@ -20,14 +21,81 @@ const ROLES = {
20
21
  it: "dashboard and logs",
21
22
  analytic: "dashboard only",
22
23
  };
23
- async function AccessConfigWizard() {
24
+ function findAccessPaths(root) {
25
+ const found = [];
26
+ const re = /\baccess\s*[:=]\s*(["'`])([^"'`\n]+?\.json)\1/g;
27
+ let files = 0;
28
+ const visit = (dir, depth) => {
29
+ if (depth > 8 || files > 3000)
30
+ return;
31
+ let entries;
32
+ try {
33
+ entries = fs_1.default.readdirSync(dir, { withFileTypes: true });
34
+ }
35
+ catch {
36
+ return;
37
+ }
38
+ for (const e of entries) {
39
+ if (e.name === "node_modules" || e.name.startsWith("."))
40
+ continue;
41
+ const abs = path_1.default.join(dir, e.name);
42
+ if (e.isDirectory()) {
43
+ visit(abs, depth + 1);
44
+ continue;
45
+ }
46
+ if (!e.isFile() || !/\.(ts|js|cjs|mjs)$/.test(e.name) || e.name.endsWith(".d.ts"))
47
+ continue;
48
+ files++;
49
+ let text;
50
+ try {
51
+ if (fs_1.default.statSync(abs).size > 1024 * 1024)
52
+ continue;
53
+ text = fs_1.default.readFileSync(abs, "utf-8");
54
+ }
55
+ catch {
56
+ continue;
57
+ }
58
+ for (const m of text.matchAll(re)) {
59
+ if (found.some(f => f.path === m[2]))
60
+ continue;
61
+ const line = text.slice(0, m.index).split("\n").length;
62
+ found.push({ path: m[2], where: `${path_1.default.relative(root, abs)}:${line}` });
63
+ }
64
+ }
65
+ };
66
+ for (const sub of ["src", "bin", "dist"]) {
67
+ const dir = path_1.default.join(root, sub);
68
+ if (fs_1.default.existsSync(dir))
69
+ visit(dir, 0);
70
+ }
71
+ return found;
72
+ }
73
+ async function AccessConfigWizard(fileArg) {
24
74
  const { red, green, yellow, dim, bold } = (0, cliCommon_1.palette)();
25
75
  if (!process_1.stdin.isTTY) {
26
76
  console.error(red("✗ access-config needs an interactive terminal — it asks for passwords."));
27
77
  process.exit(1);
28
78
  }
29
79
  console.log(bold("\nAccess file for the monitor and the documentation") + dim(" (the service re-reads it on change)"));
30
- const file = path_1.default.resolve(await (0, cliPrompt_1.ask)("File", "access.json"));
80
+ let file;
81
+ if (fileArg) {
82
+ file = path_1.default.resolve(fileArg);
83
+ }
84
+ else {
85
+ const found = findAccessPaths(process.cwd());
86
+ if (found.length === 1) {
87
+ console.log(dim(` This project's access file (from ${found[0].where}): ${found[0].path}`));
88
+ file = path_1.default.resolve(await (0, cliPrompt_1.ask)("File", found[0].path));
89
+ }
90
+ else if (found.length > 1) {
91
+ found.forEach((f, i) => console.log(` ${i + 1}) ${f.path} ${dim("— " + f.where)}`));
92
+ const pick = await (0, cliPrompt_1.askValid)("File (number or path)", "1", v => /^\d+$/.test(v) && !found[+v - 1] ? "no such number" : null);
93
+ file = path_1.default.resolve(/^\d+$/.test(pick) ? found[+pick - 1].path : pick);
94
+ }
95
+ else {
96
+ file = path_1.default.resolve(await (0, cliPrompt_1.ask)("File", "access.json"));
97
+ }
98
+ }
31
99
  let doc = { monitor: [], documentation: [] };
32
100
  if (fs_1.default.existsSync(file)) {
33
101
  try {
@@ -20,7 +20,7 @@ function usage() {
20
20
  console.error(" badmfck hash-password [--deployer] — hash a password for the monitor (or deployer setup.json)");
21
21
  console.error(" badmfck project-token [--hash] — new project token + its hash (or hash an existing one)");
22
22
  console.error(" badmfck project-config — build a project config for the deployer, step by step");
23
- console.error(" badmfck access-config — create or edit access.json (monitor & documentation users)");
23
+ console.error(" badmfck access-config [file] — create or edit access.json (found in the project's code if omitted)");
24
24
  console.error("");
25
25
  console.error("Config path is optional (auto-discovers deploy/ folder → deploy.json when omitted).");
26
26
  console.error("Via npm, prefix args with -- e.g. npm run deploy -- deploy/custom.json");
@@ -108,11 +108,11 @@ async function main() {
108
108
  break;
109
109
  }
110
110
  case "access-config": {
111
- if (rest.length) {
112
- console.error("access-config takes no arguments — the file is asked interactively.");
111
+ if (rest.length > 1) {
112
+ console.error("access-config takes at most one argument: the path to the access file.");
113
113
  process.exit(1);
114
114
  }
115
- await (0, AccessConfigWizard_1.AccessConfigWizard)();
115
+ await (0, AccessConfigWizard_1.AccessConfigWizard)(rest[0]);
116
116
  break;
117
117
  }
118
118
  case "project-config": {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "badmfck-api-server",
3
- "version": "6.0.7",
3
+ "version": "6.0.8",
4
4
  "description": "Simple API http server based on express",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",