@lushly-dev/afd-server 2.2.1 → 2.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (220) hide show
  1. package/README.md +468 -6
  2. package/dist/approval-crypto.d.ts +26 -4
  3. package/dist/approval-crypto.d.ts.map +1 -1
  4. package/dist/approval-crypto.js +90 -7
  5. package/dist/approval-crypto.js.map +1 -1
  6. package/dist/approval-relay.d.ts +5 -5
  7. package/dist/approval-relay.d.ts.map +1 -1
  8. package/dist/approval.d.ts +6 -1
  9. package/dist/approval.d.ts.map +1 -1
  10. package/dist/approval.js +43 -32
  11. package/dist/approval.js.map +1 -1
  12. package/dist/bootstrap/afd-context.d.ts +2 -27
  13. package/dist/bootstrap/afd-context.d.ts.map +1 -1
  14. package/dist/bootstrap/afd-context.js +6 -36
  15. package/dist/bootstrap/afd-context.js.map +1 -1
  16. package/dist/bootstrap/afd-help.d.ts +24 -2
  17. package/dist/bootstrap/afd-help.d.ts.map +1 -1
  18. package/dist/bootstrap/afd-help.js +17 -2
  19. package/dist/bootstrap/afd-help.js.map +1 -1
  20. package/dist/bootstrap/described-command.d.ts +1 -0
  21. package/dist/bootstrap/described-command.d.ts.map +1 -1
  22. package/dist/bootstrap/described-command.js.map +1 -1
  23. package/dist/bootstrap/registry.d.ts +13 -0
  24. package/dist/bootstrap/registry.d.ts.map +1 -1
  25. package/dist/bootstrap/registry.js +30 -1
  26. package/dist/bootstrap/registry.js.map +1 -1
  27. package/dist/command-allow.d.ts +31 -0
  28. package/dist/command-allow.d.ts.map +1 -0
  29. package/dist/command-allow.js +55 -0
  30. package/dist/command-allow.js.map +1 -0
  31. package/dist/command-names.d.ts +17 -0
  32. package/dist/command-names.d.ts.map +1 -1
  33. package/dist/command-names.js +55 -0
  34. package/dist/command-names.js.map +1 -1
  35. package/dist/command-routing.d.ts +10 -3
  36. package/dist/command-routing.d.ts.map +1 -1
  37. package/dist/command-routing.js +18 -7
  38. package/dist/command-routing.js.map +1 -1
  39. package/dist/confirmation-gate.d.ts +32 -0
  40. package/dist/confirmation-gate.d.ts.map +1 -0
  41. package/dist/confirmation-gate.js +140 -0
  42. package/dist/confirmation-gate.js.map +1 -0
  43. package/dist/confirmations.d.ts +15 -23
  44. package/dist/confirmations.d.ts.map +1 -1
  45. package/dist/confirmations.js +8 -127
  46. package/dist/confirmations.js.map +1 -1
  47. package/dist/context-fields-describe.d.ts +18 -0
  48. package/dist/context-fields-describe.d.ts.map +1 -0
  49. package/dist/context-fields-describe.js +17 -0
  50. package/dist/context-fields-describe.js.map +1 -0
  51. package/dist/context-fields.d.ts +50 -0
  52. package/dist/context-fields.d.ts.map +1 -0
  53. package/dist/context-fields.js +146 -0
  54. package/dist/context-fields.js.map +1 -0
  55. package/dist/context-scope.d.ts +29 -2
  56. package/dist/context-scope.d.ts.map +1 -1
  57. package/dist/context-scope.js +37 -1
  58. package/dist/context-scope.js.map +1 -1
  59. package/dist/credentials.d.ts +17 -0
  60. package/dist/credentials.d.ts.map +1 -0
  61. package/dist/credentials.js +16 -0
  62. package/dist/credentials.js.map +1 -0
  63. package/dist/define-core.d.ts +213 -0
  64. package/dist/define-core.d.ts.map +1 -0
  65. package/dist/define-core.js +161 -0
  66. package/dist/define-core.js.map +1 -0
  67. package/dist/define.d.ts +13 -8
  68. package/dist/define.d.ts.map +1 -1
  69. package/dist/define.js +13 -8
  70. package/dist/define.js.map +1 -1
  71. package/dist/direct-registry.d.ts +154 -7
  72. package/dist/direct-registry.d.ts.map +1 -1
  73. package/dist/direct-registry.js +111 -13
  74. package/dist/direct-registry.js.map +1 -1
  75. package/dist/direct-tool-surface.d.ts +66 -0
  76. package/dist/direct-tool-surface.d.ts.map +1 -0
  77. package/dist/direct-tool-surface.js +162 -0
  78. package/dist/direct-tool-surface.js.map +1 -0
  79. package/dist/direct.d.ts +68 -0
  80. package/dist/direct.d.ts.map +1 -0
  81. package/dist/direct.js +66 -0
  82. package/dist/direct.js.map +1 -0
  83. package/dist/execution.d.ts +11 -1
  84. package/dist/execution.d.ts.map +1 -1
  85. package/dist/execution.js +18 -4
  86. package/dist/execution.js.map +1 -1
  87. package/dist/frontmatter.d.ts +16 -0
  88. package/dist/frontmatter.d.ts.map +1 -0
  89. package/dist/frontmatter.js +111 -0
  90. package/dist/frontmatter.js.map +1 -0
  91. package/dist/http-handler.d.ts +11 -2
  92. package/dist/http-handler.d.ts.map +1 -1
  93. package/dist/http-handler.js +46 -15
  94. package/dist/http-handler.js.map +1 -1
  95. package/dist/http-security.d.ts.map +1 -1
  96. package/dist/http-security.js.map +1 -1
  97. package/dist/http-sessions.d.ts +12 -4
  98. package/dist/http-sessions.d.ts.map +1 -1
  99. package/dist/http-sessions.js +4 -4
  100. package/dist/http-sessions.js.map +1 -1
  101. package/dist/http-sse.js.map +1 -1
  102. package/dist/index.d.ts +16 -4
  103. package/dist/index.d.ts.map +1 -1
  104. package/dist/index.js +20 -4
  105. package/dist/index.js.map +1 -1
  106. package/dist/issue-message.d.ts +20 -0
  107. package/dist/issue-message.d.ts.map +1 -0
  108. package/dist/issue-message.js +55 -0
  109. package/dist/issue-message.js.map +1 -0
  110. package/dist/journal-replay.d.ts +7 -0
  111. package/dist/journal-replay.d.ts.map +1 -1
  112. package/dist/journal-replay.js +18 -1
  113. package/dist/journal-replay.js.map +1 -1
  114. package/dist/journal.d.ts +9 -1
  115. package/dist/journal.d.ts.map +1 -1
  116. package/dist/journal.js +44 -1
  117. package/dist/journal.js.map +1 -1
  118. package/dist/json-rpc.d.ts +2 -2
  119. package/dist/json-rpc.d.ts.map +1 -1
  120. package/dist/json-rpc.js.map +1 -1
  121. package/dist/json-schema.d.ts +30 -0
  122. package/dist/json-schema.d.ts.map +1 -0
  123. package/dist/json-schema.js +25 -0
  124. package/dist/json-schema.js.map +1 -0
  125. package/dist/lazy-tools.d.ts +3 -0
  126. package/dist/lazy-tools.d.ts.map +1 -1
  127. package/dist/lazy-tools.js +2 -0
  128. package/dist/lazy-tools.js.map +1 -1
  129. package/dist/mcp-auth-bearer.d.ts +23 -0
  130. package/dist/mcp-auth-bearer.d.ts.map +1 -0
  131. package/dist/mcp-auth-bearer.js +50 -0
  132. package/dist/mcp-auth-bearer.js.map +1 -0
  133. package/dist/mcp-auth-jwt.d.ts +178 -0
  134. package/dist/mcp-auth-jwt.d.ts.map +1 -0
  135. package/dist/mcp-auth-jwt.js +365 -0
  136. package/dist/mcp-auth-jwt.js.map +1 -0
  137. package/dist/mcp-auth-resource.d.ts +74 -0
  138. package/dist/mcp-auth-resource.d.ts.map +1 -0
  139. package/dist/mcp-auth-resource.js +196 -0
  140. package/dist/mcp-auth-resource.js.map +1 -0
  141. package/dist/mcp-auth-rules.d.ts +135 -0
  142. package/dist/mcp-auth-rules.d.ts.map +1 -0
  143. package/dist/mcp-auth-rules.js +306 -0
  144. package/dist/mcp-auth-rules.js.map +1 -0
  145. package/dist/mcp-auth.d.ts +81 -0
  146. package/dist/mcp-auth.d.ts.map +1 -0
  147. package/dist/mcp-auth.js +256 -0
  148. package/dist/mcp-auth.js.map +1 -0
  149. package/dist/mcp-modern.d.ts +29 -5
  150. package/dist/mcp-modern.d.ts.map +1 -1
  151. package/dist/mcp-modern.js +99 -19
  152. package/dist/mcp-modern.js.map +1 -1
  153. package/dist/meta-argument.d.ts +42 -0
  154. package/dist/meta-argument.d.ts.map +1 -0
  155. package/dist/meta-argument.js +102 -0
  156. package/dist/meta-argument.js.map +1 -0
  157. package/dist/meta-keys.d.ts +29 -0
  158. package/dist/meta-keys.d.ts.map +1 -0
  159. package/dist/meta-keys.js +90 -0
  160. package/dist/meta-keys.js.map +1 -0
  161. package/dist/meta-tools.d.ts +8 -1
  162. package/dist/meta-tools.d.ts.map +1 -1
  163. package/dist/meta-tools.js +17 -7
  164. package/dist/meta-tools.js.map +1 -1
  165. package/dist/middleware.d.ts.map +1 -1
  166. package/dist/middleware.js +0 -1
  167. package/dist/middleware.js.map +1 -1
  168. package/dist/patterns.d.ts +46 -0
  169. package/dist/patterns.d.ts.map +1 -0
  170. package/dist/patterns.js +38 -0
  171. package/dist/patterns.js.map +1 -0
  172. package/dist/principal-binding.d.ts +42 -0
  173. package/dist/principal-binding.d.ts.map +1 -0
  174. package/dist/principal-binding.js +70 -0
  175. package/dist/principal-binding.js.map +1 -0
  176. package/dist/request-context.d.ts +7 -1
  177. package/dist/request-context.d.ts.map +1 -1
  178. package/dist/request-context.js +14 -3
  179. package/dist/request-context.js.map +1 -1
  180. package/dist/resources.d.ts +122 -0
  181. package/dist/resources.d.ts.map +1 -0
  182. package/dist/resources.js +256 -0
  183. package/dist/resources.js.map +1 -0
  184. package/dist/schema.d.ts +7 -171
  185. package/dist/schema.d.ts.map +1 -1
  186. package/dist/schema.js +10 -135
  187. package/dist/schema.js.map +1 -1
  188. package/dist/server-types.d.ts +94 -7
  189. package/dist/server-types.d.ts.map +1 -1
  190. package/dist/server-types.js +2 -0
  191. package/dist/server-types.js.map +1 -1
  192. package/dist/server.d.ts +2 -2
  193. package/dist/server.d.ts.map +1 -1
  194. package/dist/server.js +45 -31
  195. package/dist/server.js.map +1 -1
  196. package/dist/skills.d.ts +95 -0
  197. package/dist/skills.d.ts.map +1 -0
  198. package/dist/skills.js +340 -0
  199. package/dist/skills.js.map +1 -0
  200. package/dist/strict-json.d.ts +17 -0
  201. package/dist/strict-json.d.ts.map +1 -0
  202. package/dist/strict-json.js +93 -0
  203. package/dist/strict-json.js.map +1 -0
  204. package/dist/tool-router.d.ts +13 -1
  205. package/dist/tool-router.d.ts.map +1 -1
  206. package/dist/tool-router.js +44 -10
  207. package/dist/tool-router.js.map +1 -1
  208. package/dist/tools.d.ts +10 -5
  209. package/dist/tools.d.ts.map +1 -1
  210. package/dist/tools.js +48 -22
  211. package/dist/tools.js.map +1 -1
  212. package/dist/validation.d.ts +6 -45
  213. package/dist/validation.d.ts.map +1 -1
  214. package/dist/validation.js +25 -58
  215. package/dist/validation.js.map +1 -1
  216. package/dist/verified-credentials.d.ts +34 -0
  217. package/dist/verified-credentials.d.ts.map +1 -0
  218. package/dist/verified-credentials.js +51 -0
  219. package/dist/verified-credentials.js.map +1 -0
  220. package/package.json +18 -6
