openmsg 0.1.0 → 0.3.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.
- package/README.md +76 -13
- package/package.json +3 -3
- package/src/adapters/claude.mjs +2 -2
- package/src/adapters/codex.mjs +83 -5
- package/src/adapters/opencode.mjs +2 -2
- package/src/canonical.mjs +30 -0
- package/src/cli.mjs +702 -23
- package/src/deliver.mjs +32 -0
- package/src/device.mjs +211 -0
- package/src/directory.mjs +194 -0
- package/src/dirsync.mjs +224 -0
- package/src/envelope.mjs +51 -8
- package/src/gateway.mjs +556 -0
- package/src/identity.mjs +174 -0
- package/src/inbound.mjs +89 -0
- package/src/invite.mjs +102 -0
- package/src/limits.mjs +63 -0
- package/src/mailbox.mjs +13 -3
- package/src/outbox.mjs +47 -0
- package/src/permissions.mjs +56 -0
- package/src/published.mjs +77 -0
- package/src/registry.mjs +17 -0
- package/src/relay.mjs +446 -0
- package/src/relaylink.mjs +248 -0
- package/src/remote.mjs +352 -0
- package/src/seal.mjs +70 -0
- package/src/selfregistry.mjs +8 -2
- package/src/settings.mjs +31 -0
- package/src/wsframe.mjs +288 -0
- package/docs/spec/openmsg-0.1.md +0 -281
- package/docs/spec/openmsg-0.2-draft.md +0 -259
package/src/deliver.mjs
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// One place that hands a message to the adapter of a vendor. The command line
|
|
2
|
+
// of 0.1 and the gateway of 0.2 both come here, so the last step is the same
|
|
3
|
+
// for a message from this machine and for a message from another person.
|
|
4
|
+
import * as claude from "./adapters/claude.mjs";
|
|
5
|
+
import * as opencode from "./adapters/opencode.mjs";
|
|
6
|
+
import * as codex from "./adapters/codex.mjs";
|
|
7
|
+
import { layoutOf, compose, bodyRange } from "./envelope.mjs";
|
|
8
|
+
|
|
9
|
+
// `parts` is the layout of the text that the model reads: the frame and the text
|
|
10
|
+
// of the sender. Each adapter shows the two apart in its own way:
|
|
11
|
+
// Claude Code shows the color of an escape sequence, so the frame is gray.
|
|
12
|
+
// Codex removes each escape sequence, but it draws a byte range in its accent
|
|
13
|
+
// color. The text of the sender gets that color, and the model never sees it.
|
|
14
|
+
// OpenCode gets the plain text. Its display is not tested.
|
|
15
|
+
// `text` alone, with no layout, goes to the adapter as it is.
|
|
16
|
+
export async function deliverLocal(agent, message, { text = null, parts = null } = {}) {
|
|
17
|
+
if (!parts && !text) parts = layoutOf(message);
|
|
18
|
+
const plain = parts ? compose(parts) : text;
|
|
19
|
+
if (agent.vendor === "claude") {
|
|
20
|
+
// The token belongs to the session that runs this command. Another session
|
|
21
|
+
// reads its socket without one.
|
|
22
|
+
const token = agent.transport?.path === process.env.CLAUDE_CODE_MESSAGING_SOCKET
|
|
23
|
+
? process.env.CLAUDE_CODE_MESSAGING_TOKEN
|
|
24
|
+
: undefined;
|
|
25
|
+
return claude.deliver(agent, message, { token, text: parts ? compose(parts, { ansi: true }) : text });
|
|
26
|
+
}
|
|
27
|
+
if (agent.vendor === "opencode") return opencode.deliver(agent, message, { text: plain });
|
|
28
|
+
if (agent.vendor === "codex") return codex.deliver(agent, message, { text: plain, highlight: parts ? bodyRange(parts) : null });
|
|
29
|
+
// An agent with no push entry point, such as Gemini CLI or Cursor CLI, reads
|
|
30
|
+
// its mailbox at the end of a turn. The message waits there.
|
|
31
|
+
return { delivered: false, transport: "mailbox" };
|
|
32
|
+
}
|
package/src/device.mjs
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
// A device key, and the delegation that an owner writes for it.
|
|
2
|
+
//
|
|
3
|
+
// Without this file, the identity of an owner is one private key, and a
|
|
4
|
+
// second machine needs a copy of it. A copy on two machines means that one
|
|
5
|
+
// stolen machine ends the identity: the owner revokes the key, every member
|
|
6
|
+
// invites the owner again, and no old message opens any more.
|
|
7
|
+
//
|
|
8
|
+
// With a delegation, each machine holds its own two keys, and the owner key
|
|
9
|
+
// signs a short record that says "these keys belong to me, for this project,
|
|
10
|
+
// until this date". One stolen machine costs one delegation.
|
|
11
|
+
//
|
|
12
|
+
// The owner key never leaves the machine that made it. A second machine asks
|
|
13
|
+
// with a request, and the first machine answers with a delegation.
|
|
14
|
+
import fs from "node:fs";
|
|
15
|
+
import path from "node:path";
|
|
16
|
+
import crypto from "node:crypto";
|
|
17
|
+
import { canonical, canonicalBytes } from "./canonical.mjs";
|
|
18
|
+
import * as identity from "./identity.mjs";
|
|
19
|
+
|
|
20
|
+
export const VERSION = 2;
|
|
21
|
+
export const DEFAULT_DAYS = 365;
|
|
22
|
+
const REQUEST = "openmsg-device-request-v2";
|
|
23
|
+
const GRANT = "openmsg-delegation-v2";
|
|
24
|
+
|
|
25
|
+
export function paths() {
|
|
26
|
+
const dir = path.join(identity.home(), "keys");
|
|
27
|
+
return {
|
|
28
|
+
dir,
|
|
29
|
+
signing: path.join(dir, "device-signing.key"),
|
|
30
|
+
encryption: path.join(dir, "device-encryption.key"),
|
|
31
|
+
record: path.join(dir, "device.json"),
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function exists() {
|
|
36
|
+
return fs.existsSync(paths().record);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// The device id comes from the two device keys, as the owner id comes from
|
|
40
|
+
// the two owner keys. A record that claims another id is false.
|
|
41
|
+
export function deviceIdOf(keys) {
|
|
42
|
+
const text = canonical({ encryption: keys.encryption, signing: keys.signing });
|
|
43
|
+
return `d_${crypto.createHash("sha256").update(`openmsg-device-v2\n${text}`).digest("hex").slice(0, 8)}`;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function create({ label, force = false } = {}) {
|
|
47
|
+
const p = paths();
|
|
48
|
+
if (exists() && !force) throw new Error(`this machine already has a device key in ${p.dir}. Use --force to replace it.`);
|
|
49
|
+
fs.mkdirSync(p.dir, { recursive: true, mode: 0o700 });
|
|
50
|
+
const signing = crypto.generateKeyPairSync("ed25519");
|
|
51
|
+
const encryption = crypto.generateKeyPairSync("x25519");
|
|
52
|
+
for (const [file, key] of [[p.signing, signing.privateKey], [p.encryption, encryption.privateKey]]) {
|
|
53
|
+
fs.writeFileSync(file, key.export({ format: "pem", type: "pkcs8" }), { mode: 0o600 });
|
|
54
|
+
fs.chmodSync(file, 0o600);
|
|
55
|
+
}
|
|
56
|
+
const keys = {
|
|
57
|
+
signing: { alg: "Ed25519", x: signing.publicKey.export({ format: "jwk" }).x },
|
|
58
|
+
encryption: { alg: "X25519", x: encryption.publicKey.export({ format: "jwk" }).x },
|
|
59
|
+
};
|
|
60
|
+
const record = {
|
|
61
|
+
version: VERSION,
|
|
62
|
+
deviceId: deviceIdOf(keys),
|
|
63
|
+
label: label ?? "device",
|
|
64
|
+
keys,
|
|
65
|
+
createdAt: new Date().toISOString(),
|
|
66
|
+
delegation: null,
|
|
67
|
+
};
|
|
68
|
+
fs.writeFileSync(p.record, JSON.stringify(record, null, 1) + "\n");
|
|
69
|
+
return record;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function load() {
|
|
73
|
+
if (!exists()) throw new Error(`no device key on this machine. Run: openmsg device create`);
|
|
74
|
+
const record = JSON.parse(fs.readFileSync(paths().record, "utf8"));
|
|
75
|
+
if (record.deviceId !== deviceIdOf(record.keys)) {
|
|
76
|
+
throw new Error(`the device record claims ${record.deviceId}, and its keys give ${deviceIdOf(record.keys)}`);
|
|
77
|
+
}
|
|
78
|
+
return record;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function signingKey() {
|
|
82
|
+
return crypto.createPrivateKey(fs.readFileSync(paths().signing, "utf8"));
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export function encryptionKey() {
|
|
86
|
+
return crypto.createPrivateKey(fs.readFileSync(paths().encryption, "utf8"));
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function sign(bytes) {
|
|
90
|
+
return crypto.sign(null, Buffer.from(bytes), signingKey());
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// The delegation that this machine holds, or null. A message carries it, so
|
|
94
|
+
// the receiver verifies the chain without asking anybody.
|
|
95
|
+
export function delegation() {
|
|
96
|
+
return exists() ? (load().delegation ?? null) : null;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function saveDelegation(record) {
|
|
100
|
+
const p = paths();
|
|
101
|
+
const mine = load();
|
|
102
|
+
if (record.device !== mine.deviceId) {
|
|
103
|
+
throw new Error(`that delegation is for ${record.device}, and this machine is ${mine.deviceId}`);
|
|
104
|
+
}
|
|
105
|
+
fs.writeFileSync(p.record, JSON.stringify({ ...mine, delegation: record }, null, 1) + "\n");
|
|
106
|
+
return record;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// --- signing, for a machine or for an owner --------------------------------
|
|
110
|
+
|
|
111
|
+
// Everything that an owner signs goes through here: a message, a request for
|
|
112
|
+
// routing data, an answer, a receipt, and the hello to a relay. A machine
|
|
113
|
+
// with a delegation signs with its own key, and a machine that holds the
|
|
114
|
+
// owner key signs with that one.
|
|
115
|
+
export function signer({ project = null } = {}) {
|
|
116
|
+
const me = identity.load();
|
|
117
|
+
const held = exists() ? delegation() : null;
|
|
118
|
+
if (!held) {
|
|
119
|
+
return { owner: me, by: me.ownerId, device: null, delegation: null, sign: (bytes) => identity.sign(bytes) };
|
|
120
|
+
}
|
|
121
|
+
const why = verify(held, me, { project });
|
|
122
|
+
if (why) throw new Error(`this machine cannot sign: ${why}`);
|
|
123
|
+
return { owner: me, by: me.ownerId, device: held.device, delegation: held, sign };
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// It gives null when the object is good, and a sentence when it is not. The
|
|
127
|
+
// owner record comes from the directory of the reader, and never from the
|
|
128
|
+
// object.
|
|
129
|
+
export function verifySignedObject(object, ownerRecord, { project = null, now = Date.now() } = {}) {
|
|
130
|
+
const { signature, ...body } = object ?? {};
|
|
131
|
+
if (signature?.alg !== "Ed25519" || !signature.value) return "the object carries no signature";
|
|
132
|
+
if (signature.by !== ownerRecord.ownerId) return `the signature is by ${signature.by}`;
|
|
133
|
+
const value = Buffer.from(signature.value, "base64url");
|
|
134
|
+
if (!signature.device) {
|
|
135
|
+
return identity.verifyWith(ownerRecord, canonicalBytes(body), value) ? null : "the signature does not match the owner";
|
|
136
|
+
}
|
|
137
|
+
const held = body.delegation;
|
|
138
|
+
if (!held) return "the signature names a machine, and the object carries no delegation";
|
|
139
|
+
if (held.device !== signature.device) return `the signature names ${signature.device}, and the delegation names ${held.device}`;
|
|
140
|
+
const why = verify(held, ownerRecord, { project, now });
|
|
141
|
+
if (why) return why;
|
|
142
|
+
return identity.verifyWith({ keys: held.keys }, canonicalBytes(body), value)
|
|
143
|
+
? null
|
|
144
|
+
: "the signature does not match the machine";
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// --- the two tokens --------------------------------------------------------
|
|
148
|
+
|
|
149
|
+
// A machine asks: "these are my public keys, and this is my name."
|
|
150
|
+
export function request() {
|
|
151
|
+
const mine = load();
|
|
152
|
+
const body = { kind: "device-request", version: VERSION, device: mine.deviceId, label: mine.label, keys: mine.keys };
|
|
153
|
+
return `${REQUEST}.${canonicalBytes(body).toString("base64url")}`;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export function readRequest(token) {
|
|
157
|
+
const parts = String(token ?? "").trim().split(".");
|
|
158
|
+
if (parts.length !== 2 || parts[0] !== REQUEST) throw new Error("this text is not a device request");
|
|
159
|
+
const body = JSON.parse(Buffer.from(parts[1], "base64url").toString("utf8"));
|
|
160
|
+
if (body.device !== deviceIdOf(body.keys)) throw new Error("the request claims a device id that its keys do not give");
|
|
161
|
+
return body;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// The owner answers. `projects` names the projects that this device may sign
|
|
165
|
+
// for, and "all" means every project of this owner.
|
|
166
|
+
export function grant(ask, { days = DEFAULT_DAYS, projects = "all" } = {}) {
|
|
167
|
+
const me = identity.load();
|
|
168
|
+
const body = {
|
|
169
|
+
kind: "delegation",
|
|
170
|
+
version: VERSION,
|
|
171
|
+
owner: me.ownerId,
|
|
172
|
+
device: ask.device,
|
|
173
|
+
label: ask.label,
|
|
174
|
+
keys: ask.keys,
|
|
175
|
+
projects,
|
|
176
|
+
createdAt: new Date().toISOString(),
|
|
177
|
+
expiresAt: new Date(Date.now() + days * 24 * 3600 * 1000).toISOString(),
|
|
178
|
+
};
|
|
179
|
+
const record = {
|
|
180
|
+
...body,
|
|
181
|
+
signature: { alg: "Ed25519", by: me.ownerId, value: identity.sign(canonicalBytes(body)).toString("base64url") },
|
|
182
|
+
};
|
|
183
|
+
return { record, token: `${GRANT}.${Buffer.from(JSON.stringify(record)).toString("base64url")}` };
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
export function readGrant(token) {
|
|
187
|
+
const parts = String(token ?? "").trim().split(".");
|
|
188
|
+
if (parts.length !== 2 || parts[0] !== GRANT) throw new Error("this text is not a delegation");
|
|
189
|
+
return JSON.parse(Buffer.from(parts[1], "base64url").toString("utf8"));
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// The receiver verifies the delegation against the owner record that its own
|
|
193
|
+
// directory holds. A delegation never carries the owner key, because a
|
|
194
|
+
// message must not carry the key that proves it.
|
|
195
|
+
export function verify(record, ownerRecord, { project = null, now = Date.now() } = {}) {
|
|
196
|
+
if (record?.kind !== "delegation" || record.version !== VERSION) return "the delegation has an unknown shape";
|
|
197
|
+
if (record.owner !== ownerRecord.ownerId) return `the delegation names the owner ${record.owner}`;
|
|
198
|
+
if (record.device !== deviceIdOf(record.keys)) return "the delegation claims a device id that its keys do not give";
|
|
199
|
+
if (record.signature?.by !== record.owner) return "the signature of the delegation is by another owner";
|
|
200
|
+
const { signature, ...body } = record;
|
|
201
|
+
if (!identity.verifyWith(ownerRecord, canonicalBytes(body), Buffer.from(signature.value, "base64url"))) {
|
|
202
|
+
return "the owner did not sign this delegation";
|
|
203
|
+
}
|
|
204
|
+
if (!(Date.parse(record.expiresAt) > now)) return `the delegation ended at ${record.expiresAt}`;
|
|
205
|
+
// The scope of section 5.5: a device signs for the projects that the owner
|
|
206
|
+
// named, and for no other one.
|
|
207
|
+
if (project && record.projects !== "all" && !(record.projects ?? []).includes(project)) {
|
|
208
|
+
return `the delegation does not cover the project ${project}`;
|
|
209
|
+
}
|
|
210
|
+
return null;
|
|
211
|
+
}
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
// The directory holds the members of each project: the owner id, the public
|
|
2
|
+
// keys, and the record of a key that the team revoked.
|
|
3
|
+
//
|
|
4
|
+
// A commit right is not a membership. Only an invitation that a person
|
|
5
|
+
// accepted, after a comparison of the fingerprint, writes a member here.
|
|
6
|
+
//
|
|
7
|
+
// The owner id comes from the keys, so one member never changes its keys. A
|
|
8
|
+
// new key is a new owner id, and the old owner id goes to the revoked list.
|
|
9
|
+
import fs from "node:fs";
|
|
10
|
+
import path from "node:path";
|
|
11
|
+
import { home, checkRecord, fingerprint } from "./identity.mjs";
|
|
12
|
+
|
|
13
|
+
export const VERSION = 2;
|
|
14
|
+
|
|
15
|
+
function file() {
|
|
16
|
+
return path.join(home(), "directory.json");
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function read() {
|
|
20
|
+
try {
|
|
21
|
+
const data = JSON.parse(fs.readFileSync(file(), "utf8"));
|
|
22
|
+
if (!data.projects) data.projects = {};
|
|
23
|
+
return data;
|
|
24
|
+
} catch {
|
|
25
|
+
return { version: VERSION, projects: {} };
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// One write, or none. A reader never sees half of a directory.
|
|
30
|
+
function save(data) {
|
|
31
|
+
const target = file();
|
|
32
|
+
fs.mkdirSync(path.dirname(target), { recursive: true });
|
|
33
|
+
const tmp = `${target}.${process.pid}.tmp`;
|
|
34
|
+
fs.writeFileSync(tmp, JSON.stringify(data, null, 1) + "\n");
|
|
35
|
+
fs.renameSync(tmp, target);
|
|
36
|
+
return target;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function projects() {
|
|
40
|
+
return Object.values(read().projects);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function project(id) {
|
|
44
|
+
return read().projects[id] ?? null;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// `put` writes one member of one project. It adds the project when the project
|
|
48
|
+
// is new. The source says how the member arrived: an invitation, or this owner.
|
|
49
|
+
export function put({ id, label }, record, { source = "invite", endpoint = null } = {}) {
|
|
50
|
+
checkRecord(record);
|
|
51
|
+
const data = read();
|
|
52
|
+
const entry = data.projects[id] ?? { id, label: label ?? id, members: {}, revoked: [], joinedAt: now() };
|
|
53
|
+
if (label) entry.label = label;
|
|
54
|
+
const before = entry.members[record.ownerId];
|
|
55
|
+
entry.members[record.ownerId] = {
|
|
56
|
+
ownerId: record.ownerId,
|
|
57
|
+
label: record.label,
|
|
58
|
+
keys: record.keys,
|
|
59
|
+
fingerprint: fingerprint(record),
|
|
60
|
+
// Where the gateway of that owner answers. A member with no endpoint is a
|
|
61
|
+
// member that this machine cannot reach yet.
|
|
62
|
+
endpoint: endpoint ?? before?.endpoint ?? null,
|
|
63
|
+
source,
|
|
64
|
+
addedAt: before?.addedAt ?? now(),
|
|
65
|
+
};
|
|
66
|
+
data.projects[id] = entry;
|
|
67
|
+
save(data);
|
|
68
|
+
return entry.members[record.ownerId];
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// A revoked owner is not a member. The caller therefore needs no second check
|
|
72
|
+
// before it trusts the answer.
|
|
73
|
+
export function member(projectId, ownerId) {
|
|
74
|
+
const entry = project(projectId);
|
|
75
|
+
if (!entry) return null;
|
|
76
|
+
if (isRevoked(projectId, ownerId)) return null;
|
|
77
|
+
return entry.members[ownerId] ?? null;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function members(projectId) {
|
|
81
|
+
const entry = project(projectId);
|
|
82
|
+
if (!entry) return [];
|
|
83
|
+
return Object.values(entry.members).filter((m) => !isRevoked(projectId, m.ownerId));
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function setEndpoint(projectId, ownerId, endpoint) {
|
|
87
|
+
const data = read();
|
|
88
|
+
const record = data.projects[projectId]?.members?.[ownerId];
|
|
89
|
+
if (!record) throw new Error(`no member ${ownerId} in the project ${projectId}`);
|
|
90
|
+
record.endpoint = endpoint;
|
|
91
|
+
save(data);
|
|
92
|
+
return record;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// The last routing data that this owner read from that member. A gateway that
|
|
96
|
+
// is offline cannot answer, and the epoch in the cache still stops a message
|
|
97
|
+
// that would land in a session that took the name later.
|
|
98
|
+
export function setRouting(projectId, ownerId, rows) {
|
|
99
|
+
const data = read();
|
|
100
|
+
const record = data.projects[projectId]?.members?.[ownerId];
|
|
101
|
+
if (!record) return null;
|
|
102
|
+
// Each row carries the encryption key of the machine that answered, under
|
|
103
|
+
// `sealTo`. A message for a session is sealed for the machine that holds
|
|
104
|
+
// that session, and not for the owner key.
|
|
105
|
+
record.routing = { at: now(), rows };
|
|
106
|
+
save(data);
|
|
107
|
+
return record.routing;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export function routingOf(projectId, ownerId) {
|
|
111
|
+
return project(projectId)?.members?.[ownerId]?.routing ?? null;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// A machine of a member, and not the member. One stolen machine costs one
|
|
115
|
+
// delegation, and the identity of that person holds.
|
|
116
|
+
export function revokeDevice(projectId, deviceId, reason = null, { owner = null } = {}) {
|
|
117
|
+
const data = read();
|
|
118
|
+
const entry = data.projects[projectId];
|
|
119
|
+
if (!entry) throw new Error(`no project ${projectId} in the directory`);
|
|
120
|
+
entry.revokedDevices = entry.revokedDevices ?? [];
|
|
121
|
+
if (entry.revokedDevices.some((r) => r.device === deviceId)) return entry.revokedDevices.find((r) => r.device === deviceId);
|
|
122
|
+
// `owner` names the person whose machine it is. That person tells the
|
|
123
|
+
// other members, and no other person speaks for those machines.
|
|
124
|
+
const record = { device: deviceId, owner, at: now(), reason };
|
|
125
|
+
entry.revokedDevices.push(record);
|
|
126
|
+
save(data);
|
|
127
|
+
return record;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// An owner that arrived from the relay, and that this person did not accept
|
|
131
|
+
// yet. A record never adds a member, and this list is where one waits.
|
|
132
|
+
export function putPending(projectId, { ownerId, label, keys, fingerprint }) {
|
|
133
|
+
const data = read();
|
|
134
|
+
const entry = data.projects[projectId];
|
|
135
|
+
if (!entry) return null;
|
|
136
|
+
entry.pending = entry.pending ?? {};
|
|
137
|
+
entry.pending[ownerId] = { ownerId, label, keys, fingerprint, at: entry.pending[ownerId]?.at ?? now() };
|
|
138
|
+
save(data);
|
|
139
|
+
return entry.pending[ownerId];
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export function pending(projectId = null) {
|
|
143
|
+
const out = [];
|
|
144
|
+
for (const entry of projects()) {
|
|
145
|
+
if (projectId && entry.id !== projectId) continue;
|
|
146
|
+
for (const row of Object.values(entry.pending ?? {})) out.push({ project: entry.id, ...row });
|
|
147
|
+
}
|
|
148
|
+
return out;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export function dropPending(projectId, ownerId) {
|
|
152
|
+
const data = read();
|
|
153
|
+
const entry = data.projects[projectId];
|
|
154
|
+
if (!entry?.pending?.[ownerId]) return false;
|
|
155
|
+
delete entry.pending[ownerId];
|
|
156
|
+
save(data);
|
|
157
|
+
return true;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export function isDeviceRevoked(projectId, deviceId) {
|
|
161
|
+
return Boolean(project(projectId)?.revokedDevices?.some((r) => r.device === deviceId));
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
export function revokedDevices(projectId) {
|
|
165
|
+
return project(projectId)?.revokedDevices ?? [];
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export function isRevoked(projectId, ownerId) {
|
|
169
|
+
const entry = project(projectId);
|
|
170
|
+
return Boolean(entry?.revoked?.some((r) => r.ownerId === ownerId));
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
export function revoked(projectId) {
|
|
174
|
+
return project(projectId)?.revoked ?? [];
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// The revoked list keeps the keys. A message that arrives later, with a key
|
|
178
|
+
// that the team revoked, therefore still finds its record here.
|
|
179
|
+
export function revoke(projectId, ownerId, reason = null) {
|
|
180
|
+
const data = read();
|
|
181
|
+
const entry = data.projects[projectId];
|
|
182
|
+
if (!entry) throw new Error(`no project ${projectId} in the directory`);
|
|
183
|
+
const known = entry.members[ownerId];
|
|
184
|
+
if (!known) throw new Error(`no member ${ownerId} in the project ${projectId}`);
|
|
185
|
+
if (entry.revoked.some((r) => r.ownerId === ownerId)) return entry.revoked.find((r) => r.ownerId === ownerId);
|
|
186
|
+
const record = { ownerId, label: known.label, keys: known.keys, at: now(), reason };
|
|
187
|
+
entry.revoked.push(record);
|
|
188
|
+
save(data);
|
|
189
|
+
return record;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
function now() {
|
|
193
|
+
return new Date().toISOString();
|
|
194
|
+
}
|
package/src/dirsync.mjs
ADDED
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
// The directory on the relay, from section 12.1 of the spec.
|
|
2
|
+
//
|
|
3
|
+
// Three records, and an owner signs each one about itself:
|
|
4
|
+
//
|
|
5
|
+
// - `self`: "this is my public record, and my gateway answers here".
|
|
6
|
+
// - `roster`: "these are the members that I accepted in this project, and
|
|
7
|
+
// this is what I let each one do". The other machines of that same owner
|
|
8
|
+
// apply it, because it is that person trusting that person. Nobody else
|
|
9
|
+
// applies it. A standing permission belongs to the person, and not to one
|
|
10
|
+
// machine, so it travels here too.
|
|
11
|
+
// - `revoke-device`: "this machine of mine is gone". Everybody applies it,
|
|
12
|
+
// because only an owner says which machines belong to that owner.
|
|
13
|
+
//
|
|
14
|
+
// A record carries the public keys of its owner, and the owner id comes from
|
|
15
|
+
// those keys. A record is therefore self-verifying, and the relay stores it
|
|
16
|
+
// without a directory of its own.
|
|
17
|
+
//
|
|
18
|
+
// A record never adds a member. A person joins a project through an
|
|
19
|
+
// invitation and a fingerprint, as rule 3.3 says. An owner that this machine
|
|
20
|
+
// does not know waits in a list, with its fingerprint, for the person to
|
|
21
|
+
// accept it.
|
|
22
|
+
import { canonicalBytes } from "./canonical.mjs";
|
|
23
|
+
import * as identity from "./identity.mjs";
|
|
24
|
+
import * as device from "./device.mjs";
|
|
25
|
+
import * as directory from "./directory.mjs";
|
|
26
|
+
import * as permissions from "./permissions.mjs";
|
|
27
|
+
import { settings } from "./settings.mjs";
|
|
28
|
+
|
|
29
|
+
export const KINDS = ["self", "roster", "revoke-device"];
|
|
30
|
+
export const MAX_RECORD_BYTES = 16 * 1024;
|
|
31
|
+
|
|
32
|
+
function sign(body) {
|
|
33
|
+
const signer = device.signer({ project: body.project });
|
|
34
|
+
const full = signer.delegation ? { ...body, delegation: signer.delegation } : body;
|
|
35
|
+
return {
|
|
36
|
+
...full,
|
|
37
|
+
signature: {
|
|
38
|
+
alg: "Ed25519",
|
|
39
|
+
by: signer.by,
|
|
40
|
+
device: signer.device,
|
|
41
|
+
value: signer.sign(canonicalBytes(full)).toString("base64url"),
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function selfRecord(project) {
|
|
47
|
+
const me = identity.load();
|
|
48
|
+
return sign({
|
|
49
|
+
kind: "self",
|
|
50
|
+
version: 2,
|
|
51
|
+
project,
|
|
52
|
+
owner: me.ownerId,
|
|
53
|
+
label: me.label,
|
|
54
|
+
keys: me.keys,
|
|
55
|
+
endpoint: settings().endpoint ?? null,
|
|
56
|
+
at: new Date().toISOString(),
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// The members that this owner accepted, with the fingerprint that the person
|
|
61
|
+
// compared. Another machine of the same owner takes this list as it is.
|
|
62
|
+
export function rosterRecord(project) {
|
|
63
|
+
const me = identity.load();
|
|
64
|
+
const members = directory
|
|
65
|
+
.members(project)
|
|
66
|
+
.filter((m) => m.ownerId !== me.ownerId)
|
|
67
|
+
.map((m) => ({
|
|
68
|
+
ownerId: m.ownerId,
|
|
69
|
+
label: m.label,
|
|
70
|
+
keys: m.keys,
|
|
71
|
+
fingerprint: m.fingerprint,
|
|
72
|
+
endpoint: m.endpoint ?? null,
|
|
73
|
+
}));
|
|
74
|
+
const standing = permissions
|
|
75
|
+
.list()
|
|
76
|
+
.filter((row) => row.project === project)
|
|
77
|
+
.map((row) => ({ owner: row.owner, value: row.value }));
|
|
78
|
+
return sign({
|
|
79
|
+
kind: "roster",
|
|
80
|
+
version: 2,
|
|
81
|
+
project,
|
|
82
|
+
// accept, hold, or refuse, for each identity. The owner decided this
|
|
83
|
+
// one time, and every machine of that owner holds the same answer.
|
|
84
|
+
permissions: standing,
|
|
85
|
+
// The short name that the team chose. A machine that learns a project
|
|
86
|
+
// from this record shows that name, and not the opaque id.
|
|
87
|
+
label: directory.project(project)?.label ?? null,
|
|
88
|
+
owner: me.ownerId,
|
|
89
|
+
keys: me.keys,
|
|
90
|
+
members,
|
|
91
|
+
at: new Date().toISOString(),
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function revokeDeviceRecord(project, deviceId, reason = null) {
|
|
96
|
+
const me = identity.load();
|
|
97
|
+
return sign({
|
|
98
|
+
kind: "revoke-device",
|
|
99
|
+
version: 2,
|
|
100
|
+
project,
|
|
101
|
+
owner: me.ownerId,
|
|
102
|
+
keys: me.keys,
|
|
103
|
+
device: deviceId,
|
|
104
|
+
reason,
|
|
105
|
+
at: new Date().toISOString(),
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// It gives null when the record is good, and a sentence when it is not.
|
|
110
|
+
export function verifyRecord(record) {
|
|
111
|
+
if (!KINDS.includes(record?.kind)) return `unknown record "${record?.kind}"`;
|
|
112
|
+
if (record.version !== 2) return "unknown version";
|
|
113
|
+
if (!record.project || !record.owner || !record.keys?.signing?.x) return "the record is not complete";
|
|
114
|
+
if (record.owner !== identity.ownerIdOf(record.keys)) return "the record claims an owner that its keys do not give";
|
|
115
|
+
if (!Date.parse(record.at)) return "the record has no time";
|
|
116
|
+
return device.verifySignedObject(record, { ownerId: record.owner, keys: record.keys }, { project: record.project });
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// The reader applies what it may apply, and it lists the rest.
|
|
120
|
+
export function apply(project, records, { now = Date.now() } = {}) {
|
|
121
|
+
const me = identity.load();
|
|
122
|
+
const out = { updated: [], added: [], revoked: [], pending: [], refused: [] };
|
|
123
|
+
for (const record of records) {
|
|
124
|
+
const why = verifyRecord(record);
|
|
125
|
+
if (why) {
|
|
126
|
+
out.refused.push({ owner: record?.owner ?? "unknown", why });
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
if (record.project !== project) {
|
|
130
|
+
out.refused.push({ owner: record.owner, why: "that record belongs to another project" });
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
if (record.kind === "revoke-device") {
|
|
134
|
+
// Only an owner says which machines belong to that owner.
|
|
135
|
+
if (!directory.isDeviceRevoked(project, record.device)) {
|
|
136
|
+
directory.revokeDevice(project, record.device, record.reason ?? `${record.owner} revoked it`, { owner: record.owner });
|
|
137
|
+
out.revoked.push({ owner: record.owner, device: record.device });
|
|
138
|
+
}
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
if (record.kind === "self") {
|
|
142
|
+
if (record.owner === me.ownerId) continue;
|
|
143
|
+
const known = directory.member(project, record.owner);
|
|
144
|
+
if (!known) {
|
|
145
|
+
const row = {
|
|
146
|
+
ownerId: record.owner,
|
|
147
|
+
label: record.label,
|
|
148
|
+
keys: record.keys,
|
|
149
|
+
fingerprint: identity.fingerprint(record),
|
|
150
|
+
};
|
|
151
|
+
directory.putPending(project, row);
|
|
152
|
+
out.pending.push(row);
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
// The keys of an owner never change, because the owner id comes from
|
|
156
|
+
// them. Only the address of the gateway changes here.
|
|
157
|
+
if (record.endpoint && record.endpoint !== known.endpoint) {
|
|
158
|
+
directory.setEndpoint(project, record.owner, record.endpoint);
|
|
159
|
+
out.updated.push({ owner: record.owner, endpoint: record.endpoint });
|
|
160
|
+
}
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
// A roster of this owner, from another machine of this owner.
|
|
164
|
+
if (record.owner !== me.ownerId) continue;
|
|
165
|
+
// A machine that learns a project from a roster belongs to that project
|
|
166
|
+
// too, so its own record goes in with the others.
|
|
167
|
+
if (!directory.member(record.project, me.ownerId)) {
|
|
168
|
+
directory.put({ id: record.project, label: record.label ?? record.project }, me, { source: "self" });
|
|
169
|
+
}
|
|
170
|
+
for (const member of record.members ?? []) {
|
|
171
|
+
if (identity.ownerIdOf(member.keys) !== member.ownerId) continue;
|
|
172
|
+
if (directory.isRevoked(project, member.ownerId)) continue;
|
|
173
|
+
const known = directory.member(project, member.ownerId);
|
|
174
|
+
if (known) {
|
|
175
|
+
if (member.endpoint && member.endpoint !== known.endpoint) {
|
|
176
|
+
directory.setEndpoint(project, member.ownerId, member.endpoint);
|
|
177
|
+
out.updated.push({ owner: member.ownerId, endpoint: member.endpoint });
|
|
178
|
+
}
|
|
179
|
+
continue;
|
|
180
|
+
}
|
|
181
|
+
directory.put({ id: project, label: record.label ?? project }, {
|
|
182
|
+
version: 2,
|
|
183
|
+
ownerId: member.ownerId,
|
|
184
|
+
label: member.label,
|
|
185
|
+
keys: member.keys,
|
|
186
|
+
createdAt: record.at,
|
|
187
|
+
}, { source: "roster", endpoint: member.endpoint ?? null });
|
|
188
|
+
out.added.push({ owner: member.ownerId, label: member.label });
|
|
189
|
+
}
|
|
190
|
+
// The owner said accept, hold, or refuse one time, on one machine. Every
|
|
191
|
+
// machine of that owner holds the same answer.
|
|
192
|
+
for (const row of record.permissions ?? []) {
|
|
193
|
+
if (!permissions.VALUES.includes(row.value)) continue;
|
|
194
|
+
if (permissions.of(project, row.owner) === row.value) continue;
|
|
195
|
+
permissions.set(project, row.owner, row.value);
|
|
196
|
+
out.permissions = out.permissions ?? [];
|
|
197
|
+
out.permissions.push(row);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
return out;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// What this machine publishes for one project.
|
|
204
|
+
// The person compared the fingerprint, and the owner becomes a member.
|
|
205
|
+
export function acceptPending(project, ownerId, fingerprint) {
|
|
206
|
+
const row = directory.pending(project).find((r) => r.ownerId === ownerId || r.ownerId.startsWith(ownerId));
|
|
207
|
+
if (!row) throw new Error(`no owner ${ownerId} waits in ${project}`);
|
|
208
|
+
if (!identity.sameFingerprint(fingerprint, row.fingerprint)) {
|
|
209
|
+
throw new Error(`the fingerprint does not match. The record holds ${row.fingerprint}. Do not accept it.`);
|
|
210
|
+
}
|
|
211
|
+
const record = { version: 2, ownerId: row.ownerId, label: row.label, keys: row.keys, createdAt: row.at };
|
|
212
|
+
directory.put({ id: project }, record, { source: "relay" });
|
|
213
|
+
directory.dropPending(project, row.ownerId);
|
|
214
|
+
return record;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
export function mine(project) {
|
|
218
|
+
const records = [selfRecord(project), rosterRecord(project)];
|
|
219
|
+
const me = identity.load().ownerId;
|
|
220
|
+
for (const row of directory.revokedDevices(project)) {
|
|
221
|
+
if (row.owner === me) records.push(revokeDeviceRecord(project, row.device, row.reason));
|
|
222
|
+
}
|
|
223
|
+
return records;
|
|
224
|
+
}
|