@fedify/fedify 2.0.0-pr.412.1559 → 2.0.0-pr.412.1794

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 (204) hide show
  1. package/README.md +3 -0
  2. package/dist/{actor-Cc6B76eG.js → actor-BXHc5r-q.js} +1 -1
  3. package/dist/actor-Ba9Z1eNJ.cjs +42079 -0
  4. package/dist/{actor-C22bXuuC.d.ts → actor-DqFajh9s.d.ts} +2 -2
  5. package/dist/{actor-CTAuCsWy.js → actor-DzhunPC_.js} +7058 -2722
  6. package/dist/actor-f2NtjyCg.d.cts +128 -0
  7. package/dist/{assert_rejects-DiIiJbZn.js → assert_rejects-Ce45JcFg.js} +1 -1
  8. package/dist/{assert_is_error-BPGph1Jx.js → assert_throws-BNXdRGWP.js} +31 -1
  9. package/dist/{builder-BOEBObR8.js → builder-u7usRsT3.js} +13 -8
  10. package/dist/chunk-DqRYRqnO.cjs +34 -0
  11. package/dist/client-94iWEfQa.d.cts +222 -0
  12. package/dist/{client-DF8anIB5.d.ts → client-BsGzbnV-.d.ts} +3 -75
  13. package/dist/{client-De-z2UnN.js → client-pY7-3icS.js} +3 -22
  14. package/dist/compat/mod.cjs +10 -0
  15. package/dist/compat/mod.d.cts +12 -0
  16. package/dist/compat/mod.d.ts +11 -12
  17. package/dist/compat/mod.js +5 -5
  18. package/dist/compat/transformers.test.js +20 -20
  19. package/dist/compat-DmDDELst.cjs +4 -0
  20. package/dist/compat-nxUqe4Z-.js +4 -0
  21. package/dist/{context-9gCpIkiz.d.ts → context-DG0huGW-.d.ts} +184 -127
  22. package/dist/context-DJ8aSy2Q.d.cts +2312 -0
  23. package/dist/{authdocloader-CLgDGafZ.js → docloader-CrbAy9Oc.js} +17 -8
  24. package/dist/{esm-CHdxdkuH.js → esm-C-Qa1zEM.js} +11 -11
  25. package/dist/federation/builder.test.js +9 -12
  26. package/dist/federation/collection.test.js +7 -9
  27. package/dist/federation/handler.test.js +27 -148
  28. package/dist/federation/idempotency.test.js +201 -0
  29. package/dist/federation/inbox.test.js +4 -6
  30. package/dist/federation/keycache.test.js +3 -4
  31. package/dist/federation/kv.test.js +7 -8
  32. package/dist/federation/middleware.test.js +242 -64
  33. package/dist/federation/mod.cjs +27 -0
  34. package/dist/federation/mod.d.cts +12 -0
  35. package/dist/federation/mod.d.ts +12 -13
  36. package/dist/federation/mod.js +13 -15
  37. package/dist/federation/mq.test.js +8 -10
  38. package/dist/federation/negotiation.test.js +26 -0
  39. package/dist/federation/retry.test.js +4 -5
  40. package/dist/federation/router.test.js +6 -8
  41. package/dist/federation/send.test.js +13 -15
  42. package/dist/federation-CRpdnOMS.cjs +244 -0
  43. package/dist/{federation-CMX7WzeL.js → federation-jcR8-ZxP.js} +3 -3
  44. package/dist/fixtures/media.example.com/avatars/test-avatar.jpg.json +6 -0
  45. package/dist/{http-DqSNLFNY.d.ts → http-BbO0ejuk.d.ts} +2 -2
  46. package/dist/{http-l0TEupZK.js → http-CUVx-vzb.js} +260 -10
  47. package/dist/{http-CcdM1brU.js → http-D2kIm9la.js} +3 -3
  48. package/dist/http-Dofes42e.cjs +1106 -0
  49. package/dist/http-M8k5mKc0.d.cts +253 -0
  50. package/dist/{inbox-B6DZbGNf.js → inbox-HjG5peXY.js} +24 -7
  51. package/dist/{key-CzLv1phF.js → key-CxdLUFS6.js} +3 -2
  52. package/dist/{keycache-B8HdZJSt.js → keycache-bU7COwsh.js} +1 -1
  53. package/dist/{keys-zZwiKkfx.js → keys-DMHs8XNn.js} +2 -1
  54. package/dist/kv-Bxr0Q87_.d.cts +81 -0
  55. package/dist/kv-cache-CGXcDejL.js +122 -0
  56. package/dist/kv-cache-DN9pfMBe.js +94 -0
  57. package/dist/kv-cache-Dkvbn6rg.cjs +134 -0
  58. package/dist/{ld-CBcQdZ6N.js → ld-CIhvSCCr.js} +5 -4
  59. package/dist/lookup-CHkCVZTU.js +260 -0
  60. package/dist/lookup-CfFkONZD.cjs +272 -0
  61. package/dist/{type-SK-d7Tbw.js → lookup-DQRtjvb1.js} +7266 -7087
  62. package/dist/middleware-Bot7EEwV.js +26 -0
  63. package/dist/middleware-CFJgmUMW.cjs +15 -0
  64. package/dist/{middleware-BRgu1IH8.js → middleware-CJlj5Olw.js} +86 -181
  65. package/dist/middleware-CjHl2NHA.js +15 -0
  66. package/dist/middleware-D74TgW2E.cjs +4225 -0
  67. package/dist/{middleware-CxEkcFW0.js → middleware-DMk2Mdn9.js} +117 -127
  68. package/dist/mod-0noXw66R.d.cts +107 -0
  69. package/dist/mod-B-hUPT2N.d.cts +1 -0
  70. package/dist/{mod-Drmz72EK.d.ts → mod-B2iOw50L.d.ts} +3 -3
  71. package/dist/mod-B9M-8jm2.d.ts +109 -0
  72. package/dist/{mod-Cxt4Kpf6.d.ts → mod-BlVovdcy.d.ts} +20 -2
  73. package/dist/{mod-Bqxcp7eN.d.ts → mod-BoHnwOCs.d.ts} +2 -2
  74. package/dist/mod-BxRCHTz-.d.cts +307 -0
  75. package/dist/mod-C2tOeRkN.d.cts +1 -0
  76. package/dist/mod-C58MZ7Wx.d.cts +113 -0
  77. package/dist/mod-DJcZDvjA.d.cts +80 -0
  78. package/dist/mod-DgdBYYa0.d.cts +266 -0
  79. package/dist/{mod-DBzN0aCM.d.ts → mod-Ds0mpFZU.d.ts} +1 -1
  80. package/dist/mod.cjs +137 -0
  81. package/dist/mod.d.cts +16 -0
  82. package/dist/mod.d.ts +16 -17
  83. package/dist/mod.js +19 -21
  84. package/dist/mq-DcJPkXD5.d.cts +140 -0
  85. package/dist/negotiation-C4nFufNk.js +71 -0
  86. package/dist/nodeinfo/client.test.js +30 -111
  87. package/dist/nodeinfo/handler.test.js +25 -26
  88. package/dist/nodeinfo/mod.cjs +10 -0
  89. package/dist/nodeinfo/mod.d.cts +3 -0
  90. package/dist/nodeinfo/mod.d.ts +3 -5
  91. package/dist/nodeinfo/mod.js +6 -7
  92. package/dist/nodeinfo/types.test.js +8 -15
  93. package/dist/nodeinfo-BnthBobC.js +4 -0
  94. package/dist/nodeinfo-CdN0rEnZ.cjs +4 -0
  95. package/dist/owner-B4HbyP8s.d.cts +67 -0
  96. package/dist/{owner-VEIjmR8r.js → owner-Dvh7mBvr.js} +3 -2
  97. package/dist/{owner-CQPnQVtf.d.ts → owner-kQRGVXG1.d.ts} +3 -3
  98. package/dist/proof-D0uThUvD.cjs +674 -0
  99. package/dist/{proof-DoSQAGkE.js → proof-Dgy35fzc.js} +3 -3
  100. package/dist/{proof-tgUlT8hw.js → proof-fqzaMJ4g.js} +11 -10
  101. package/dist/{send-BzS7w-QF.js → send-Drp20VO9.js} +2 -2
  102. package/dist/sig/http.test.js +13 -14
  103. package/dist/sig/key.test.js +9 -11
  104. package/dist/sig/ld.test.js +8 -10
  105. package/dist/sig/mod.cjs +28 -0
  106. package/dist/sig/mod.d.cts +6 -0
  107. package/dist/sig/mod.d.ts +5 -7
  108. package/dist/sig/mod.js +8 -10
  109. package/dist/sig/owner.test.js +10 -12
  110. package/dist/sig/proof.test.js +13 -14
  111. package/dist/sig-C34-oHBl.js +4 -0
  112. package/dist/sig-YYj5tCnr.cjs +4 -0
  113. package/dist/testing/docloader.test.js +6 -8
  114. package/dist/testing/mod.d.ts +387 -224
  115. package/dist/testing/mod.js +2 -3
  116. package/dist/{testing-Z2omCvKy.js → testing-g4UC4liW.js} +1 -2
  117. package/dist/{transformers-Dna8Fg7k.js → transformers-BFT6d7J5.js} +3 -3
  118. package/dist/transformers-CoBS-oFG.cjs +116 -0
  119. package/dist/{types-DgPvoUWN.js → types-BtUjyi5y.js} +8 -169
  120. package/dist/{types-BIgY6c-l.js → types-C2XVl6gj.js} +1 -3
  121. package/dist/types-CWgzGaqk.cjs +315 -0
  122. package/dist/{runtime/authdocloader.test.js → utils/docloader.test.js} +14 -15
  123. package/dist/utils/kv-cache.test.js +209 -0
  124. package/dist/utils/mod.cjs +12 -0
  125. package/dist/utils/mod.d.cts +5 -0
  126. package/dist/utils/mod.d.ts +7 -0
  127. package/dist/utils/mod.js +11 -0
  128. package/dist/utils-D-Va7aXC.js +4 -0
  129. package/dist/utils-DyRU1gdZ.cjs +4 -0
  130. package/dist/vocab/actor.test.js +8 -10
  131. package/dist/vocab/lookup.test.js +257 -9
  132. package/dist/vocab/mod.cjs +86 -0
  133. package/dist/vocab/mod.d.cts +4 -0
  134. package/dist/vocab/mod.d.ts +3 -5
  135. package/dist/vocab/mod.js +6 -7
  136. package/dist/vocab/type.test.js +2 -3
  137. package/dist/vocab/vocab.test.js +438 -15
  138. package/dist/{vocab-SOE1ifCr.d.ts → vocab-BCWe1Ih5.d.ts} +292 -21
  139. package/dist/{vocab-CvD6Vbml.js → vocab-BnR3nIU9.js} +26 -17
  140. package/dist/vocab-CeDBzu-f.d.cts +14903 -0
  141. package/dist/{lookup-Bn_HEC_d.js → vocab-DFaWWYDn.cjs} +108 -139
  142. package/dist/webfinger/handler.test.js +24 -25
  143. package/dist/webfinger/lookup.test.js +7 -9
  144. package/dist/webfinger/mod.cjs +8 -0
  145. package/dist/webfinger/mod.d.cts +2 -0
  146. package/dist/webfinger/mod.d.ts +1 -3
  147. package/dist/webfinger/mod.js +5 -6
  148. package/dist/webfinger-C72Y8lrh.js +4 -0
  149. package/dist/webfinger-vAtLmxOF.cjs +4 -0
  150. package/dist/x/cfworkers.cjs +100 -0
  151. package/dist/x/cfworkers.d.cts +59 -0
  152. package/dist/x/cfworkers.d.ts +2 -2
  153. package/dist/x/cfworkers.js +3 -3
  154. package/dist/x/cfworkers.test.js +6 -8
  155. package/dist/x/hono.cjs +61 -0
  156. package/dist/x/hono.d.cts +53 -0
  157. package/dist/x/hono.d.ts +10 -11
  158. package/dist/x/hono.js +3 -3
  159. package/dist/x/sveltekit.cjs +69 -0
  160. package/dist/x/sveltekit.d.cts +45 -0
  161. package/dist/x/sveltekit.d.ts +10 -11
  162. package/dist/x/sveltekit.js +3 -3
  163. package/package.json +79 -23
  164. package/dist/assert_throws-BOO88avQ.js +0 -39
  165. package/dist/authdocloader-BFVqUbyo.js +0 -52
  166. package/dist/compat-Bb5myD13.js +0 -4
  167. package/dist/docloader-CxWcuWqQ.d.ts +0 -221
  168. package/dist/docloader-DEhniCVa.js +0 -4615
  169. package/dist/key-CUZQgVlf.js +0 -10
  170. package/dist/key-Deb0_wWL.js +0 -10
  171. package/dist/key-DxA6xRtZ.js +0 -260
  172. package/dist/lookup-dtdr2ftf.js +0 -131
  173. package/dist/middleware-BE_geSiJ.js +0 -17
  174. package/dist/middleware-BnU6hzVp.js +0 -26
  175. package/dist/mod-TFoH2Ql8.d.ts +0 -104
  176. package/dist/nodeinfo/semver.test.js +0 -143
  177. package/dist/nodeinfo-CyEbLjHs.js +0 -4
  178. package/dist/runtime/docloader.test.js +0 -522
  179. package/dist/runtime/key.test.js +0 -103
  180. package/dist/runtime/langstr.test.d.ts +0 -3
  181. package/dist/runtime/langstr.test.js +0 -39
  182. package/dist/runtime/mod.d.ts +0 -8
  183. package/dist/runtime/mod.js +0 -13
  184. package/dist/runtime/multibase/multibase.test.d.ts +0 -3
  185. package/dist/runtime/multibase/multibase.test.js +0 -358
  186. package/dist/runtime/url.test.d.ts +0 -3
  187. package/dist/runtime/url.test.js +0 -45
  188. package/dist/runtime-BSkOVUWM.js +0 -4
  189. package/dist/semver-dArNLkR9.js +0 -149
  190. package/dist/sig-BXJO--F9.js +0 -4
  191. package/dist/webfinger-C3GIyXIg.js +0 -4
  192. /package/dist/{assert_not_equals-f3m3epl3.js → assert_not_equals-C80BG-_5.js} +0 -0
  193. /package/dist/{collection-CSzG2j1P.js → collection-BzWsN9pB.js} +0 -0
  194. /package/dist/{denokv-Bv33Xxea.js → denokv-CCssOzMJ.js} +0 -0
  195. /package/dist/{nodeinfo/semver.test.d.ts → federation/idempotency.test.d.ts} +0 -0
  196. /package/dist/{runtime/authdocloader.test.d.ts → federation/negotiation.test.d.ts} +0 -0
  197. /package/dist/{kv-C7sopW2E.d.ts → kv-BKNZ-Tb-.d.ts} +0 -0
  198. /package/dist/{mod-1pDWKvUL.d.ts → mod-CVgZgliM.d.ts} +0 -0
  199. /package/dist/{mod-g0xFzAP9.d.ts → mod-xIj-IT58.d.ts} +0 -0
  200. /package/dist/{mq-CRGm1e_F.d.ts → mq-CUKlBw08.d.ts} +0 -0
  201. /package/dist/{retry-D4GJ670a.js → retry-CfF8Gn4d.js} +0 -0
  202. /package/dist/{std__assert-X-_kMxKM.js → std__assert-DWivtrGR.js} +0 -0
  203. /package/dist/{runtime → utils}/docloader.test.d.ts +0 -0
  204. /package/dist/{runtime/key.test.d.ts → utils/kv-cache.test.d.ts} +0 -0
