@fedify/fedify 2.0.0-pr.449.1725 → 2.0.0-pr.451.1730

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 (193) hide show
  1. package/dist/{actor-T6RyhRgk.d.ts → actor-Cd2CREO0.d.ts} +2 -2
  2. package/dist/{actor-VrXd7EdX.js → actor-Cg8ZhoVF.js} +1 -1
  3. package/dist/{actor-CsRJa7wV.cjs → actor-D020hlx3.cjs} +922 -1451
  4. package/dist/{actor-D6K058Tb.d.cts → actor-DDhZuMGl.d.cts} +2 -2
  5. package/dist/{actor-C0gLJq8I.js → actor-DPD6Gbdn.js} +190 -677
  6. package/dist/{assert_rejects-DiIiJbZn.js → assert_rejects-Ce45JcFg.js} +1 -1
  7. package/dist/{assert_is_error-BPGph1Jx.js → assert_throws-BNXdRGWP.js} +31 -1
  8. package/dist/{builder-JjsppXTK.js → builder-Dn87hy75.js} +3 -4
  9. package/dist/{client-bgSdkFa2.d.ts → client-Bkaw0_PR.d.ts} +1 -1
  10. package/dist/{client-CegPX0Rn.d.cts → client-Cle5j1T1.d.cts} +1 -1
  11. package/dist/{client-BS-GE3XI.js → client-D6-5okV1.js} +1 -1
  12. package/dist/compat/mod.d.cts +11 -11
  13. package/dist/compat/mod.d.ts +12 -12
  14. package/dist/compat/transformers.test.js +20 -20
  15. package/dist/{context-DYCJXr7J.d.cts → context-BOYzcFSe.d.cts} +10 -10
  16. package/dist/{context-B1X8-X33.d.ts → context-DqFifMwj.d.ts} +11 -11
  17. package/dist/{authdocloader-C8LXxsmU.js → docloader-B2BFBGqv.js} +18 -8
  18. package/dist/{esm-CvUgdJZ_.js → esm-CU0Dnk7T.js} +11 -11
  19. package/dist/federation/builder.test.js +8 -10
  20. package/dist/federation/collection.test.js +7 -9
  21. package/dist/federation/handler.test.js +25 -26
  22. package/dist/federation/idempotency.test.js +25 -26
  23. package/dist/federation/inbox.test.js +4 -6
  24. package/dist/federation/keycache.test.js +3 -4
  25. package/dist/federation/kv.test.js +6 -8
  26. package/dist/federation/middleware.test.js +26 -27
  27. package/dist/federation/mod.cjs +10 -11
  28. package/dist/federation/mod.d.cts +10 -10
  29. package/dist/federation/mod.d.ts +12 -12
  30. package/dist/federation/mod.js +10 -11
  31. package/dist/federation/mq.test.js +8 -10
  32. package/dist/federation/negotiation.test.js +7 -9
  33. package/dist/federation/retry.test.js +4 -5
  34. package/dist/federation/router.test.js +6 -8
  35. package/dist/federation/send.test.js +13 -15
  36. package/dist/{http-D-e6AFwR.d.cts → http-B6SKO6NJ.d.cts} +2 -2
  37. package/dist/{http-D6Uj2x2y.d.ts → http-BE7aFuqn.d.ts} +2 -2
  38. package/dist/{http-D_BI5KHC.cjs → http-DMsjudj8.cjs} +289 -9
  39. package/dist/{http-DbyMqL2X.js → http-DN0v2zoF.js} +2 -2
  40. package/dist/{http-DVQEn98K.js → http-DcDb8r1W.js} +256 -6
  41. package/dist/{inbox-DQlf_-Dz.js → inbox-CgGy7Hzc.js} +1 -1
  42. package/dist/{key-BSJX6n9o.js → key-jSvnymAk.js} +3 -2
  43. package/dist/{keycache-CcIfdj0m.js → keycache-IVLjlAK9.js} +1 -1
  44. package/dist/{keys-DnSaJmvD.js → keys-BKM2Mqu2.js} +2 -1
  45. package/dist/{docloader-BdF5STdg.cjs → kv-cache-BHIupktM.cjs} +69 -651
  46. package/dist/kv-cache-DNvS-egZ.js +4236 -0
  47. package/dist/{docloader-AMdJU291.js → kv-cache-ydIs4Ul6.js} +68 -596
  48. package/dist/{ld-CAJ6Q2od.js → ld-CcsryBo0.js} +3 -2
  49. package/dist/{lookup-BGCuyJRy.js → lookup-BV3A9Zbc.js} +63 -2
  50. package/dist/{type-COb6KNlm.js → lookup-BbEekiru.js} +588 -5303
  51. package/dist/{lookup-B2Bsau2g.cjs → lookup-CMAGfQ1Q.cjs} +78 -5
  52. package/dist/{middleware-1oxZY_0z.js → middleware-D-vPitR-.js} +11 -11
  53. package/dist/{middleware-BDqkoMAQ.js → middleware-Dm8sXYUr.js} +18 -17
  54. package/dist/middleware-DzA970CF.js +16 -0
  55. package/dist/middleware-VpoC5jyA.js +26 -0
  56. package/dist/middleware-W_-inBoE.cjs +16 -0
  57. package/dist/{middleware-D0XMPoN8.cjs → middleware-ekn2KaOw.cjs} +31 -31
  58. package/dist/{mod-CxkWO3Mg.d.cts → mod--K7l84wp.d.cts} +3 -2
  59. package/dist/{mod-DBzN0aCM.d.ts → mod-CB80AlIA.d.ts} +1 -1
  60. package/dist/mod-CLKu6Uo_.d.cts +107 -0
  61. package/dist/{mod-Djzcw2ry.d.cts → mod-COlOrmr9.d.cts} +3 -3
  62. package/dist/{mod-DlU8ISoa.d.ts → mod-CRENK2dd.d.ts} +3 -2
  63. package/dist/{mod-DcKxhFQ8.d.ts → mod-CaWbCg0N.d.ts} +2 -2
  64. package/dist/mod-Cm97bAiT.d.ts +109 -0
  65. package/dist/{mod-twdvV2hR.d.cts → mod-D9XZsft2.d.cts} +2 -2
  66. package/dist/{mod-BhUKmBJD.d.ts → mod-DLIidI_j.d.ts} +3 -3
  67. package/dist/{mod-jQ4OODsl.d.cts → mod-DuclhZjh.d.cts} +1 -1
  68. package/dist/mod.cjs +21 -32
  69. package/dist/mod.d.cts +14 -14
  70. package/dist/mod.d.ts +17 -17
  71. package/dist/mod.js +15 -16
  72. package/dist/nodeinfo/client.test.js +8 -10
  73. package/dist/nodeinfo/handler.test.js +24 -25
  74. package/dist/nodeinfo/mod.cjs +3 -3
  75. package/dist/nodeinfo/mod.d.cts +2 -3
  76. package/dist/nodeinfo/mod.d.ts +3 -4
  77. package/dist/nodeinfo/mod.js +3 -3
  78. package/dist/nodeinfo/types.test.js +7 -9
  79. package/dist/{owner-hd9lvQcP.d.ts → owner-CktUdA0h.d.ts} +3 -3
  80. package/dist/{owner-BN_tO3cY.d.cts → owner-dkg5OpSC.d.cts} +3 -3
  81. package/dist/{owner-CaIfLBwg.js → owner-n5c_oZ_M.js} +3 -2
  82. package/dist/{proof-CKXppjee.js → proof-CLwByIT1.js} +5 -4
  83. package/dist/{proof-AhyVJcNZ.cjs → proof-CZfrp13P.cjs} +17 -16
  84. package/dist/{proof-BQwXHakc.js → proof-CsFR7fiS.js} +2 -2
  85. package/dist/request-BbHkedIU.d.cts +35 -0
  86. package/dist/request-DKGQaofB.js +184 -0
  87. package/dist/request-U1t6zZtk.d.ts +39 -0
  88. package/dist/request-XHWUW2bi.cjs +208 -0
  89. package/dist/{send-DQd3R1Oc.js → send-eW9JIzQV.js} +2 -2
  90. package/dist/sig/http.test.js +12 -13
  91. package/dist/sig/key.test.js +9 -11
  92. package/dist/sig/ld.test.js +8 -10
  93. package/dist/sig/mod.cjs +10 -11
  94. package/dist/sig/mod.d.cts +6 -7
  95. package/dist/sig/mod.d.ts +6 -7
  96. package/dist/sig/mod.js +6 -7
  97. package/dist/sig/owner.test.js +10 -12
  98. package/dist/sig/proof.test.js +13 -14
  99. package/dist/testing/docloader.test.js +6 -8
  100. package/dist/testing/mod.d.ts +3 -62
  101. package/dist/testing/mod.js +2 -3
  102. package/dist/{testing-BljKU-GG.js → testing-ClYSmXdi.js} +1 -2
  103. package/dist/{types-DI0yutHB.cjs → types-BT0xc4-R.cjs} +3 -3
  104. package/dist/{types-CEn4wB51.js → types-DaPoJTSc.js} +1 -1
  105. package/dist/{runtime/authdocloader.test.js → utils/docloader.test.js} +13 -15
  106. package/dist/utils/kv-cache.test.js +208 -0
  107. package/dist/utils/mod.cjs +14 -0
  108. package/dist/utils/mod.d.cts +6 -0
  109. package/dist/utils/mod.d.ts +8 -0
  110. package/dist/utils/mod.js +12 -0
  111. package/dist/utils/request.test.js +48 -0
  112. package/dist/utils/url.test.js +41 -0
  113. package/dist/vocab/actor.test.js +8 -10
  114. package/dist/vocab/lookup.test.js +7 -9
  115. package/dist/vocab/mod.cjs +4 -4
  116. package/dist/vocab/mod.d.cts +4 -5
  117. package/dist/vocab/mod.d.ts +4 -5
  118. package/dist/vocab/mod.js +4 -4
  119. package/dist/vocab/type.test.js +2 -3
  120. package/dist/vocab/vocab.test.js +9 -10
  121. package/dist/{vocab-NZXL5Pr-.cjs → vocab-CxTuoEVd.cjs} +6 -5
  122. package/dist/{vocab-Dw1-yVGg.d.cts → vocab-DCBw44JZ.d.cts} +2 -21
  123. package/dist/{vocab-BI0Ak5lL.d.ts → vocab-ivDKb439.d.ts} +2 -21
  124. package/dist/{vocab-CkWH9P5l.js → vocab-yFT-fQBj.js} +4 -3
  125. package/dist/webfinger/handler.test.js +24 -25
  126. package/dist/webfinger/lookup.test.js +7 -9
  127. package/dist/webfinger/mod.cjs +3 -3
  128. package/dist/webfinger/mod.d.cts +2 -3
  129. package/dist/webfinger/mod.d.ts +2 -3
  130. package/dist/webfinger/mod.js +3 -3
  131. package/dist/x/cfworkers.d.cts +1 -1
  132. package/dist/x/cfworkers.d.ts +2 -2
  133. package/dist/x/cfworkers.test.js +6 -8
  134. package/dist/x/hono.d.cts +10 -10
  135. package/dist/x/hono.d.ts +11 -11
  136. package/dist/x/sveltekit.d.cts +10 -10
  137. package/dist/x/sveltekit.d.ts +11 -11
  138. package/package.json +16 -14
  139. package/dist/assert_throws-BOO88avQ.js +0 -39
  140. package/dist/authdocloader-6F9IP-VO.js +0 -52
  141. package/dist/authdocloader-BkuVo8LL.cjs +0 -58
  142. package/dist/docloader-CxWcuWqQ.d.ts +0 -221
  143. package/dist/docloader-D-MrRyHl.d.cts +0 -219
  144. package/dist/key-BBzfhQGE.js +0 -10
  145. package/dist/key-BMz_uAnc.cjs +0 -10
  146. package/dist/key-D-RgWfcf.cjs +0 -290
  147. package/dist/key-DFefr8X2.js +0 -260
  148. package/dist/key-DW2DrPGl.js +0 -10
  149. package/dist/lookup-C3pnuyiD.js +0 -331
  150. package/dist/middleware-B8WWe8Q2.js +0 -26
  151. package/dist/middleware-DipQbJmB.js +0 -17
  152. package/dist/middleware-mLaQeD_Z.cjs +0 -17
  153. package/dist/mod-CerN_Sza.d.ts +0 -104
  154. package/dist/mod-Cj1tHXBR.d.cts +0 -102
  155. package/dist/runtime/docloader.test.js +0 -522
  156. package/dist/runtime/key.test.js +0 -103
  157. package/dist/runtime/langstr.test.d.ts +0 -3
  158. package/dist/runtime/langstr.test.js +0 -39
  159. package/dist/runtime/link.test.d.ts +0 -3
  160. package/dist/runtime/link.test.js +0 -61
  161. package/dist/runtime/mod.cjs +0 -25
  162. package/dist/runtime/mod.d.cts +0 -6
  163. package/dist/runtime/mod.d.ts +0 -8
  164. package/dist/runtime/mod.js +0 -13
  165. package/dist/runtime/multibase/multibase.test.d.ts +0 -3
  166. package/dist/runtime/multibase/multibase.test.js +0 -358
  167. package/dist/runtime/url.test.js +0 -45
  168. /package/dist/{assert_not_equals-f3m3epl3.js → assert_not_equals-C80BG-_5.js} +0 -0
  169. /package/dist/{collection-CcnIw1qY.js → collection-BzWsN9pB.js} +0 -0
  170. /package/dist/{denokv-Bv33Xxea.js → denokv-CCssOzMJ.js} +0 -0
  171. /package/dist/{federation-H2_En3j5.cjs → federation-DOuZpcAw.cjs} +0 -0
  172. /package/dist/{federation-D1U8YY9t.js → federation-pZJsapBn.js} +0 -0
  173. /package/dist/{kv-63Cil1MD.d.cts → kv-BMKIFXNW.d.cts} +0 -0
  174. /package/dist/{kv-C7sopW2E.d.ts → kv-Dt06smFt.d.ts} +0 -0
  175. /package/dist/{mod-1pDWKvUL.d.ts → mod-B-ZQqbGe.d.ts} +0 -0
  176. /package/dist/{mod-g0xFzAP9.d.ts → mod-CjsiSl7_.d.ts} +0 -0
  177. /package/dist/{mq-CRGm1e_F.d.ts → mq-CPRJk4mZ.d.ts} +0 -0
  178. /package/dist/{negotiation-5NPJL6zp.js → negotiation-C4nFufNk.js} +0 -0
  179. /package/dist/{nodeinfo-DfycQ8Wf.js → nodeinfo-B0UefMXO.js} +0 -0
  180. /package/dist/{nodeinfo-Co9lJrWl.cjs → nodeinfo-ByTd7HpT.cjs} +0 -0
  181. /package/dist/{retry-D4GJ670a.js → retry-CfF8Gn4d.js} +0 -0
  182. /package/dist/{runtime-C58AJWSv.cjs → sig-DE_NC_VQ.cjs} +0 -0
  183. /package/dist/{runtime-DPYEDf-o.js → sig-Dx59zAgn.js} +0 -0
  184. /package/dist/{std__assert-X-_kMxKM.js → std__assert-DWivtrGR.js} +0 -0
  185. /package/dist/{types-BSuWJsOm.js → types-C2XVl6gj.js} +0 -0
  186. /package/dist/{runtime → utils}/docloader.test.d.ts +0 -0
  187. /package/dist/{runtime/authdocloader.test.d.ts → utils/kv-cache.test.d.ts} +0 -0
  188. /package/dist/{runtime/key.test.d.ts → utils/request.test.d.ts} +0 -0
  189. /package/dist/{runtime → utils}/url.test.d.ts +0 -0
  190. /package/dist/{sig-ByHXzqUi.cjs → utils-B6yXfxAQ.cjs} +0 -0
  191. /package/dist/{sig-Cj3tk-ig.js → utils-BXMGcmqK.js} +0 -0
  192. /package/dist/{webfinger-BjOEdFPs.cjs → webfinger-C949bxcD.cjs} +0 -0
  193. /package/dist/{webfinger-De_bU0iE.js → webfinger-qG8R5jc3.js} +0 -0
