@verax-ai/body 0.5.0 → 0.6.0

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.
Files changed (46) hide show
  1. package/README.md +4 -3
  2. package/dist/approval-signature.d.ts +19 -0
  3. package/dist/approval-signature.js +70 -5
  4. package/dist/approve-cli.js +20 -24
  5. package/dist/cli.d.ts +1 -0
  6. package/dist/cli.js +146 -14
  7. package/dist/config.d.ts +23 -0
  8. package/dist/config.js +41 -0
  9. package/dist/control-signature.d.ts +4 -0
  10. package/dist/control-signature.js +20 -5
  11. package/dist/definitions.d.ts +9 -0
  12. package/dist/definitions.js +53 -0
  13. package/dist/demo.js +15 -12
  14. package/dist/doctor.js +72 -0
  15. package/dist/downstream-pin.d.ts +9 -0
  16. package/dist/downstream-pin.js +273 -0
  17. package/dist/downstream.d.ts +22 -2
  18. package/dist/downstream.js +312 -44
  19. package/dist/heartbeat.d.ts +26 -2
  20. package/dist/heartbeat.js +90 -7
  21. package/dist/held-spend.d.ts +16 -0
  22. package/dist/held-spend.js +20 -0
  23. package/dist/http-io.d.ts +14 -0
  24. package/dist/http-io.js +46 -0
  25. package/dist/init-local.d.ts +11 -0
  26. package/dist/init-local.js +32 -3
  27. package/dist/install.d.ts +5 -0
  28. package/dist/install.js +37 -5
  29. package/dist/main.d.ts +1 -0
  30. package/dist/main.js +34 -5
  31. package/dist/operator-cli.d.ts +49 -1
  32. package/dist/operator-cli.js +132 -7
  33. package/dist/operator-credentials.d.ts +40 -1
  34. package/dist/operator-credentials.js +97 -7
  35. package/dist/operator-door-page.d.ts +3 -0
  36. package/dist/operator-door-page.js +819 -0
  37. package/dist/operator-door.d.ts +22 -0
  38. package/dist/operator-door.js +487 -0
  39. package/dist/operator-handlers.d.ts +28 -0
  40. package/dist/operator-handlers.js +221 -0
  41. package/dist/server.d.ts +2 -0
  42. package/dist/server.js +108 -255
  43. package/dist/verify-cli.js +30 -10
  44. package/dist/webauthn-verify.d.ts +48 -4
  45. package/dist/webauthn-verify.js +183 -25
  46. package/package.json +4 -4
@@ -11,7 +11,32 @@ export type CredentialStore = {
11
11
  };
12
12
  export declare function credentialsPath(stateDir: string): string;
13
13
  export declare function readCredentialsAt(path: string): StoredCredential[];
14
+ export declare function removedPath(stateDir: string): string;
15
+ export type Removed = {
16
+ has: (id: string) => boolean;
17
+ };
18
+ /**
19
+ * The passkeys an administrator has taken away. A removed passkey keeps its
20
+ * row in the credentials file: the approvals it signed are in the ledger, and
21
+ * `verax verify` reads them back under that key. What stops it being used
22
+ * again is a mark in this directory, one file per passkey.
23
+ *
24
+ * A removal creates a file and changes none. Nothing here is read, altered
25
+ * and written back, so two removals at once cannot undo each other, and a
26
+ * counter the body writes a moment later is to another file altogether.
27
+ *
28
+ * Null when something is at the path and cannot be listed. That is not "none
29
+ * removed": see `readCredentials`.
30
+ */
31
+ export declare function readRemoved(stateDir: string): Removed | null;
32
+ /**
33
+ * The passkeys that can be used: those in the file, less the removed. When
34
+ * the list of the removed cannot be read there are none, since any of them
35
+ * might be on it.
36
+ */
14
37
  export declare function readCredentials(stateDir: string): StoredCredential[];