@@ -0,0 +1,674 @@
1
+
2
+ const { Temporal } = require("@js-temporal/polyfill");
3
+ const { URLPattern } = require("urlpattern-polyfill");
4
+
5
+ const require_chunk = require('./chunk-DqRYRqnO.cjs');
6
+ const require_lookup = require('./lookup-CfFkONZD.cjs');
7
+ const require_actor = require('./actor-Ba9Z1eNJ.cjs');
8
+ const require_http = require('./http-Dofes42e.cjs');
9
+ const __logtape_logtape = require_chunk.__toESM(require("@logtape/logtape"));
10
+ const __opentelemetry_api = require_chunk.__toESM(require("@opentelemetry/api"));
11
+ const jsonld = require_chunk.__toESM(require("jsonld"));
12
+ const __fedify_vocab_runtime = require_chunk.__toESM(require("@fedify/vocab-runtime"));
13
+ const byte_encodings_hex = require_chunk.__toESM(require("byte-encodings/hex"));
14
+ const byte_encodings_base64 = require_chunk.__toESM(require("byte-encodings/base64"));
15
+ const json_canon = require_chunk.__toESM(require("json-canon"));
16
+
17
+ //#region src/sig/ld.ts
18
+ const logger$1 = (0, __logtape_logtape.getLogger)([
19
+ "fedify",
20
+ "sig",
21
+ "ld"
22
+ ]);
23
+ /**
24
+ * Attaches a LD signature to the given JSON-LD document.
25
+ * @param jsonLd The JSON-LD document to attach the signature to. It is not
26
+ * modified.
27
+ * @param signature The signature to attach.
28
+ * @returns The JSON-LD document with the attached signature.
29
+ * @throws {TypeError} If the input document is not a valid JSON-LD document.
30
+ * @since 1.0.0
31
+ */
32
+ function attachSignature(jsonLd, signature) {
33
+ if (typeof jsonLd !== "object" || jsonLd == null) throw new TypeError("Failed to attach signature; invalid JSON-LD document.");
34
+ return {
35
+ ...jsonLd,
36
+ signature
37
+ };
38
+ }
39
+ /**
40
+ * Creates a LD signature for the given JSON-LD document.
41
+ * @param jsonLd The JSON-LD document to sign.
42
+ * @param privateKey The private key to sign the document.
43
+ * @param keyId The ID of the public key that corresponds to the private key.
44
+ * @param options Additional options for creating the signature.
45
+ * See also {@link CreateSignatureOptions}.
46
+ * @return The created signature.
47
+ * @throws {TypeError} If the private key is invalid or unsupported.
48
+ * @since 1.0.0
49
+ */
50
+ async function createSignature(jsonLd, privateKey, keyId, { contextLoader, created } = {}) {
51
+ require_http.validateCryptoKey(privateKey, "private");
52
+ if (privateKey.algorithm.name !== "RSASSA-PKCS1-v1_5") throw new TypeError("Unsupported algorithm: " + privateKey.algorithm.name);
53
+ const options = {
54
+ "@context": "https://w3id.org/identity/v1",
55
+ creator: keyId.href,
56
+ created: created?.toString() ?? (/* @__PURE__ */ new Date()).toISOString()
57
+ };
58
+ const optionsHash = await hashJsonLd(options, contextLoader);
59
+ const docHash = await hashJsonLd(jsonLd, contextLoader);
60
+ const message = optionsHash + docHash;
61
+ const encoder = new TextEncoder();
62
+ const messageBytes = encoder.encode(message);
63
+ const signature = await crypto.subtle.sign("RSASSA-PKCS1-v1_5", privateKey, messageBytes);
64
+ return {
65
+ ...options,
66
+ type: "RsaSignature2017",
67
+ signatureValue: (0, byte_encodings_base64.encodeBase64)(signature)
68
+ };
69
+ }
70
+ /**
71
+ * Signs the given JSON-LD document with the private key and returns the signed
72
+ * JSON-LD document.
73
+ * @param jsonLd The JSON-LD document to sign.
74
+ * @param privateKey The private key to sign the document.
75
+ * @param keyId The key ID to use in the signature. It will be used by the
76
+ * verifier to fetch the corresponding public key.
77
+ * @param options Additional options for signing the document.
78
+ * See also {@link SignJsonLdOptions}.
79
+ * @returns The signed JSON-LD document.
80
+ * @throws {TypeError} If the private key is invalid or unsupported.
81
+ * @since 1.0.0
82
+ */
83
+ async function signJsonLd(jsonLd, privateKey, keyId, options) {
84
+ const tracerProvider = options.tracerProvider ?? __opentelemetry_api.trace.getTracerProvider();
85
+ const tracer = tracerProvider.getTracer(require_lookup.deno_default.name, require_lookup.deno_default.version);
86
+ return await tracer.startActiveSpan("ld_signatures.sign", { attributes: { "ld_signatures.key_id": keyId.href } }, async (span) => {
87
+ try {
88
+ const signature = await createSignature(jsonLd, privateKey, keyId, options);
89
+ if (span.isRecording()) {
90
+ span.setAttribute("ld_signatures.type", signature.type);
91
+ span.setAttribute("ld_signatures.signature", (0, byte_encodings_hex.encodeHex)((0, byte_encodings_base64.decodeBase64)(signature.signatureValue)));
92
+ }
93
+ return attachSignature(jsonLd, signature);
94
+ } catch (error) {
95
+ span.setStatus({
96
+ code: __opentelemetry_api.SpanStatusCode.ERROR,
97
+ message: String(error)
98
+ });
99
+ throw error;
100
+ } finally {
101
+ span.end();
102
+ }
103
+ });
104
+ }
105
+ /**
106
+ * Checks if the given JSON-LD document has a Linked Data Signature.
107
+ * @param jsonLd The JSON-LD document to check.
108
+ * @returns `true` if the document has a signature; `false` otherwise.
109
+ * @since 1.0.0
110
+ */
111
+ function hasSignature(jsonLd) {
112
+ if (typeof jsonLd !== "object" || jsonLd == null) return false;
113
+ if ("signature" in jsonLd) {
114
+ const signature = jsonLd.signature;
115
+ if (typeof signature !== "object" || signature == null) return false;
116
+ return "type" in signature && signature.type === "RsaSignature2017" && "creator" in signature && typeof signature.creator === "string" && "created" in signature && typeof signature.created === "string" && "signatureValue" in signature && typeof signature.signatureValue === "string";
117
+ }
118
+ return false;
119
+ }
120
+ /**
121
+ * Detaches Linked Data Signatures from the given JSON-LD document.
122
+ * @param jsonLd The JSON-LD document to modify.
123
+ * @returns The modified JSON-LD document. If the input document does not
124
+ * contain a signature, the original document is returned.
125
+ * @since 1.0.0
126
+ */
127
+ function detachSignature(jsonLd) {
128
+ if (typeof jsonLd !== "object" || jsonLd == null) return jsonLd;
129
+ const doc = { ...jsonLd };
130
+ delete doc.signature;
131
+ return doc;
132
+ }
133
+ /**
134
+ * Verifies Linked Data Signatures of the given JSON-LD document.
135
+ * @param jsonLd The JSON-LD document to verify.
136
+ * @param options Options for verifying the signature.
137
+ * @returns The public key that signed the document or `null` if the signature
138
+ * is invalid or the key is not found.
139
+ * @since 1.0.0
140
+ */
141
+ async function verifySignature(jsonLd, options = {}) {
142
+ if (!hasSignature(jsonLd)) return null;
143
+ const sig = jsonLd.signature;
144
+ let signature;
145
+ try {
146
+ signature = (0, byte_encodings_base64.decodeBase64)(sig.signatureValue);
147
+ } catch (error) {
148
+ logger$1.debug("Failed to verify; invalid base64 signatureValue: {signatureValue}", {
149
+ ...sig,
150
+ error
151
+ });
152
+ return null;
153
+ }
154
+ const { key, cached } = await require_http.fetchKey(new URL(sig.creator), require_actor.CryptographicKey, options);
155
+ if (key == null) return null;
156
+ const sigOpts = {
157
+ ...sig,
158
+ "@context": "https://w3id.org/identity/v1"
159
+ };
160
+ delete sigOpts.type;
161
+ delete sigOpts.id;
162
+ delete sigOpts.signatureValue;
163
+ let sigOptsHash;
164
+ try {
165
+ sigOptsHash = await hashJsonLd(sigOpts, options.contextLoader);
166
+ } catch (error) {
167
+ logger$1.warn("Failed to verify; failed to hash the signature options: {signatureOptions}\n{error}", {
168
+ signatureOptions: sigOpts,
169
+ error
170
+ });
171
+ return null;
172
+ }
173
+ const document = { ...jsonLd };
174
+ delete document.signature;
175
+ let docHash;
176
+ try {
177
+ docHash = await hashJsonLd(document, options.contextLoader);
178
+ } catch (error) {
179
+ logger$1.warn("Failed to verify; failed to hash the document: {document}\n{error}", {
180
+ document,
181
+ error
182
+ });
183
+ return null;
184
+ }
185
+ const encoder = new TextEncoder();
186
+ const message = sigOptsHash + docHash;
187
+ const messageBytes = encoder.encode(message);
188
+ const verified = await crypto.subtle.verify("RSASSA-PKCS1-v1_5", key.publicKey, signature.slice(), messageBytes);
189
+ if (verified) return key;
190
+ if (cached) {
191
+ logger$1.debug("Failed to verify with the cached key {keyId}; signature {signatureValue} is invalid. Retrying with the freshly fetched key...", {
192
+ keyId: sig.creator,
193
+ ...sig
194
+ });
195
+ const { key: key$1 } = await require_http.fetchKey(new URL(sig.creator), require_actor.CryptographicKey, {
196
+ ...options,
197
+ keyCache: {
198
+ get: () => Promise.resolve(void 0),
199
+ set: async (keyId, key$2) => await options.keyCache?.set(keyId, key$2)
200
+ }
201
+ });
202
+ if (key$1 == null) return null;
203
+ const verified$1 = await crypto.subtle.verify("RSASSA-PKCS1-v1_5", key$1.publicKey, signature.slice(), messageBytes);
204
+ return verified$1 ? key$1 : null;
205
+ }
206
+ logger$1.debug("Failed to verify with the fetched key {keyId}; signature {signatureValue} is invalid. Check if the key is correct or if the signed message is correct. The message to sign is:\n{message}", {
207
+ keyId: sig.creator,
208
+ ...sig,
209
+ message
210
+ });
211
+ return null;
212
+ }
213
+ /**
214
+ * Verify the authenticity of the given JSON-LD document using Linked Data
215
+ * Signatures. If the document is signed, this function verifies the signature
216
+ * and checks if the document is attributed to the owner of the public key.
217
+ * If the document is not signed, this function returns `false`.
218
+ * @param jsonLd The JSON-LD document to verify.
219
+ * @param options Options for verifying the document.
220
+ * @returns `true` if the document is authentic; `false` otherwise.
221
+ */
222
+ async function verifyJsonLd(jsonLd, options = {}) {
223
+ const tracerProvider = options.tracerProvider ?? __opentelemetry_api.trace.getTracerProvider();
224
+ const tracer = tracerProvider.getTracer(require_lookup.deno_default.name, require_lookup.deno_default.version);
225
+ return await tracer.startActiveSpan("ld_signatures.verify", async (span) => {
226
+ try {
227
+ const object = await require_actor.Object.fromJsonLd(jsonLd, options);
228
+ if (object.id != null) span.setAttribute("activitypub.object.id", object.id.href);
229
+ span.setAttribute("activitypub.object.type", require_actor.getTypeId(object).href);
230
+ if (typeof jsonLd === "object" && jsonLd != null && "signature" in jsonLd && typeof jsonLd.signature === "object" && jsonLd.signature != null) {
231
+ if ("creator" in jsonLd.signature && typeof jsonLd.signature.creator === "string") span.setAttribute("ld_signatures.key_id", jsonLd.signature.creator);
232
+ if ("signatureValue" in jsonLd.signature && typeof jsonLd.signature.signatureValue === "string") span.setAttribute("ld_signatures.signature", jsonLd.signature.signatureValue);
233
+ if ("type" in jsonLd.signature && typeof jsonLd.signature.type === "string") span.setAttribute("ld_signatures.type", jsonLd.signature.type);
234
+ }
235
+ const attributions = new Set(object.attributionIds.map((uri) => uri.href));
236
+ if (object instanceof require_actor.Activity) for (const uri of object.actorIds) attributions.add(uri.href);
237
+ const key = await verifySignature(jsonLd, options);
238
+ if (key == null) return false;
239
+ if (key.ownerId == null) {
240
+ logger$1.debug("Key {keyId} has no owner.", { keyId: key.id?.href });
241
+ return false;
242
+ }
243
+ attributions.delete(key.ownerId.href);
244
+ if (attributions.size > 0) {
245
+ logger$1.debug("Some attributions are not authenticated by the Linked Data Signatures: {attributions}.", { attributions: [...attributions] });
246
+ return false;
247
+ }
248
+ return true;
249
+ } catch (error) {
250
+ span.setStatus({
251
+ code: __opentelemetry_api.SpanStatusCode.ERROR,
252
+ message: String(error)
253
+ });
254
+ throw error;
255
+ } finally {
256
+ span.end();
257
+ }
258
+ });
259
+ }
260
+ async function hashJsonLd(jsonLd, contextLoader) {
261
+ const canon = await jsonld.default.canonize(jsonLd, {
262
+ format: "application/n-quads",
263
+ documentLoader: contextLoader ?? (0, __fedify_vocab_runtime.getDocumentLoader)()
264
+ });
265
+ const encoder = new TextEncoder();
266
+ const hash = await crypto.subtle.digest("SHA-256", encoder.encode(canon));
267
+ return (0, byte_encodings_hex.encodeHex)(hash);
268
+ }
269
+
270
+ //#endregion
271
+ //#region src/sig/owner.ts
272
+ /**
273
+ * Checks if the actor of the given activity owns the specified key.
274
+ * @param activity The activity to check.
275
+ * @param key The public key to check.
276
+ * @param options Options for checking the key ownership.
277
+ * @returns Whether the actor is the owner of the key.
278
+ */
279
+ async function doesActorOwnKey(activity, key, options) {
280
+ if (key.ownerId != null) return key.ownerId.href === activity.actorId?.href;
281
+ const actor = await activity.getActor(options);
282
+ if (actor == null || !require_actor.isActor(actor)) return false;
283
+ for (const publicKeyId of actor.publicKeyIds) if (key.id != null && publicKeyId.href === key.id.href) return true;
284
+ return false;
285
+ }
286
+ /**
287
+ * Gets the actor that owns the specified key. Returns `null` if the key has no
288
+ * known owner.
289
+ *
290
+ * @param keyId The ID of the key to check, or the key itself.
291
+ * @param options Options for getting the key owner.
292
+ * @returns The actor that owns the key, or `null` if the key has no known
293
+ * owner.
294
+ * @since 0.7.0
295
+ */
296
+ async function getKeyOwner(keyId, options) {
297
+ const tracerProvider = options.tracerProvider ?? __opentelemetry_api.trace.getTracerProvider();
298
+ const documentLoader = options.documentLoader ?? (0, __fedify_vocab_runtime.getDocumentLoader)();
299
+ const contextLoader = options.contextLoader ?? (0, __fedify_vocab_runtime.getDocumentLoader)();
300
+ let object;
301
+ if (keyId instanceof require_actor.CryptographicKey) {
302
+ object = keyId;
303
+ if (object.id == null) return null;
304
+ keyId = object.id;
305
+ } else {
306
+ let keyDoc;
307
+ try {
308
+ const { document } = await documentLoader(keyId.href);
309
+ keyDoc = document;
310
+ } catch (_) {
311
+ return null;
312
+ }
313
+ try {
314
+ object = await require_actor.Object.fromJsonLd(keyDoc, {
315
+ documentLoader,
316
+ contextLoader,
317
+ tracerProvider
318
+ });
319
+ } catch (e) {
320
+ if (!(e instanceof TypeError)) throw e;
321
+ try {
322
+ object = await require_actor.CryptographicKey.fromJsonLd(keyDoc, {
323
+ documentLoader,
324
+ contextLoader,
325
+ tracerProvider
326
+ });
327
+ } catch (e$1) {
328
+ if (e$1 instanceof TypeError) return null;
329
+ throw e$1;
330
+ }
331
+ }
332
+ }
333
+ let owner = null;
334
+ if (object instanceof require_actor.CryptographicKey) {
335
+ if (object.ownerId == null) return null;
336
+ owner = await object.getOwner({
337
+ documentLoader,
338
+ contextLoader,
339
+ tracerProvider
340
+ });
341
+ } else if (require_actor.isActor(object)) owner = object;
342
+ else return null;
343
+ if (owner == null) return null;
344
+ for (const kid of owner.publicKeyIds) if (kid.href === keyId.href) return owner;
345
+ return null;
346
+ }
347
+
348
+ //#endregion
349
+ //#region src/sig/proof.ts
350
+ const logger = (0, __logtape_logtape.getLogger)([
351
+ "fedify",
352
+ "sig",
353
+ "proof"
354
+ ]);
355
+ /**
356
+ * Creates a proof for the given object.
357
+ * @param object The object to create a proof for.
358
+ * @param privateKey The private key to sign the proof with.
359
+ * @param keyId The key ID to use in the proof. It will be used by the verifier.
360
+ * @param options Additional options. See also {@link CreateProofOptions}.
361
+ * @returns The created proof.
362
+ * @throws {TypeError} If the private key is invalid or unsupported.
363
+ * @since 0.10.0
364
+ */
365
+ async function createProof(object, privateKey, keyId, { contextLoader, context, created } = {}) {
366
+ require_http.validateCryptoKey(privateKey, "private");
367
+ if (privateKey.algorithm.name !== "Ed25519") throw new TypeError("Unsupported algorithm: " + privateKey.algorithm.name);
368
+ const objectWithoutProofs = object.clone({ proofs: [] });
369
+ const compactMsg = await objectWithoutProofs.toJsonLd({
370
+ format: "compact",
371
+ contextLoader,
372
+ context
373
+ });
374
+ const msgCanon = (0, json_canon.default)(compactMsg);
375
+ const encoder = new TextEncoder();
376
+ const msgBytes = encoder.encode(msgCanon);
377
+ const msgDigest = await crypto.subtle.digest("SHA-256", msgBytes);
378
+ created ??= Temporal.Now.instant();
379
+ const proofConfig = {
380
+ "@context": compactMsg["@context"],
381
+ type: "DataIntegrityProof",
382
+ cryptosuite: "eddsa-jcs-2022",
383
+ verificationMethod: keyId.href,
384
+ proofPurpose: "assertionMethod",
385
+ created: created.toString()
386
+ };
387
+ const proofCanon = (0, json_canon.default)(proofConfig);
388
+ const proofBytes = encoder.encode(proofCanon);
389
+ const proofDigest = await crypto.subtle.digest("SHA-256", proofBytes);
390
+ const digest = new Uint8Array(proofDigest.byteLength + msgDigest.byteLength);
391
+ digest.set(new Uint8Array(proofDigest), 0);
392
+ digest.set(new Uint8Array(msgDigest), proofDigest.byteLength);
393
+ const sig = await crypto.subtle.sign("Ed25519", privateKey, digest);
394
+ return new require_actor.DataIntegrityProof({
395
+ cryptosuite: "eddsa-jcs-2022",
396
+ verificationMethod: keyId,
397
+ proofPurpose: "assertionMethod",
398
+ created: created ?? Temporal.Now.instant(),
399
+ proofValue: new Uint8Array(sig)
400
+ });
401
+ }
402
+ /**
403
+ * Signs the given object with the private key and returns the signed object.
404
+ * @param object The object to create a proof for.
405
+ * @param privateKey The private key to sign the proof with.
406
+ * @param keyId The key ID to use in the proof. It will be used by the verifier.
407
+ * @param options Additional options. See also {@link SignObjectOptions}.
408
+ * @returns The signed object.
409
+ * @throws {TypeError} If the private key is invalid or unsupported.
410
+ * @since 0.10.0
411
+ */
412
+ async function signObject(object, privateKey, keyId, options = {}) {
413
+ const tracerProvider = options.tracerProvider ?? __opentelemetry_api.trace.getTracerProvider();
414
+ const tracer = tracerProvider.getTracer(require_lookup.deno_default.name, require_lookup.deno_default.version);
415
+ return await tracer.startActiveSpan("object_integrity_proofs.sign", { attributes: { "activitypub.object.type": require_actor.getTypeId(object).href } }, async (span) => {
416
+ try {
417
+ if (object.id != null) span.setAttribute("activitypub.object.id", object.id.href);
418
+ const existingProofs = [];
419
+ for await (const proof$1 of object.getProofs(options)) existingProofs.push(proof$1);
420
+ const proof = await createProof(object, privateKey, keyId, options);
421
+ if (span.isRecording()) {
422
+ if (proof.cryptosuite != null) span.setAttribute("object_integrity_proofs.cryptosuite", proof.cryptosuite);
423
+ if (proof.verificationMethodId != null) span.setAttribute("object_integrity_proofs.key_id", proof.verificationMethodId.href);
424
+ if (proof.proofValue != null) span.setAttribute("object_integrity_proofs.signature", (0, byte_encodings_hex.encodeHex)(proof.proofValue));
425
+ }
426
+ return object.clone({ proofs: [...existingProofs, proof] });
427
+ } catch (error) {
428
+ span.setStatus({
429
+ code: __opentelemetry_api.SpanStatusCode.ERROR,
430
+ message: String(error)
431
+ });
432
+ throw error;
433
+ } finally {
434
+ span.end();
435
+ }
436
+ });
437
+ }
438
+ /**
439
+ * Verifies the given proof for the object.
440
+ * @param jsonLd The JSON-LD object to verify the proof for. If it contains
441
+ * any proofs, they will be ignored.
442
+ * @param proof The proof to verify.
443
+ * @param options Additional options. See also {@link VerifyProofOptions}.
444
+ * @returns The public key that was used to sign the proof, or `null` if the
445
+ * proof is invalid.
446
+ * @since 0.10.0
447
+ */
448
+ async function verifyProof(jsonLd, proof, options = {}) {
449
+ const tracerProvider = options.tracerProvider ?? __opentelemetry_api.trace.getTracerProvider();
450
+ const tracer = tracerProvider.getTracer(require_lookup.deno_default.name, require_lookup.deno_default.version);
451
+ return await tracer.startActiveSpan("object_integrity_proofs.verify", async (span) => {
452
+ if (span.isRecording()) {
453
+ if (proof.cryptosuite != null) span.setAttribute("object_integrity_proofs.cryptosuite", proof.cryptosuite);
454
+ if (proof.verificationMethodId != null) span.setAttribute("object_integrity_proofs.key_id", proof.verificationMethodId.href);
455
+ if (proof.proofValue != null) span.setAttribute("object_integrity_proofs.signature", (0, byte_encodings_hex.encodeHex)(proof.proofValue));
456
+ }
457
+ try {
458
+ const key = await verifyProofInternal(jsonLd, proof, options);
459
+ if (key == null) span.setStatus({ code: __opentelemetry_api.SpanStatusCode.ERROR });
460
+ return key;
461
+ } catch (error) {
462
+ span.setStatus({
463
+ code: __opentelemetry_api.SpanStatusCode.ERROR,
464
+ message: String(error)
465
+ });
466
+ throw error;
467
+ } finally {
468
+ span.end();
469
+ }
470
+ });
471
+ }
472
+ async function verifyProofInternal(jsonLd, proof, options) {
473
+ if (typeof jsonLd !== "object" || proof.cryptosuite !== "eddsa-jcs-2022" || proof.verificationMethodId == null || proof.proofPurpose !== "assertionMethod" || proof.proofValue == null || proof.created == null) return null;
474
+ const publicKeyPromise = require_http.fetchKey(proof.verificationMethodId, require_actor.Multikey, options);
475
+ const proofConfig = {
476
+ "@context": jsonLd["@context"],
477
+ type: "DataIntegrityProof",
478
+ cryptosuite: proof.cryptosuite,
479
+ verificationMethod: proof.verificationMethodId.href,
480
+ proofPurpose: proof.proofPurpose,
481
+ created: proof.created.toString()
482
+ };
483
+ const proofCanon = (0, json_canon.default)(proofConfig);
484
+ const encoder = new TextEncoder();
485
+ const proofBytes = encoder.encode(proofCanon);
486
+ const proofDigest = await crypto.subtle.digest("SHA-256", proofBytes);
487
+ const msg = { ...jsonLd };
488
+ if ("proof" in msg) delete msg.proof;
489
+ const msgCanon = (0, json_canon.default)(msg);
490
+ const msgBytes = encoder.encode(msgCanon);
491
+ const msgDigest = await crypto.subtle.digest("SHA-256", msgBytes);
492
+ const digest = new Uint8Array(proofDigest.byteLength + msgDigest.byteLength);
493
+ digest.set(new Uint8Array(proofDigest), 0);
494
+ digest.set(new Uint8Array(msgDigest), proofDigest.byteLength);
495
+ let fetchedKey;
496
+ try {
497
+ fetchedKey = await publicKeyPromise;
498
+ } catch (error) {
499
+ logger.debug("Failed to get the key (verificationMethod) for the proof:\n{proof}", {
500
+ proof,
501
+ keyId: proof.verificationMethodId.href,
502
+ error
503
+ });
504
+ return null;
505
+ }
506
+ const publicKey = fetchedKey.key;
507
+ if (publicKey == null) {
508
+ logger.debug("Failed to get the key (verificationMethod) for the proof:\n{proof}", {
509
+ proof,
510
+ keyId: proof.verificationMethodId.href
511
+ });
512
+ return null;
513
+ }
514
+ if (publicKey.publicKey.algorithm.name !== "Ed25519") {
515
+ if (fetchedKey.cached) {
516
+ logger.debug("The cached key (verificationMethod) for the proof is not a valid Ed25519 key:\n{keyId}; retrying with the freshly fetched key...", {
517
+ proof,
518
+ keyId: proof.verificationMethodId.href
519
+ });
520
+ return await verifyProof(jsonLd, proof, {
521
+ ...options,
522
+ keyCache: {
523
+ get: () => Promise.resolve(null),
524
+ set: async (keyId, key) => await options.keyCache?.set(keyId, key)
525
+ }
526
+ });
527
+ }
528
+ logger.debug("The fetched key (verificationMethod) for the proof is not a valid Ed25519 key:\n{keyId}", {
529
+ proof,
530
+ keyId: proof.verificationMethodId.href
531
+ });
532
+ return null;
533
+ }
534
+ const verified = await crypto.subtle.verify("Ed25519", publicKey.publicKey, proof.proofValue.slice(), digest);
535
+ if (!verified) {
536
+ if (fetchedKey.cached) {
537
+ logger.debug("Failed to verify the proof with the cached key {keyId}; retrying with the freshly fetched key...", {
538
+ keyId: proof.verificationMethodId.href,
539
+ proof
540
+ });
541
+ return await verifyProof(jsonLd, proof, {
542
+ ...options,
543
+ keyCache: {
544
+ get: () => Promise.resolve(void 0),
545
+ set: async (keyId, key) => await options.keyCache?.set(keyId, key)
546
+ }
547
+ });
548
+ }
549
+ logger.debug("Failed to verify the proof with the fetched key {keyId}:\n{proof}", {
550
+ keyId: proof.verificationMethodId.href,
551
+ proof
552
+ });
553
+ return null;
554
+ }
555
+ return publicKey;
556
+ }
557
+ /**
558
+ * Verifies the given object. It will verify all the proofs in the object,
559
+ * and succeed only if all the proofs are valid and all attributions and
560
+ * actors are authenticated by the proofs.
561
+ * @template T The type of the object to verify.
562
+ * @param cls The class of the object to verify. It must be a subclass of
563
+ * the {@link Object}.
564
+ * @param jsonLd The JSON-LD object to verify. It's assumed that the object
565
+ * is a compacted JSON-LD representation of a `T` with `@context`.
566
+ * @param options Additional options. See also {@link VerifyObjectOptions}.
567
+ * @returns The object if it's verified, or `null` if it's not.
568
+ * @throws {TypeError} If the object is invalid or unsupported.
569
+ * @since 0.10.0
570
+ */
571
+ async function verifyObject(cls, jsonLd, options = {}) {
572
+ const logger$2 = (0, __logtape_logtape.getLogger)([
573
+ "fedify",
574
+ "sig",
575
+ "proof"
576
+ ]);
577
+ const object = await cls.fromJsonLd(jsonLd, options);
578
+ const attributions = new Set(object.attributionIds.map((uri) => uri.href));
579
+ if (object instanceof require_actor.Activity) for (const uri of object.actorIds) attributions.add(uri.href);
580
+ for await (const proof of object.getProofs(options)) {
581
+ const key = await verifyProof(jsonLd, proof, options);
582
+ if (key === null) return null;
583
+ if (key.controllerId == null) {
584
+ logger$2.debug("Key {keyId} does not have a controller.", { keyId: key.id?.href });
585
+ continue;
586
+ }
587
+ attributions.delete(key.controllerId.href);
588
+ }
589
+ if (attributions.size > 0) {
590
+ logger$2.debug("Some attributions are not authenticated by the proofs: {attributions}.", { attributions: [...attributions] });
591
+ return null;
592
+ }
593
+ return object;
594
+ }
595
+
596
+ //#endregion
597
+ Object.defineProperty(exports, 'attachSignature', {
598
+ enumerable: true,
599
+ get: function () {
600
+ return attachSignature;
601
+ }
602
+ });
603
+ Object.defineProperty(exports, 'createProof', {
604
+ enumerable: true,
605
+ get: function () {
606
+ return createProof;
607
+ }
608
+ });
609
+ Object.defineProperty(exports, 'createSignature', {
610
+ enumerable: true,
611
+ get: function () {
612
+ return createSignature;
613
+ }
614
+ });
615
+ Object.defineProperty(exports, 'detachSignature', {
616
+ enumerable: true,
617
+ get: function () {
618
+ return detachSignature;
619
+ }
620
+ });
621
+ Object.defineProperty(exports, 'doesActorOwnKey', {
622
+ enumerable: true,
623
+ get: function () {
624
+ return doesActorOwnKey;
625
+ }
626
+ });
627
+ Object.defineProperty(exports, 'getKeyOwner', {
628
+ enumerable: true,
629
+ get: function () {
630
+ return getKeyOwner;
631
+ }
632
+ });
633
+ Object.defineProperty(exports, 'hasSignature', {
634
+ enumerable: true,
635
+ get: function () {
636
+ return hasSignature;
637
+ }
638
+ });
639
+ Object.defineProperty(exports, 'signJsonLd', {
640
+ enumerable: true,
641
+ get: function () {
642
+ return signJsonLd;
643
+ }
644
+ });
645
+ Object.defineProperty(exports, 'signObject', {
646
+ enumerable: true,
647
+ get: function () {
648
+ return signObject;
649
+ }
650
+ });
651
+ Object.defineProperty(exports, 'verifyJsonLd', {
652
+ enumerable: true,
653
+ get: function () {
654
+ return verifyJsonLd;
655
+ }
656
+ });
657
+ Object.defineProperty(exports, 'verifyObject', {
658
+ enumerable: true,
659
+ get: function () {
660
+ return verifyObject;
661
+ }
662
+ });
663
+ Object.defineProperty(exports, 'verifyProof', {
664
+ enumerable: true,
665
+ get: function () {
666
+ return verifyProof;
667
+ }
668
+ });
669
+ Object.defineProperty(exports, 'verifySignature', {
670
+ enumerable: true,
671
+ get: function () {
672
+ return verifySignature;
673
+ }
674
+ });
@@ -3,8 +3,8 @@
3
3
  import { URLPattern } from "urlpattern-polyfill";