@@ -3,16 +3,266 @@
3
3
  const { URLPattern } = require("urlpattern-polyfill");
4
4
 
5
5
  const require_chunk = require('./chunk-DqRYRqnO.cjs');
6
- const require_docloader = require('./docloader-BdF5STdg.cjs');
7
- const require_actor = require('./actor-CsRJa7wV.cjs');
8
- const require_key = require('./key-D-RgWfcf.cjs');
6
+ const require_request = require('./request-XHWUW2bi.cjs');
7
+ const require_actor = require('./actor-D020hlx3.cjs');
9
8
  const __logtape_logtape = require_chunk.__toESM(require("@logtape/logtape"));
10
9
  const __opentelemetry_api = require_chunk.__toESM(require("@opentelemetry/api"));
11
- const byte_encodings_base64 = require_chunk.__toESM(require("byte-encodings/base64"));
10
+ const __fedify_vocab_runtime = require_chunk.__toESM(require("@fedify/vocab-runtime"));
12
11
  const byte_encodings_hex = require_chunk.__toESM(require("byte-encodings/hex"));
13
12
  const __opentelemetry_semantic_conventions = require_chunk.__toESM(require("@opentelemetry/semantic-conventions"));
13
+ const byte_encodings_base64 = require_chunk.__toESM(require("byte-encodings/base64"));
14
14
  const structured_field_values = require_chunk.__toESM(require("structured-field-values"));