38
+ /** Whether this id was ever enrolled here, removed since or not. */
39
+ export declare function everEnrolled(stateDir: string, id: string): boolean;
15
40
  /**
16
41
  * The file is written only when an operator enrolls, so its presence is the
17
42
  * answer. Asking whether it parses would read a truncated or mistyped file as
@@ -20,5 +45,19 @@ export declare function readCredentials(stateDir: string): StoredCredential[];
20
45
  */
21
46
  export declare function hasRegisteredOperator(stateDir: string): boolean;
22
47
  export declare function findCredential(stateDir: string, id: string): StoredCredential | null;
23
- export declare function saveCredential(stateDir: string, credential: StoredCredential): void;
48
+ /**
49
+ * Adds a passkey. An id the file already holds keeps the row it has, removed
50
+ * or not: approvals in the ledger were signed under that key, and a second
51
+ * enrolment must not put another in its place. False when nothing was written.
52
+ */
53
+ export declare function saveCredential(stateDir: string, credential: StoredCredential): boolean;
54
+ /**
55
+ * Whether the credentials file can be listed from. `readCredentialsAt` gives
56
+ * no rows for a file that does not parse, which is what a caller deciding
57
+ * whom to let in wants, and not what someone reading a list wants to be told.
58
+ */
59
+ export declare function credentialsReadable(stateDir: string): boolean;
24
60
  export declare function updateCounter(stateDir: string, id: string, counter: number): boolean;
61
+ export type Removal = "removed" | "already-removed" | "unknown" | "unreadable";
62
+ /** Takes a passkey out of use. Its row, and so its public key, stays where it was. */
63
+ export declare function removeCredential(stateDir: string, id: string): Removal;
@@ -1,5 +1,6 @@
1
- import { existsSync, readFileSync } from "node:fs";
1
+ import { existsSync, mkdirSync, readdirSync, readFileSync, writeFileSync } from "node:fs";
2
2
  import { join } from "node:path";
3
+ import { OPERATOR_REMOVED_DIR, operatorRemovalMark } from "@verax-ai/proxy";
3
4
  import { writeFileAtomic } from "./atomic-write.js";
4
5
  export const CREDENTIALS_FILE = "operator-credentials.json";
5
6
  export const DEFAULT_OPERATOR_SUB = "operator-1";
@@ -22,9 +23,53 @@ export function readCredentialsAt(path) {
22
23
  return [];
23
24
  }
24
25
  }
