@seed-hypermedia/client 0.0.68 → 0.0.70

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. package/LICENSE +201 -0
  2. package/dist/auth.d.ts +79 -0
  3. package/dist/auth.mjs +266 -0
  4. package/dist/auto-link.d.ts +3 -2
  5. package/dist/auto-link.mjs +21 -0
  6. package/dist/base64.mjs +9 -0
  7. package/dist/blobs.d.ts +170 -0
  8. package/dist/blobs.mjs +51 -0
  9. package/dist/block-diff.mjs +17 -0
  10. package/dist/blocks-to-markdown.mjs +22 -0
  11. package/dist/capability.d.ts +7 -2
  12. package/dist/capability.mjs +14 -0
  13. package/dist/cbor.d.ts +13 -0
  14. package/dist/cbor.mjs +13 -0
  15. package/dist/change.d.ts +8 -7
  16. package/dist/change.mjs +24 -0
  17. package/dist/chunk-2MD3FXKB.mjs +14 -0
  18. package/dist/chunk-37WRGUK4.mjs +125 -0
  19. package/dist/chunk-3KESF5XJ.mjs +297 -0
  20. package/dist/chunk-3TPCJCVO.mjs +24 -0
  21. package/dist/chunk-4Q3SAL5R.mjs +75 -0
  22. package/dist/chunk-52P3E6H6.mjs +459 -0
  23. package/dist/chunk-BUPMCRR4.mjs +48 -0
  24. package/dist/chunk-CBELM7KX.mjs +29 -0
  25. package/dist/{chunk-KCR3277A.mjs → chunk-DBHL3BYB.mjs} +9 -14
  26. package/dist/chunk-DR3HJ6BV.mjs +25 -0
  27. package/dist/chunk-EU36RFR3.mjs +159 -0
  28. package/dist/chunk-EY6RQVAP.mjs +138 -0
  29. package/dist/chunk-H7L4TI6X.mjs +705 -0
  30. package/dist/chunk-HBIVZA6C.mjs +102 -0
  31. package/dist/chunk-IT5LLTMP.mjs +209 -0
  32. package/dist/chunk-JT57MV7Y.mjs +136 -0
  33. package/dist/chunk-KO4XH5L2.mjs +475 -0
  34. package/dist/chunk-LVAHHQIK.mjs +41 -0
  35. package/dist/chunk-LYECQKZE.mjs +49 -0
  36. package/dist/chunk-N3P7S5YZ.mjs +118 -0
  37. package/dist/chunk-NPG76WUS.mjs +348 -0
  38. package/dist/chunk-NYWCMQTH.mjs +412 -0
  39. package/dist/chunk-OZKQOI2X.mjs +334 -0
  40. package/dist/chunk-PJ2RFT5A.mjs +113 -0
  41. package/dist/chunk-PPAY4WZX.mjs +40 -0
  42. package/dist/chunk-S2PVJK2P.mjs +517 -0
  43. package/dist/chunk-SHOAYNJY.mjs +289 -0
  44. package/dist/chunk-T5BTAJPW.mjs +725 -0
  45. package/dist/{chunk-EGCUJ6D6.mjs → chunk-UOL3F6XQ.mjs} +0 -12
  46. package/dist/chunk-VN7F33SS.mjs +108 -0
  47. package/dist/chunk-YNVFRCUH.mjs +171 -0
  48. package/dist/client.d.ts +3 -2
  49. package/dist/client.mjs +15 -0
  50. package/dist/comment.d.ts +5 -4
  51. package/dist/comment.mjs +19 -0
  52. package/dist/contact.d.ts +5 -4
  53. package/dist/contact.mjs +15 -0
  54. package/dist/document-state.mjs +8 -0
  55. package/dist/editor-types.mjs +0 -0
  56. package/dist/editorblock-to-hmblock.mjs +11 -0
  57. package/dist/encryption.mjs +13 -0
  58. package/dist/errors.mjs +11 -0
  59. package/dist/file-to-ipfs.mjs +13 -0
  60. package/dist/grobid.mjs +11 -0
  61. package/dist/hm-resolver.mjs +10 -0
  62. package/dist/hmauth.d.ts +169 -0
  63. package/dist/hmauth.mjs +53 -0
  64. package/dist/hmblock-to-editorblock.mjs +12 -0
  65. package/dist/index.d.ts +2 -0
  66. package/dist/index.mjs +132 -5453
  67. package/dist/keyfile.mjs +19 -0
  68. package/dist/markdown-to-blocks.mjs +15 -0
  69. package/dist/pdf-to-blocks-embedded.mjs +7 -0
  70. package/dist/pdf-to-blocks.mjs +11 -0
  71. package/dist/ref.d.ts +5 -4
  72. package/dist/ref.mjs +13 -0
  73. package/dist/resource-read.mjs +15 -0
  74. package/dist/signer.d.ts +42 -0
  75. package/dist/signer.mjs +9 -0
  76. package/dist/signing.d.ts +10 -2
  77. package/dist/signing.mjs +16 -0
  78. package/dist/tei-to-blocks.mjs +8 -0
  79. package/dist/unicode.mjs +19 -0
  80. package/dist/vault-local.mjs +10 -6
  81. package/dist/vault.d.ts +14 -4
  82. package/dist/vault.mjs +6 -2
  83. package/package.json +18 -28
  84. package/src/index.ts +0 -144