4
4
  globalThis.addEventListener = () => {};
5
5
 
6
- import { Activity, DataIntegrityProof, Multikey, deno_default, getTypeId } from "./type-SK-d7Tbw.js";
7
- import { fetchKey, validateCryptoKey } from "./key-CzLv1phF.js";
6
+ import { Activity, DataIntegrityProof, Multikey, deno_default, getTypeId } from "./lookup-DQRtjvb1.js";
7
+ import { fetchKey, validateCryptoKey } from "./key-CxdLUFS6.js";
8
8
  import { getLogger } from "@logtape/logtape";
9
9
  import { SpanStatusCode, trace } from "@opentelemetry/api";
10
10
  import { encodeHex } from "byte-encodings/hex";
@@ -195,7 +195,7 @@ async function verifyProofInternal(jsonLd, proof, options) {
195
195
  });
196
196
  return null;
197
197
  }
198
- const verified = await crypto.subtle.verify("Ed25519", publicKey.publicKey, proof.proofValue, digest);
198
+ const verified = await crypto.subtle.verify("Ed25519", publicKey.publicKey, proof.proofValue.slice(), digest);
199
199
  if (!verified) {
200
200
  if (fetchedKey.cached) {
201
201
  logger.debug("Failed to verify the proof with the cached key {keyId}; retrying with the freshly fetched key...", {