15
15
 
16
+ //#region src/sig/key.ts
17
+ /**
18
+ * Checks if the given key is valid and supported. No-op if the key is valid,
19
+ * otherwise throws an error.
20
+ * @param key The key to check.
21
+ * @param type Which type of key to check. If not specified, the key can be
22
+ * either public or private.
23
+ * @throws {TypeError} If the key is invalid or unsupported.
24
+ */
25
+ function validateCryptoKey(key, type) {
26
+ if (type != null && key.type !== type) throw new TypeError(`The key is not a ${type} key.`);
27
+ if (!key.extractable) throw new TypeError("The key is not extractable.");
28
+ if (key.algorithm.name !== "RSASSA-PKCS1-v1_5" && key.algorithm.name !== "Ed25519") throw new TypeError("Currently only RSASSA-PKCS1-v1_5 and Ed25519 keys are supported. More algorithms will be added in the future!");
29
+ if (key.algorithm.name === "RSASSA-PKCS1-v1_5") {
30
+ const algorithm = key.algorithm;
31
+ if (algorithm.hash.name !== "SHA-256") throw new TypeError("For compatibility with the existing Fediverse software (e.g., Mastodon), hash algorithm for RSASSA-PKCS1-v1_5 keys must be SHA-256.");
32
+ }
33
+ }
34
+ /**
35
+ * Generates a key pair which is appropriate for Fedify.
36
+ * @param algorithm The algorithm to use. Currently only RSASSA-PKCS1-v1_5 and
37
+ * Ed25519 are supported.
38
+ * @returns The generated key pair.
39
+ * @throws {TypeError} If the algorithm is unsupported.
40
+ */
41
+ function generateCryptoKeyPair(algorithm) {
42
+ if (algorithm == null) (0, __logtape_logtape.getLogger)([
43
+ "fedify",
44
+ "sig",
45
+ "key"
46
+ ]).warn("No algorithm specified. Using RSASSA-PKCS1-v1_5 by default, but it is recommended to specify the algorithm explicitly as the parameter will be required in the future.");
47
+ if (algorithm == null || algorithm === "RSASSA-PKCS1-v1_5") return crypto.subtle.generateKey({
48
+ name: "RSASSA-PKCS1-v1_5",
49
+ modulusLength: 4096,
50
+ publicExponent: new Uint8Array([
51
+ 1,
52
+ 0,
53
+ 1
54
+ ]),
55
+ hash: "SHA-256"
56
+ }, true, ["sign", "verify"]);
57
+ else if (algorithm === "Ed25519") return crypto.subtle.generateKey("Ed25519", true, ["sign", "verify"]);
58
+ throw new TypeError("Unsupported algorithm: " + algorithm);
59
+ }
60
+ /**
61
+ * Exports a key in JWK format.
62
+ * @param key The key to export. Either public or private key.
63
+ * @returns The exported key in JWK format. The key is suitable for
64
+ * serialization and storage.
65
+ * @throws {TypeError} If the key is invalid or unsupported.
66
+ */
67
+ async function exportJwk(key) {
68
+ validateCryptoKey(key);
69
+ const jwk = await crypto.subtle.exportKey("jwk", key);
70
+ if (jwk.crv === "Ed25519") jwk.alg = "Ed25519";
71
+ return jwk;
72
+ }
73
+ /**
74
+ * Imports a key from JWK format.
75
+ * @param jwk The key in JWK format.
76
+ * @param type Which type of key to import, either `"public"` or `"private"`.
77
+ * @returns The imported key.
78
+ * @throws {TypeError} If the key is invalid or unsupported.
79
+ */
80
+ async function importJwk(jwk, type) {
81
+ let key;
82
+ if (jwk.kty === "RSA" && jwk.alg === "RS256") key = await crypto.subtle.importKey("jwk", jwk, {
83
+ name: "RSASSA-PKCS1-v1_5",
84
+ hash: "SHA-256"
85
+ }, true, type === "public" ? ["verify"] : ["sign"]);
86
+ else if (jwk.kty === "OKP" && jwk.crv === "Ed25519") {
87
+ if (navigator?.userAgent === "Cloudflare-Workers") {
88
+ jwk = { ...jwk };
89
+ delete jwk.alg;
90
+ }
91
+ key = await crypto.subtle.importKey("jwk", jwk, "Ed25519", true, type === "public" ? ["verify"] : ["sign"]);
92
+ } else throw new TypeError("Unsupported JWK format.");
93
+ validateCryptoKey(key, type);
94
+ return key;
95
+ }
96
+ /**
97
+ * Fetches a {@link CryptographicKey} or {@link Multikey} from the given URL.
98
+ * If the given URL contains an {@link Actor} object, it tries to find
99
+ * the corresponding key in the `publicKey` or `assertionMethod` property.
100
+ * @template T The type of the key to fetch. Either {@link CryptographicKey}
101
+ * or {@link Multikey}.
102
+ * @param keyId The URL of the key.
103
+ * @param cls The class of the key to fetch. Either {@link CryptographicKey}
104
+ * or {@link Multikey}.
105
+ * @param options Options for fetching the key. See {@link FetchKeyOptions}.
106
+ * @returns The fetched key or `null` if the key is not found.
107
+ * @since 1.3.0
108
+ */
109
+ function fetchKey(keyId, cls, options = {}) {
110
+ const tracerProvider = options.tracerProvider ?? __opentelemetry_api.trace.getTracerProvider();
111
+ const tracer = tracerProvider.getTracer(require_request.deno_default.name, require_request.deno_default.version);
112
+ keyId = typeof keyId === "string" ? new URL(keyId) : keyId;
113
+ return tracer.startActiveSpan("activitypub.fetch_key", {
114
+ kind: __opentelemetry_api.SpanKind.CLIENT,
115
+ attributes: {
116
+ "http.method": "GET",
117
+ "url.full": keyId.href,
118
+ "url.scheme": keyId.protocol.replace(/:$/, ""),
119
+ "url.domain": keyId.hostname,
120
+ "url.path": keyId.pathname,
121
+ "url.query": keyId.search.replace(/^\?/, ""),
122
+ "url.fragment": keyId.hash.replace(/^#/, "")
123
+ }
124
+ }, async (span) => {
125
+ try {
126
+ const result = await fetchKeyInternal(keyId, cls, options);
127
+ span.setAttribute("activitypub.actor.key.cached", result.cached);
128
+ return result;
129
+ } catch (e) {
130
+ span.setStatus({
131
+ code: __opentelemetry_api.SpanStatusCode.ERROR,
132
+ message: String(e)
133
+ });
134
+ throw e;
135
+ } finally {
136
+ span.end();
137
+ }
138
+ });
139
+ }
140
+ async function fetchKeyInternal(keyId, cls, { documentLoader, contextLoader, keyCache, tracerProvider } = {}) {
141
+ const logger = (0, __logtape_logtape.getLogger)([
142
+ "fedify",
143
+ "sig",
144
+ "key"
145
+ ]);
146
+ const cacheKey = typeof keyId === "string" ? new URL(keyId) : keyId;
147
+ keyId = typeof keyId === "string" ? keyId : keyId.href;
148
+ if (keyCache != null) {
149
+ const cachedKey = await keyCache.get(cacheKey);
150
+ if (cachedKey instanceof cls && cachedKey.publicKey != null) {
151
+ logger.debug("Key {keyId} found in cache.", { keyId });
152
+ return {
153
+ key: cachedKey,
154
+ cached: true
155
+ };
156
+ } else if (cachedKey === null) {
157
+ logger.debug("Entry {keyId} found in cache, but it is unavailable.", { keyId });
158
+ return {
159
+ key: null,
160
+ cached: true
161
+ };
162
+ }
163
+ }
164
+ logger.debug("Fetching key {keyId} to verify signature...", { keyId });
165
+ let document;
166
+ try {
167
+ const remoteDocument = await (documentLoader ?? (0, __fedify_vocab_runtime.getDocumentLoader)())(keyId);
168
+ document = remoteDocument.document;
169
+ } catch (_) {
170
+ logger.debug("Failed to fetch key {keyId}.", { keyId });
171
+ await keyCache?.set(cacheKey, null);
172
+ return {
173
+ key: null,
174
+ cached: false
175
+ };
176
+ }
177
+ let object;
178
+ try {
179
+ object = await require_actor.Object.fromJsonLd(document, {
180
+ documentLoader,
181
+ contextLoader,
182
+ tracerProvider
183
+ });
184
+ } catch (e) {
185
+ if (!(e instanceof TypeError)) throw e;
186
+ try {
187
+ object = await cls.fromJsonLd(document, {
188
+ documentLoader,
189
+ contextLoader,
190
+ tracerProvider
191
+ });
192
+ } catch (e$1) {
193
+ if (e$1 instanceof TypeError) {
194
+ logger.debug("Failed to verify; key {keyId} returned an invalid object.", { keyId });
195
+ await keyCache?.set(cacheKey, null);
196
+ return {
197
+ key: null,
198
+ cached: false
199
+ };
200
+ }
201
+ throw e$1;
202
+ }
203
+ }
204
+ let key = null;
205
+ if (object instanceof cls) key = object;
206
+ else if (require_actor.isActor(object)) {
207
+ const keys = cls === require_actor.CryptographicKey ? object.getPublicKeys({
208
+ documentLoader,
209
+ contextLoader,
210
+ tracerProvider
211
+ }) : object.getAssertionMethods({
212
+ documentLoader,
213
+ contextLoader,
214
+ tracerProvider
215
+ });
216
+ let length = 0;
217
+ let lastKey = null;
218
+ for await (const k of keys) {
219
+ length++;
220
+ lastKey = k;
221
+ if (k.id?.href === keyId) {
222
+ key = k;
223
+ break;
224
+ }
225
+ }
226
+ const keyIdUrl = new URL(keyId);
227
+ if (key == null && keyIdUrl.hash === "" && length === 1) key = lastKey;
228
+ if (key == null) {
229
+ logger.debug("Failed to verify; object {keyId} returned an {actorType}, but has no key matching {keyId}.", {
230
+ keyId,
231
+ actorType: object.constructor.name
232
+ });
233
+ await keyCache?.set(cacheKey, null);
234
+ return {
235
+ key: null,
236
+ cached: false
237
+ };
238
+ }
239
+ } else {
240
+ logger.debug("Failed to verify; key {keyId} returned an invalid object.", { keyId });
241
+ await keyCache?.set(cacheKey, null);
242
+ return {
243
+ key: null,
244
+ cached: false
245
+ };
246
+ }
247
+ if (key.publicKey == null) {
248
+ logger.debug("Failed to verify; key {keyId} has no publicKeyPem field.", { keyId });
249
+ await keyCache?.set(cacheKey, null);
250
+ return {
251
+ key: null,
252
+ cached: false
253
+ };
254
+ }
255
+ if (keyCache != null) {
256
+ await keyCache.set(cacheKey, key);
257
+ logger.debug("Key {keyId} cached.", { keyId });
258
+ }
259
+ return {
260
+ key,
261
+ cached: false
262
+ };
263
+ }
264
+
265
+ //#endregion
16
266
  //#region src/sig/http.ts
17
267
  /**
18
268
  * Signs a request using the given private key.
@@ -24,9 +274,9 @@ const structured_field_values = require_chunk.__toESM(require("structured-field-
24
274
  * @throws {TypeError} If the private key is invalid or unsupported.
25
275
  */
26
276
  async function signRequest(request, privateKey, keyId, options = {}) {
27
- require_key.validateCryptoKey(privateKey, "private");
277
+ validateCryptoKey(privateKey, "private");
28
278
  const tracerProvider = options.tracerProvider ?? __opentelemetry_api.trace.getTracerProvider();
29
- const tracer = tracerProvider.getTracer(require_docloader.deno_default.name, require_docloader.deno_default.version);
279
+ const tracer = tracerProvider.getTracer(require_request.deno_default.name, require_request.deno_default.version);
30
280
  return await tracer.startActiveSpan("http_signatures.sign", async (span) => {
31
281
  try {
32
282
  const spec = options.spec ?? "draft-cavage-http-signatures-12";
@@ -255,7 +505,7 @@ const supportedHashAlgorithms = {
255
505
  */
256
506
  async function verifyRequest(request, options = {}) {
257
507
  const tracerProvider = options.tracerProvider ?? __opentelemetry_api.trace.getTracerProvider();
258
- const tracer = tracerProvider.getTracer(require_docloader.deno_default.name, require_docloader.deno_default.version);
508
+ const tracer = tracerProvider.getTracer(require_request.deno_default.name, require_request.deno_default.version);
259
509
  return await tracer.startActiveSpan("http_signatures.verify", async (span) => {
260
510
  if (span.isRecording()) {
261
511
  span.setAttribute(__opentelemetry_semantic_conventions.ATTR_HTTP_REQUEST_METHOD, request.method);
@@ -427,7 +677,7 @@ async function verifyRequestDraft(request, span, { documentLoader, contextLoader
427
677
  const { keyId, headers, signature } = sigValues;
428
678
  span?.setAttribute("http_signatures.key_id", keyId);
429
679
  if ("algorithm" in sigValues) span?.setAttribute("http_signatures.algorithm", sigValues.algorithm);
430
- const { key, cached } = await require_key.fetchKey(new URL(keyId), require_actor.CryptographicKey, {
680
+ const { key, cached } = await fetchKey(new URL(keyId), require_actor.CryptographicKey, {
431
681
  documentLoader,
432
682
  contextLoader,
433
683
  keyCache,
@@ -620,7 +870,7 @@ async function verifyRequestRfc9421(request, span, { documentLoader, contextLoad
620
870
  }
621
871
  span?.setAttribute("http_signatures.key_id", sigInput.keyId);
622
872
  span?.setAttribute("http_signatures.created", sigInput.created.toString());
623
- const { key, cached } = await require_key.fetchKey(new URL(sigInput.keyId), require_actor.CryptographicKey, {
873
+ const { key, cached } = await fetchKey(new URL(sigInput.keyId), require_actor.CryptographicKey, {
624
874
  documentLoader,
625
875
  contextLoader,
626
876
  keyCache,
@@ -812,12 +1062,42 @@ Object.defineProperty(exports, 'doubleKnock', {
812
1062
  return doubleKnock;
813
1063
  }
814
1064
  });
1065
+ Object.defineProperty(exports, 'exportJwk', {
1066
+ enumerable: true,
1067
+ get: function () {
1068
+ return exportJwk;
1069
+ }
1070
+ });
1071
+ Object.defineProperty(exports, 'fetchKey', {
1072
+ enumerable: true,
1073
+ get: function () {
1074
+ return fetchKey;
1075
+ }
1076
+ });
1077
+ Object.defineProperty(exports, 'generateCryptoKeyPair', {
1078
+ enumerable: true,
1079
+ get: function () {
1080
+ return generateCryptoKeyPair;
1081
+ }
1082
+ });
1083
+ Object.defineProperty(exports, 'importJwk', {
1084
+ enumerable: true,
1085
+ get: function () {
1086
+ return importJwk;
1087
+ }
1088
+ });
815
1089
  Object.defineProperty(exports, 'signRequest', {
816
1090
  enumerable: true,
817
1091
  get: function () {
818
1092
  return signRequest;
819
1093
  }
820
1094
  });
1095
+ Object.defineProperty(exports, 'validateCryptoKey', {
1096
+ enumerable: true,
1097
+ get: function () {
1098
+ return validateCryptoKey;
1099
+ }
1100
+ });
821
1101
  Object.defineProperty(exports, 'verifyRequest', {
822
1102
  enumerable: true,
823
1103
  get: function () {
@@ -3,8 +3,8 @@
3
3
  import { URLPattern } from "urlpattern-polyfill";
4
4
  globalThis.addEventListener = () => {};
5
5
 
6
- import { CryptographicKey, deno_default } from "./type-COb6KNlm.js";
7
- import { fetchKey, validateCryptoKey } from "./key-BSJX6n9o.js";
6
+ import { CryptographicKey, deno_default } from "./lookup-BbEekiru.js";
7
+ import { fetchKey, validateCryptoKey } from "./key-jSvnymAk.js";
8
8
  import { getLogger } from "@logtape/logtape";
9
9
  import { SpanStatusCode, trace } from "@opentelemetry/api";
10
10
  import { ATTR_HTTP_REQUEST_HEADER, ATTR_HTTP_REQUEST_METHOD, ATTR_URL_FULL } from "@opentelemetry/semantic-conventions";
@@ -2,16 +2,266 @@
2
2
  import { Temporal } from "@js-temporal/polyfill";
3
3
  import { URLPattern } from "urlpattern-polyfill";
4
4
 
5
- import { deno_default } from "./docloader-AMdJU291.js";
6
- import { CryptographicKey } from "./actor-C0gLJq8I.js";
7
- import { fetchKey, validateCryptoKey } from "./key-DFefr8X2.js";
5
+ import { deno_default } from "./request-DKGQaofB.js";
6
+ import { CryptographicKey, Object as Object$1, isActor } from "./actor-DPD6Gbdn.js";
8
7
  import { getLogger } from "@logtape/logtape";
9
- import { SpanStatusCode, trace } from "@opentelemetry/api";
10
- import { decodeBase64, encodeBase64 } from "byte-encodings/base64";
8
+ import { SpanKind, SpanStatusCode, trace } from "@opentelemetry/api";
9
+ import { getDocumentLoader } from "@fedify/vocab-runtime";
11
10
  import { encodeHex } from "byte-encodings/hex";
12
11
  import { ATTR_HTTP_REQUEST_HEADER, ATTR_HTTP_REQUEST_METHOD, ATTR_URL_FULL } from "@opentelemetry/semantic-conventions";
12
+ import { decodeBase64, encodeBase64 } from "byte-encodings/base64";
13
13
  import { Item, decodeDict, encodeItem } from "structured-field-values";
14
14
 
15
+ //#region src/sig/key.ts
16
+ /**
17
+ * Checks if the given key is valid and supported. No-op if the key is valid,
18
+ * otherwise throws an error.
19
+ * @param key The key to check.
20
+ * @param type Which type of key to check. If not specified, the key can be
21
+ * either public or private.
22
+ * @throws {TypeError} If the key is invalid or unsupported.
23
+ */
24
+ function validateCryptoKey(key, type) {
25
+ if (type != null && key.type !== type) throw new TypeError(`The key is not a ${type} key.`);
26
+ if (!key.extractable) throw new TypeError("The key is not extractable.");
27
+ if (key.algorithm.name !== "RSASSA-PKCS1-v1_5" && key.algorithm.name !== "Ed25519") throw new TypeError("Currently only RSASSA-PKCS1-v1_5 and Ed25519 keys are supported. More algorithms will be added in the future!");
28
+ if (key.algorithm.name === "RSASSA-PKCS1-v1_5") {
29
+ const algorithm = key.algorithm;
30
+ if (algorithm.hash.name !== "SHA-256") throw new TypeError("For compatibility with the existing Fediverse software (e.g., Mastodon), hash algorithm for RSASSA-PKCS1-v1_5 keys must be SHA-256.");
31
+ }
32
+ }
33
+ /**
34
+ * Generates a key pair which is appropriate for Fedify.
35
+ * @param algorithm The algorithm to use. Currently only RSASSA-PKCS1-v1_5 and
36
+ * Ed25519 are supported.
37
+ * @returns The generated key pair.
38
+ * @throws {TypeError} If the algorithm is unsupported.
39
+ */
40
+ function generateCryptoKeyPair(algorithm) {
41
+ if (algorithm == null) getLogger([
42
+ "fedify",
43
+ "sig",
44
+ "key"
45
+ ]).warn("No algorithm specified. Using RSASSA-PKCS1-v1_5 by default, but it is recommended to specify the algorithm explicitly as the parameter will be required in the future.");
46
+ if (algorithm == null || algorithm === "RSASSA-PKCS1-v1_5") return crypto.subtle.generateKey({
47
+ name: "RSASSA-PKCS1-v1_5",
48
+ modulusLength: 4096,
49
+ publicExponent: new Uint8Array([
50
+ 1,
51
+ 0,
52
+ 1
53
+ ]),
54
+ hash: "SHA-256"
55
+ }, true, ["sign", "verify"]);
56
+ else if (algorithm === "Ed25519") return crypto.subtle.generateKey("Ed25519", true, ["sign", "verify"]);
57
+ throw new TypeError("Unsupported algorithm: " + algorithm);
58
+ }
59
+ /**
60
+ * Exports a key in JWK format.
61
+ * @param key The key to export. Either public or private key.
62
+ * @returns The exported key in JWK format. The key is suitable for
63
+ * serialization and storage.
64
+ * @throws {TypeError} If the key is invalid or unsupported.
65
+ */
66
+ async function exportJwk(key) {
67
+ validateCryptoKey(key);
68
+ const jwk = await crypto.subtle.exportKey("jwk", key);
69
+ if (jwk.crv === "Ed25519") jwk.alg = "Ed25519";
70
+ return jwk;
71
+ }
72
+ /**
73
+ * Imports a key from JWK format.
74
+ * @param jwk The key in JWK format.
75
+ * @param type Which type of key to import, either `"public"` or `"private"`.
76
+ * @returns The imported key.
77
+ * @throws {TypeError} If the key is invalid or unsupported.
78
+ */
79
+ async function importJwk(jwk, type) {
80
+ let key;
81
+ if (jwk.kty === "RSA" && jwk.alg === "RS256") key = await crypto.subtle.importKey("jwk", jwk, {
82
+ name: "RSASSA-PKCS1-v1_5",
83
+ hash: "SHA-256"
84
+ }, true, type === "public" ? ["verify"] : ["sign"]);
85
+ else if (jwk.kty === "OKP" && jwk.crv === "Ed25519") {
86
+ if (navigator?.userAgent === "Cloudflare-Workers") {
87
+ jwk = { ...jwk };
88
+ delete jwk.alg;
89
+ }
90
+ key = await crypto.subtle.importKey("jwk", jwk, "Ed25519", true, type === "public" ? ["verify"] : ["sign"]);
91
+ } else throw new TypeError("Unsupported JWK format.");
92
+ validateCryptoKey(key, type);
93
+ return key;
94
+ }
95
+ /**
96
+ * Fetches a {@link CryptographicKey} or {@link Multikey} from the given URL.
97
+ * If the given URL contains an {@link Actor} object, it tries to find
98
+ * the corresponding key in the `publicKey` or `assertionMethod` property.
99
+ * @template T The type of the key to fetch. Either {@link CryptographicKey}
100
+ * or {@link Multikey}.
101
+ * @param keyId The URL of the key.
102
+ * @param cls The class of the key to fetch. Either {@link CryptographicKey}
103
+ * or {@link Multikey}.
104
+ * @param options Options for fetching the key. See {@link FetchKeyOptions}.
105
+ * @returns The fetched key or `null` if the key is not found.
106
+ * @since 1.3.0
107
+ */
108
+ function fetchKey(keyId, cls, options = {}) {
109
+ const tracerProvider = options.tracerProvider ?? trace.getTracerProvider();
110
+ const tracer = tracerProvider.getTracer(deno_default.name, deno_default.version);
111
+ keyId = typeof keyId === "string" ? new URL(keyId) : keyId;
112
+ return tracer.startActiveSpan("activitypub.fetch_key", {
113
+ kind: SpanKind.CLIENT,
114
+ attributes: {
115
+ "http.method": "GET",
116
+ "url.full": keyId.href,
117
+ "url.scheme": keyId.protocol.replace(/:$/, ""),
118
+ "url.domain": keyId.hostname,
119
+ "url.path": keyId.pathname,
120
+ "url.query": keyId.search.replace(/^\?/, ""),
121
+ "url.fragment": keyId.hash.replace(/^#/, "")
122
+ }
123
+ }, async (span) => {
124
+ try {
125
+ const result = await fetchKeyInternal(keyId, cls, options);
126
+ span.setAttribute("activitypub.actor.key.cached", result.cached);
127
+ return result;
128
+ } catch (e) {
129
+ span.setStatus({
130
+ code: SpanStatusCode.ERROR,
131
+ message: String(e)
132
+ });
133
+ throw e;
134
+ } finally {
135
+ span.end();
136
+ }
137
+ });
138
+ }
139
+ async function fetchKeyInternal(keyId, cls, { documentLoader, contextLoader, keyCache, tracerProvider } = {}) {
140
+ const logger = getLogger([
141
+ "fedify",
142
+ "sig",
143
+ "key"
144
+ ]);
145
+ const cacheKey = typeof keyId === "string" ? new URL(keyId) : keyId;
146
+ keyId = typeof keyId === "string" ? keyId : keyId.href;
147
+ if (keyCache != null) {
148
+ const cachedKey = await keyCache.get(cacheKey);
149
+ if (cachedKey instanceof cls && cachedKey.publicKey != null) {
150
+ logger.debug("Key {keyId} found in cache.", { keyId });
151
+ return {
152
+ key: cachedKey,
153
+ cached: true
154
+ };
155
+ } else if (cachedKey === null) {
156
+ logger.debug("Entry {keyId} found in cache, but it is unavailable.", { keyId });
157
+ return {
158
+ key: null,
159
+ cached: true
160
+ };
161
+ }
162
+ }
163
+ logger.debug("Fetching key {keyId} to verify signature...", { keyId });
164
+ let document;
165
+ try {
166
+ const remoteDocument = await (documentLoader ?? getDocumentLoader())(keyId);
167
+ document = remoteDocument.document;
168
+ } catch (_) {
169
+ logger.debug("Failed to fetch key {keyId}.", { keyId });
170
+ await keyCache?.set(cacheKey, null);
171
+ return {
172
+ key: null,
173
+ cached: false
174
+ };
175
+ }
176
+ let object;
177
+ try {
178
+ object = await Object$1.fromJsonLd(document, {
179
+ documentLoader,
180
+ contextLoader,
181
+ tracerProvider
182
+ });
183
+ } catch (e) {
184
+ if (!(e instanceof TypeError)) throw e;
185
+ try {
186
+ object = await cls.fromJsonLd(document, {
187
+ documentLoader,
188
+ contextLoader,
189
+ tracerProvider
190
+ });
191
+ } catch (e$1) {
192
+ if (e$1 instanceof TypeError) {
193
+ logger.debug("Failed to verify; key {keyId} returned an invalid object.", { keyId });
194
+ await keyCache?.set(cacheKey, null);
195
+ return {
196
+ key: null,
197
+ cached: false
198
+ };
199
+ }
200
+ throw e$1;
201
+ }
202
+ }
203
+ let key = null;
204
+ if (object instanceof cls) key = object;
205
+ else if (isActor(object)) {
206
+ const keys = cls === CryptographicKey ? object.getPublicKeys({
207
+ documentLoader,
208
+ contextLoader,
209
+ tracerProvider
210
+ }) : object.getAssertionMethods({
211
+ documentLoader,
212
+ contextLoader,
213
+ tracerProvider
214
+ });
215
+ let length = 0;
216
+ let lastKey = null;
217
+ for await (const k of keys) {
218
+ length++;
219
+ lastKey = k;
220
+ if (k.id?.href === keyId) {
221
+ key = k;
222
+ break;
223
+ }
224
+ }
225
+ const keyIdUrl = new URL(keyId);
226
+ if (key == null && keyIdUrl.hash === "" && length === 1) key = lastKey;
227
+ if (key == null) {
228
+ logger.debug("Failed to verify; object {keyId} returned an {actorType}, but has no key matching {keyId}.", {
229
+ keyId,
230
+ actorType: object.constructor.name
231
+ });
232
+ await keyCache?.set(cacheKey, null);
233
+ return {
234
+ key: null,
235
+ cached: false
236
+ };
237
+ }
238
+ } else {
239
+ logger.debug("Failed to verify; key {keyId} returned an invalid object.", { keyId });
240
+ await keyCache?.set(cacheKey, null);
241
+ return {
242
+ key: null,
243
+ cached: false
244
+ };
245
+ }
246
+ if (key.publicKey == null) {
247
+ logger.debug("Failed to verify; key {keyId} has no publicKeyPem field.", { keyId });
248
+ await keyCache?.set(cacheKey, null);
249
+ return {
250
+ key: null,
251
+ cached: false
252
+ };
253
+ }
254
+ if (keyCache != null) {
255
+ await keyCache.set(cacheKey, key);
256
+ logger.debug("Key {keyId} cached.", { keyId });
257
+ }
258
+ return {
259
+ key,
260
+ cached: false
261
+ };
262
+ }
263
+
264
+ //#endregion
15
265
  //#region src/sig/http.ts
16
266
  /**
17
267
  * Signs a request using the given private key.
@@ -805,4 +1055,4 @@ function timingSafeEqual(a, b) {
805
1055
  }
806
1056
 
807
1057
  //#endregion
808
- export { doubleKnock, signRequest, verifyRequest };
1058
+ export { doubleKnock, exportJwk, fetchKey, generateCryptoKeyPair, importJwk, signRequest, validateCryptoKey, verifyRequest };
@@ -3,7 +3,7 @@
3
3
  import { URLPattern } from "urlpattern-polyfill";
4
4
  globalThis.addEventListener = () => {};
5
5
 
6
- import { Activity, deno_default, getTypeId } from "./type-COb6KNlm.js";
6
+ import { Activity, deno_default, getTypeId } from "./lookup-BbEekiru.js";
7
7
  import { getLogger } from "@logtape/logtape";
8
8
  import { SpanKind, SpanStatusCode, context, propagation, trace } from "@opentelemetry/api";
9
9
 
@@ -3,8 +3,9 @@
3
3
  import { URLPattern } from "urlpattern-polyfill";
4
4
  globalThis.addEventListener = () => {};
5
5
 
6
- import { CryptographicKey, Object as Object$1, deno_default, getDocumentLoader } from "./type-COb6KNlm.js";
7
- import { isActor } from "./actor-VrXd7EdX.js";
6
+ import { CryptographicKey, Object as Object$1, deno_default } from "./lookup-BbEekiru.js";
7
+ import { isActor } from "./actor-Cg8ZhoVF.js";
8
+ import { getDocumentLoader } from "@fedify/vocab-runtime";
8
9
  import { getLogger } from "@logtape/logtape";
9
10
  import { SpanKind, SpanStatusCode, trace } from "@opentelemetry/api";
10
11
 
@@ -3,7 +3,7 @@
3
3
  import { URLPattern } from "urlpattern-polyfill";
4
4
  globalThis.addEventListener = () => {};
5
5
 
6
- import { CryptographicKey, Multikey } from "./type-COb6KNlm.js";
6
+ import { CryptographicKey, Multikey } from "./lookup-BbEekiru.js";
7
7
 
8
8
  //#region src/federation/keycache.ts
9
9
  var KvKeyCache = class {
@@ -3,7 +3,8 @@
3
3
  import { URLPattern } from "urlpattern-polyfill";
4
4
  globalThis.addEventListener = () => {};
5
5
 
6
- import { CryptographicKey, Multikey, importSpki } from "./type-COb6KNlm.js";
6
+ import { CryptographicKey, Multikey } from "./lookup-BbEekiru.js";
7
+ import { importSpki } from "@fedify/vocab-runtime";
7
8
 
8
9
  //#region src/testing/keys.ts
9
10
  const rsaPublicKey1 = new CryptographicKey({