@codemem/core 0.42.0 → 0.43.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/dist/better-sqlite-coordinator-store.d.ts.map +1 -1
- package/dist/coordinator-actions.d.ts +2 -0
- package/dist/coordinator-actions.d.ts.map +1 -1
- package/dist/{coordinator-api-SeLosPIq.js → coordinator-api-DGTIU3Ri.js} +394 -385
- package/dist/coordinator-api-DGTIU3Ri.js.map +1 -0
- package/dist/coordinator-api.d.ts.map +1 -1
- package/dist/coordinator-enrollment-reconciler.d.ts +1 -0
- package/dist/coordinator-enrollment-reconciler.d.ts.map +1 -1
- package/dist/coordinator-runtime.d.ts +2 -1
- package/dist/coordinator-runtime.d.ts.map +1 -1
- package/dist/coordinator-store-contract.d.ts +7 -0
- package/dist/coordinator-store-contract.d.ts.map +1 -1
- package/dist/coordinator-store-test-harness.d.ts.map +1 -1
- package/dist/coordinator-store.d.ts +1 -0
- package/dist/coordinator-store.d.ts.map +1 -1
- package/dist/d1-coordinator-store.d.ts.map +1 -1
- package/dist/db.d.ts +1 -1
- package/dist/db.d.ts.map +1 -1
- package/dist/device-identity-binding.d.ts.map +1 -1
- package/dist/identity-device-assignment.d.ts +40 -0
- package/dist/identity-device-assignment.d.ts.map +1 -0
- package/dist/index.d.ts +10 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9885 -7727
- package/dist/index.js.map +1 -1
- package/dist/internal/cloudflare-coordinator.js +317 -312
- package/dist/internal/cloudflare-coordinator.js.map +1 -1
- package/dist/legacy-recipient-policy-projection.d.ts +31 -0
- package/dist/legacy-recipient-policy-projection.d.ts.map +1 -1
- package/dist/legacy-team-assignment-expectation.d.ts +8 -0
- package/dist/legacy-team-assignment-expectation.d.ts.map +1 -0
- package/dist/legacy-team-candidate.d.ts +61 -0
- package/dist/legacy-team-candidate.d.ts.map +1 -0
- package/dist/legacy-team-project-canonical-preflight.d.ts +36 -0
- package/dist/legacy-team-project-canonical-preflight.d.ts.map +1 -0
- package/dist/legacy-team-setup-activation.d.ts +44 -0
- package/dist/legacy-team-setup-activation.d.ts.map +1 -0
- package/dist/legacy-team-setup-attempt.d.ts +10 -0
- package/dist/legacy-team-setup-attempt.d.ts.map +1 -0
- package/dist/legacy-team-setup-draft.d.ts +118 -0
- package/dist/legacy-team-setup-draft.d.ts.map +1 -0
- package/dist/legacy-team-setup-errors.d.ts +34 -0
- package/dist/legacy-team-setup-errors.d.ts.map +1 -0
- package/dist/legacy-team-setup-limits.d.ts +11 -0
- package/dist/legacy-team-setup-limits.d.ts.map +1 -0
- package/dist/policy-team-device-eligibility.d.ts.map +1 -1
- package/dist/project-scope-settings.d.ts +3 -0
- package/dist/project-scope-settings.d.ts.map +1 -1
- package/dist/recipient-policy-actor-eligibility.d.ts +12 -0
- package/dist/recipient-policy-actor-eligibility.d.ts.map +1 -0
- package/dist/recipient-policy-contract.d.ts +52 -0
- package/dist/recipient-policy-contract.d.ts.map +1 -1
- package/dist/recipient-policy-edges.d.ts.map +1 -1
- package/dist/recipient-policy-identifiers.d.ts +41 -0
- package/dist/recipient-policy-identifiers.d.ts.map +1 -0
- package/dist/recipient-policy-migration.d.ts +13 -1
- package/dist/recipient-policy-migration.d.ts.map +1 -1
- package/dist/recipient-policy-onboarding.d.ts.map +1 -1
- package/dist/recipient-policy-reconciler.d.ts.map +1 -1
- package/dist/recipient-policy-reconciliation.d.ts +2 -0
- package/dist/recipient-policy-reconciliation.d.ts.map +1 -1
- package/dist/recipient-policy-review.d.ts +17 -0
- package/dist/recipient-policy-review.d.ts.map +1 -1
- package/dist/schema-bootstrap.d.ts +1 -0
- package/dist/schema-bootstrap.d.ts.map +1 -1
- package/dist/schema.d.ts +4912 -3150
- package/dist/schema.d.ts.map +1 -1
- package/dist/team-ownership-transitions.d.ts +22 -0
- package/dist/team-ownership-transitions.d.ts.map +1 -0
- package/dist/test-schema.generated.d.ts +1 -1
- package/dist/test-schema.generated.d.ts.map +1 -1
- package/package.json +1 -1
- package/dist/coordinator-api-SeLosPIq.js.map +0 -1
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { B as e, D as t, G as n, H as r,
|
|
1
|
+
import { B as e, D as t, G as n, H as r, J as i, K as a, R as o, U as s, V as c, W as l, j as u, k as d, n as f, q as p, t as m, w as h } from "../coordinator-api-DGTIU3Ri.js";
|
|
2
2
|
//#region src/d1-coordinator-store.ts
|
|
3
|
-
function
|
|
3
|
+
function g(e) {
|
|
4
4
|
if (e == null) throw Error("expected row");
|
|
5
5
|
return e;
|
|
6
6
|
}
|
|
7
|
-
function
|
|
8
|
-
let { presence_capabilities_json: t, presence_expires_at: n, ...r } =
|
|
7
|
+
function _(e) {
|
|
8
|
+
let { presence_capabilities_json: t, presence_expires_at: n, ...r } = g(e);
|
|
9
9
|
if (typeof n != "string" || typeof t != "string") return r;
|
|
10
10
|
try {
|
|
11
11
|
let e = JSON.parse(t);
|
|
@@ -23,7 +23,7 @@ function g(e) {
|
|
|
23
23
|
return r;
|
|
24
24
|
}
|
|
25
25
|
}
|
|
26
|
-
function
|
|
26
|
+
function v(e) {
|
|
27
27
|
let t = e.trim();
|
|
28
28
|
if (!t) return "";
|
|
29
29
|
let n = t.includes("://") ? t : `http://${t}`;
|
|
@@ -34,7 +34,7 @@ function _(e) {
|
|
|
34
34
|
return "";
|
|
35
35
|
}
|
|
36
36
|
}
|
|
37
|
-
function
|
|
37
|
+
function y(e) {
|
|
38
38
|
if (!e) return "";
|
|
39
39
|
try {
|
|
40
40
|
let t = new URL(e), n = t.hostname.toLowerCase();
|
|
@@ -42,18 +42,18 @@ function v(e) {
|
|
|
42
42
|
} catch {}
|
|
43
43
|
return e;
|
|
44
44
|
}
|
|
45
|
-
function
|
|
45
|
+
function b(e, t) {
|
|
46
46
|
let n = [], r = /* @__PURE__ */ new Set();
|
|
47
47
|
for (let i of [...e, ...t]) {
|
|
48
|
-
let e =
|
|
48
|
+
let e = v(i), t = y(e);
|
|
49
49
|
!e || r.has(t) || (r.add(t), n.push(e));
|
|
50
50
|
}
|
|
51
51
|
return n;
|
|
52
52
|
}
|
|
53
|
-
function
|
|
53
|
+
function x() {
|
|
54
54
|
return (/* @__PURE__ */ new Date()).toISOString();
|
|
55
55
|
}
|
|
56
|
-
function
|
|
56
|
+
function S(e, t) {
|
|
57
57
|
return e <= t ? {
|
|
58
58
|
low: e,
|
|
59
59
|
high: t
|
|
@@ -62,22 +62,22 @@ function x(e, t) {
|
|
|
62
62
|
high: e
|
|
63
63
|
};
|
|
64
64
|
}
|
|
65
|
-
function
|
|
65
|
+
function C(e) {
|
|
66
66
|
return e instanceof Error && /unique|constraint/i.test(e.message);
|
|
67
67
|
}
|
|
68
|
-
function
|
|
68
|
+
function w(e) {
|
|
69
69
|
let t = new Uint8Array(e);
|
|
70
70
|
globalThis.crypto.getRandomValues(t);
|
|
71
71
|
let n = [];
|
|
72
72
|
for (let e of t) n.push("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"[e % 64] ?? "A");
|
|
73
73
|
return n.join("");
|
|
74
74
|
}
|
|
75
|
-
async function
|
|
75
|
+
async function T(e) {
|
|
76
76
|
let t = await globalThis.crypto.subtle.digest("SHA-256", new TextEncoder().encode(e));
|
|
77
77
|
return Array.from(new Uint8Array(t), (e) => e.toString(16).padStart(2, "0")).join("");
|
|
78
78
|
}
|
|
79
|
-
var
|
|
80
|
-
function
|
|
79
|
+
var E = "invite_id, group_id, token, policy, expires_at, created_at, created_by,\n team_name_snapshot, revoked_at, operation_id, reviewed_project_set_digest, token_digest,\n inviter_actor_id, inviter_display_name, inviter_device_id, pending_person_id,\n project_summaries_json, project_intent_json, consumed_at, bound_device_id, bound_public_key, bound_fingerprint,\n recipient_actor_id, recipient_display_name, recipient_device_display_name, trust_state,\n bootstrap_grant_id, invite_kind, policy_team_id, target_identity_id, assigned_identity_id,\n reviewed_preview_digest, reviewed_intent_json", D = "group_id, device_id, public_key, fingerprint, identity_id, display_name, enabled, created_at", O = "enrolled_devices.group_id, enrolled_devices.device_id,\n enrolled_devices.public_key, enrolled_devices.fingerprint, enrolled_devices.identity_id,\n enrolled_devices.display_name, enrolled_devices.enabled, enrolled_devices.created_at,\n presence_records.expires_at AS presence_expires_at,\n presence_records.capabilities_json AS presence_capabilities_json";
|
|
80
|
+
function k(e) {
|
|
81
81
|
let t = String(e.groupId ?? "").trim(), n = String(e.seedDeviceId ?? "").trim(), r = String(e.workerDeviceId ?? "").trim(), i = String(e.expiresAt ?? "").trim(), a = String(e.createdBy ?? "").trim() || null;
|
|
82
82
|
if (!t || !n || !r || !i) throw Error("groupId, seedDeviceId, workerDeviceId, and expiresAt are required.");
|
|
83
83
|
return {
|
|
@@ -88,11 +88,11 @@ function O(e) {
|
|
|
88
88
|
createdBy: a
|
|
89
89
|
};
|
|
90
90
|
}
|
|
91
|
-
function
|
|
91
|
+
function A(e) {
|
|
92
92
|
return e?.trim() || null;
|
|
93
93
|
}
|
|
94
|
-
async function
|
|
95
|
-
let n = e.inviteKind ?? (
|
|
94
|
+
async function j(e) {
|
|
95
|
+
let n = e.inviteKind ?? (A(e.operationId) ? "project_share" : "legacy_enrollment"), r = A(e.policyTeamId), i = A(e.targetIdentityId), a = A(e.reviewedPreviewDigest);
|
|
96
96
|
if (![
|
|
97
97
|
"legacy_enrollment",
|
|
98
98
|
"project_share",
|
|
@@ -102,14 +102,14 @@ async function A(e) {
|
|
|
102
102
|
if ([r, i].filter((e) => !!e).some((e) => e.length > 256 || /[\p{Cc}\p{Cf}]/u.test(e))) throw Error("recipient invite identifier is invalid.");
|
|
103
103
|
if (a && !/^[a-f0-9]{64}$/u.test(a)) throw Error("reviewedPreviewDigest must be a SHA-256 digest.");
|
|
104
104
|
if (n === "project_share") {
|
|
105
|
-
if (!
|
|
105
|
+
if (!A(e.operationId)) throw Error("project_share invite requires operationId.");
|
|
106
106
|
if (r || i || a) throw Error("recipient invite metadata requires a recipient invite kind.");
|
|
107
107
|
} else if (n === "legacy_enrollment") {
|
|
108
|
-
if (
|
|
108
|
+
if (A(e.operationId)) throw Error("legacy_enrollment invite cannot reference an operation.");
|
|
109
109
|
if (r || i || a) throw Error("recipient invite metadata requires a recipient invite kind.");
|
|
110
110
|
} else if (n === "team_member") {
|
|
111
|
-
if (!r || !a || i ||
|
|
112
|
-
} else if (n === "add_device" && (!i || !a || r ||
|
|
111
|
+
if (!r || !a || i || A(e.operationId)) throw Error("team_member invite metadata is invalid.");
|
|
112
|
+
} else if (n === "add_device" && (!i || !a || r || A(e.operationId))) throw Error("add_device invite metadata is invalid.");
|
|
113
113
|
if (!(e.reviewedIntent !== void 0 && e.reviewedIntent !== null)) {
|
|
114
114
|
if (n === "team_member" || n === "add_device") throw Error("recipient_invite_review_unavailable");
|
|
115
115
|
return {
|
|
@@ -130,7 +130,7 @@ async function A(e) {
|
|
|
130
130
|
targetIdentityId: i
|
|
131
131
|
};
|
|
132
132
|
else throw Error("recipient invite metadata requires a recipient invite kind.");
|
|
133
|
-
return await
|
|
133
|
+
return await u(e.reviewedIntent, {
|
|
134
134
|
target: o,
|
|
135
135
|
digest: a
|
|
136
136
|
}), {
|
|
@@ -141,141 +141,141 @@ async function A(e) {
|
|
|
141
141
|
reviewedIntentJson: t(e.reviewedIntent, o)
|
|
142
142
|
};
|
|
143
143
|
}
|
|
144
|
-
async function
|
|
145
|
-
if (
|
|
146
|
-
if (!
|
|
147
|
-
let
|
|
144
|
+
async function M(t) {
|
|
145
|
+
if (t.invite_kind === "team_member") {
|
|
146
|
+
if (!t.policy_team_id || !e(t.assigned_identity_id) || !t.reviewed_preview_digest) throw Error("invite_invalid");
|
|
147
|
+
let n = await d(t.reviewed_intent_json, {
|
|
148
148
|
target: {
|
|
149
149
|
kind: "team_member",
|
|
150
|
-
policyTeamId:
|
|
150
|
+
policyTeamId: t.policy_team_id
|
|
151
151
|
},
|
|
152
|
-
digest:
|
|
152
|
+
digest: t.reviewed_preview_digest
|
|
153
153
|
});
|
|
154
154
|
return {
|
|
155
155
|
kind: "team_member",
|
|
156
|
-
invite:
|
|
157
|
-
policy_team_id:
|
|
158
|
-
assigned_identity_id:
|
|
159
|
-
reviewed_preview_digest:
|
|
160
|
-
reviewed_intent:
|
|
161
|
-
bound: !!
|
|
156
|
+
invite: t,
|
|
157
|
+
policy_team_id: t.policy_team_id,
|
|
158
|
+
assigned_identity_id: t.assigned_identity_id,
|
|
159
|
+
reviewed_preview_digest: t.reviewed_preview_digest,
|
|
160
|
+
reviewed_intent: n,
|
|
161
|
+
bound: !!t.consumed_at
|
|
162
162
|
};
|
|
163
163
|
}
|
|
164
|
-
if (
|
|
165
|
-
if (!
|
|
166
|
-
let
|
|
164
|
+
if (t.invite_kind === "add_device") {
|
|
165
|
+
if (!t.target_identity_id || !t.reviewed_preview_digest) throw Error("invite_invalid");
|
|
166
|
+
let e = await d(t.reviewed_intent_json, {
|
|
167
167
|
target: {
|
|
168
168
|
kind: "add_device",
|
|
169
|
-
targetIdentityId:
|
|
169
|
+
targetIdentityId: t.target_identity_id
|
|
170
170
|
},
|
|
171
|
-
digest:
|
|
171
|
+
digest: t.reviewed_preview_digest
|
|
172
172
|
});
|
|
173
173
|
return {
|
|
174
174
|
kind: "add_device",
|
|
175
|
-
invite:
|
|
176
|
-
target_identity_id:
|
|
177
|
-
reviewed_preview_digest:
|
|
178
|
-
reviewed_intent:
|
|
179
|
-
bound: !!
|
|
175
|
+
invite: t,
|
|
176
|
+
target_identity_id: t.target_identity_id,
|
|
177
|
+
reviewed_preview_digest: t.reviewed_preview_digest,
|
|
178
|
+
reviewed_intent: e,
|
|
179
|
+
bound: !!t.consumed_at
|
|
180
180
|
};
|
|
181
181
|
}
|
|
182
182
|
return null;
|
|
183
183
|
}
|
|
184
|
-
function
|
|
184
|
+
function N(e, t = 0) {
|
|
185
185
|
if (e == null) return t;
|
|
186
186
|
if (!Number.isFinite(e) || e < 0) throw Error("membershipEpoch must be non-negative.");
|
|
187
187
|
return Math.trunc(e);
|
|
188
188
|
}
|
|
189
|
-
function
|
|
190
|
-
let t =
|
|
189
|
+
function P(e) {
|
|
190
|
+
let t = A(e.scopeId), n = A(e.label);
|
|
191
191
|
if (!t || !n) throw Error("scopeId and label are required.");
|
|
192
192
|
return {
|
|
193
193
|
scopeId: t,
|
|
194
194
|
label: n,
|
|
195
|
-
kind:
|
|
196
|
-
authorityType:
|
|
197
|
-
coordinatorId:
|
|
198
|
-
groupId:
|
|
199
|
-
manifestIssuerDeviceId:
|
|
200
|
-
membershipEpoch:
|
|
201
|
-
manifestHash:
|
|
202
|
-
status:
|
|
195
|
+
kind: A(e.kind) ?? "user",
|
|
196
|
+
authorityType: A(e.authorityType) ?? "coordinator",
|
|
197
|
+
coordinatorId: A(e.coordinatorId),
|
|
198
|
+
groupId: A(e.groupId),
|
|
199
|
+
manifestIssuerDeviceId: A(e.manifestIssuerDeviceId),
|
|
200
|
+
membershipEpoch: N(e.membershipEpoch),
|
|
201
|
+
manifestHash: A(e.manifestHash),
|
|
202
|
+
status: A(e.status) ?? "active"
|
|
203
203
|
};
|
|
204
204
|
}
|
|
205
|
-
function
|
|
205
|
+
function F(e, t, n) {
|
|
206
206
|
if (e === void 0) return t;
|
|
207
|
-
let r =
|
|
207
|
+
let r = A(e);
|
|
208
208
|
if (!r) throw Error(`${n} must not be empty.`);
|
|
209
209
|
return r;
|
|
210
210
|
}
|
|
211
|
-
function F(e, t) {
|
|
212
|
-
return e === void 0 ? t : k(e);
|
|
213
|
-
}
|
|
214
211
|
function I(e, t) {
|
|
215
|
-
|
|
212
|
+
return e === void 0 ? t : A(e);
|
|
213
|
+
}
|
|
214
|
+
function L(e, t) {
|
|
215
|
+
let n = A(e.scopeId);
|
|
216
216
|
if (!n) throw Error("scopeId is required.");
|
|
217
|
-
let r = e.membershipEpoch == null ? null :
|
|
217
|
+
let r = e.membershipEpoch == null ? null : N(e.membershipEpoch);
|
|
218
218
|
if (r != null && r < t.membership_epoch) throw Error("membershipEpoch must not move backwards.");
|
|
219
219
|
return {
|
|
220
220
|
scopeId: n,
|
|
221
|
-
label:
|
|
222
|
-
kind:
|
|
223
|
-
authorityType:
|
|
224
|
-
coordinatorId:
|
|
225
|
-
groupId:
|
|
226
|
-
manifestIssuerDeviceId:
|
|
221
|
+
label: F(e.label, t.label, "label"),
|
|
222
|
+
kind: F(e.kind, t.kind, "kind"),
|
|
223
|
+
authorityType: F(e.authorityType, t.authority_type, "authorityType"),
|
|
224
|
+
coordinatorId: I(e.coordinatorId, t.coordinator_id),
|
|
225
|
+
groupId: I(e.groupId, t.group_id),
|
|
226
|
+
manifestIssuerDeviceId: I(e.manifestIssuerDeviceId, t.manifest_issuer_device_id),
|
|
227
227
|
membershipEpoch: r ?? t.membership_epoch,
|
|
228
|
-
manifestHash:
|
|
229
|
-
status:
|
|
228
|
+
manifestHash: I(e.manifestHash, t.manifest_hash),
|
|
229
|
+
status: F(e.status, t.status, "status")
|
|
230
230
|
};
|
|
231
231
|
}
|
|
232
|
-
function
|
|
233
|
-
let r =
|
|
232
|
+
function R(e, t, n) {
|
|
233
|
+
let r = A(e.scopeId), i = A(e.deviceId);
|
|
234
234
|
if (!r || !i) throw Error("scopeId and deviceId are required.");
|
|
235
|
-
let a =
|
|
235
|
+
let a = A(e.coordinatorId), o = A(e.groupId);
|
|
236
236
|
if (a && a !== t?.coordinator_id) throw Error("membership coordinatorId must match the scope coordinatorId.");
|
|
237
237
|
if (o && o !== t?.group_id) throw new s("scope_group_mismatch");
|
|
238
|
-
let c = e.membershipEpoch == null ? null :
|
|
238
|
+
let c = e.membershipEpoch == null ? null : N(e.membershipEpoch), l = t?.membership_epoch ?? 0;
|
|
239
239
|
if (c != null && c < l) throw Error("membershipEpoch must not be lower than the scope membershipEpoch.");
|
|
240
240
|
if (c != null && n && c < (n.status === "revoked" ? n.membership_epoch + 1 : n.membership_epoch)) throw Error("membershipEpoch must not move backwards.");
|
|
241
241
|
let u = c ?? (n ? Math.max(l, n.membership_epoch + +(n.status === "revoked")) : l);
|
|
242
242
|
return {
|
|
243
243
|
scopeId: r,
|
|
244
244
|
deviceId: i,
|
|
245
|
-
role:
|
|
245
|
+
role: A(e.role) ?? "member",
|
|
246
246
|
membershipEpoch: u,
|
|
247
247
|
coordinatorId: t?.coordinator_id ?? null,
|
|
248
248
|
groupId: t?.group_id ?? null,
|
|
249
|
-
manifestIssuerDeviceId:
|
|
250
|
-
manifestHash:
|
|
251
|
-
signedManifestJson:
|
|
252
|
-
actorType:
|
|
253
|
-
actorId:
|
|
249
|
+
manifestIssuerDeviceId: A(e.manifestIssuerDeviceId) ?? t?.manifest_issuer_device_id ?? null,
|
|
250
|
+
manifestHash: A(e.manifestHash) ?? t?.manifest_hash ?? null,
|
|
251
|
+
signedManifestJson: A(e.signedManifestJson),
|
|
252
|
+
actorType: A(e.actorType),
|
|
253
|
+
actorId: A(e.actorId)
|
|
254
254
|
};
|
|
255
255
|
}
|
|
256
|
-
function
|
|
256
|
+
function z(e) {
|
|
257
257
|
return e == null || !Number.isFinite(e) ? 100 : Math.max(1, Math.min(1e3, Math.trunc(e)));
|
|
258
258
|
}
|
|
259
|
-
function
|
|
259
|
+
function B(e, t) {
|
|
260
260
|
return e.prepare("INSERT INTO coordinator_scope_membership_audit_log(\n effect_id, action, scope_id, device_id, role, status, membership_epoch,\n previous_role, previous_status, previous_membership_epoch,\n coordinator_id, group_id, actor_type, actor_id, manifest_hash, created_at\n )\n SELECT receipt.effect_id, ?, membership.scope_id, membership.device_id, membership.role, membership.status,\n membership.membership_epoch, ?, ?, ?, membership.coordinator_id, membership.group_id,\n ?, ?, membership.manifest_hash, ?\n FROM (SELECT 1 WHERE changes() > 0) AS required\n JOIN coordinator_scope_membership_effect_receipts AS receipt\n ON receipt.effect_id = ? AND receipt.action = ? AND receipt.request_json = ?\n AND receipt.outcome_applied = 1\n LEFT JOIN coordinator_scope_memberships AS membership\n ON membership.scope_id = ?\n AND membership.device_id = ?\n AND membership.status = ?\n AND membership.membership_epoch = ?\n AND membership.updated_at = ?").bind(t.action, t.previous?.role ?? null, t.previous?.status ?? null, t.previous?.membership_epoch ?? null, t.actorType, t.actorId, t.createdAt, t.effectId, t.action, t.requestJson, t.scopeId, t.deviceId, t.status, t.membershipEpoch, t.updatedAt);
|
|
261
261
|
}
|
|
262
|
-
async function
|
|
263
|
-
return await
|
|
262
|
+
async function V(e, t) {
|
|
263
|
+
return await J(e.prepare("SELECT effect_id, action, request_json, outcome_applied, scope_id, device_id,\n role, status, membership_epoch, coordinator_id, group_id, manifest_issuer_device_id,\n manifest_hash, signed_manifest_json, updated_at, created_at\n FROM coordinator_scope_membership_effect_receipts WHERE effect_id = ?").bind(t));
|
|
264
264
|
}
|
|
265
|
-
function
|
|
265
|
+
function H(e, t) {
|
|
266
266
|
return e.prepare("INSERT INTO coordinator_scope_membership_effect_receipts(\n effect_id, action, request_json, outcome_applied, scope_id, device_id,\n role, status, membership_epoch, coordinator_id, group_id, manifest_issuer_device_id,\n manifest_hash, signed_manifest_json, updated_at, created_at\n )\n SELECT ?, 'grant', ?, 1, membership.scope_id, membership.device_id,\n membership.role, membership.status, membership.membership_epoch,\n membership.coordinator_id, membership.group_id, membership.manifest_issuer_device_id,\n membership.manifest_hash, membership.signed_manifest_json, membership.updated_at, ?\n FROM coordinator_scope_memberships AS membership\n WHERE changes() > 0 AND membership.scope_id = ? AND membership.device_id = ?").bind(t.effectId, t.requestJson, t.createdAt, t.scopeId, t.deviceId);
|
|
267
267
|
}
|
|
268
|
-
function
|
|
268
|
+
function U(e, t) {
|
|
269
269
|
return e.prepare("INSERT INTO coordinator_scope_membership_effect_receipts(\n effect_id, action, request_json, outcome_applied, scope_id, device_id,\n role, status, membership_epoch, coordinator_id, group_id, manifest_issuer_device_id,\n manifest_hash, signed_manifest_json, updated_at, created_at\n )\n SELECT ?, 'revoke', ?, changed.applied, ?, ?, membership.role, membership.status,\n membership.membership_epoch, membership.coordinator_id, membership.group_id,\n membership.manifest_issuer_device_id, membership.manifest_hash,\n membership.signed_manifest_json, membership.updated_at, ?\n FROM (SELECT CASE WHEN changes() > 0 THEN 1 ELSE 0 END AS applied) AS changed\n LEFT JOIN coordinator_scope_memberships AS membership\n ON changed.applied = 1 AND membership.scope_id = ? AND membership.device_id = ?\n WHERE NOT EXISTS (\n SELECT 1 FROM coordinator_scope_membership_effect_receipts WHERE effect_id = ?\n )").bind(t.effectId, t.requestJson, t.scopeId, t.deviceId, t.createdAt, t.scopeId, t.deviceId, t.effectId);
|
|
270
270
|
}
|
|
271
|
-
async function
|
|
271
|
+
async function W(e, t) {
|
|
272
272
|
if (!e.batch) throw Error("D1 batch support is required for audited scope membership changes.");
|
|
273
273
|
return await e.batch(t);
|
|
274
274
|
}
|
|
275
|
-
function
|
|
275
|
+
function G(e) {
|
|
276
276
|
return Number(e?.meta?.changes ?? 0);
|
|
277
277
|
}
|
|
278
|
-
function
|
|
278
|
+
function K(e) {
|
|
279
279
|
if (!e) return null;
|
|
280
280
|
let t = String(e.seedDeviceId ?? "").trim(), n = String(e.expiresAt ?? "").trim(), r = String(e.createdBy ?? "").trim() || null;
|
|
281
281
|
if (!t || !n) throw Error("bootstrapGrant.seedDeviceId and expiresAt are required.");
|
|
@@ -287,195 +287,195 @@ function G(e) {
|
|
|
287
287
|
createdBy: r
|
|
288
288
|
};
|
|
289
289
|
}
|
|
290
|
-
async function
|
|
290
|
+
async function q(e) {
|
|
291
291
|
let t = await e.all();
|
|
292
292
|
return Array.isArray(t?.results) ? t.results : [];
|
|
293
293
|
}
|
|
294
|
-
async function
|
|
294
|
+
async function J(e) {
|
|
295
295
|
return await e.first();
|
|
296
296
|
}
|
|
297
|
-
async function
|
|
297
|
+
async function Y(e) {
|
|
298
298
|
let t = await e.run();
|
|
299
299
|
return Number(t?.meta?.changes ?? 0);
|
|
300
300
|
}
|
|
301
|
-
var
|
|
301
|
+
var X = class {
|
|
302
302
|
db;
|
|
303
303
|
constructor(e) {
|
|
304
304
|
this.db = e;
|
|
305
305
|
}
|
|
306
306
|
async close() {}
|
|
307
307
|
async createGroup(e, t) {
|
|
308
|
-
await this.db.prepare("INSERT OR IGNORE INTO groups(group_id, display_name, archived_at, created_at) VALUES (?, ?, NULL, ?)").bind(e, t ?? null,
|
|
308
|
+
await this.db.prepare("INSERT OR IGNORE INTO groups(group_id, display_name, archived_at, created_at) VALUES (?, ?, NULL, ?)").bind(e, t ?? null, x()).run();
|
|
309
309
|
}
|
|
310
310
|
async getGroup(e) {
|
|
311
|
-
let t = await
|
|
312
|
-
return t ?
|
|
311
|
+
let t = await J(this.db.prepare("SELECT group_id, display_name, archived_at, created_at FROM groups WHERE group_id = ?").bind(e));
|
|
312
|
+
return t ? g(t) : null;
|
|
313
313
|
}
|
|
314
314
|
async listGroups(e = !1) {
|
|
315
315
|
let t = e ? "" : "WHERE archived_at IS NULL";
|
|
316
|
-
return (await
|
|
316
|
+
return (await q(this.db.prepare(`SELECT group_id, display_name, archived_at, created_at FROM groups ${t} ORDER BY created_at ASC`))).map((e) => g(e));
|
|
317
317
|
}
|
|
318
318
|
async renameGroup(e, t) {
|
|
319
|
-
return await
|
|
319
|
+
return await Y(this.db.prepare("UPDATE groups SET display_name = ? WHERE group_id = ?").bind(t, e)) > 0;
|
|
320
320
|
}
|
|
321
|
-
async archiveGroup(e, t =
|
|
322
|
-
return await
|
|
321
|
+
async archiveGroup(e, t = x()) {
|
|
322
|
+
return await Y(this.db.prepare("UPDATE groups SET archived_at = ? WHERE group_id = ? AND archived_at IS NULL").bind(t, e)) > 0;
|
|
323
323
|
}
|
|
324
324
|
async unarchiveGroup(e) {
|
|
325
|
-
return await
|
|
325
|
+
return await Y(this.db.prepare("UPDATE groups SET archived_at = NULL WHERE group_id = ? AND archived_at IS NOT NULL").bind(e)) > 0;
|
|
326
326
|
}
|
|
327
327
|
async enrollDevice(e, t) {
|
|
328
|
-
if (await
|
|
328
|
+
if (await Y(this.db.prepare("INSERT INTO enrolled_devices(\n group_id, device_id, public_key, fingerprint, identity_id, display_name, enabled, created_at\n ) VALUES (?, ?, ?, ?, ?, ?, 1, ?)\n ON CONFLICT(group_id, device_id) DO UPDATE SET\n public_key = excluded.public_key,\n fingerprint = excluded.fingerprint,\n identity_id = COALESCE(enrolled_devices.identity_id, excluded.identity_id),\n display_name = excluded.display_name,\n enabled = 1\n WHERE excluded.identity_id IS NULL\n OR enrolled_devices.identity_id IS NULL\n OR enrolled_devices.identity_id = excluded.identity_id").bind(e, t.deviceId, t.publicKey, t.fingerprint, t.identityId ?? null, t.displayName ?? null, x())) === 0) throw Error("invite_identity_conflict");
|
|
329
329
|
}
|
|
330
330
|
async listEnrolledDevices(e, t) {
|
|
331
331
|
let n = t ? "" : "AND enrolled_devices.enabled = 1";
|
|
332
|
-
return (await
|
|
332
|
+
return (await q(this.db.prepare(`SELECT ${O}
|
|
333
333
|
FROM enrolled_devices
|
|
334
334
|
LEFT JOIN presence_records
|
|
335
335
|
ON presence_records.group_id = enrolled_devices.group_id
|
|
336
336
|
AND presence_records.device_id = enrolled_devices.device_id
|
|
337
337
|
WHERE enrolled_devices.group_id = ? ${n}
|
|
338
|
-
ORDER BY enrolled_devices.created_at ASC, enrolled_devices.device_id ASC`).bind(e))).map(
|
|
338
|
+
ORDER BY enrolled_devices.created_at ASC, enrolled_devices.device_id ASC`).bind(e))).map(_);
|
|
339
339
|
}
|
|
340
340
|
async getEnrollment(e, t, n = !1) {
|
|
341
|
-
let r = n ? "" : "AND enabled = 1", i = await
|
|
341
|
+
let r = n ? "" : "AND enabled = 1", i = await J(this.db.prepare(`SELECT ${D}
|
|
342
342
|
FROM enrolled_devices
|
|
343
343
|
WHERE group_id = ? AND device_id = ? ${r}`).bind(e, t));
|
|
344
|
-
return i ?
|
|
344
|
+
return i ? g(i) : null;
|
|
345
345
|
}
|
|
346
346
|
async renameDevice(e, t, n) {
|
|
347
|
-
return await
|
|
347
|
+
return await Y(this.db.prepare("UPDATE enrolled_devices SET display_name = ?\n WHERE group_id = ? AND device_id = ?").bind(n, e, t)) > 0;
|
|
348
348
|
}
|
|
349
349
|
async setDeviceEnabled(e, t, n) {
|
|
350
|
-
return await
|
|
350
|
+
return await Y(this.db.prepare("UPDATE enrolled_devices SET enabled = ?\n WHERE group_id = ? AND device_id = ?").bind(+!!n, e, t)) > 0;
|
|
351
351
|
}
|
|
352
352
|
async removeDevice(e, t) {
|
|
353
353
|
if (!this.db.batch) throw Error("D1 batch support is required for atomic device removal.");
|
|
354
|
-
return
|
|
354
|
+
return G((await this.db.batch([
|
|
355
355
|
this.db.prepare("DELETE FROM presence_records WHERE group_id = ? AND device_id = ?").bind(e, t),
|
|
356
356
|
this.db.prepare("DELETE FROM coordinator_reciprocal_approvals WHERE group_id = ? AND (requesting_device_id = ? OR requested_device_id = ?)").bind(e, t, t),
|
|
357
357
|
this.db.prepare("DELETE FROM enrolled_devices WHERE group_id = ? AND device_id = ?").bind(e, t)
|
|
358
358
|
]))[2]) > 0;
|
|
359
359
|
}
|
|
360
360
|
async recordNonce(e, t, n) {
|
|
361
|
-
return await
|
|
361
|
+
return await Y(this.db.prepare("INSERT OR IGNORE INTO request_nonces(device_id, nonce, created_at) VALUES (?, ?, ?)").bind(e, t, n)) > 0;
|
|
362
362
|
}
|
|
363
363
|
async cleanupNonces(e) {
|
|
364
364
|
await this.db.prepare("DELETE FROM request_nonces WHERE created_at < ?").bind(e).run();
|
|
365
365
|
}
|
|
366
366
|
async createInvite(e) {
|
|
367
|
-
let t =
|
|
368
|
-
if (!!s != !!
|
|
369
|
-
let
|
|
367
|
+
let t = x(), n = w(12), r = w(24), i = await T(r), a = c(e.expiresAt), o = await this.getGroup(e.groupId), s = String(e.operationId ?? "").trim() || null, l = String(e.reviewedProjectSetDigest ?? "").trim() || null, u = await j(e), d = u.inviteKind === "team_member" ? `identity:${w(18)}` : null;
|
|
368
|
+
if (!!s != !!l) throw Error("operationId and reviewedProjectSetDigest must be provided together.");
|
|
369
|
+
let f = async () => {
|
|
370
370
|
if (!s) return null;
|
|
371
|
-
let t = await
|
|
371
|
+
let t = await J(this.db.prepare(`SELECT ${E} FROM coordinator_invites WHERE operation_id = ?`).bind(s));
|
|
372
372
|
if (!t) return null;
|
|
373
373
|
if (t.consumed_at) throw Error("invite_already_bound");
|
|
374
|
-
if (t.group_id !== e.groupId || t.policy !== e.policy || t.invite_kind !==
|
|
375
|
-
return
|
|
376
|
-
},
|
|
374
|
+
if (t.group_id !== e.groupId || t.policy !== e.policy || t.invite_kind !== u.inviteKind || t.reviewed_project_set_digest !== l || t.inviter_actor_id !== (String(e.inviterActorId ?? "").trim() || null) || t.inviter_display_name !== (String(e.inviterDisplayName ?? "").trim() || null) || t.inviter_device_id !== (String(e.inviterDeviceId ?? "").trim() || null) || t.pending_person_id !== (String(e.pendingPersonId ?? "").trim() || null) || t.project_summaries_json !== (e.projectSummaries ? JSON.stringify(e.projectSummaries) : null) || t.project_intent_json !== (e.projectIntent ? JSON.stringify(e.projectIntent) : null) || t.policy_team_id !== u.policyTeamId || t.target_identity_id !== u.targetIdentityId || t.reviewed_preview_digest !== u.reviewedPreviewDigest || t.reviewed_intent_json !== u.reviewedIntentJson) throw Error("invite_operation_intent_conflict");
|
|
375
|
+
return g(t);
|
|
376
|
+
}, p = async (n) => {
|
|
377
377
|
if (!n.revoked_at && n.expires_at > t) return n;
|
|
378
|
-
await
|
|
379
|
-
let c = await
|
|
378
|
+
await Y(this.db.prepare("UPDATE coordinator_invites\n SET token = ?, token_digest = ?, expires_at = ?, created_at = ?, created_by = ?,\n team_name_snapshot = ?, revoked_at = NULL\n WHERE operation_id = ? AND token = ?\n AND (revoked_at IS NOT NULL OR expires_at <= ?)").bind(r, i, a, t, e.createdBy ?? null, o?.display_name ?? null, s, n.token, t));
|
|
379
|
+
let c = await f();
|
|
380
380
|
if (!c) throw Error("invite_operation_reissue_failed");
|
|
381
381
|
return c;
|
|
382
382
|
};
|
|
383
383
|
if (s) {
|
|
384
|
-
let e = await
|
|
385
|
-
if (e) return await
|
|
384
|
+
let e = await f();
|
|
385
|
+
if (e) return await p(e);
|
|
386
386
|
}
|
|
387
387
|
try {
|
|
388
|
-
await this.db.prepare("INSERT INTO coordinator_invites(\n invite_id, group_id, token, policy, expires_at, created_at, created_by,\n team_name_snapshot, revoked_at, operation_id, reviewed_project_set_digest,\n token_digest, inviter_actor_id, inviter_display_name, inviter_device_id,\n pending_person_id, project_summaries_json, project_intent_json, trust_state,\n invite_kind, policy_team_id, target_identity_id, assigned_identity_id,\n reviewed_preview_digest, reviewed_intent_json\n ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)").bind(n, e.groupId, r, e.policy, a, t, e.createdBy ?? null, o?.display_name ?? null, s,
|
|
388
|
+
await this.db.prepare("INSERT INTO coordinator_invites(\n invite_id, group_id, token, policy, expires_at, created_at, created_by,\n team_name_snapshot, revoked_at, operation_id, reviewed_project_set_digest,\n token_digest, inviter_actor_id, inviter_display_name, inviter_device_id,\n pending_person_id, project_summaries_json, project_intent_json, trust_state,\n invite_kind, policy_team_id, target_identity_id, assigned_identity_id,\n reviewed_preview_digest, reviewed_intent_json\n ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)").bind(n, e.groupId, r, e.policy, a, t, e.createdBy ?? null, o?.display_name ?? null, s, l, i, String(e.inviterActorId ?? "").trim() || null, String(e.inviterDisplayName ?? "").trim() || null, String(e.inviterDeviceId ?? "").trim() || null, String(e.pendingPersonId ?? "").trim() || null, e.projectSummaries ? JSON.stringify(e.projectSummaries) : null, e.projectIntent ? JSON.stringify(e.projectIntent) : null, s ? "pending" : null, u.inviteKind, u.policyTeamId, u.targetIdentityId, d, u.reviewedPreviewDigest, u.reviewedIntentJson).run();
|
|
389
389
|
} catch (e) {
|
|
390
390
|
if (s) {
|
|
391
|
-
let e = await
|
|
392
|
-
if (e) return await
|
|
391
|
+
let e = await f();
|
|
392
|
+
if (e) return await p(e);
|
|
393
393
|
}
|
|
394
394
|
throw e;
|
|
395
395
|
}
|
|
396
|
-
return
|
|
396
|
+
return g(await J(this.db.prepare(`SELECT ${E} FROM coordinator_invites WHERE invite_id = ?`).bind(n)));
|
|
397
397
|
}
|
|
398
398
|
async getInviteByToken(e) {
|
|
399
|
-
let t = await
|
|
399
|
+
let t = await T(e), n = await J(this.db.prepare(`SELECT ${E}
|
|
400
400
|
FROM coordinator_invites
|
|
401
401
|
WHERE (token_digest = ? OR token = ?)
|
|
402
402
|
AND revoked_at IS NULL
|
|
403
|
-
AND expires_at > ?`).bind(t, e,
|
|
404
|
-
return n ?
|
|
403
|
+
AND expires_at > ?`).bind(t, e, x()));
|
|
404
|
+
return n ? g(n) : null;
|
|
405
405
|
}
|
|
406
406
|
async getInviteByTokenForInspection(e) {
|
|
407
|
-
let t = await
|
|
408
|
-
return n ?
|
|
407
|
+
let t = await T(e), n = await J(this.db.prepare(`SELECT ${E} FROM coordinator_invites WHERE token_digest = ? OR token = ?`).bind(t, e));
|
|
408
|
+
return n ? g(n) : null;
|
|
409
409
|
}
|
|
410
410
|
async inspectRecipientInvite(e) {
|
|
411
411
|
let t = await this.getInviteByTokenForInspection(e.token);
|
|
412
412
|
if (!t) return null;
|
|
413
|
-
let n = await
|
|
413
|
+
let n = await M(t);
|
|
414
414
|
if (!n) return null;
|
|
415
415
|
if (t.revoked_at) throw Error("invite_invalid");
|
|
416
|
-
if (!t.consumed_at && new Date(t.expires_at) <= new Date(
|
|
416
|
+
if (!t.consumed_at && new Date(t.expires_at) <= new Date(c(e.now))) throw Error("invite_expired");
|
|
417
417
|
let r = await this.getGroup(t.group_id);
|
|
418
418
|
if (!r) throw Error("group_not_found");
|
|
419
419
|
if (r.archived_at) throw Error("group_archived");
|
|
420
420
|
return n;
|
|
421
421
|
}
|
|
422
|
-
async consumeRecipientInvite(
|
|
423
|
-
let
|
|
424
|
-
if (!
|
|
425
|
-
let a = await
|
|
426
|
-
if (!o || !s || s.kind !==
|
|
427
|
-
let
|
|
428
|
-
if (!
|
|
429
|
-
if (
|
|
430
|
-
if (!o.consumed_at && new Date(o.expires_at) <= new Date(
|
|
431
|
-
if (
|
|
432
|
-
let
|
|
433
|
-
if (
|
|
434
|
-
let
|
|
435
|
-
if (o.consumed_at && !
|
|
436
|
-
if (o.consumed_at && o.recipient_actor_id !==
|
|
437
|
-
let
|
|
438
|
-
FROM enrolled_devices WHERE group_id = ? AND device_id = ?`).bind(o.group_id,
|
|
439
|
-
if (
|
|
440
|
-
let
|
|
422
|
+
async consumeRecipientInvite(e) {
|
|
423
|
+
let t = c(e.now), n = e.recipientDisplayName ?? null, i = e.deviceDisplayName ?? null;
|
|
424
|
+
if (!e.identityId || !e.deviceId || !e.publicKey || !e.fingerprint || e.identityId !== e.identityId.trim() || e.deviceId !== e.deviceId.trim() || e.identityId.length > 256 || e.deviceId.length > 256 || /[\p{Cc}\p{Cf}]/u.test(e.identityId) || /[\p{Cc}\p{Cf}]/u.test(e.deviceId)) throw Error("invite_identity_conflict");
|
|
425
|
+
let a = await T(e.token), o = await this.getInviteByTokenForInspection(e.token), s = o ? await M(o) : null;
|
|
426
|
+
if (!o || !s || s.kind !== e.inviteKind || o.revoked_at) throw Error("invite_invalid");
|
|
427
|
+
let l = await this.getGroup(o.group_id);
|
|
428
|
+
if (!l) throw Error("group_not_found");
|
|
429
|
+
if (l.archived_at) throw Error("group_archived");
|
|
430
|
+
if (!o.consumed_at && new Date(o.expires_at) <= new Date(t)) throw Error("invite_expired");
|
|
431
|
+
if (h(e.publicKey) !== e.fingerprint) throw Error("fingerprint_mismatch");
|
|
432
|
+
let u = r(s);
|
|
433
|
+
if (u !== e.identityId) throw Error("invite_identity_conflict");
|
|
434
|
+
let d = o.bound_device_id === e.deviceId && o.bound_public_key === e.publicKey && o.bound_fingerprint === e.fingerprint;
|
|
435
|
+
if (o.consumed_at && !d) throw Error("invite_already_bound");
|
|
436
|
+
if (o.consumed_at && o.recipient_actor_id !== u) throw Error("invite_identity_conflict");
|
|
437
|
+
let f = await J(this.db.prepare(`SELECT ${D}
|
|
438
|
+
FROM enrolled_devices WHERE group_id = ? AND device_id = ?`).bind(o.group_id, e.deviceId));
|
|
439
|
+
if (f && (f.public_key !== e.publicKey || f.fingerprint !== e.fingerprint || f.identity_id !== null && f.identity_id !== u)) throw Error("invite_identity_conflict");
|
|
440
|
+
let p = 0, m = w(12);
|
|
441
441
|
if (!o.consumed_at) {
|
|
442
442
|
if (!this.db.batch) throw Error("D1 batch support is required for atomic invite consume.");
|
|
443
|
-
|
|
444
|
-
this.db.prepare("UPDATE coordinator_invites SET token = ?, consumed_at = ?, bound_device_id = ?,\n bound_public_key = ?, bound_fingerprint = ?, recipient_actor_id = ?,\n recipient_display_name = ?, recipient_device_display_name = ?\n WHERE (token_digest = ? OR token = ?) AND consumed_at IS NULL\n AND revoked_at IS NULL AND expires_at > ? AND invite_kind = ?\n AND reviewed_intent_json = ? AND reviewed_preview_digest = ?\n AND ((invite_kind = 'team_member' AND assigned_identity_id = ?)\n OR (invite_kind = 'add_device' AND target_identity_id = ?))\n AND NOT EXISTS (\n SELECT 1 FROM enrolled_devices e\n WHERE e.group_id = coordinator_invites.group_id AND e.device_id = ?\n AND (e.public_key <> ? OR e.fingerprint <> ?\n OR (e.identity_id IS NOT NULL AND e.identity_id <> ?))\n )\n AND EXISTS (SELECT 1 FROM groups g WHERE g.group_id = coordinator_invites.group_id\n AND g.archived_at IS NULL)").bind(`consumed:${o.invite_id}`,
|
|
445
|
-
this.db.prepare("INSERT INTO enrolled_devices(\n group_id, device_id, public_key, fingerprint, identity_id, display_name, enabled, created_at\n ) SELECT i.group_id, i.bound_device_id, i.bound_public_key, i.bound_fingerprint,\n i.recipient_actor_id, i.recipient_device_display_name, 1, ? FROM coordinator_invites i\n JOIN groups g ON g.group_id = i.group_id AND g.archived_at IS NULL\n WHERE (i.token_digest = ? OR i.token = ?) AND i.bound_device_id = ?\n AND i.bound_public_key = ? AND i.bound_fingerprint = ?\n AND i.recipient_actor_id = ?\n ON CONFLICT(group_id, device_id) DO UPDATE SET\n identity_id = COALESCE(enrolled_devices.identity_id, excluded.identity_id),\n display_name = COALESCE(enrolled_devices.display_name, excluded.display_name),\n enabled = 1\n WHERE enrolled_devices.identity_id IS NULL\n OR enrolled_devices.identity_id = excluded.identity_id").bind(
|
|
446
|
-
this.db.prepare("UPDATE coordinator_invites SET bootstrap_grant_id = ?,\n trust_state = 'bootstrap_grant_created'\n WHERE (token_digest = ? OR token = ?) AND invite_kind = 'add_device'\n AND bootstrap_grant_id IS NULL AND inviter_device_id IS NOT NULL\n AND expires_at > ?\n AND EXISTS (SELECT 1 FROM enrolled_devices e\n WHERE e.group_id = coordinator_invites.group_id\n AND e.device_id = coordinator_invites.inviter_device_id AND e.enabled = 1)").bind(
|
|
447
|
-
this.db.prepare("INSERT OR IGNORE INTO coordinator_bootstrap_grants(\n grant_id, group_id, seed_device_id, worker_device_id, expires_at,\n created_at, created_by, revoked_at\n ) SELECT i.bootstrap_grant_id, i.group_id, i.inviter_device_id, i.bound_device_id,\n i.expires_at, ?, i.recipient_actor_id, NULL FROM coordinator_invites i\n JOIN enrolled_devices e ON e.group_id = i.group_id\n AND e.device_id = i.inviter_device_id AND e.enabled = 1\n WHERE (i.token_digest = ? OR i.token = ?) AND i.invite_kind = 'add_device'\n AND i.bootstrap_grant_id IS NOT NULL AND i.bound_device_id = ?").bind(
|
|
443
|
+
p = G((await this.db.batch([
|
|
444
|
+
this.db.prepare("UPDATE coordinator_invites SET token = ?, consumed_at = ?, bound_device_id = ?,\n bound_public_key = ?, bound_fingerprint = ?, recipient_actor_id = ?,\n recipient_display_name = ?, recipient_device_display_name = ?\n WHERE (token_digest = ? OR token = ?) AND consumed_at IS NULL\n AND revoked_at IS NULL AND expires_at > ? AND invite_kind = ?\n AND reviewed_intent_json = ? AND reviewed_preview_digest = ?\n AND ((invite_kind = 'team_member' AND assigned_identity_id = ?)\n OR (invite_kind = 'add_device' AND target_identity_id = ?))\n AND NOT EXISTS (\n SELECT 1 FROM enrolled_devices e\n WHERE e.group_id = coordinator_invites.group_id AND e.device_id = ?\n AND (e.public_key <> ? OR e.fingerprint <> ?\n OR (e.identity_id IS NOT NULL AND e.identity_id <> ?))\n )\n AND EXISTS (SELECT 1 FROM groups g WHERE g.group_id = coordinator_invites.group_id\n AND g.archived_at IS NULL)").bind(`consumed:${o.invite_id}`, t, e.deviceId, e.publicKey, e.fingerprint, u, n, i, a, e.token, t, e.inviteKind, o.reviewed_intent_json, o.reviewed_preview_digest, u, u, e.deviceId, e.publicKey, e.fingerprint, u),
|
|
445
|
+
this.db.prepare("INSERT INTO enrolled_devices(\n group_id, device_id, public_key, fingerprint, identity_id, display_name, enabled, created_at\n ) SELECT i.group_id, i.bound_device_id, i.bound_public_key, i.bound_fingerprint,\n i.recipient_actor_id, i.recipient_device_display_name, 1, ? FROM coordinator_invites i\n JOIN groups g ON g.group_id = i.group_id AND g.archived_at IS NULL\n WHERE (i.token_digest = ? OR i.token = ?) AND i.bound_device_id = ?\n AND i.bound_public_key = ? AND i.bound_fingerprint = ?\n AND i.recipient_actor_id = ?\n ON CONFLICT(group_id, device_id) DO UPDATE SET\n identity_id = COALESCE(enrolled_devices.identity_id, excluded.identity_id),\n display_name = COALESCE(enrolled_devices.display_name, excluded.display_name),\n enabled = 1\n WHERE enrolled_devices.identity_id IS NULL\n OR enrolled_devices.identity_id = excluded.identity_id").bind(t, a, e.token, e.deviceId, e.publicKey, e.fingerprint, u),
|
|
446
|
+
this.db.prepare("UPDATE coordinator_invites SET bootstrap_grant_id = ?,\n trust_state = 'bootstrap_grant_created'\n WHERE (token_digest = ? OR token = ?) AND invite_kind = 'add_device'\n AND bootstrap_grant_id IS NULL AND inviter_device_id IS NOT NULL\n AND expires_at > ?\n AND EXISTS (SELECT 1 FROM enrolled_devices e\n WHERE e.group_id = coordinator_invites.group_id\n AND e.device_id = coordinator_invites.inviter_device_id AND e.enabled = 1)").bind(m, a, e.token, t),
|
|
447
|
+
this.db.prepare("INSERT OR IGNORE INTO coordinator_bootstrap_grants(\n grant_id, group_id, seed_device_id, worker_device_id, expires_at,\n created_at, created_by, revoked_at\n ) SELECT i.bootstrap_grant_id, i.group_id, i.inviter_device_id, i.bound_device_id,\n i.expires_at, ?, i.recipient_actor_id, NULL FROM coordinator_invites i\n JOIN enrolled_devices e ON e.group_id = i.group_id\n AND e.device_id = i.inviter_device_id AND e.enabled = 1\n WHERE (i.token_digest = ? OR i.token = ?) AND i.invite_kind = 'add_device'\n AND i.bootstrap_grant_id IS NOT NULL AND i.bound_device_id = ?").bind(t, a, e.token, e.deviceId)
|
|
448
448
|
]))[0]);
|
|
449
449
|
}
|
|
450
450
|
let g = await this.getGroup(o.group_id);
|
|
451
451
|
if (!g) throw Error("group_not_found");
|
|
452
452
|
if (g.archived_at) throw Error("group_archived");
|
|
453
|
-
let _ = await
|
|
454
|
-
FROM enrolled_devices WHERE group_id = ? AND device_id = ?`).bind(o.group_id,
|
|
455
|
-
if (_ && (_.public_key !==
|
|
456
|
-
let v = await this.getInviteByTokenForInspection(
|
|
457
|
-
if (!v || v.revoked_at || !v.consumed_at && new Date(v.expires_at) <= new Date(
|
|
458
|
-
let y = await
|
|
459
|
-
if (!y || y.kind !==
|
|
460
|
-
if (
|
|
461
|
-
if (v.bound_device_id !==
|
|
462
|
-
if (v.recipient_actor_id !==
|
|
463
|
-
await this.db.prepare("UPDATE enrolled_devices SET identity_id = ?\n WHERE group_id = ? AND device_id = ? AND identity_id IS NULL\n AND public_key = ? AND fingerprint = ?").bind(
|
|
464
|
-
let b = await this.getEnrollment(v.group_id,
|
|
453
|
+
let _ = await J(this.db.prepare(`SELECT ${D}
|
|
454
|
+
FROM enrolled_devices WHERE group_id = ? AND device_id = ?`).bind(o.group_id, e.deviceId));
|
|
455
|
+
if (_ && (_.public_key !== e.publicKey || _.fingerprint !== e.fingerprint || _.identity_id !== null && _.identity_id !== u)) throw Error("invite_identity_conflict");
|
|
456
|
+
let v = await this.getInviteByTokenForInspection(e.token);
|
|
457
|
+
if (!v || v.revoked_at || !v.consumed_at && new Date(v.expires_at) <= new Date(t)) throw Error("invite_invalid");
|
|
458
|
+
let y = await M(v);
|
|
459
|
+
if (!y || y.kind !== e.inviteKind) throw Error("invite_invalid");
|
|
460
|
+
if (r(y) !== u) throw Error("invite_identity_conflict");
|
|
461
|
+
if (v.bound_device_id !== e.deviceId || v.bound_public_key !== e.publicKey || v.bound_fingerprint !== e.fingerprint) throw Error("invite_already_bound");
|
|
462
|
+
if (v.recipient_actor_id !== u) throw Error("invite_identity_conflict");
|
|
463
|
+
await this.db.prepare("UPDATE enrolled_devices SET identity_id = ?\n WHERE group_id = ? AND device_id = ? AND identity_id IS NULL\n AND public_key = ? AND fingerprint = ?").bind(u, v.group_id, e.deviceId, e.publicKey, e.fingerprint).run();
|
|
464
|
+
let b = await this.getEnrollment(v.group_id, e.deviceId);
|
|
465
465
|
if (!b) throw Error("invite_acceptance_incomplete");
|
|
466
|
-
if (b.public_key !==
|
|
466
|
+
if (b.public_key !== e.publicKey || b.fingerprint !== e.fingerprint || b.identity_id !== u) throw Error("invite_identity_conflict");
|
|
467
467
|
let x = v;
|
|
468
|
-
if (y.kind === "add_device" && v.inviter_device_id && !v.bootstrap_grant_id && new Date(v.expires_at) > new Date(
|
|
468
|
+
if (y.kind === "add_device" && v.inviter_device_id && !v.bootstrap_grant_id && new Date(v.expires_at) > new Date(t)) {
|
|
469
469
|
if (!this.db.batch) throw Error("D1 batch support is required for atomic bootstrap grant recovery.");
|
|
470
|
-
let
|
|
471
|
-
await this.db.batch([this.db.prepare("UPDATE coordinator_invites SET bootstrap_grant_id = ?,\n trust_state = 'bootstrap_grant_created'\n WHERE invite_id = ? AND invite_kind = 'add_device'\n AND bootstrap_grant_id IS NULL AND inviter_device_id IS NOT NULL\n AND bound_device_id = ? AND recipient_actor_id = ? AND expires_at > ?\n AND EXISTS (SELECT 1 FROM enrolled_devices e\n WHERE e.group_id = coordinator_invites.group_id\n AND e.device_id = coordinator_invites.inviter_device_id AND e.enabled = 1)").bind(
|
|
472
|
-
let r = await this.getInviteByTokenForInspection(
|
|
470
|
+
let n = w(12);
|
|
471
|
+
await this.db.batch([this.db.prepare("UPDATE coordinator_invites SET bootstrap_grant_id = ?,\n trust_state = 'bootstrap_grant_created'\n WHERE invite_id = ? AND invite_kind = 'add_device'\n AND bootstrap_grant_id IS NULL AND inviter_device_id IS NOT NULL\n AND bound_device_id = ? AND recipient_actor_id = ? AND expires_at > ?\n AND EXISTS (SELECT 1 FROM enrolled_devices e\n WHERE e.group_id = coordinator_invites.group_id\n AND e.device_id = coordinator_invites.inviter_device_id AND e.enabled = 1)").bind(n, v.invite_id, e.deviceId, u, t), this.db.prepare("INSERT OR IGNORE INTO coordinator_bootstrap_grants(\n grant_id, group_id, seed_device_id, worker_device_id, expires_at,\n created_at, created_by, revoked_at\n ) SELECT i.bootstrap_grant_id, i.group_id, i.inviter_device_id, i.bound_device_id,\n i.expires_at, ?, i.recipient_actor_id, NULL FROM coordinator_invites i\n JOIN enrolled_devices e ON e.group_id = i.group_id\n AND e.device_id = i.inviter_device_id AND e.enabled = 1\n WHERE i.invite_id = ? AND i.invite_kind = 'add_device'\n AND i.bootstrap_grant_id IS NOT NULL AND i.bound_device_id = ?").bind(t, v.invite_id, e.deviceId)]);
|
|
472
|
+
let r = await this.getInviteByTokenForInspection(e.token);
|
|
473
473
|
if (!r) throw Error("invite_invalid");
|
|
474
474
|
x = r;
|
|
475
475
|
}
|
|
476
476
|
let S = x.bootstrap_grant_id ? await this.getBootstrapGrant(x.bootstrap_grant_id) : null;
|
|
477
477
|
return {
|
|
478
|
-
status:
|
|
478
|
+
status: p === 1 ? "accepted" : "existing",
|
|
479
479
|
invite: x,
|
|
480
480
|
reviewed_intent: y.reviewed_intent,
|
|
481
481
|
bootstrap_grant: S
|
|
@@ -483,68 +483,68 @@ var Y = class {
|
|
|
483
483
|
}
|
|
484
484
|
async consumeProjectInvite(e) {
|
|
485
485
|
if (!this.db.batch) throw Error("D1 batch support is required for atomic invite consume.");
|
|
486
|
-
let t =
|
|
486
|
+
let t = c(e.now), n = await T(e.token), r = await this.getInviteByTokenForInspection(e.token);
|
|
487
487
|
if (!r?.operation_id || !r.project_intent_json || r.revoked_at) throw Error("invite_invalid");
|
|
488
488
|
let i = await this.getGroup(r.group_id);
|
|
489
489
|
if (!i) throw Error("group_not_found");
|
|
490
490
|
if (i.archived_at) throw Error("group_archived");
|
|
491
491
|
if (r.operation_id !== e.operationId) throw Error("invite_invalid");
|
|
492
|
-
if (
|
|
492
|
+
if (h(e.publicKey) !== e.fingerprint) throw Error("fingerprint_mismatch");
|
|
493
493
|
if (!r.consumed_at && new Date(r.expires_at) <= new Date(t)) throw Error("invite_expired");
|
|
494
494
|
let a = r.bound_device_id === e.deviceId && r.bound_public_key === e.publicKey && r.bound_fingerprint === e.fingerprint;
|
|
495
495
|
if (r.consumed_at && !a) throw Error("invite_already_bound");
|
|
496
496
|
if (r.consumed_at && (r.recipient_actor_id !== e.recipientActorId || r.recipient_display_name !== e.recipientDisplayName || r.recipient_device_display_name !== e.deviceDisplayName)) throw Error("invite_identity_conflict");
|
|
497
|
-
let o = await
|
|
497
|
+
let o = await J(this.db.prepare(`SELECT ${D}
|
|
498
498
|
FROM enrolled_devices WHERE group_id = ? AND device_id = ?`).bind(r.group_id, e.deviceId));
|
|
499
499
|
if (o && (o.public_key !== e.publicKey || o.fingerprint !== e.fingerprint || o.identity_id != null && o.identity_id !== e.recipientActorId)) throw Error("invite_identity_conflict");
|
|
500
|
-
let s =
|
|
500
|
+
let s = w(12), l = G((await this.db.batch([
|
|
501
501
|
this.db.prepare("UPDATE coordinator_invites SET token = ?, consumed_at = ?, bound_device_id = ?,\n bound_public_key = ?, bound_fingerprint = ?, recipient_actor_id = ?,\n recipient_display_name = ?, recipient_device_display_name = ?,\n trust_state = 'pending_inviter_device'\n WHERE (token_digest = ? OR token = ?) AND consumed_at IS NULL\n AND EXISTS (SELECT 1 FROM groups g WHERE g.group_id = coordinator_invites.group_id\n AND g.archived_at IS NULL)").bind(`consumed:${r.invite_id}`, t, e.deviceId, e.publicKey, e.fingerprint, e.recipientActorId, e.recipientDisplayName, e.deviceDisplayName, n, e.token),
|
|
502
502
|
this.db.prepare("INSERT INTO enrolled_devices(\n group_id, device_id, public_key, fingerprint, identity_id, display_name, enabled, created_at\n ) SELECT i.group_id, i.bound_device_id, i.bound_public_key, i.bound_fingerprint,\n i.recipient_actor_id, i.recipient_device_display_name, 1, ? FROM coordinator_invites i\n JOIN groups g ON g.group_id = i.group_id AND g.archived_at IS NULL\n WHERE (i.token_digest = ? OR i.token = ?) AND i.bound_device_id = ? AND ? = 1\n ON CONFLICT(group_id, device_id) DO UPDATE SET\n identity_id = COALESCE(enrolled_devices.identity_id, excluded.identity_id),\n display_name = excluded.display_name, enabled = 1\n WHERE enrolled_devices.identity_id IS NULL\n OR enrolled_devices.identity_id = excluded.identity_id").bind(t, n, e.token, e.deviceId, +!r.consumed_at),
|
|
503
503
|
this.db.prepare("UPDATE coordinator_invites SET bootstrap_grant_id = ?,\n trust_state = 'bootstrap_grant_created'\n WHERE (token_digest = ? OR token = ?) AND bootstrap_grant_id IS NULL\n AND bound_device_id = ?\n AND EXISTS (SELECT 1 FROM groups g WHERE g.group_id = coordinator_invites.group_id\n AND g.archived_at IS NULL)\n AND expires_at > ?\n AND EXISTS (SELECT 1 FROM enrolled_devices e WHERE e.group_id = coordinator_invites.group_id\n AND e.device_id = coordinator_invites.inviter_device_id AND e.enabled = 1)").bind(s, n, e.token, e.deviceId, t),
|
|
504
504
|
this.db.prepare("INSERT OR IGNORE INTO coordinator_bootstrap_grants(\n grant_id, group_id, seed_device_id, worker_device_id, expires_at, created_at,\n created_by, revoked_at\n ) SELECT i.bootstrap_grant_id, i.group_id, i.inviter_device_id, i.bound_device_id,\n i.expires_at, ?, i.inviter_actor_id, NULL FROM coordinator_invites i\n JOIN groups g ON g.group_id = i.group_id AND g.archived_at IS NULL\n JOIN enrolled_devices e ON e.group_id = i.group_id AND e.device_id = i.inviter_device_id\n WHERE (i.token_digest = ? OR i.token = ?) AND i.bootstrap_grant_id IS NOT NULL\n AND i.bound_device_id = ? AND e.enabled = 1").bind(t, n, e.token, e.deviceId)
|
|
505
|
-
]))[0]) === 1,
|
|
506
|
-
if (!
|
|
507
|
-
if (
|
|
508
|
-
let
|
|
509
|
-
if (!
|
|
510
|
-
if (
|
|
511
|
-
if (
|
|
512
|
-
|
|
513
|
-
let
|
|
514
|
-
if (!
|
|
515
|
-
if (
|
|
516
|
-
let
|
|
505
|
+
]))[0]) === 1, u = await this.getGroup(r.group_id);
|
|
506
|
+
if (!u) throw Error("group_not_found");
|
|
507
|
+
if (u.archived_at) throw Error("group_archived");
|
|
508
|
+
let d = await this.getInviteByTokenForInspection(e.token);
|
|
509
|
+
if (!d) throw Error("invite_invalid");
|
|
510
|
+
if (d.bound_device_id !== e.deviceId || d.bound_public_key !== e.publicKey || d.bound_fingerprint !== e.fingerprint) throw Error("invite_already_bound");
|
|
511
|
+
if (d.recipient_actor_id !== e.recipientActorId || d.recipient_display_name !== e.recipientDisplayName || d.recipient_device_display_name !== e.deviceDisplayName) throw Error("invite_identity_conflict");
|
|
512
|
+
l || await this.db.prepare("UPDATE enrolled_devices SET identity_id = ?\n WHERE group_id = ? AND device_id = ? AND identity_id IS NULL AND enabled = 1\n AND public_key = ? AND fingerprint = ?").bind(e.recipientActorId, d.group_id, e.deviceId, e.publicKey, e.fingerprint).run();
|
|
513
|
+
let f = await this.getEnrollment(d.group_id, e.deviceId);
|
|
514
|
+
if (!f) throw Error("invite_acceptance_incomplete");
|
|
515
|
+
if (f.identity_id !== e.recipientActorId) throw Error("invite_identity_conflict");
|
|
516
|
+
let p = d.inviter_device_id ? await this.getEnrollment(d.group_id, d.inviter_device_id) : null, m = d.bootstrap_grant_id ? await this.getBootstrapGrant(d.bootstrap_grant_id) : null;
|
|
517
517
|
return {
|
|
518
|
-
status:
|
|
519
|
-
invite:
|
|
520
|
-
enrollment:
|
|
521
|
-
seed_enrollment:
|
|
522
|
-
bootstrap_grant:
|
|
518
|
+
status: l ? "accepted" : "existing",
|
|
519
|
+
invite: d,
|
|
520
|
+
enrollment: f,
|
|
521
|
+
seed_enrollment: p,
|
|
522
|
+
bootstrap_grant: m
|
|
523
523
|
};
|
|
524
524
|
}
|
|
525
525
|
async listInvites(e) {
|
|
526
|
-
return (await
|
|
526
|
+
return (await q(this.db.prepare(`SELECT ${E}
|
|
527
527
|
FROM coordinator_invites WHERE group_id = ?
|
|
528
|
-
ORDER BY created_at DESC`).bind(e))).map((e) =>
|
|
528
|
+
ORDER BY created_at DESC`).bind(e))).map((e) => g(e));
|
|
529
529
|
}
|
|
530
530
|
async createJoinRequest(e) {
|
|
531
|
-
let t =
|
|
532
|
-
return await this.db.prepare("INSERT INTO coordinator_join_requests(\n request_id, group_id, device_id, public_key, fingerprint, display_name, token, status, created_at, reviewed_at, reviewed_by\n ) VALUES (?, ?, ?, ?, ?, ?, ?, 'pending', ?, NULL, NULL)").bind(n, e.groupId, e.deviceId, e.publicKey, e.fingerprint, e.displayName ?? null, e.token, t).run(),
|
|
531
|
+
let t = x(), n = w(12);
|
|
532
|
+
return await this.db.prepare("INSERT INTO coordinator_join_requests(\n request_id, group_id, device_id, public_key, fingerprint, display_name, token, status, created_at, reviewed_at, reviewed_by\n ) VALUES (?, ?, ?, ?, ?, ?, ?, 'pending', ?, NULL, NULL)").bind(n, e.groupId, e.deviceId, e.publicKey, e.fingerprint, e.displayName ?? null, e.token, t).run(), g(await J(this.db.prepare("SELECT request_id, group_id, device_id, public_key, fingerprint, display_name, token, status, created_at, reviewed_at, reviewed_by\n FROM coordinator_join_requests WHERE request_id = ?").bind(n)));
|
|
533
533
|
}
|
|
534
534
|
async listJoinRequests(e, t) {
|
|
535
535
|
let n = t ?? "pending";
|
|
536
|
-
return (await
|
|
536
|
+
return (await q(this.db.prepare("SELECT request_id, group_id, device_id, public_key, fingerprint, display_name, token, status, created_at, reviewed_at, reviewed_by\n FROM coordinator_join_requests\n WHERE group_id = ? AND status = ?\n ORDER BY created_at ASC, device_id ASC").bind(e, n))).map((e) => g(e));
|
|
537
537
|
}
|
|
538
538
|
async reviewJoinRequest(e) {
|
|
539
|
-
let t = await
|
|
539
|
+
let t = await J(this.db.prepare("SELECT request_id, group_id, device_id, public_key, fingerprint, display_name, token, status,\n created_at, reviewed_at, reviewed_by\n FROM coordinator_join_requests WHERE request_id = ?").bind(e.requestId));
|
|
540
540
|
if (!t) return null;
|
|
541
541
|
if (t.status !== "pending") return {
|
|
542
|
-
...
|
|
542
|
+
...g(t),
|
|
543
543
|
_no_transition: !0
|
|
544
544
|
};
|
|
545
|
-
let n =
|
|
545
|
+
let n = K(e.bootstrapGrant), r = x(), i = e.approved ? "approved" : "denied", a = null, o = null;
|
|
546
546
|
if (e.approved && n) {
|
|
547
|
-
if (!await
|
|
547
|
+
if (!await J(this.db.prepare("SELECT device_id FROM enrolled_devices WHERE group_id = ? AND device_id = ? AND enabled = 1").bind(t.group_id, n.seedDeviceId))) throw Error("bootstrap grant seed device is not enrolled in the group.");
|
|
548
548
|
if (n.seedDeviceId === t.device_id) throw Error("bootstrap grant seed and worker device ids must differ.");
|
|
549
549
|
a = {
|
|
550
550
|
...n,
|
|
@@ -554,9 +554,9 @@ var Y = class {
|
|
|
554
554
|
}
|
|
555
555
|
if (this.db.batch) {
|
|
556
556
|
let t = [];
|
|
557
|
-
if (t.push(this.db.prepare("UPDATE coordinator_join_requests\n SET status = ?, reviewed_at = ?, reviewed_by = ?\n WHERE request_id = ? AND status = 'pending'").bind(i, r, e.reviewedBy ?? null, e.requestId)), e.approved && (t.push(this.db.prepare("INSERT INTO enrolled_devices(\n group_id, device_id, public_key, fingerprint, display_name, enabled, created_at\n )\n SELECT group_id, device_id, public_key, fingerprint, display_name, 1, ?\n FROM coordinator_join_requests\n WHERE request_id = ?\n AND status = 'approved'\n AND reviewed_at = ?\n ON CONFLICT(group_id, device_id) DO UPDATE SET\n public_key = excluded.public_key,\n fingerprint = excluded.fingerprint,\n display_name = excluded.display_name,\n enabled = 1").bind(
|
|
558
|
-
o =
|
|
559
|
-
let n =
|
|
557
|
+
if (t.push(this.db.prepare("UPDATE coordinator_join_requests\n SET status = ?, reviewed_at = ?, reviewed_by = ?\n WHERE request_id = ? AND status = 'pending'").bind(i, r, e.reviewedBy ?? null, e.requestId)), e.approved && (t.push(this.db.prepare("INSERT INTO enrolled_devices(\n group_id, device_id, public_key, fingerprint, display_name, enabled, created_at\n )\n SELECT group_id, device_id, public_key, fingerprint, display_name, 1, ?\n FROM coordinator_join_requests\n WHERE request_id = ?\n AND status = 'approved'\n AND reviewed_at = ?\n ON CONFLICT(group_id, device_id) DO UPDATE SET\n public_key = excluded.public_key,\n fingerprint = excluded.fingerprint,\n display_name = excluded.display_name,\n enabled = 1").bind(x(), e.requestId, r)), a)) {
|
|
558
|
+
o = w(12);
|
|
559
|
+
let n = x();
|
|
560
560
|
t.push(this.db.prepare("INSERT INTO coordinator_bootstrap_grants(\n grant_id, group_id, seed_device_id, worker_device_id, expires_at, created_at, created_by, revoked_at\n ) SELECT ?, ?, ?, ?, ?, ?, ?, NULL\n WHERE EXISTS (\n SELECT 1 FROM coordinator_join_requests\n WHERE request_id = ? AND status = 'approved' AND reviewed_at = ?\n )").bind(o, a.groupId, a.seedDeviceId, a.workerDeviceId, a.expiresAt, n, a.createdBy ?? null, e.requestId, r));
|
|
561
561
|
}
|
|
562
562
|
await this.db.batch(t);
|
|
@@ -571,212 +571,217 @@ var Y = class {
|
|
|
571
571
|
let e = await this.createBootstrapGrant(a);
|
|
572
572
|
n = e.grant_id, o = e.grant_id;
|
|
573
573
|
}
|
|
574
|
-
if (await
|
|
574
|
+
if (await Y(this.db.prepare("UPDATE coordinator_join_requests\n SET status = ?, reviewed_at = ?, reviewed_by = ?\n WHERE request_id = ? AND status = 'pending'").bind(i, r, e.reviewedBy ?? null, e.requestId)) === 0) {
|
|
575
575
|
n && await this.revokeBootstrapGrant(n);
|
|
576
|
-
let t = await
|
|
576
|
+
let t = await J(this.db.prepare("SELECT request_id, group_id, device_id, public_key, fingerprint, display_name, token, status, created_at, reviewed_at, reviewed_by\n FROM coordinator_join_requests WHERE request_id = ?").bind(e.requestId));
|
|
577
577
|
return t ? {
|
|
578
|
-
...
|
|
578
|
+
...g(t),
|
|
579
579
|
_no_transition: !0
|
|
580
580
|
} : null;
|
|
581
581
|
}
|
|
582
582
|
}
|
|
583
|
-
let s = await
|
|
583
|
+
let s = await J(this.db.prepare("SELECT request_id, group_id, device_id, public_key, fingerprint, display_name, token, status, created_at, reviewed_at, reviewed_by\n FROM coordinator_join_requests WHERE request_id = ?").bind(e.requestId));
|
|
584
584
|
if (!s) return null;
|
|
585
585
|
let c = a ? o ? await this.getBootstrapGrant(o) : await this.createBootstrapGrant(a) : null;
|
|
586
586
|
return {
|
|
587
|
-
...
|
|
587
|
+
...g(s),
|
|
588
588
|
bootstrap_grant: c
|
|
589
589
|
};
|
|
590
590
|
}
|
|
591
591
|
async createReciprocalApproval(e) {
|
|
592
|
-
let t = e.groupId.trim(), n = e.requestingDeviceId.trim(), r = e.requestedDeviceId.trim();
|
|
592
|
+
let t = e.groupId.trim(), n = e.requestingDeviceId.trim(), r = e.requestedDeviceId.trim(), i = e.expectedIncomingRequestId !== void 0, a = e.expectedIncomingRequestId?.trim() ?? "";
|
|
593
593
|
if (!t || !n || !r) throw Error("groupId, requestingDeviceId, and requestedDeviceId are required.");
|
|
594
594
|
if (n === r) throw Error("requesting and requested device ids must differ.");
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
595
|
+
if (i) {
|
|
596
|
+
let e = x();
|
|
597
|
+
if (await Y(this.db.prepare("UPDATE coordinator_reciprocal_approvals\n SET status = 'completed', resolved_at = ?\n WHERE request_id = ?\n AND group_id = ?\n AND requesting_device_id = ?\n AND requested_device_id = ?\n AND status = 'pending'").bind(e, a, t, r, n)) !== 1) throw new o();
|
|
598
|
+
return g(await J(this.db.prepare("SELECT request_id, group_id, requesting_device_id, requested_device_id, status, created_at, resolved_at\n FROM coordinator_reciprocal_approvals WHERE request_id = ?").bind(a)));
|
|
599
|
+
}
|
|
600
|
+
let s = S(n, r), c = await J(this.db.prepare("SELECT request_id, group_id, requesting_device_id, requested_device_id, status, created_at, resolved_at\n FROM coordinator_reciprocal_approvals\n WHERE group_id = ? AND requesting_device_id = ? AND requested_device_id = ? AND status = 'pending'\n ORDER BY created_at DESC LIMIT 1").bind(t, n, r));
|
|
601
|
+
if (c) return g(c);
|
|
602
|
+
let l = await J(this.db.prepare("SELECT request_id, group_id, requesting_device_id, requested_device_id, status, created_at, resolved_at\n FROM coordinator_reciprocal_approvals\n WHERE group_id = ? AND requesting_device_id = ? AND requested_device_id = ? AND status = 'pending'\n ORDER BY created_at DESC LIMIT 1").bind(t, r, n));
|
|
603
|
+
if (l) {
|
|
604
|
+
let e = x();
|
|
605
|
+
return await this.db.prepare("UPDATE coordinator_reciprocal_approvals SET status = 'completed', resolved_at = ? WHERE request_id = ?").bind(e, l.request_id).run(), g(await J(this.db.prepare("SELECT request_id, group_id, requesting_device_id, requested_device_id, status, created_at, resolved_at\n FROM coordinator_reciprocal_approvals WHERE request_id = ?").bind(l.request_id)));
|
|
601
606
|
}
|
|
602
|
-
let
|
|
607
|
+
let u = w(12), d = x();
|
|
603
608
|
try {
|
|
604
|
-
await this.db.prepare("INSERT INTO coordinator_reciprocal_approvals(\n request_id,\n group_id,\n requesting_device_id,\n requested_device_id,\n pending_pair_low_device_id,\n pending_pair_high_device_id,\n status,\n created_at,\n resolved_at\n ) VALUES (?, ?, ?, ?, ?, ?, 'pending', ?, NULL)").bind(
|
|
609
|
+
await this.db.prepare("INSERT INTO coordinator_reciprocal_approvals(\n request_id,\n group_id,\n requesting_device_id,\n requested_device_id,\n pending_pair_low_device_id,\n pending_pair_high_device_id,\n status,\n created_at,\n resolved_at\n ) VALUES (?, ?, ?, ?, ?, ?, 'pending', ?, NULL)").bind(u, t, n, r, s.low, s.high, d).run();
|
|
605
610
|
} catch (e) {
|
|
606
|
-
if (!
|
|
607
|
-
let i = await
|
|
608
|
-
if (i) return
|
|
609
|
-
let a = await
|
|
611
|
+
if (!C(e)) throw e;
|
|
612
|
+
let i = await J(this.db.prepare("SELECT request_id, group_id, requesting_device_id, requested_device_id, status, created_at, resolved_at\n FROM coordinator_reciprocal_approvals\n WHERE group_id = ? AND requesting_device_id = ? AND requested_device_id = ? AND status = 'pending'\n ORDER BY created_at DESC LIMIT 1").bind(t, n, r));
|
|
613
|
+
if (i) return g(i);
|
|
614
|
+
let a = await J(this.db.prepare("SELECT request_id, group_id, requesting_device_id, requested_device_id, status, created_at, resolved_at\n FROM coordinator_reciprocal_approvals\n WHERE group_id = ? AND requesting_device_id = ? AND requested_device_id = ? AND status = 'pending'\n ORDER BY created_at DESC LIMIT 1").bind(t, r, n));
|
|
610
615
|
if (a) {
|
|
611
|
-
let e =
|
|
612
|
-
return await this.db.prepare("UPDATE coordinator_reciprocal_approvals SET status = 'completed', resolved_at = ? WHERE request_id = ?").bind(e, a.request_id).run(),
|
|
616
|
+
let e = x();
|
|
617
|
+
return await this.db.prepare("UPDATE coordinator_reciprocal_approvals SET status = 'completed', resolved_at = ? WHERE request_id = ?").bind(e, a.request_id).run(), g(await J(this.db.prepare("SELECT request_id, group_id, requesting_device_id, requested_device_id, status, created_at, resolved_at\n FROM coordinator_reciprocal_approvals WHERE request_id = ?").bind(a.request_id)));
|
|
613
618
|
}
|
|
614
619
|
throw e;
|
|
615
620
|
}
|
|
616
|
-
return
|
|
621
|
+
return g(await J(this.db.prepare("SELECT request_id, group_id, requesting_device_id, requested_device_id, status, created_at, resolved_at\n FROM coordinator_reciprocal_approvals WHERE request_id = ?").bind(u)));
|
|
617
622
|
}
|
|
618
623
|
async createBootstrapGrant(e) {
|
|
619
|
-
let t =
|
|
620
|
-
return await this.db.prepare("INSERT INTO coordinator_bootstrap_grants(\n grant_id, group_id, seed_device_id, worker_device_id, expires_at, created_at, created_by, revoked_at\n ) VALUES (?, ?, ?, ?, ?, ?, ?, NULL)").bind(n, t.groupId, t.seedDeviceId, t.workerDeviceId, t.expiresAt, r, t.createdBy).run(),
|
|
624
|
+
let t = k(e), n = w(12), r = x();
|
|
625
|
+
return await this.db.prepare("INSERT INTO coordinator_bootstrap_grants(\n grant_id, group_id, seed_device_id, worker_device_id, expires_at, created_at, created_by, revoked_at\n ) VALUES (?, ?, ?, ?, ?, ?, ?, NULL)").bind(n, t.groupId, t.seedDeviceId, t.workerDeviceId, t.expiresAt, r, t.createdBy).run(), g(await J(this.db.prepare("SELECT grant_id, group_id, seed_device_id, worker_device_id, expires_at, created_at, created_by, revoked_at\n FROM coordinator_bootstrap_grants WHERE grant_id = ?").bind(n)));
|
|
621
626
|
}
|
|
622
627
|
async getScope(e) {
|
|
623
|
-
let t = await
|
|
624
|
-
return t ?
|
|
628
|
+
let t = await J(this.db.prepare("SELECT scope_id, label, kind, authority_type, coordinator_id, group_id,\n manifest_issuer_device_id, membership_epoch, manifest_hash, status, created_at, updated_at\n FROM coordinator_scopes WHERE scope_id = ?").bind(e));
|
|
629
|
+
return t ? g(t) : null;
|
|
625
630
|
}
|
|
626
631
|
async getScopeMembership(e, t) {
|
|
627
|
-
let n = await
|
|
628
|
-
return n ?
|
|
632
|
+
let n = await J(this.db.prepare("SELECT scope_id, device_id, role, status, membership_epoch, coordinator_id, group_id,\n manifest_issuer_device_id, manifest_hash, signed_manifest_json, updated_at\n FROM coordinator_scope_memberships\n WHERE scope_id = ? AND device_id = ?").bind(e, t));
|
|
633
|
+
return n ? g(n) : null;
|
|
629
634
|
}
|
|
630
635
|
async createScope(e) {
|
|
631
|
-
let t =
|
|
636
|
+
let t = P(e);
|
|
632
637
|
if (await this.getScope(t.scopeId)) throw Error("scopeId already exists.");
|
|
633
|
-
let n =
|
|
638
|
+
let n = x();
|
|
634
639
|
await this.db.prepare("INSERT INTO coordinator_scopes(\n scope_id, label, kind, authority_type, coordinator_id, group_id,\n manifest_issuer_device_id, membership_epoch, manifest_hash, status, created_at, updated_at\n ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)").bind(t.scopeId, t.label, t.kind, t.authorityType, t.coordinatorId, t.groupId, t.manifestIssuerDeviceId, t.membershipEpoch, t.manifestHash, t.status, n, n).run();
|
|
635
640
|
let r = await this.getScope(t.scopeId);
|
|
636
641
|
if (!r) throw Error("scope insert returned no row");
|
|
637
642
|
return r;
|
|
638
643
|
}
|
|
639
644
|
async updateScope(e) {
|
|
640
|
-
let t =
|
|
645
|
+
let t = A(e.scopeId), n = t ? await this.getScope(t) : null;
|
|
641
646
|
if (!n) return null;
|
|
642
|
-
let r =
|
|
643
|
-
return await this.db.prepare("UPDATE coordinator_scopes\n SET label = ?,\n kind = ?,\n authority_type = ?,\n coordinator_id = ?,\n group_id = ?,\n manifest_issuer_device_id = ?,\n membership_epoch = ?,\n manifest_hash = ?,\n status = ?,\n updated_at = ?\n WHERE scope_id = ?").bind(r.label, r.kind, r.authorityType, r.coordinatorId, r.groupId, r.manifestIssuerDeviceId, r.membershipEpoch, r.manifestHash, r.status,
|
|
647
|
+
let r = L(e, n);
|
|
648
|
+
return await this.db.prepare("UPDATE coordinator_scopes\n SET label = ?,\n kind = ?,\n authority_type = ?,\n coordinator_id = ?,\n group_id = ?,\n manifest_issuer_device_id = ?,\n membership_epoch = ?,\n manifest_hash = ?,\n status = ?,\n updated_at = ?\n WHERE scope_id = ?").bind(r.label, r.kind, r.authorityType, r.coordinatorId, r.groupId, r.manifestIssuerDeviceId, r.membershipEpoch, r.manifestHash, r.status, x(), r.scopeId).run(), await this.getScope(r.scopeId);
|
|
644
649
|
}
|
|
645
650
|
async listScopes(e = {}) {
|
|
646
|
-
let t = [], n = [], r =
|
|
651
|
+
let t = [], n = [], r = A(e.coordinatorId), i = A(e.groupId), a = A(e.status);
|
|
647
652
|
r && (t.push("coordinator_id = ?"), n.push(r)), i && (t.push("group_id = ?"), n.push(i)), a ? (t.push("status = ?"), n.push(a)) : e.includeInactive || t.push("status = 'active'");
|
|
648
653
|
let o = t.length ? `WHERE ${t.join(" AND ")}` : "", s = this.db.prepare(`SELECT scope_id, label, kind, authority_type, coordinator_id, group_id,
|
|
649
654
|
manifest_issuer_device_id, membership_epoch, manifest_hash, status, created_at, updated_at
|
|
650
655
|
FROM coordinator_scopes ${o}
|
|
651
656
|
ORDER BY coordinator_id ASC, group_id ASC, scope_id ASC`);
|
|
652
|
-
return (await
|
|
657
|
+
return (await q(n.length ? s.bind(...n) : s)).map((e) => g(e));
|
|
653
658
|
}
|
|
654
659
|
async grantScopeMembership(e) {
|
|
655
|
-
let t =
|
|
656
|
-
if (
|
|
657
|
-
let
|
|
658
|
-
if (!
|
|
659
|
-
if (
|
|
660
|
-
let
|
|
661
|
-
if (
|
|
662
|
-
let m =
|
|
663
|
-
await
|
|
664
|
-
this.db.prepare("INSERT INTO coordinator_scope_memberships(\n scope_id, device_id, role, status, membership_epoch, coordinator_id, group_id,\n manifest_issuer_device_id, manifest_hash, signed_manifest_json, updated_at\n ) SELECT ?, ?, ?, 'active', ?, ?, ?, ?, ?, ?, ?\n WHERE NOT EXISTS (\n SELECT 1 FROM coordinator_scope_membership_effect_receipts WHERE effect_id = ?\n )\n ON CONFLICT(scope_id, device_id) DO UPDATE SET\n role = excluded.role,\n status = 'active',\n membership_epoch = excluded.membership_epoch,\n coordinator_id = excluded.coordinator_id,\n group_id = excluded.group_id,\n manifest_issuer_device_id = excluded.manifest_issuer_device_id,\n manifest_hash = excluded.manifest_hash,\n signed_manifest_json = excluded.signed_manifest_json,\n updated_at = excluded.updated_at\n WHERE excluded.membership_epoch > coordinator_scope_memberships.membership_epoch\n OR (\n excluded.membership_epoch = coordinator_scope_memberships.membership_epoch\n AND coordinator_scope_memberships.status != 'revoked'\n )").bind(
|
|
665
|
-
|
|
660
|
+
let t = p(e.effectId), r = n(e), i = await V(this.db, t);
|
|
661
|
+
if (i) return l(i, "grant", r), a(i);
|
|
662
|
+
let o = A(e.scopeId), c = A(e.deviceId), u = o ? await this.getScope(o) : null;
|
|
663
|
+
if (!u) throw new s("scope_not_found");
|
|
664
|
+
if (u.status !== "active") throw new s("scope_inactive");
|
|
665
|
+
let d = o && c ? await this.getScopeMembership(o, c) : null, f = R(e, u, d);
|
|
666
|
+
if (f.groupId && !await this.getEnrollment(f.groupId, f.deviceId)) throw new s("device_not_enrolled");
|
|
667
|
+
let m = x();
|
|
668
|
+
await W(this.db, [
|
|
669
|
+
this.db.prepare("INSERT INTO coordinator_scope_memberships(\n scope_id, device_id, role, status, membership_epoch, coordinator_id, group_id,\n manifest_issuer_device_id, manifest_hash, signed_manifest_json, updated_at\n ) SELECT ?, ?, ?, 'active', ?, ?, ?, ?, ?, ?, ?\n WHERE NOT EXISTS (\n SELECT 1 FROM coordinator_scope_membership_effect_receipts WHERE effect_id = ?\n )\n ON CONFLICT(scope_id, device_id) DO UPDATE SET\n role = excluded.role,\n status = 'active',\n membership_epoch = excluded.membership_epoch,\n coordinator_id = excluded.coordinator_id,\n group_id = excluded.group_id,\n manifest_issuer_device_id = excluded.manifest_issuer_device_id,\n manifest_hash = excluded.manifest_hash,\n signed_manifest_json = excluded.signed_manifest_json,\n updated_at = excluded.updated_at\n WHERE excluded.membership_epoch > coordinator_scope_memberships.membership_epoch\n OR (\n excluded.membership_epoch = coordinator_scope_memberships.membership_epoch\n AND coordinator_scope_memberships.status != 'revoked'\n )").bind(f.scopeId, f.deviceId, f.role, f.membershipEpoch, f.coordinatorId, f.groupId, f.manifestIssuerDeviceId, f.manifestHash, f.signedManifestJson, m, t),
|
|
670
|
+
H(this.db, {
|
|
666
671
|
effectId: t,
|
|
667
|
-
requestJson:
|
|
668
|
-
scopeId:
|
|
669
|
-
deviceId:
|
|
672
|
+
requestJson: r,
|
|
673
|
+
scopeId: f.scopeId,
|
|
674
|
+
deviceId: f.deviceId,
|
|
670
675
|
createdAt: m
|
|
671
676
|
}),
|
|
672
|
-
|
|
677
|
+
B(this.db, {
|
|
673
678
|
effectId: t,
|
|
674
|
-
requestJson:
|
|
679
|
+
requestJson: r,
|
|
675
680
|
action: "grant",
|
|
676
|
-
previous:
|
|
677
|
-
scopeId:
|
|
678
|
-
deviceId:
|
|
681
|
+
previous: d,
|
|
682
|
+
scopeId: f.scopeId,
|
|
683
|
+
deviceId: f.deviceId,
|
|
679
684
|
status: "active",
|
|
680
|
-
membershipEpoch:
|
|
685
|
+
membershipEpoch: f.membershipEpoch,
|
|
681
686
|
updatedAt: m,
|
|
682
|
-
actorType:
|
|
683
|
-
actorId:
|
|
687
|
+
actorType: f.actorType,
|
|
688
|
+
actorId: f.actorId,
|
|
684
689
|
createdAt: m
|
|
685
690
|
})
|
|
686
691
|
]);
|
|
687
|
-
let h = await
|
|
692
|
+
let h = await V(this.db, t);
|
|
688
693
|
if (!h) throw Error("scope membership grant was not applied.");
|
|
689
|
-
return
|
|
694
|
+
return l(h, "grant", r), a(h);
|
|
690
695
|
}
|
|
691
696
|
async revokeScopeMembership(e) {
|
|
692
|
-
let t =
|
|
693
|
-
if (!t || !
|
|
694
|
-
let
|
|
695
|
-
if (
|
|
696
|
-
let
|
|
697
|
-
if (
|
|
698
|
-
let
|
|
699
|
-
if (
|
|
700
|
-
let m =
|
|
701
|
-
effectId:
|
|
702
|
-
requestJson:
|
|
697
|
+
let t = A(e.scopeId), n = A(e.deviceId);
|
|
698
|
+
if (!t || !n) throw Error("scopeId and deviceId are required.");
|
|
699
|
+
let r = p(e.effectId), a = i(e), o = await V(this.db, r);
|
|
700
|
+
if (o) return l(o, "revoke", a), o.outcome_applied === 1;
|
|
701
|
+
let c = e.membershipEpoch == null ? null : N(e.membershipEpoch), u = A(e.groupId), d = u ? await this.getScope(t) : null;
|
|
702
|
+
if (u && u !== d?.group_id) throw new s("scope_group_mismatch");
|
|
703
|
+
let f = await this.getScopeMembership(t, n);
|
|
704
|
+
if (c != null && f && c <= f.membership_epoch) throw Error("membershipEpoch must increase on revoke.");
|
|
705
|
+
let m = x(), h = c ?? (f?.membership_epoch ?? -1) + 1, g = this.db.prepare("UPDATE coordinator_scope_memberships\n SET status = 'revoked',\n membership_epoch = CASE WHEN ? IS NULL THEN membership_epoch + 1 ELSE ? END,\n manifest_hash = COALESCE(?, manifest_hash),\n signed_manifest_json = COALESCE(?, signed_manifest_json),\n updated_at = ?\n WHERE scope_id = ? AND device_id = ? AND membership_epoch = ? AND status = ?\n AND NOT EXISTS (\n SELECT 1 FROM coordinator_scope_membership_effect_receipts WHERE effect_id = ?\n )").bind(c, c, A(e.manifestHash), A(e.signedManifestJson), m, t, n, f?.membership_epoch ?? -1, f?.status ?? "", r), _ = U(this.db, {
|
|
706
|
+
effectId: r,
|
|
707
|
+
requestJson: a,
|
|
703
708
|
scopeId: t,
|
|
704
|
-
deviceId:
|
|
709
|
+
deviceId: n,
|
|
705
710
|
createdAt: m
|
|
706
|
-
}), v =
|
|
707
|
-
effectId:
|
|
708
|
-
requestJson:
|
|
711
|
+
}), v = B(this.db, {
|
|
712
|
+
effectId: r,
|
|
713
|
+
requestJson: a,
|
|
709
714
|
action: "revoke",
|
|
710
|
-
previous:
|
|
715
|
+
previous: f,
|
|
711
716
|
scopeId: t,
|
|
712
|
-
deviceId:
|
|
717
|
+
deviceId: n,
|
|
713
718
|
status: "revoked",
|
|
714
719
|
membershipEpoch: h,
|
|
715
720
|
updatedAt: m,
|
|
716
|
-
actorType:
|
|
717
|
-
actorId:
|
|
721
|
+
actorType: A(e.actorType),
|
|
722
|
+
actorId: A(e.actorId),
|
|
718
723
|
createdAt: m
|
|
719
724
|
});
|
|
720
725
|
try {
|
|
721
|
-
await
|
|
726
|
+
await W(this.db, [
|
|
722
727
|
g,
|
|
723
728
|
_,
|
|
724
729
|
v
|
|
725
730
|
]);
|
|
726
731
|
} catch (e) {
|
|
727
|
-
let
|
|
728
|
-
if (
|
|
732
|
+
let r = await this.getScopeMembership(t, n);
|
|
733
|
+
if (f && (!r || r.membership_epoch !== f.membership_epoch || r.status !== f.status)) return !1;
|
|
729
734
|
throw e;
|
|
730
735
|
}
|
|
731
|
-
let y = await
|
|
736
|
+
let y = await V(this.db, r);
|
|
732
737
|
if (!y) throw Error("scope membership revoke receipt was not persisted.");
|
|
733
|
-
return
|
|
738
|
+
return l(y, "revoke", a), y.outcome_applied === 1;
|
|
734
739
|
}
|
|
735
740
|
async listScopeMemberships(e, t = !1) {
|
|
736
|
-
let n =
|
|
741
|
+
let n = A(e);
|
|
737
742
|
if (!n) throw Error("scopeId is required.");
|
|
738
743
|
let r = t ? "" : "AND status = 'active'";
|
|
739
|
-
return (await
|
|
744
|
+
return (await q(this.db.prepare(`SELECT scope_id, device_id, role, status, membership_epoch, coordinator_id, group_id,
|
|
740
745
|
manifest_issuer_device_id, manifest_hash, signed_manifest_json, updated_at
|
|
741
746
|
FROM coordinator_scope_memberships
|
|
742
747
|
WHERE scope_id = ? ${r}
|
|
743
|
-
ORDER BY device_id ASC`).bind(n))).map((e) =>
|
|
748
|
+
ORDER BY device_id ASC`).bind(n))).map((e) => g(e));
|
|
744
749
|
}
|
|
745
750
|
async listScopeMembershipAuditEvents(e) {
|
|
746
|
-
let t =
|
|
751
|
+
let t = A(e.scopeId);
|
|
747
752
|
if (!t) throw Error("scopeId is required.");
|
|
748
|
-
let n =
|
|
753
|
+
let n = A(e.deviceId), r = z(e.limit), i = n ? "scope_id = ? AND device_id = ?" : "scope_id = ?", a = n ? [
|
|
749
754
|
t,
|
|
750
755
|
n,
|
|
751
756
|
r
|
|
752
757
|
] : [t, r];
|
|
753
|
-
return (await
|
|
758
|
+
return (await q(this.db.prepare(`SELECT event_id, effect_id, action, scope_id, device_id, role, status, membership_epoch,
|
|
754
759
|
previous_role, previous_status, previous_membership_epoch,
|
|
755
760
|
coordinator_id, group_id, actor_type, actor_id, manifest_hash, created_at
|
|
756
761
|
FROM coordinator_scope_membership_audit_log
|
|
757
762
|
WHERE ${i}
|
|
758
763
|
ORDER BY event_id ASC
|
|
759
|
-
LIMIT ?`).bind(...a))).map((e) =>
|
|
764
|
+
LIMIT ?`).bind(...a))).map((e) => g(e));
|
|
760
765
|
}
|
|
761
766
|
async getBootstrapGrant(e) {
|
|
762
|
-
let t = await
|
|
763
|
-
return t ?
|
|
767
|
+
let t = await J(this.db.prepare("SELECT grant_id, group_id, seed_device_id, worker_device_id, expires_at, created_at, created_by, revoked_at\n FROM coordinator_bootstrap_grants WHERE grant_id = ?").bind(e));
|
|
768
|
+
return t ? g(t) : null;
|
|
764
769
|
}
|
|
765
770
|
async listBootstrapGrants(e) {
|
|
766
|
-
return (await
|
|
771
|
+
return (await q(this.db.prepare("SELECT grant_id, group_id, seed_device_id, worker_device_id, expires_at, created_at, created_by, revoked_at\n FROM coordinator_bootstrap_grants WHERE group_id = ?\n ORDER BY created_at DESC, grant_id DESC").bind(e))).map((e) => g(e));
|
|
767
772
|
}
|
|
768
|
-
async revokeBootstrapGrant(e, t =
|
|
769
|
-
return await
|
|
773
|
+
async revokeBootstrapGrant(e, t = x()) {
|
|
774
|
+
return await Y(this.db.prepare("UPDATE coordinator_bootstrap_grants\n SET revoked_at = COALESCE(revoked_at, ?)\n WHERE grant_id = ?").bind(t, e)) > 0;
|
|
770
775
|
}
|
|
771
776
|
async listReciprocalApprovals(e) {
|
|
772
777
|
let t = e.direction === "incoming" ? "requested_device_id" : "requesting_device_id", n = e.status?.trim() || "pending";
|
|
773
|
-
return (await
|
|
778
|
+
return (await q(this.db.prepare(`SELECT request_id, group_id, requesting_device_id, requested_device_id, status, created_at, resolved_at
|
|
774
779
|
FROM coordinator_reciprocal_approvals
|
|
775
780
|
WHERE group_id = ? AND ${t} = ? AND status = ?
|
|
776
|
-
ORDER BY created_at ASC, request_id ASC`).bind(e.groupId, e.deviceId, n))).map((e) =>
|
|
781
|
+
ORDER BY created_at ASC, request_id ASC`).bind(e.groupId, e.deviceId, n))).map((e) => g(e));
|
|
777
782
|
}
|
|
778
783
|
async upsertPresence(e) {
|
|
779
|
-
let t = /* @__PURE__ */ new Date(), n = new Date(t.getTime() + e.ttlS * 1e3).toISOString(), r =
|
|
784
|
+
let t = /* @__PURE__ */ new Date(), n = new Date(t.getTime() + e.ttlS * 1e3).toISOString(), r = b([], e.addresses);
|
|
780
785
|
return await this.db.prepare("INSERT INTO presence_records(group_id, device_id, addresses_json, last_seen_at, expires_at, capabilities_json)\n VALUES (?, ?, ?, ?, ?, ?)\n ON CONFLICT(group_id, device_id) DO UPDATE SET\n addresses_json = excluded.addresses_json,\n last_seen_at = excluded.last_seen_at,\n expires_at = excluded.expires_at,\n capabilities_json = excluded.capabilities_json").bind(e.groupId, e.deviceId, JSON.stringify(r), t.toISOString(), n, JSON.stringify(e.capabilities ?? {})).run(), {
|
|
781
786
|
group_id: e.groupId,
|
|
782
787
|
device_id: e.deviceId,
|
|
@@ -785,14 +790,14 @@ var Y = class {
|
|
|
785
790
|
};
|
|
786
791
|
}
|
|
787
792
|
async listGroupPeers(e, t) {
|
|
788
|
-
let n =
|
|
789
|
-
return (await
|
|
793
|
+
let n = x();
|
|
794
|
+
return (await q(this.db.prepare("SELECT enrolled_devices.device_id, enrolled_devices.public_key, enrolled_devices.fingerprint, enrolled_devices.display_name,\n presence_records.addresses_json, presence_records.last_seen_at, presence_records.expires_at,\n presence_records.capabilities_json\n FROM enrolled_devices\n LEFT JOIN presence_records\n ON presence_records.group_id = enrolled_devices.group_id\n AND presence_records.device_id = enrolled_devices.device_id\n WHERE enrolled_devices.group_id = ?\n AND enrolled_devices.enabled = 1\n AND enrolled_devices.device_id != ?\n ORDER BY enrolled_devices.device_id ASC").bind(e, t))).map((e) => {
|
|
790
795
|
let t = String(e.expires_at ?? "").trim(), r = !0;
|
|
791
796
|
if (t) {
|
|
792
797
|
let e = new Date(t);
|
|
793
798
|
r = Number.isNaN(e.getTime()) || e.toISOString() <= n;
|
|
794
799
|
}
|
|
795
|
-
let i = r ? [] :
|
|
800
|
+
let i = r ? [] : b([], JSON.parse(String(e.addresses_json ?? "[]")));
|
|
796
801
|
return {
|
|
797
802
|
device_id: String(e.device_id ?? ""),
|
|
798
803
|
public_key: String(e.public_key ?? ""),
|
|
@@ -809,9 +814,9 @@ var Y = class {
|
|
|
809
814
|
};
|
|
810
815
|
//#endregion
|
|
811
816
|
//#region src/d1-coordinator-runtime.ts
|
|
812
|
-
function
|
|
813
|
-
return
|
|
814
|
-
storeFactory: () => new
|
|
817
|
+
function Z(e) {
|
|
818
|
+
return m({
|
|
819
|
+
storeFactory: () => new X(e.db),
|
|
815
820
|
runtime: {
|
|
816
821
|
adminSecret: () => String(e.adminSecret ?? "").trim() || null,
|
|
817
822
|
now: e.now ?? (() => (/* @__PURE__ */ new Date()).toISOString())
|
|
@@ -820,6 +825,6 @@ function X(e) {
|
|
|
820
825
|
});
|
|
821
826
|
}
|
|
822
827
|
//#endregion
|
|
823
|
-
export {
|
|
828
|
+
export { X as D1CoordinatorStore, f as DEFAULT_TIME_WINDOW_S, Z as createD1CoordinatorApp };
|
|
824
829
|
|
|
825
830
|
//# sourceMappingURL=cloudflare-coordinator.js.map
|