@cookiemunch/sdk 0.37.0 → 0.39.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/CHANGELOG.md +17 -0
- package/dist/index.cjs +19 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +172 -6
- package/dist/index.js +19 -2
- package/dist/index.js.map +1 -1
- package/dist/rights.cjs +88 -17
- package/dist/rights.cjs.map +1 -1
- package/dist/rights.d.ts +58 -3
- package/dist/rights.js +85 -17
- package/dist/rights.js.map +1 -1
- package/dist/types.d.ts +14 -0
- package/package.json +1 -1
package/dist/rights.cjs
CHANGED
|
@@ -31,11 +31,14 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
31
31
|
var rights_exports = {};
|
|
32
32
|
__export(rights_exports, {
|
|
33
33
|
IDEMPOTENCY_TTL_SECONDS: () => IDEMPOTENCY_TTL_SECONDS,
|
|
34
|
+
RIGHTS_STORE_PRISMA_MODEL: () => RIGHTS_STORE_PRISMA_MODEL,
|
|
34
35
|
RIGHTS_STORE_TABLE_SQL: () => RIGHTS_STORE_TABLE_SQL,
|
|
35
36
|
RIGHTS_STORE_TABLE_SQLITE: () => RIGHTS_STORE_TABLE_SQLITE,
|
|
36
37
|
createRightsHandlers: () => createRightsHandlers,
|
|
37
38
|
fileStore: () => fileStore,
|
|
38
39
|
ioredisStore: () => ioredisStore,
|
|
40
|
+
lookupByRule: () => lookupByRule,
|
|
41
|
+
prismaStore: () => prismaStore,
|
|
39
42
|
redisStore: () => redisStore,
|
|
40
43
|
sqlStore: () => sqlStore
|
|
41
44
|
});
|
|
@@ -45,6 +48,23 @@ module.exports = __toCommonJS(rights_exports);
|
|
|
45
48
|
var CANARY_ISSUER = "https://canary.cookiemunch.net";
|
|
46
49
|
|
|
47
50
|
// src/rights.ts
|
|
51
|
+
async function lookupByRule(f, input) {
|
|
52
|
+
const sub = input.identity?.sub;
|
|
53
|
+
if (sub && f.bySubject) {
|
|
54
|
+
const id = await f.bySubject(sub, input.identity, input.scope);
|
|
55
|
+
if (typeof id === "string" && id) return id;
|
|
56
|
+
}
|
|
57
|
+
for (const email of input.emails) {
|
|
58
|
+
const found = await f.byEmail(email, input.scope);
|
|
59
|
+
const list = found == null ? [] : Array.isArray(found) ? found : [found];
|
|
60
|
+
for (const m of list) {
|
|
61
|
+
const id = typeof m === "string" ? m : m?.id;
|
|
62
|
+
const other = typeof m === "object" && m?.subject && sub && m.subject !== sub;
|
|
63
|
+
if (typeof id === "string" && id && !other) return id;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
48
68
|
var IDEMPOTENCY_TTL_SECONDS = 24 * 60 * 60;
|
|
49
69
|
var CANONICAL = {
|
|
50
70
|
access: "access",
|
|
@@ -110,7 +130,9 @@ function createRightsHandlers(o) {
|
|
|
110
130
|
if (typeof o.key !== "string" || !o.key) throw new Error("createRightsHandlers: key is required");
|
|
111
131
|
if (o.key.length < 24) console.warn("createRightsHandlers: key is shorter than 24 characters; rotate it to a longer one (PATCH /v1/dsar/executors/{id} {secretKey}) so it cannot be guessed");
|
|
112
132
|
if (typeof o.idSecret !== "string" || o.idSecret.length < 32) throw new Error("createRightsHandlers: idSecret must be at least 32 characters");
|
|
113
|
-
|
|
133
|
+
const finders = typeof o.lookup === "object" && o.lookup !== null ? o.lookup : null;
|
|
134
|
+
if (finders ? typeof finders.byEmail !== "function" : typeof o.lookup !== "function") throw new Error("createRightsHandlers: lookup is required (a function, or { bySubject?, byEmail })");
|
|
135
|
+
const find = finders ? (i) => lookupByRule(finders, i) : o.lookup;
|
|
114
136
|
const subtle = globalThis.crypto?.subtle;
|
|
115
137
|
if (!subtle) throw new Error("createRightsHandlers: needs Web Crypto (globalThis.crypto.subtle)");
|
|
116
138
|
const store = o.store ?? /* @__PURE__ */ new Map();
|
|
@@ -140,10 +162,40 @@ function createRightsHandlers(o) {
|
|
|
140
162
|
return null;
|
|
141
163
|
}
|
|
142
164
|
}
|
|
143
|
-
const getStored = async (id) => {
|
|
165
|
+
const getStored = async (id, signed) => {
|
|
144
166
|
const v = await store.get(`req:${id}`);
|
|
145
|
-
return v ? JSON.parse(v) : null;
|
|
167
|
+
return v ? JSON.parse(v) : signed ?? null;
|
|
146
168
|
};
|
|
169
|
+
const sl = o.stateless === true ? {} : o.stateless || null;
|
|
170
|
+
const jwksUrl = sl?.jwksUrl ?? "https://api.cookiemunch.net/v1/partner/jwks.json";
|
|
171
|
+
let keys = null;
|
|
172
|
+
let keysAt = 0;
|
|
173
|
+
const loadKeys = async (jwks) => {
|
|
174
|
+
const list = jwks?.keys ?? (await (await globalThis.fetch(jwksUrl)).json()).keys;
|
|
175
|
+
keys = new Map(list.filter((k) => k.kty === "OKP" && k.crv === "Ed25519").map((k) => [k.kid ?? "", subtle.importKey("jwk", k, { name: "Ed25519" }, false, ["verify"])]));
|
|
176
|
+
keysAt = now();
|
|
177
|
+
};
|
|
178
|
+
const STATE_STATUSES = /* @__PURE__ */ new Set([...OUTCOMES, "scheduled", "cancelled"]);
|
|
179
|
+
async function signedState(req, id) {
|
|
180
|
+
const jws = sl && req.headers.get("cookiemunch-request-state");
|
|
181
|
+
if (!jws) return null;
|
|
182
|
+
try {
|
|
183
|
+
const [h, p, sig, extra] = jws.split(".");
|
|
184
|
+
if (!h || !p || !sig || extra !== void 0) return null;
|
|
185
|
+
const header = JSON.parse(dec.decode(fromB64url(h) ?? new Uint8Array()));
|
|
186
|
+
if (header.alg !== "EdDSA" || !header.kid) return null;
|
|
187
|
+
if (!keys || !keys.has(header.kid) && !sl.jwks && now() - keysAt > 3e5) await loadKeys(sl.jwks);
|
|
188
|
+
const key = keys.get(header.kid);
|
|
189
|
+
const sigBytes = fromB64url(sig);
|
|
190
|
+
if (!key || !sigBytes || !await subtle.verify({ name: "Ed25519" }, await key, sigBytes, enc.encode(`${h}.${p}`))) return null;
|
|
191
|
+
const cl = JSON.parse(dec.decode(fromB64url(p)));
|
|
192
|
+
const t = now() / 1e3;
|
|
193
|
+
if (cl.id !== id || typeof cl.exp !== "number" || cl.exp < t || cl.exp > t + 900 || !cl.status || !STATE_STATUSES.has(cl.status)) return null;
|
|
194
|
+
return { status: cl.status, ...cl.status === "scheduled" && cl.executeAfter ? { eta: cl.executeAfter } : {} };
|
|
195
|
+
} catch {
|
|
196
|
+
return null;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
147
199
|
const putStored = async (id, s) => {
|
|
148
200
|
await store.set(`req:${id}`, JSON.stringify(s));
|
|
149
201
|
};
|
|
@@ -227,14 +279,17 @@ function createRightsHandlers(o) {
|
|
|
227
279
|
return bad("identity must be { iss, sub, ext?, principal? }", "identity");
|
|
228
280
|
}
|
|
229
281
|
const identity = isObject(raw) ? raw : void 0;
|
|
282
|
+
if (body.scope !== void 0 && !isObject(body.scope)) return bad("scope must be an object", "scope");
|
|
283
|
+
const scope = isObject(body.scope) ? body.scope : void 0;
|
|
284
|
+
const echo = scope ? { scope } : {};
|
|
230
285
|
const canaryByEmail = emails.map((e) => CANARY_EMAIL.exec(e.toLowerCase())?.[1]).find(Boolean);
|
|
231
286
|
const canaryExt = typeof identity?.ext === "string" ? CANARY_EXT.exec(identity.ext)?.[1] : void 0;
|
|
232
287
|
if (canaryByEmail || canaryExt || identity?.iss === CANARY_ISSUER) {
|
|
233
288
|
const tag = identity ? (identity.iss === CANARY_ISSUER ? CANARY_SUBJECT.exec(identity.sub)?.[1] : void 0) ?? canaryExt : canaryByEmail;
|
|
234
|
-
return json(200, tag ? { subjectId: `canary_${tag}`, status: "found" } : { subjectId: null, status: "no_match" });
|
|
289
|
+
return json(200, tag ? { subjectId: `canary_${tag}`, status: "found", ...echo } : { subjectId: null, status: "no_match", ...echo });
|
|
235
290
|
}
|
|
236
|
-
const subjectId = await
|
|
237
|
-
return json(200, typeof subjectId === "string" && subjectId ? { subjectId, status: "found" } : { subjectId: null, status: "no_match" });
|
|
291
|
+
const subjectId = await find({ emails, ...identity ? { identity } : {}, ...scope ? { scope } : {} });
|
|
292
|
+
return json(200, typeof subjectId === "string" && subjectId ? { subjectId, status: "found", ...echo } : { subjectId: null, status: "no_match", ...echo });
|
|
238
293
|
}
|
|
239
294
|
async function open(body) {
|
|
240
295
|
const word = typeof body.right === "string" && CANONICAL[body.right] ? body.right : body.type;
|
|
@@ -259,21 +314,21 @@ function createRightsHandlers(o) {
|
|
|
259
314
|
}
|
|
260
315
|
return json(201, view(id, c, await act(id, c, s, cb)));
|
|
261
316
|
}
|
|
262
|
-
async function read(id, c) {
|
|
263
|
-
let s = await getStored(id);
|
|
317
|
+
async function read(id, c, signed) {
|
|
318
|
+
let s = await getStored(id, signed);
|
|
264
319
|
if (!s) {
|
|
265
320
|
return json(200, view(id, c, { status: "rejected", detail: "this system no longer holds the request's state; nothing was done. Open it again." }));
|
|
266
321
|
}
|
|
267
322
|
if ((s.status === "pending" || s.status === "processing" || s.status === "blocked") && o.status && !CANARY_SUBJECT.test(c.s)) s = await act(id, c, s, o.status);
|
|
268
323
|
return json(200, view(id, c, s));
|
|
269
324
|
}
|
|
270
|
-
async function run(id, c) {
|
|
271
|
-
const s = await getStored(id);
|
|
325
|
+
async function run(id, c, signed) {
|
|
326
|
+
const s = await getStored(id, signed);
|
|
272
327
|
if (s?.status === "scheduled" && c.a !== void 0 && now() >= c.a) return json(200, view(id, c, await act(id, c, s, callbackFor(c.t))));
|
|
273
|
-
return read(id, c);
|
|
328
|
+
return read(id, c, signed);
|
|
274
329
|
}
|
|
275
|
-
async function cancel(id, c) {
|
|
276
|
-
const s = await getStored(id);
|
|
330
|
+
async function cancel(id, c, signed) {
|
|
331
|
+
const s = await getStored(id, signed);
|
|
277
332
|
if (!s) return json(409, { error: "this system no longer holds the request's state", field: "id" });
|
|
278
333
|
if (s.status === "cancelled") return json(200, view(id, c, s));
|
|
279
334
|
if (s.status !== "scheduled" && s.status !== "pending") return json(409, { error: `too late to cancel a ${s.status} request`, field: "id" });
|
|
@@ -324,9 +379,10 @@ function createRightsHandlers(o) {
|
|
|
324
379
|
}
|
|
325
380
|
const c = await readId(id);
|
|
326
381
|
if (!c) return json(404, { error: "unknown request" });
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
else if (
|
|
382
|
+
const signed = await signedState(req, id);
|
|
383
|
+
if (action === "cancel" && method === "POST") res = await cancel(id, c, signed);
|
|
384
|
+
else if (action === "run" && method === "POST") res = await run(id, c, signed);
|
|
385
|
+
else if (!action && method === "GET") res = await read(id, c, signed);
|
|
330
386
|
else return json(404, { error: "not_found" });
|
|
331
387
|
} else return json(404, { error: "not_found" });
|
|
332
388
|
if (idemKey !== null && res.ok) {
|
|
@@ -342,7 +398,7 @@ function createRightsHandlers(o) {
|
|
|
342
398
|
async function complete(requestId, outcome) {
|
|
343
399
|
const c = await readId(requestId);
|
|
344
400
|
if (!c) throw new Error("createRightsHandlers.complete: not a request id these handlers issued");
|
|
345
|
-
const s = await getStored(requestId);
|
|
401
|
+
const s = await getStored(requestId) ?? (sl ? { status: "processing" } : null);
|
|
346
402
|
if (!s) throw new Error("createRightsHandlers.complete: this system no longer holds the request's state");
|
|
347
403
|
if (FINAL.has(s.status)) return { status: s.status, delivered: await notify(requestId, c, s) };
|
|
348
404
|
const next = await record(requestId, s, outcome ?? { status: "completed" });
|
|
@@ -436,6 +492,18 @@ function sqlStore(o) {
|
|
|
436
492
|
}
|
|
437
493
|
};
|
|
438
494
|
}
|
|
495
|
+
var RIGHTS_STORE_PRISMA_MODEL = `model CookiemunchRightsStore {
|
|
496
|
+
key String @id
|
|
497
|
+
value String
|
|
498
|
+
expiresAt DateTime? @map("expires_at") @db.Timestamptz(6)
|
|
499
|
+
|
|
500
|
+
@@map("cookiemunch_rights_store")
|
|
501
|
+
}`;
|
|
502
|
+
function prismaStore(prisma) {
|
|
503
|
+
return sqlStore({
|
|
504
|
+
query: async (sql, params) => /^\s*select\b/i.test(sql) ? await prisma.$queryRawUnsafe(sql, ...params) : (await prisma.$executeRawUnsafe(sql, ...params), [])
|
|
505
|
+
});
|
|
506
|
+
}
|
|
439
507
|
function redisStore(client) {
|
|
440
508
|
const k = (key) => `cookiemunch:rights:${key}`;
|
|
441
509
|
return {
|
|
@@ -463,11 +531,14 @@ function ioredisStore(client) {
|
|
|
463
531
|
// Annotate the CommonJS export names for ESM import in node:
|
|
464
532
|
0 && (module.exports = {
|
|
465
533
|
IDEMPOTENCY_TTL_SECONDS,
|
|
534
|
+
RIGHTS_STORE_PRISMA_MODEL,
|
|
466
535
|
RIGHTS_STORE_TABLE_SQL,
|
|
467
536
|
RIGHTS_STORE_TABLE_SQLITE,
|
|
468
537
|
createRightsHandlers,
|
|
469
538
|
fileStore,
|
|
470
539
|
ioredisStore,
|
|
540
|
+
lookupByRule,
|
|
541
|
+
prismaStore,
|
|
471
542
|
redisStore,
|
|
472
543
|
sqlStore
|
|
473
544
|
});
|