@@ -1 +1 @@
1
- {"version":3,"file":"lazy-tools.js","sourceRoot":"","sources":["../src/lazy-tools.ts"],"names":[],"mappings":"AAAA;;GAEG;AAGH,OAAO,EAAE,YAAY,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAC3E,OAAO,EAAE,mBAAmB,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAuD/E,kFAAkF;AAClF,UAAU;AACV,kFAAkF;AAElF,SAAS,mBAAmB,CAAC,IAAY,EAAE,GAAG,GAAG,GAAG;IACnD,MAAM,aAAa,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,IAAI,IAAI,CAAC;IACpD,IAAI,aAAa,CAAC,MAAM,IAAI,GAAG;QAAE,OAAO,aAAa,CAAC;IACtD,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,GAAG,GAAG,CAAC,CAAC,QAAQ,CAAC;AAC1C,CAAC;AAED,SAAS,aAAa,CAAC,GAAyB,EAAE,KAAa;IAC9D,MAAM,MAAM,GAAG,KAAK,CAAC,WAAW,EAAE,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;IAChE,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,IAAI,CAAC;IACrC,MAAM,MAAM,GAAG,GAAG,GAAG,CAAC,IAAI,IAAI,GAAG,CAAC,WAAW,EAAE,CAAC,WAAW,EAAE,CAAC;IAC9D,OAAO,MAAM,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC;AACxD,CAAC;AAED,SAAS,WAAW,CAAC,GAAyB,EAAE,IAAc,EAAE,IAAmB;IAClF,MAAM,OAAO,GAA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AC,QAAQ,IAAI,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC,MAAM,GAAG,CAAC,IAAI,EAAE,QAAQ,EAAE,GAAG,CAAC,QAAQ,EAAE,CAAC;SACvF,CAAC;IACH,CAAC,CAAC,CAAC;IAEH,MAAM,UAAU,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC;IACzD,MAAM,eAAe,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,WAAW,CAAC,GAAG,EAAE,GAAG,SAAS,CAAC,GAAG,GAAG,CAAC,GAAG,GAAG,CAAC;IAEhF,OAAO,OAAO,CAAC,OAAO,EAAE;QACvB,SAAS,EAAE,YAAY,UAAU,OAAO,KAAK,CAAC,MAAM,qBAAqB;QACzE,UAAU,EAAE,GAAG;QACf,QAAQ,EAAE,EAAE,eAAe,EAAE;KAC7B,CAAiC,CAAC;AACpC,CAAC"}
@@ -0,0 +1,23 @@
1
+ /**
2
+ * @fileoverview The one bearer token of a request (`spec/authorization.md`, M2).
3
+ */
4
+ import type { AuthRuleCode } from './mcp-auth-rules.js';
5
+ /** The token, or the rule a request broke. */
6
+ export type BearerResult = {
7
+ token: string;
8
+ } | {
9
+ refused: AuthRuleCode;
10
+ };
11
+ /**
12
+ * The one bearer token of a request (`spec/authorization.md`, M2), or the rule it breaks.
13
+ *
14
+ * - `authorization` holds every `Authorization` header value, in order (`[]` for none).
15
+ * - `query` is the request's query string without its `?`, and `formBody` the body of a request
16
+ * whose `Content-Type` is `application/x-www-form-urlencoded`. An `access_token` parameter in
17
+ * either refuses the request, even beside a valid header.
18
+ *
19
+ * Refused with `token-location` (a token in the query or a form body, or a second header;
20
+ * checked first), `no-token`, or `token-malformed` (not `Bearer <b64token>`).
21
+ */
22
+ export declare function extractBearer(authorization: readonly string[], query?: string, formBody?: string): BearerResult;
23
+ //# sourceMappingURL=mcp-auth-bearer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mcp-auth-bearer.d.ts","sourceRoot":"","sources":["../src/mcp-auth-bearer.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AA+BxD,8CAA8C;AAC9C,MAAM,MAAM,YAAY,GAAG;IAAE,KAAK,EAAE,MAAM,CAAA;CAAE,GAAG;IAAE,OAAO,EAAE,YAAY,CAAA;CAAE,CAAC;AAEzE;;;;;;;;;;GAUG;AACH,wBAAgB,aAAa,CAC5B,aAAa,EAAE,SAAS,MAAM,EAAE,EAChC,KAAK,CAAC,EAAE,MAAM,EACd,QAAQ,CAAC,EAAE,MAAM,GACf,YAAY,CAYd"}
@@ -0,0 +1,50 @@
1
+ /**
2
+ * @fileoverview The one bearer token of a request (`spec/authorization.md`, M2).
3
+ */
4
+ /**
5
+ * `Bearer 1*SP b64token` (RFC 6750 2.1), the scheme in any ASCII case, around which an HTTP parser
6
+ * may have left spaces or tabs. One anchored match whose parts cannot overlap (no space or tab
7
+ * inside a `b64token`), so it runs in time linear in the value: it reads an unauthenticated
8
+ * header on every route.
9
+ */
10
+ const BEARER = /^[ \t]*bearer +([A-Za-z0-9\-._~+/]+=*)[ \t]*$/i;
11
+ /** The token of a `Bearer` value, or `undefined` when the value breaks M2's grammar. */
12
+ function parseBearer(value) {
13
+ return BEARER.exec(value)?.[1];
14
+ }
15
+ /** Percent- and `+`-decoding of one form name, bytewise. A malformed escape is kept as written. */
16
+ function formDecode(encoded) {
17
+ return encoded
18
+ .replace(/\+/g, ' ')
19
+ .replace(/%([0-9A-Fa-f]{2})/g, (_match, hex) => String.fromCharCode(Number.parseInt(hex, 16)));
20
+ }
21
+ /** Whether an `application/x-www-form-urlencoded` string has a parameter named `access_token`. */
22
+ function namesAccessToken(encoded) {
23
+ return encoded
24
+ .split('&')
25
+ .some((pair) => formDecode(pair.split('=', 1)[0] ?? '') === 'access_token');
26
+ }
27
+ /**
28
+ * The one bearer token of a request (`spec/authorization.md`, M2), or the rule it breaks.
29
+ *
30
+ * - `authorization` holds every `Authorization` header value, in order (`[]` for none).
31
+ * - `query` is the request's query string without its `?`, and `formBody` the body of a request
32
+ * whose `Content-Type` is `application/x-www-form-urlencoded`. An `access_token` parameter in
33
+ * either refuses the request, even beside a valid header.
34
+ *
35
+ * Refused with `token-location` (a token in the query or a form body, or a second header;
36
+ * checked first), `no-token`, or `token-malformed` (not `Bearer <b64token>`).
37
+ */
38
+ export function extractBearer(authorization, query, formBody) {
39
+ if ((query !== undefined && namesAccessToken(query)) ||
40
+ (formBody !== undefined && namesAccessToken(formBody)) ||
41
+ authorization.length > 1) {
42
+ return { refused: 'token-location' };
43
+ }
44
+ const value = authorization[0];
45
+ if (value === undefined)
46
+ return { refused: 'no-token' };
47
+ const token = parseBearer(value);
48
+ return token === undefined ? { refused: 'token-malformed' } : { token };
49
+ }
50
+ //# sourceMappingURL=mcp-auth-bearer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mcp-auth-bearer.js","sourceRoot":"","sources":["../src/mcp-auth-bearer.ts"],"names":[],"mappings":"AAAA;;GAEG;AAIH;;;;;GAKG;AACH,MAAM,MAAM,GAAG,gDAAgD,CAAC;AAEhE,wFAAwF;AACxF,SAAS,WAAW,CAAC,KAAa;IACjC,OAAO,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;AAChC,CAAC;AAED,mGAAmG;AACnG,SAAS,UAAU,CAAC,OAAe;IAClC,OAAO,OAAO;SACZ,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC;SACnB,OAAO,CAAC,oBAAoB,EAAE,CAAC,MAAM,EAAE,GAAW,EAAE,EAAE,CACtD,MAAM,CAAC,YAAY,CAAC,MAAM,CAAC,QAAQ,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC,CAC7C,CAAC;AACJ,CAAC;AAED,kGAAkG;AAClG,SAAS,gBAAgB,CAAC,OAAe;IACxC,OAAO,OAAO;SACZ,KAAK,CAAC,GAAG,CAAC;SACV,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,UAAU,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,KAAK,cAAc,CAAC,CAAC;AAC9E,CAAC;AAKD;;;;;;;;;;GAUG;AACH,MAAM,UAAU,aAAa,CAC5B,aAAgC,EAChC,KAAc,EACd,QAAiB;IAEjB,IACC,CAAC,KAAK,KAAK,SAAS,IAAI,gBAAgB,CAAC,KAAK,CAAC,CAAC;QAChD,CAAC,QAAQ,KAAK,SAAS,IAAI,gBAAgB,CAAC,QAAQ,CAAC,CAAC;QACtD,aAAa,CAAC,MAAM,GAAG,CAAC,EACvB,CAAC;QACF,OAAO,EAAE,OAAO,EAAE,gBAAgB,EAAE,CAAC;IACtC,CAAC;IACD,MAAM,KAAK,GAAG,aAAa,CAAC,CAAC,CAAC,CAAC;IAC/B,IAAI,KAAK,KAAK,SAAS;QAAE,OAAO,EAAE,OAAO,EAAE,UAAU,EAAE,CAAC;IACxD,MAAM,KAAK,GAAG,WAAW,CAAC,KAAK,CAAC,CAAC;IACjC,OAAO,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,iBAAiB,EAAE,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC;AACzE,CAAC"}
@@ -0,0 +1,178 @@
1
+ /**
2
+ * @fileoverview AFD's JWT access-token verifier (`spec/authorization.md`, M3, RFC 9068), its key
3
+ * sources, and introspection as the alternative for opaque tokens.
4
+ *
5
+ * `jose` checks the signature, through WebCrypto, and nothing else: AFD never calls `jwtVerify`,
6
+ * so no library default applies. Routing, every header and key rule, and every claim rule are
7
+ * this module's and `mcp-auth-rules.ts`'s own code, in the specification's order. AFD opens no
8
+ * socket: keys and introspection answers come from the host.
9
+ */
10
+ import { type Clock } from '@lushly-dev/afd-core';
11
+ import { type AuthRuleCode, type Credentials, type SigningAlgorithm, type TokenRules, type TokenRulesOptions } from './mcp-auth-rules.js';
12
+ /** What a {@link TokenVerifier} knows of the request a token came on. Never a header. */
13
+ export interface TokenRequest {
14
+ /**
15
+ * The SHA-256 thumbprint (unpadded base64url, RFC 8705 3.1) of the TLS client certificate the
16
+ * request was made with; `undefined` when there is none.
17
+ */
18
+ readonly certificate?: string;
19
+ }
20
+ /**
21
+ * A verifier's answer: the credentials, or the rule the token broke. `rule` is an M5 rule code,
22
+ * or a host's own code for a refusal of its own (logged, never sent; every refusal is the same
23
+ * 401 with `error="invalid_token"`). `issuer` is the alias of the issuer the token was routed
24
+ * to, once known: never the `iss` claim as sent.
25
+ */
26
+ export type TokenVerification = {
27
+ ok: true;
28
+ credentials: Credentials;
29
+ } | {
30
+ ok: false;
31
+ rule: AuthRuleCode | (string & {});
32
+ issuer?: string;
33
+ };
34
+ /**
35
+ * Verifies one request's bearer token (`spec/authorization.md`, M3). AFD's {@link JwtVerifier}
36
+ * handles JWTs; {@link createIntrospectionVerifier} handles opaque tokens through the host's
37
+ * introspection call. A host may wrap either to refuse more (its own `rule`), never to accept
38
+ * what it refused. A throw fails the request closed as `unavailable`.
39
+ */
40
+ export interface TokenVerifier {
41
+ verify(token: string, request: TokenRequest): Promise<TokenVerification>;
42
+ /**
43
+ * The claim rules this verifier applies, when it can say: `createMcpHandler` then checks that
44
+ * the metadata publishes the same resource and binding requirement.
45
+ */
46
+ readonly rules?: TokenRules;
47
+ /**
48
+ * The exact `iss` strings whose tokens this verifier accepts, when it can say:
49
+ * `createMcpHandler` then checks that the metadata names each in `authorizationServers`.
50
+ */
51
+ readonly issuers?: readonly string[];
52
+ }
53
+ /** Why a {@link KeySource} gave no key: `unknown` refuses with `key`, `unavailable` fails closed. */
54
+ export type KeyLookup = {
55
+ key: Readonly<Record<string, unknown>>;
56
+ } | {
57
+ error: 'unknown' | 'unavailable';
58
+ };
59
+ /**
60
+ * Where an issuer's signing keys come from (`spec/authorization.md`, M3 rule 3). The host fetches
61
+ * them; AFD opens no socket. The `kid` comes from a token not yet verified, so it is the
62
+ * caller's choice: refetch on an unknown `kid` at most once in a while, and keep the last good
63
+ * keys, as {@link jwksKeySource} does.
64
+ */
65
+ export interface KeySource {
66
+ /** The key of `issuer` (its exact configured string) with this `kid`. */
67
+ key(issuer: string, kid: string): Promise<KeyLookup>;
68
+ }
69
+ /** What {@link jwksKeySource} takes besides the document or its loader. */
70
+ export interface JwksKeySourceOptions {
71
+ /** The shortest time between two loads, in milliseconds. Default: 30 seconds. */
72
+ minRefetchIntervalMs?: number;
73
+ /** How long loaded keys are used before they are loaded again, in milliseconds. Default: 5 minutes. */
74
+ maxAgeMs?: number;
75
+ /**
76
+ * How long the last good keys are kept while every load fails, in milliseconds, counted from
77
+ * the load that fetched them. Past it the source answers `unavailable`, so a key the issuer
78
+ * withdrew stops verifying even when its JWKS cannot be reached. Default: 1 hour; at least
79
+ * `maxAgeMs`.
80
+ */
81
+ maxStaleMs?: number;
82
+ /** The clock the intervals read. Default: the system clock. */
83
+ clock?: Clock;
84
+ }
85
+ /**
86
+ * A {@link KeySource} over a JWKS document (RFC 7517 5): fixed, or loaded by the host's `load`
87
+ * (which fetches the issuer's `jwks_uri`, say). Loaded keys are used for `maxAgeMs`; an unknown
88
+ * `kid` loads again at most once per `minRefetchIntervalMs`; a failed load keeps the last good
89
+ * keys for up to `maxStaleMs`, and with none (or when a load for an unknown `kid` fails, or the
90
+ * keys are older than `maxStaleMs`) answers `unavailable`.
91
+ *
92
+ * Give each issuer a key source of its own: a `kid` names a key only within one issuer's set,
93
+ * and `JwtVerifier` refuses two issuers that share one.
94
+ *
95
+ * @throws Error for a fixed document that has no `keys` array, or a key without a non-empty
96
+ * `kid` or with one another key has; or `maxStaleMs` shorter than `maxAgeMs`
97
+ */
98
+ export declare function jwksKeySource(jwks: unknown, options?: JwksKeySourceOptions): KeySource;
99
+ /** One issuer whose JWT access tokens a {@link JwtVerifier} accepts. */
100
+ export interface JwtIssuer {
101
+ /** The issuer's exact `iss` string: an absolute `https` URL. */
102
+ issuer: string;
103
+ /** The algorithms it signs with. `none` and `HS*` are never accepted. */
104
+ algorithms: readonly SigningAlgorithm[];
105
+ /** Where its keys come from. */
106
+ keys: KeySource;
107
+ /** Logged with a refused token's rule code. Default: `issuer`. */
108
+ alias?: string;
109
+ }
110
+ /** What {@link JwtVerifier} takes. */
111
+ export interface JwtVerifierOptions {
112
+ /** The claim rules: the resource (the only audience accepted), skew, lifetime and scopes. */
113
+ rules: TokenRules | TokenRulesOptions;
114
+ /** The issuers whose tokens are accepted; at least one, none twice. */
115
+ issuers: readonly JwtIssuer[];
116
+ /** The one introspection issuer, which every token that is not a compact JWS goes to. */
117
+ introspection?: TokenVerifier;
118
+ /** The clock the time rules read. Default: the system clock. */
119
+ clock?: Clock;
120
+ }
121
+ /** Whether `token` has the shape of a compact JWS: the routing test of M3 rule 1. */
122
+ export declare function isCompactJws(token: string): boolean;
123
+ /**
124
+ * AFD's verifier of JWT access tokens (RFC 9068), applying `spec/authorization.md` M3 in order.
125
+ *
126
+ * A compact JWS goes to the configured issuer its unverified `iss` names exactly (else
127
+ * `unknown-issuer`). Any other token goes to the introspection verifier, when one is configured,
128
+ * and is otherwise refused with `unknown-issuer`: a token is never sent to an issuer that did not
129
+ * issue it. A JWS whose header or payload is not a JSON object with unique member names is
130
+ * refused (`typ` for the header, `unknown-issuer` for the payload). The audience check (M3.1)
131
+ * always runs: `rules.resource` is required.
132
+ */
133
+ export declare class JwtVerifier implements TokenVerifier {
134
+ #private;
135
+ readonly rules: TokenRules;
136
+ /** The `iss` strings accepted: the JWT issuers', then the introspection verifier's. */
137
+ readonly issuers: readonly string[];
138
+ /**
139
+ * @throws Error for invalid rules, no issuer, an issuer that is not an absolute `https` URL,
140
+ * one with no algorithm or an algorithm AFD cannot verify, two issuers with the same `iss`
141
+ * string, alias or key source object, or an introspection verifier whose rules name another
142
+ * resource or binding requirement
143
+ */
144
+ constructor(options: JwtVerifierOptions);
145
+ verify(token: string, request?: TokenRequest): Promise<TokenVerification>;
146
+ }
147
+ /** What {@link createIntrospectionVerifier} takes. */
148
+ export interface IntrospectionVerifierOptions {
149
+ /** The introspection issuer's exact `iss` string: an absolute `https` URL. */
150
+ issuer: string;
151
+ /** The claim rules, as for {@link JwtVerifier}. */
152
+ rules: TokenRules | TokenRulesOptions;
153
+ /**
154
+ * The host's RFC 7662 call: POST the token to the issuer's introspection endpoint with the
155
+ * server's own client authentication, and resolve to the parsed JSON response. A throw fails
156
+ * the request closed (`unavailable`). The token goes to this issuer alone.
157
+ */
158
+ introspect(token: string): Promise<unknown>;
159
+ /** Logged with a refused token's rule code. Default: `issuer`. */
160
+ alias?: string;
161
+ /** The clock the time rules and the cache read. Default: the system clock. */
162
+ clock?: Clock;
163
+ /**
164
+ * Active responses cached by the token's SHA-256 until the earlier of their `exp` and 60
165
+ * seconds, re-checked on every use. Default 1000 entries; 0 turns caching off.
166
+ */
167
+ cacheSize?: number;
168
+ }
169
+ /**
170
+ * A {@link TokenVerifier} for opaque tokens through the host's introspection call
171
+ * (`spec/authorization.md`, M3, "Introspection"): `active: true` and the six required fields,
172
+ * then rules 5 to 10. Give it to {@link JwtVerifier} as `introspection`, so only tokens that are
173
+ * not a compact JWS reach it.
174
+ *
175
+ * @throws Error for invalid rules or an issuer that is not an absolute `https` URL
176
+ */
177
+ export declare function createIntrospectionVerifier(options: IntrospectionVerifierOptions): TokenVerifier;
178
+ //# sourceMappingURL=mcp-auth-jwt.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mcp-auth-jwt.d.ts","sourceRoot":"","sources":["../src/mcp-auth-jwt.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAGH,OAAO,EAAE,KAAK,KAAK,EAAe,MAAM,sBAAsB,CAAC;AAG/D,OAAO,EACN,KAAK,YAAY,EACjB,KAAK,WAAW,EAQhB,KAAK,gBAAgB,EACrB,KAAK,UAAU,EACf,K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BAAiB,CAAC;IACtC;;;;OAIG;IACH,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IAC5C,kEAAkE;IAClE,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,8EAA8E;IAC9E,KAAK,CAAC,EAAE,KAAK,CAAC;IACd;;;OAGG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;CACnB;AAKD;;;;;;;GAOG;AACH,wBAAgB,2BAA2B,CAAC,OAAO,EAAE,4BAA4B,GAAG,aAAa,CA2ChG"}
@@ -0,0 +1,365 @@
1
+ /**
2
+ * @fileoverview AFD's JWT access-token verifier (`spec/authorization.md`, M3, RFC 9068), its key
3
+ * sources, and introspection as the alternative for opaque tokens.
4
+ *
5
+ * `jose` checks the signature, through WebCrypto, and nothing else: AFD never calls `jwtVerify`,
6
+ * so no library default applies. Routing, every header and key rule, and every claim rule are
7
+ * this module's and `mcp-auth-rules.ts`'s own code, in the specification's order. AFD opens no
8
+ * socket: keys and introspection answers come from the host.
9
+ */
10
+ import { createHash } from 'node:crypto';
11
+ import { systemClock } from '@lushly-dev/afd-core';
12
+ import { compactVerify, errors, importJWK } from 'jose';
13
+ import { assertHttpsUrl } from './mcp-auth-resource.js';
14
+ import { checkClaims, checkIntrospection, checkJwsHeader, checkKeyFit, createTokenRules, SIGNING_ALGORITHMS, } from './mcp-auth-rules.js';
15
+ import { decodeBase64url, decodeJoseObject } from './strict-json.js';
16
+ function isRecord(value) {
17
+ return !!value && typeof value === 'object' && !Array.isArray(value);
18
+ }
19
+ /** The keys of a JWKS document by `kid`, or the reason it is not one. */
20
+ function readJwks(document) {
21
+ const keys = isRecord(document) ? document.keys : undefined;
22
+ if (!Array.isArray(keys))
23
+ return 'a JWKS document has a "keys" array';
24
+ const byKid = new Map();
25
+ for (const key of keys) {
26
+ if (!isRecord(key) || typeof key.kid !== 'string' || key.kid === '') {
27
+ return 'every key of a JWKS document is an object with a non-empty kid';
28
+ }
29
+ if (byKid.has(key.kid))
30
+ return `kid ${JSON.stringify(key.kid)} appears more than once`;
31
+ byKid.set(key.kid, Object.freeze({ ...key }));
32
+ }
33
+ return byKid;
34
+ }
35
+ /**
36
+ * A {@link KeySource} over a JWKS document (RFC 7517 5): fixed, or loaded by the host's `load`
37
+ * (which fetches the issuer's `jwks_uri`, say). Loaded keys are used for `maxAgeMs`; an unknown
38
+ * `kid` loads again at most once per `minRefetchIntervalMs`; a failed load keeps the last good
39
+ * keys for up to `maxStaleMs`, and with none (or when a load for an unknown `kid` fails, or the
40
+ * keys are older than `maxStaleMs`) answers `unavailable`.
41
+ *
42
+ * Give each issuer a key source of its own: a `kid` names a key only within one issuer's set,
43
+ * and `JwtVerifier` refuses two issuers that share one.
44
+ *
45
+ * @throws Error for a fixed document that has no `keys` array, or a key without a non-empty
46
+ * `kid` or with one another key has; or `maxStaleMs` shorter than `maxAgeMs`
47
+ */
48
+ export function jwksKeySource(jwks, options = {}) {
49
+ if (typeof jwks !== 'function') {
50
+ const keys = readJwks(jwks);
51
+ if (typeof keys === 'string')
52
+ throw new Error(`[AFD] ${keys}`);
53
+ return {
54
+ key: async (_issuer, kid) => {
55
+ const key = keys.get(kid);
56
+ return key ? { key } : { error: 'unknown' };
57
+ },
58
+ };
59
+ }
60
+ const load = jwks;
61
+ const clock = options.clock ?? systemClock;
62
+ const minInterval = options.minRefetchIntervalMs ?? 30_000;
63
+ const maxAge = options.maxAgeMs ?? 300_000;
64
+ const maxStale = options.maxStaleMs ?? 3_600_000;
65
+ if (!(maxStale >= maxAge))
66
+ throw new Error('[AFD] maxStaleMs must be at least maxAgeMs');
67
+ let keys;
68
+ let loadedAt = Number.NEGATIVE_INFINITY;
69
+ let attemptedAt = Number.NEGATIVE_INFINITY;
70
+ let pending;
71
+ async function attempt() {
72
+ try {
73
+ const read = readJwks(await load());
74
+ if (typeof read === 'string')
75
+ return false;
76
+ keys = read;
77
+ loadedAt = clock.now();
78
+ return true;
79
+ }
80
+ catch {
81
+ return false;
82
+ }
83
+ }
84
+ /** Load once, shared by concurrent callers; whether this load succeeded. */
85
+ function refresh() {
86
+ if (!pending) {
87
+ attemptedAt = clock.now();
88
+ pending = attempt().finally(() => {
89
+ pending = undefined;
90
+ });
91
+ }
92
+ return pending;
93
+ }
94
+ return {
95
+ async key(_issuer, kid) {
96
+ const now = clock.now();
97
+ const mayLoad = now - attemptedAt >= minInterval;
98
+ if ((keys === undefined || now - loadedAt >= maxAge) && (mayLoad || pending)) {
99
+ await refresh();
100
+ }
101
+ // Keys older than `maxStaleMs` (every load since has failed) verify nothing.
102
+ if (keys === undefined || clock.now() - loadedAt >= maxStale)
103
+ return { error: 'unavailable' };
104
+ const known = keys.get(kid);
105
+ if (known)
106
+ return { key: known };
107
+ if (clock.now() - attemptedAt >= minInterval || pending) {
108
+ if (!(await refresh()))
109
+ return { error: 'unavailable' };
110
+ const fetched = keys?.get(kid);
111
+ if (fetched)
112
+ return { key: fetched };
113
+ }
114
+ return { error: 'unknown' };
115
+ },
116
+ };
117
+ }
118
+ const COMPACT_PART = /^[A-Za-z0-9_-]*$/;
119
+ /** The three parts of a compact JWS (the first two non-empty), or `undefined` (M3 rule 1). */
120
+ function compactJwsParts(token) {
121
+ const parts = token.split('.');
122
+ if (parts.length !== 3)
123
+ return undefined;
124
+ const [header, payload, signature] = parts;
125
+ if (header === '' || payload === '' || !parts.every((part) => COMPACT_PART.test(part))) {
126
+ return undefined;
127
+ }
128
+ return [header, payload, signature];
129
+ }
130
+ /** Whether `token` has the shape of a compact JWS: the routing test of M3 rule 1. */
131
+ export function isCompactJws(token) {
132
+ return compactJwsParts(token) !== undefined;
133
+ }
134
+ /** The public members each key type needs for verification; nothing else reaches `jose`. */
135
+ const PUBLIC_MEMBERS = {
136
+ RSA: ['n', 'e'],
137
+ EC: ['crv', 'x', 'y'],
138
+ OKP: ['crv', 'x'],
139
+ };
140
+ /** Coordinate sizes in bytes, where the curve fixes them. */
141
+ const COORDINATE_BYTES = {
142
+ ES256: 32,
143
+ ES384: 48,
144
+ EdDSA: 32,
145
+ };
146
+ /**
147
+ * The verification key of a JWK that passed rule 3, holding only its public members, or
148
+ * `undefined` when its material is unusable (bad base64url, a coordinate of the wrong length, an
149
+ * Ed448 key, which this verifier does not implement).
150
+ */
151
+ async function verificationKey(alg, jwk) {
152
+ const kty = jwk.kty;
153
+ const members = PUBLIC_MEMBERS[kty] ?? [];
154
+ const material = { kty };
155
+ for (const member of members) {
156
+ const value = jwk[member];
157
+ if (typeof value !== 'string' || value === '')
158
+ return undefined;
159
+ if (member !== 'crv' && !decodeBase64url(value)?.length)
160
+ return undefined;
161
+ material[member] = value;
162
+ }
163
+ if (alg === 'EdDSA' && material.crv !== 'Ed25519')
164
+ return undefined;
165
+ const size = COORDINATE_BYTES[alg];
166
+ if (size !== undefined) {
167
+ const coordinates = alg === 'EdDSA' ? ['x'] : ['x', 'y'];
168
+ for (const member of coordinates) {
169
+ if (decodeBase64url(material[member])?.length !== size)
170
+ return undefined;
171
+ }
172
+ }
173
+ try {
174
+ return await importJWK(material, alg);
175
+ }
176
+ catch {
177
+ return undefined;
178
+ }
179
+ }
180
+ /**
181
+ * AFD's verifier of JWT access tokens (RFC 9068), applying `spec/authorization.md` M3 in order.
182
+ *
183
+ * A compact JWS goes to the configured issuer its unverified `iss` names exactly (else
184
+ * `unknown-issuer`). Any other token goes to the introspection verifier, when one is configured,
185
+ * and is otherwise refused with `unknown-issuer`: a token is never sent to an issuer that did not
186
+ * issue it. A JWS whose header or payload is not a JSON object with unique member names is
187
+ * refused (`typ` for the header, `unknown-issuer` for the payload). The audience check (M3.1)
188
+ * always runs: `rules.resource` is required.
189
+ */
190
+ export class JwtVerifier {
191
+ rules;
192
+ /** The `iss` strings accepted: the JWT issuers', then the introspection verifier's. */
193
+ issuers;
194
+ #issuers;
195
+ #introspection;
196
+ #clock;
197
+ /**
198
+ * @throws Error for invalid rules, no issuer, an issuer that is not an absolute `https` URL,
199
+ * one with no algorithm or an algorithm AFD cannot verify, two issuers with the same `iss`
200
+ * string, alias or key source object, or an introspection verifier whose rules name another
201
+ * resource or binding requirement
202
+ */
203
+ constructor(options) {
204
+ this.rules = createTokenRules(options.rules);
205
+ const issuers = [];
206
+ for (const entry of options.issuers ?? []) {
207
+ assertHttpsUrl('issuer', entry.issuer);
208
+ const alias = entry.alias ?? entry.issuer;
209
+ if (typeof alias !== 'string' || !/^[\x21-\x7E]{1,256}$/.test(alias)) {
210
+ throw new Error(`[AFD] issuer alias ${JSON.stringify(alias)} is not visible ASCII`);
211
+ }
212
+ const algorithms = [...new Set(entry.algorithms ?? [])];
213
+ if (algorithms.length === 0) {
214
+ throw new Error(`[AFD] issuer ${JSON.stringify(alias)} lists no signing algorithm`);
215
+ }
216
+ for (const algorithm of algorithms) {
217
+ if (!SIGNING_ALGORITHMS.includes(algorithm)) {
218
+ throw new Error(`[AFD] AFD cannot verify ${JSON.stringify(algorithm)} signatures`);
219
+ }
220
+ }
221
+ if (typeof entry.keys?.key !== 'function') {
222
+ throw new Error(`[AFD] issuer ${JSON.stringify(alias)} has no key source`);
223
+ }
224
+ if (issuers.some((kept) => kept.issuer === entry.issuer || kept.alias === alias)) {
225
+ throw new Error(`[AFD] issuer ${JSON.stringify(alias)} is configured twice`);
226
+ }
227
+ // A kid names a key only within one issuer's set: a shared source would let one
228
+ // issuer's token be checked against another issuer's key.
229
+ if (issuers.some((kept) => kept.keys === entry.keys)) {
230
+ throw new Error(`[AFD] issuer ${JSON.stringify(alias)} shares its key source with another issuer; give each issuer its own`);
231
+ }
232
+ issuers.push(Object.freeze({ ...entry, alias, algorithms: Object.freeze(algorithms) }));
233
+ }
234
+ if (issuers.length === 0)
235
+ throw new Error('[AFD] a JWT verifier needs at least one issuer');
236
+ const introspection = options.introspection;
237
+ const inner = introspection?.rules;
238
+ if (inner &&
239
+ (inner.resource !== this.rules.resource ||
240
+ inner.requireBoundTokens !== this.rules.requireBoundTokens)) {
241
+ throw new Error('[AFD] the introspection verifier accepts another audience or binding requirement than the JWT verifier');
242
+ }
243
+ this.#issuers = Object.freeze(issuers);
244
+ this.issuers = Object.freeze([
245
+ ...issuers.map((entry) => entry.issuer),
246
+ ...(introspection?.issuers ?? []),
247
+ ]);
248
+ this.#introspection = introspection;
249
+ this.#clock = options.clock ?? systemClock;
250
+ }
251
+ async verify(token, request = {}) {
252
+ const parts = compactJwsParts(token);
253
+ if (!parts) {
254
+ return this.#introspection
255
+ ? this.#introspection.verify(token, request)
256
+ : { ok: false, rule: 'unknown-issuer' };
257
+ }
258
+ const [headerPart, payloadPart, signaturePart] = parts;
259
+ // 1. Route by the unverified `iss`.
260
+ const payload = decodeJoseObject(payloadPart);
261
+ const iss = payload?.iss;
262
+ const issuer = this.#issuers.find((candidate) => candidate.issuer === iss);
263
+ if (!payload || !issuer)
264
+ return { ok: false, rule: 'unknown-issuer' };
265
+ const refused = (rule) => ({
266
+ ok: false,
267
+ rule,
268
+ issuer: issuer.alias,
269
+ });
270
+ const outcome = (result) => (result.ok ? undefined : refused(result.rule));
271
+ // 2. Header.
272
+ const header = decodeJoseObject(headerPart);
273
+ if (!header)
274
+ return refused('typ');
275
+ const headerRefusal = outcome(checkJwsHeader(header, issuer.algorithms));
276
+ if (headerRefusal)
277
+ return headerRefusal;
278
+ const alg = header.alg;
279
+ // 3. Key.
280
+ let lookup;
281
+ try {
282
+ lookup = await issuer.keys.key(issuer.issuer, header.kid);
283
+ }
284
+ catch {
285
+ return refused('unavailable');
286
+ }
287
+ if ('error' in lookup)
288
+ return refused(lookup.error === 'unknown' ? 'key' : 'unavailable');
289
+ const keyRefusal = outcome(checkKeyFit(header, lookup.key));
290
+ if (keyRefusal)
291
+ return keyRefusal;
292
+ const key = await verificationKey(alg, lookup.key);
293
+ if (!key)
294
+ return refused('key');
295
+ // 4. Signature.
296
+ if (!decodeBase64url(signaturePart))
297
+ return refused('signature');
298
+ try {
299
+ await compactVerify(token, key, { algorithms: [alg] });
300
+ }
301
+ catch (error) {
302
+ return refused(error instanceof errors.JWSSignatureVerificationFailed ? 'signature' : 'key');
303
+ }
304
+ // 5 to 10. Claims.
305
+ const now = Math.floor(this.#clock.now() / 1000);
306
+ const claims = checkClaims(payload, issuer.issuer, this.rules, now, request.certificate);
307
+ return claims.ok ? { ok: true, credentials: claims.value } : refused(claims.rule);
308
+ }
309
+ }
310
+ /** How long an introspection answer may be reused (M3, "Introspection"). */
311
+ const INTROSPECTION_CACHE_MS = 60_000;
312
+ /**
313
+ * A {@link TokenVerifier} for opaque tokens through the host's introspection call
314
+ * (`spec/authorization.md`, M3, "Introspection"): `active: true` and the six required fields,
315
+ * then rules 5 to 10. Give it to {@link JwtVerifier} as `introspection`, so only tokens that are
316
+ * not a compact JWS reach it.
317
+ *
318
+ * @throws Error for invalid rules or an issuer that is not an absolute `https` URL
319
+ */
320
+ export function createIntrospectionVerifier(options) {
321
+ assertHttpsUrl('issuer', options.issuer);
322
+ const rules = createTokenRules(options.rules);
323
+ const { issuer, introspect } = options;
324
+ const alias = options.alias ?? issuer;
325
+ const clock = options.clock ?? systemClock;
326
+ const cacheSize = options.cacheSize ?? 1000;
327
+ const cache = new Map();
328
+ async function response(token) {
329
+ const digest = createHash('sha256').update(token).digest('base64url');
330
+ const cached = cache.get(digest);
331
+ if (cached && cached.until > clock.now())
332
+ return cached.response;
333
+ cache.delete(digest);
334
+ const answer = await introspect(token);
335
+ const exp = isRecord(answer) && answer.active === true ? answer.exp : undefined;
336
+ if (cacheSize > 0 && typeof exp === 'number' && Number.isFinite(exp)) {
337
+ if (cache.size >= cacheSize)
338
+ cache.delete(cache.keys().next().value);
339
+ cache.set(digest, {
340
+ response: answer,
341
+ until: Math.min(exp * 1000, clock.now() + INTROSPECTION_CACHE_MS),
342
+ });
343
+ }
344
+ return answer;
345
+ }
346
+ return {
347
+ rules,
348
+ issuers: Object.freeze([issuer]),
349
+ async verify(token, request = {}) {
350
+ let answer;
351
+ try {
352
+ answer = await response(token);
353
+ }
354
+ catch {
355
+ return { ok: false, rule: 'unavailable', issuer: alias };
356
+ }
357
+ const now = Math.floor(clock.now() / 1000);
358
+ const checked = checkIntrospection(answer, issuer, rules, now, request.certificate);
359
+ return checked.ok
360
+ ? { ok: true, credentials: checked.value }
361
+ : { ok: false, rule: checked.rule, issuer: alias };
362
+ },
363
+ };
364
+ }
365
+ //# sourceMappingURL=mcp-auth-jwt.js.map