25
- export function readCredentials(stateDir) {
26
+ export function removedPath(stateDir) {
27
+ return join(stateDir, OPERATOR_REMOVED_DIR);
28
+ }
29
+ /**
30
+ * The passkeys an administrator has taken away. A removed passkey keeps its
31
+ * row in the credentials file: the approvals it signed are in the ledger, and
32
+ * `verax verify` reads them back under that key. What stops it being used
33
+ * again is a mark in this directory, one file per passkey.
34
+ *
35
+ * A removal creates a file and changes none. Nothing here is read, altered
36
+ * and written back, so two removals at once cannot undo each other, and a
37
+ * counter the body writes a moment later is to another file altogether.
38
+ *
39
+ * Null when something is at the path and cannot be listed. That is not "none
40
+ * removed": see `readCredentials`.
41
+ */
42
+ export function readRemoved(stateDir) {
43
+ let marks;
44
+ try {
45
+ marks = new Set(readdirSync(removedPath(stateDir)));
46
+ }
47
+ catch (err) {
48
+ if (err.code !== "ENOENT")
49
+ return null;
50
+ marks = new Set();
51
+ }
52
+ return { has: (id) => marks.has(operatorRemovalMark(id)) };
53
+ }
54
+ /** Every row in the file, the removed among them: for rewriting the file, and for asking whether an id was ever enrolled. */
55
+ function everyCredential(stateDir) {
26
56
  return readCredentialsAt(credentialsPath(stateDir));
27
57
  }
58
+ /**
59
+ * The passkeys that can be used: those in the file, less the removed. When
60
+ * the list of the removed cannot be read there are none, since any of them
61
+ * might be on it.
62
+ */
63
+ export function readCredentials(stateDir) {
64
+ const removed = readRemoved(stateDir);
65
+ if (!removed)
66
+ return [];
67
+ return everyCredential(stateDir).filter((c) => !removed.has(c.id));
68
+ }
69
+ /** Whether this id was ever enrolled here, removed since or not. */
70
+ export function everEnrolled(stateDir, id) {
71
+ return everyCredential(stateDir).some((c) => c.id === id);
72
+ }
28
73
  /**
29
74
  * The file is written only when an operator enrolls, so its presence is the
30
75
  * answer. Asking whether it parses would read a truncated or mistyped file as
@@ -37,14 +82,37 @@ export function hasRegisteredOperator(stateDir) {
37
82
  export function findCredential(stateDir, id) {
38
83
  return readCredentials(stateDir).find((c) => c.id === id) ?? null;
39
84
  }
85
+ /**
86
+ * Adds a passkey. An id the file already holds keeps the row it has, removed
87
+ * or not: approvals in the ledger were signed under that key, and a second
88
+ * enrolment must not put another in its place. False when nothing was written.
89
+ */
40
90
  export function saveCredential(stateDir, credential) {
41
- const credentials = readCredentials(stateDir);
42
- const next = credentials.filter((c) => c.id !== credential.id);
43
- next.push(credential);
44
- writeFileAtomic(credentialsPath(stateDir), `${JSON.stringify({ credentials: next })}\n`);
91
+ const credentials = everyCredential(stateDir);
92
+ if (credentials.some((c) => c.id === credential.id))
93
+ return false;
94
+ credentials.push(credential);
95
+ writeFileAtomic(credentialsPath(stateDir), `${JSON.stringify({ credentials })}\n`);
96
+ return true;
97
+ }
98
+ /**
99
+ * Whether the credentials file can be listed from. `readCredentialsAt` gives
100
+ * no rows for a file that does not parse, which is what a caller deciding
101
+ * whom to let in wants, and not what someone reading a list wants to be told.
102
+ */
103
+ export function credentialsReadable(stateDir) {
104
+ const path = credentialsPath(stateDir);
105
+ if (!existsSync(path))
106
+ return true;
107
+ try {
108
+ return Array.isArray(JSON.parse(readFileSync(path, "utf8")).credentials);
109
+ }
110
+ catch {
111
+ return false;
112
+ }
45
113
  }
46
114
  export function updateCounter(stateDir, id, counter) {
47
- const credentials = readCredentials(stateDir);
115
+ const credentials = everyCredential(stateDir);
48
116
  const row = credentials.find((c) => c.id === id);
49
117
  if (!row)
50
118
  return false;
@@ -56,3 +124,25 @@ export function updateCounter(stateDir, id, counter) {
56
124
  writeFileAtomic(credentialsPath(stateDir), `${JSON.stringify({ credentials })}\n`);
57
125
  return true;
58
126
  }
127
+ /** Takes a passkey out of use. Its row, and so its public key, stays where it was. */
128
+ export function removeCredential(stateDir, id) {
129
+ const removed = readRemoved(stateDir);
130
+ if (!removed)
131
+ return "unreadable";
132
+ if (!everEnrolled(stateDir, id))
133
+ return "unknown";
134
+ if (removed.has(id))
135
+ return "already-removed";
136
+ mkdirSync(removedPath(stateDir), { recursive: true, mode: 0o700 });
137
+ try {
138
+ // The name is the mark. The id inside is for whoever reads the directory.
139
+ writeFileSync(join(removedPath(stateDir), operatorRemovalMark(id)), `${id}\n`, { encoding: "utf8", flag: "wx", mode: 0o600 });
140
+ }
141
+ catch (err) {
142
+ // Another removal of the same passkey got there between the look and the write.
143
+ if (err.code === "EEXIST")
144
+ return "already-removed";
145
+ throw err;
146
+ }
147
+ return "removed";
148
+ }
@@ -0,0 +1,3 @@
1
+ export declare const DOOR_HTML: string;
2
+ export declare const DOOR_CSS: string;
3
+ export declare const DOOR_JS: string;