@@ -0,0 +1,289 @@
1
+ import {
2
+ decode,
3
+ encode
4
+ } from "./chunk-2MD3FXKB.mjs";
5
+ import {
6
+ ED25519_VARINT_PREFIX,
7
+ createCapability,
8
+ principalFromEd25519,
9
+ principalFromString,
10
+ principalToString
11
+ } from "./chunk-IT5LLTMP.mjs";
12
+ import {
13
+ encode as encode2
14
+ } from "./chunk-3TPCJCVO.mjs";
15
+
16
+ // src/hmauth.ts
17
+ var PARAM_CLIENT_ID = "client_id";
18
+ var PARAM_REDIRECT_URI = "redirect_uri";
19
+ var PARAM_SESSION_KEY = "session_key";
20
+ var PARAM_STATE = "state";
21
+ var PARAM_TS = "ts";
22
+ var PARAM_PROOF = "proof";
23
+ var PARAM_DATA = "data";
24
+ var PARAM_ERROR = "error";
25
+ var PARAM_EMAIL = "email";
26
+ var PARAM_SITE_NAME = "site_name";
27
+ var DELEGATION_PATH = "/delegate";
28
+ var AUTH_STATE_BYTES = 16;
29
+ var REQUEST_PROOF_MAX_AGE_MS = 24 * 60 * 60 * 1e3;
30
+ var REQUEST_PROOF_FUTURE_SKEW_MS = 60 * 1e3;
31
+ function principalEncode(rawPublicKey) {
32
+ return principalToString(principalFromEd25519(rawPublicKey));
33
+ }
34
+ function principalDecode(principal) {
35
+ const packedPrincipal = principalFromString(principal);
36
+ return new Uint8Array(packedPrincipal.slice(ED25519_VARINT_PREFIX.length));
37
+ }
38
+ async function collectStream(readable) {
39
+ const chunks = [];
40
+ const reader = readable.getReader();
41
+ for (; ; ) {
42
+ const { done, value } = await reader.read();
43
+ if (done) break;
44
+ chunks.push(value);
45
+ }
46
+ const total = chunks.reduce((sum, c) => sum + c.length, 0);
47
+ const result = new Uint8Array(total);
48
+ let offset = 0;
49
+ for (const chunk of chunks) {
50
+ result.set(chunk, offset);
51
+ offset += chunk.length;
52
+ }
53
+ return result;
54
+ }
55
+ async function compress(data) {
56
+ const cs = new CompressionStream("gzip");
57
+ const writer = cs.writable.getWriter();
58
+ writer.write(data);
59
+ writer.close();
60
+ return collectStream(cs.readable);
61
+ }
62
+ function hasProofParam(url) {
63
+ return url.includes(`?${PARAM_PROOF}=`) || url.includes(`&${PARAM_PROOF}=`);
64
+ }
65
+ function stripTrailingProofParam(url) {
66
+ const escapedParam = PARAM_PROOF.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
67
+ const trailingProofPattern = new RegExp(`[?&]${escapedParam}=[^&#]*$`);
68
+ return url.replace(trailingProofPattern, "");
69
+ }
70
+ function ensureProofIsTrailing(url) {
71
+ const signedUrl = stripTrailingProofParam(url);
72
+ if (hasProofParam(url) && signedUrl === url) {
73
+ throw new Error("Invalid delegation request URL: proof must be the final query parameter");
74
+ }
75
+ }
76
+ function isLocalhost(hostname) {
77
+ return hostname === "localhost" || hostname === "127.0.0.1";
78
+ }
79
+ function decodeProofSignature(proof) {
80
+ let signature;
81
+ try {
82
+ signature = decode(proof);
83
+ } catch (error) {
84
+ const message = error instanceof Error ? error.message : String(error);
85
+ throw new Error(`Invalid proof signature encoding: ${message}`);
86
+ }
87
+ if (signature.length !== 64) {
88
+ throw new Error(`Invalid proof signature length: expected 64 bytes, got ${signature.length}`);
89
+ }
90
+ return signature;
91
+ }
92
+ function validateClientId(clientId) {
93
+ let parsed;
94
+ try {
95
+ parsed = new URL(clientId);
96
+ } catch {
97
+ throw new Error(`Invalid client_id: not a valid URL: ${clientId}`);
98
+ }
99
+ if (parsed.protocol !== "https:" && !(parsed.protocol === "http:" && isLocalhost(parsed.hostname))) {
100
+ throw new Error(`Invalid client_id: must use HTTPS (HTTP allowed only for localhost): ${clientId}`);
101
+ }
102
+ if (parsed.pathname !== "/" && parsed.pathname !== "") {
103
+ throw new Error(`Invalid client_id: must not have a path: ${clientId}`);
104
+ }
105
+ if (parsed.search) {
106
+ throw new Error(`Invalid client_id: must not have a query string: ${clientId}`);
107
+ }
108
+ if (parsed.hash) {
109
+ throw new Error(`Invalid client_id: must not have a fragment: ${clientId}`);
110
+ }
111
+ }
112
+ function validateRedirectUri(redirectUri, clientId) {
113
+ let parsed;
114
+ try {
115
+ parsed = new URL(redirectUri);
116
+ } catch {
117
+ throw new Error(`Invalid redirect_uri: not a valid URL: ${redirectUri}`);
118
+ }
119
+ if (parsed.protocol !== "https:" && !(parsed.protocol === "http:" && isLocalhost(parsed.hostname))) {
120
+ throw new Error(`Invalid redirect_uri: must use HTTPS (HTTP allowed only for localhost): ${redirectUri}`);
121
+ }
122
+ const redirectOrigin = parsed.origin;
123
+ const clientOrigin = new URL(clientId).origin;
124
+ if (redirectOrigin !== clientOrigin) {
125
+ throw new Error(`Invalid redirect_uri: origin "${redirectOrigin}" does not match client_id "${clientOrigin}"`);
126
+ }
127
+ }
128
+ function validateSessionKeyPrincipal(sessionKeyPrincipal) {
129
+ try {
130
+ principalFromString(sessionKeyPrincipal);
131
+ } catch {
132
+ throw new Error(`Invalid session_key principal: ${sessionKeyPrincipal}`);
133
+ }
134
+ }
135
+ function validateState(state) {
136
+ const valid = /^[A-Za-z0-9_-]+$/.test(state);
137
+ if (!valid) {
138
+ throw new Error("Invalid state: must be base64url");
139
+ }
140
+ const decoded = decode(state);
141
+ if (decoded.length < AUTH_STATE_BYTES) {
142
+ throw new Error("Invalid state: must be at least 128 bits");
143
+ }
144
+ }
145
+ function validateRequestTimestamp(ts) {
146
+ const parsed = Number(ts);
147
+ if (!Number.isFinite(parsed) || !Number.isInteger(parsed) || parsed <= 0) {
148
+ throw new Error(`Invalid ts: ${ts}`);
149
+ }
150
+ return parsed;
151
+ }
152
+ function parseDelegationRequest(url) {
153
+ const originalUrl = typeof url === "string" ? url : url.toString();
154
+ ensureProofIsTrailing(originalUrl);
155
+ const parsedUrl = new URL(originalUrl);
156
+ const clientId = parsedUrl.searchParams.get(PARAM_CLIENT_ID);
157
+ const redirectUri = parsedUrl.searchParams.get(PARAM_REDIRECT_URI);
158
+ const sessionKey = parsedUrl.searchParams.get(PARAM_SESSION_KEY);
159
+ const state = parsedUrl.searchParams.get(PARAM_STATE);
160
+ const ts = parsedUrl.searchParams.get(PARAM_TS);
161
+ const proof = parsedUrl.searchParams.get(PARAM_PROOF);
162
+ if (!clientId && !redirectUri && !sessionKey) {
163
+ return null;
164
+ }
165
+ if (!clientId) {
166
+ throw new Error(`Missing required parameter: ${PARAM_CLIENT_ID}`);
167
+ }
168
+ if (!redirectUri) {
169
+ throw new Error(`Missing required parameter: ${PARAM_REDIRECT_URI}`);
170
+ }
171
+ if (!sessionKey) {
172
+ throw new Error(`Missing required parameter: ${PARAM_SESSION_KEY}`);
173
+ }
174
+ if (!state) {
175
+ throw new Error(`Missing required parameter: ${PARAM_STATE}`);
176
+ }
177
+ if (!ts) {
178
+ throw new Error(`Missing required parameter: ${PARAM_TS}`);
179
+ }
180
+ if (!proof) {
181
+ throw new Error(`Missing required parameter: ${PARAM_PROOF}`);
182
+ }
183
+ validateClientId(clientId);
184
+ validateRedirectUri(redirectUri, clientId);
185
+ validateSessionKeyPrincipal(sessionKey);
186
+ validateState(state);
187
+ const requestTs = validateRequestTimestamp(ts);
188
+ const email = parsedUrl.searchParams.get(PARAM_EMAIL) || void 0;
189
+ const siteName = parsedUrl.searchParams.get(PARAM_SITE_NAME) || void 0;
190
+ return {
191
+ originalUrl,
192
+ clientId,
193
+ redirectUri,
194
+ sessionKeyPrincipal: sessionKey,
195
+ state,
196
+ requestTs,
197
+ proof,
198
+ vaultOrigin: parsedUrl.origin,
199
+ email,
200
+ siteName
201
+ };
202
+ }
203
+ function buildSignedRequestPayload(request, vaultOrigin) {
204
+ if (request.vaultOrigin !== vaultOrigin) {
205
+ throw new Error("Delegation request vault origin mismatch");
206
+ }
207
+ ensureProofIsTrailing(request.originalUrl);
208
+ const signedUrl = stripTrailingProofParam(request.originalUrl);
209
+ return new TextEncoder().encode(signedUrl);
210
+ }
211
+ async function verifyDelegationRequestProof(request, vaultOrigin, now = Date.now()) {
212
+ if (request.requestTs < now - REQUEST_PROOF_MAX_AGE_MS) {
213
+ throw new Error("Delegation request proof expired");
214
+ }
215
+ if (request.requestTs > now + REQUEST_PROOF_FUTURE_SKEW_MS) {
216
+ throw new Error("Delegation request proof timestamp is in the future");
217
+ }
218
+ const signature = decodeProofSignature(request.proof);
219
+ const rawPubKey = principalDecode(request.sessionKeyPrincipal);
220
+ const payload = buildSignedRequestPayload(request, vaultOrigin);
221
+ const publicKey = await crypto.subtle.importKey(
222
+ "raw",
223
+ rawPubKey,
224
+ "Ed25519",
225
+ false,
226
+ ["verify"]
227
+ );
228
+ let valid;
229
+ try {
230
+ valid = await crypto.subtle.verify(
231
+ "Ed25519",
232
+ publicKey,
233
+ signature,
234
+ payload
235
+ );
236
+ } catch (error) {
237
+ const message = error instanceof Error ? error.message : String(error);
238
+ throw new Error(`Failed to verify request proof signature: ${message}`);
239
+ }
240
+ if (!valid) {
241
+ throw new Error("Request proof signature does not match session key");
242
+ }
243
+ }
244
+ async function buildCallbackUrl(redirectUri, state, accountPrincipal, capability, profile, notifyServerUrl) {
245
+ const url = new URL(redirectUri);
246
+ const callbackData = {
247
+ account: accountPrincipal,
248
+ capability: capability.decoded,
249
+ capabilityCid: capability.cid,
250
+ profile: profile.decoded,
251
+ profileCid: profile.cid,
252
+ ...notifyServerUrl ? { notifyServerUrl } : {}
253
+ };
254
+ const encodedCbor = encode2(callbackData);
255
+ const compressed = await compress(new Uint8Array(encodedCbor));
256
+ url.searchParams.set(PARAM_DATA, encode(compressed));
257
+ url.searchParams.set(PARAM_STATE, state);
258
+ return url.toString();
259
+ }
260
+ async function createDelegation(issuer, sessionKeyPrincipal, clientId) {
261
+ return await createCapability(issuer, sessionKeyPrincipal, "AGENT", Date.now(), {
262
+ label: `Session key for ${clientId}`
263
+ });
264
+ }
265
+
266
+ export {
267
+ PARAM_CLIENT_ID,
268
+ PARAM_REDIRECT_URI,
269
+ PARAM_SESSION_KEY,
270
+ PARAM_STATE,
271
+ PARAM_TS,
272
+ PARAM_PROOF,
273
+ PARAM_DATA,
274
+ PARAM_ERROR,
275
+ PARAM_EMAIL,
276
+ PARAM_SITE_NAME,
277
+ DELEGATION_PATH,
278
+ principalEncode,
279
+ principalDecode,
280
+ validateClientId,
281
+ validateRedirectUri,
282
+ validateSessionKeyPrincipal,
283
+ validateState,
284
+ validateRequestTimestamp,
285
+ parseDelegationRequest,
286
+ verifyDelegationRequestProof,
287
+ buildCallbackUrl,
288
+ createDelegation
289
+ };