@kya-os/mcp-i-cloudflare 1.18.0 → 1.18.1

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 (256) hide show
  1. package/README.md +273 -54
  2. package/dist/adapter.d.ts +3 -17
  3. package/dist/adapter.d.ts.map +1 -1
  4. package/dist/adapter.js +30 -477
  5. package/dist/adapter.js.map +1 -1
  6. package/dist/agent.d.ts +59 -35
  7. package/dist/agent.d.ts.map +1 -1
  8. package/dist/agent.js +383 -406
  9. package/dist/agent.js.map +1 -1
  10. package/dist/app.d.ts.map +1 -1
  11. package/dist/app.js +150 -94
  12. package/dist/app.js.map +1 -1
  13. package/dist/audit/deployment-binding.d.ts +16 -16
  14. package/dist/authority-outcome.d.ts +110 -0
  15. package/dist/authority-outcome.d.ts.map +1 -0
  16. package/dist/authority-outcome.js +154 -0
  17. package/dist/authority-outcome.js.map +1 -0
  18. package/dist/config.d.ts +8 -77
  19. package/dist/config.d.ts.map +1 -1
  20. package/dist/config.js +0 -83
  21. package/dist/config.js.map +1 -1
  22. package/dist/delegation-http/bearer-retry.d.ts +84 -32
  23. package/dist/delegation-http/bearer-retry.d.ts.map +1 -1
  24. package/dist/delegation-http/bearer-retry.js +124 -139
  25. package/dist/delegation-http/bearer-retry.js.map +1 -1
  26. package/dist/delegation-http/connect-gate.d.ts +47 -0
  27. package/dist/delegation-http/connect-gate.d.ts.map +1 -0
  28. package/dist/delegation-http/connect-gate.js +104 -0
  29. package/dist/delegation-http/connect-gate.js.map +1 -0
  30. package/dist/delegation-http/constants.d.ts +4 -49
  31. package/dist/delegation-http/constants.d.ts.map +1 -1
  32. package/dist/delegation-http/constants.js +4 -58
  33. package/dist/delegation-http/constants.js.map +1 -1
  34. package/dist/delegation-http/issuer-binding.d.ts +26 -0
  35. package/dist/delegation-http/issuer-binding.d.ts.map +1 -0
  36. package/dist/delegation-http/issuer-binding.js +51 -0
  37. package/dist/delegation-http/issuer-binding.js.map +1 -0
  38. package/dist/delegation-http/lazy-auth-gate.d.ts +88 -0
  39. package/dist/delegation-http/lazy-auth-gate.d.ts.map +1 -0
  40. package/dist/delegation-http/lazy-auth-gate.js +203 -0
  41. package/dist/delegation-http/lazy-auth-gate.js.map +1 -0
  42. package/dist/delegation-http/native-challenge.d.ts +52 -0
  43. package/dist/delegation-http/native-challenge.d.ts.map +1 -0
  44. package/dist/delegation-http/native-challenge.js +124 -0
  45. package/dist/delegation-http/native-challenge.js.map +1 -0
  46. package/dist/delegation-http/native-consent-redirect.d.ts +21 -0
  47. package/dist/delegation-http/native-consent-redirect.d.ts.map +1 -0
  48. package/dist/delegation-http/native-consent-redirect.js +43 -0
  49. package/dist/delegation-http/native-consent-redirect.js.map +1 -0
  50. package/dist/delegation-http/native-http.d.ts +1 -86
  51. package/dist/delegation-http/native-http.d.ts.map +1 -1
  52. package/dist/delegation-http/native-http.js +2 -130
  53. package/dist/delegation-http/native-http.js.map +1 -1
  54. package/dist/delegation-http/native-oauth-state-do.d.ts +13 -35
  55. package/dist/delegation-http/native-oauth-state-do.d.ts.map +1 -1
  56. package/dist/delegation-http/native-oauth-state-do.js +10 -165
  57. package/dist/delegation-http/native-oauth-state-do.js.map +1 -1
  58. package/dist/delegation-http/protected-resource.d.ts +37 -0
  59. package/dist/delegation-http/protected-resource.d.ts.map +1 -0
  60. package/dist/delegation-http/protected-resource.js +43 -0
  61. package/dist/delegation-http/protected-resource.js.map +1 -0
  62. package/dist/delegation-http/request-proof.d.ts +20 -0
  63. package/dist/delegation-http/request-proof.d.ts.map +1 -0
  64. package/dist/delegation-http/request-proof.js +29 -0
  65. package/dist/delegation-http/request-proof.js.map +1 -0
  66. package/dist/delegation-http/sid.d.ts +4 -12
  67. package/dist/delegation-http/sid.d.ts.map +1 -1
  68. package/dist/delegation-http/sid.js +4 -19
  69. package/dist/delegation-http/sid.js.map +1 -1
  70. package/dist/delegation-http/well-known-capability.d.ts +8 -12
  71. package/dist/delegation-http/well-known-capability.d.ts.map +1 -1
  72. package/dist/delegation-http/well-known-capability.js +9 -18
  73. package/dist/delegation-http/well-known-capability.js.map +1 -1
  74. package/dist/helpers/env-mapper.d.ts.map +1 -1
  75. package/dist/helpers/env-mapper.js +7 -29
  76. package/dist/helpers/env-mapper.js.map +1 -1
  77. package/dist/index.d.ts +2 -4
  78. package/dist/index.d.ts.map +1 -1
  79. package/dist/index.js +11 -8
  80. package/dist/index.js.map +1 -1
  81. package/dist/proof-generator.d.ts +24 -17
  82. package/dist/proof-generator.d.ts.map +1 -1
  83. package/dist/proof-generator.js +42 -56
  84. package/dist/proof-generator.js.map +1 -1
  85. package/dist/runtime/oauth-handler.d.ts +1 -1
  86. package/dist/runtime/oauth-handler.d.ts.map +1 -1
  87. package/dist/runtime/oauth-handler.js +15 -30
  88. package/dist/runtime/oauth-handler.js.map +1 -1
  89. package/dist/runtime.d.ts +34 -10
  90. package/dist/runtime.d.ts.map +1 -1
  91. package/dist/runtime.js +54 -12
  92. package/dist/runtime.js.map +1 -1
  93. package/dist/security/admission-refusal.d.ts +3 -3
  94. package/dist/security/admission-refusal.d.ts.map +1 -1
  95. package/dist/security/admission-refusal.js +14 -6
  96. package/dist/security/admission-refusal.js.map +1 -1
  97. package/dist/security/holder-binding.d.ts +34 -0
  98. package/dist/security/holder-binding.d.ts.map +1 -0
  99. package/dist/security/holder-binding.js +75 -0
  100. package/dist/security/holder-binding.js.map +1 -0
  101. package/dist/security/mcp-admission.d.ts +29 -1
  102. package/dist/security/mcp-admission.d.ts.map +1 -1
  103. package/dist/security/mcp-admission.js +78 -12
  104. package/dist/security/mcp-admission.js.map +1 -1
  105. package/dist/security/native-holder-admission.d.ts +39 -0
  106. package/dist/security/native-holder-admission.d.ts.map +1 -0
  107. package/dist/security/native-holder-admission.js +65 -0
  108. package/dist/security/native-holder-admission.js.map +1 -0
  109. package/dist/security/outcome-proof.d.ts +39 -0
  110. package/dist/security/outcome-proof.d.ts.map +1 -0
  111. package/dist/security/outcome-proof.js +54 -0
  112. package/dist/security/outcome-proof.js.map +1 -0
  113. package/dist/security/request-nonce-store.d.ts +43 -0
  114. package/dist/security/request-nonce-store.d.ts.map +1 -0
  115. package/dist/security/request-nonce-store.js +129 -0
  116. package/dist/security/request-nonce-store.js.map +1 -0
  117. package/dist/security/routed-request.d.ts +41 -0
  118. package/dist/security/routed-request.d.ts.map +1 -0
  119. package/dist/security/routed-request.js +86 -0
  120. package/dist/security/routed-request.js.map +1 -0
  121. package/dist/security/sign-in-amr.d.ts +23 -0
  122. package/dist/security/sign-in-amr.d.ts.map +1 -0
  123. package/dist/security/sign-in-amr.js +44 -0
  124. package/dist/security/sign-in-amr.js.map +1 -0
  125. package/dist/services/approval-route.d.ts +3 -2
  126. package/dist/services/approval-route.d.ts.map +1 -1
  127. package/dist/services/approval-route.js.map +1 -1
  128. package/dist/services/checkpoint-decision.d.ts +56 -0
  129. package/dist/services/checkpoint-decision.d.ts.map +1 -0
  130. package/dist/services/checkpoint-decision.js +109 -0
  131. package/dist/services/checkpoint-decision.js.map +1 -0
  132. package/dist/services/consent-page-renderer.d.ts.map +1 -1
  133. package/dist/services/consent-page-renderer.js +2 -3
  134. package/dist/services/consent-page-renderer.js.map +1 -1
  135. package/dist/services/consent.service.d.ts +11 -49
  136. package/dist/services/consent.service.d.ts.map +1 -1
  137. package/dist/services/consent.service.js +81 -307
  138. package/dist/services/consent.service.js.map +1 -1
  139. package/dist/services/credential-auth.handler.d.ts.map +1 -1
  140. package/dist/services/credential-auth.handler.js +2 -8
  141. package/dist/services/credential-auth.handler.js.map +1 -1
  142. package/dist/services/delegation-authority.d.ts +53 -0
  143. package/dist/services/delegation-authority.d.ts.map +1 -0
  144. package/dist/services/delegation-authority.js +130 -0
  145. package/dist/services/delegation-authority.js.map +1 -0
  146. package/dist/services/delegation-trust.d.ts +35 -64
  147. package/dist/services/delegation-trust.d.ts.map +1 -1
  148. package/dist/services/delegation-trust.js +98 -279
  149. package/dist/services/delegation-trust.js.map +1 -1
  150. package/dist/services/delegation.service.d.ts +83 -105
  151. package/dist/services/delegation.service.d.ts.map +1 -1
  152. package/dist/services/delegation.service.js +166 -518
  153. package/dist/services/delegation.service.js.map +1 -1
  154. package/dist/services/idp-token-storage.d.ts +32 -12
  155. package/dist/services/idp-token-storage.d.ts.map +1 -1
  156. package/dist/services/idp-token-storage.js +79 -36
  157. package/dist/services/idp-token-storage.js.map +1 -1
  158. package/dist/services/issuer-keys.d.ts +55 -0
  159. package/dist/services/issuer-keys.d.ts.map +1 -0
  160. package/dist/services/issuer-keys.js +184 -0
  161. package/dist/services/issuer-keys.js.map +1 -0
  162. package/dist/services/oauth-security.service.d.ts +17 -1
  163. package/dist/services/oauth-security.service.d.ts.map +1 -1
  164. package/dist/services/oauth-security.service.js +29 -10
  165. package/dist/services/oauth-security.service.js.map +1 -1
  166. package/dist/services/resource-audience.d.ts +12 -9
  167. package/dist/services/resource-audience.d.ts.map +1 -1
  168. package/dist/services/resource-audience.js +12 -9
  169. package/dist/services/resource-audience.js.map +1 -1
  170. package/dist/services/unenforceable-tool-protection.d.ts +32 -0
  171. package/dist/services/unenforceable-tool-protection.d.ts.map +1 -0
  172. package/dist/services/unenforceable-tool-protection.js +90 -0
  173. package/dist/services/unenforceable-tool-protection.js.map +1 -0
  174. package/dist/services/upstream-fetch.d.ts +14 -0
  175. package/dist/services/upstream-fetch.d.ts.map +1 -0
  176. package/dist/services/upstream-fetch.js +56 -0
  177. package/dist/services/upstream-fetch.js.map +1 -0
  178. package/dist/types.d.ts +5 -18
  179. package/dist/types.d.ts.map +1 -1
  180. package/dist/utils/tool-security-schemes.d.ts +65 -0
  181. package/dist/utils/tool-security-schemes.d.ts.map +1 -0
  182. package/dist/utils/tool-security-schemes.js +168 -0
  183. package/dist/utils/tool-security-schemes.js.map +1 -0
  184. package/package.json +3 -4
  185. package/dist/delegation-http/anchor-do.d.ts +0 -84
  186. package/dist/delegation-http/anchor-do.d.ts.map +0 -1
  187. package/dist/delegation-http/anchor-do.js +0 -412
  188. package/dist/delegation-http/anchor-do.js.map +0 -1
  189. package/dist/delegation-http/anchor-registration-validate.d.ts +0 -61
  190. package/dist/delegation-http/anchor-registration-validate.d.ts.map +0 -1
  191. package/dist/delegation-http/anchor-registration-validate.js +0 -191
  192. package/dist/delegation-http/anchor-registration-validate.js.map +0 -1
  193. package/dist/delegation-http/anchor-registration.d.ts +0 -62
  194. package/dist/delegation-http/anchor-registration.d.ts.map +0 -1
  195. package/dist/delegation-http/anchor-registration.js +0 -337
  196. package/dist/delegation-http/anchor-registration.js.map +0 -1
  197. package/dist/delegation-http/anchor-routing.d.ts +0 -20
  198. package/dist/delegation-http/anchor-routing.d.ts.map +0 -1
  199. package/dist/delegation-http/anchor-routing.js +0 -27
  200. package/dist/delegation-http/anchor-routing.js.map +0 -1
  201. package/dist/delegation-http/challenge-middleware.d.ts +0 -28
  202. package/dist/delegation-http/challenge-middleware.d.ts.map +0 -1
  203. package/dist/delegation-http/challenge-middleware.js +0 -190
  204. package/dist/delegation-http/challenge-middleware.js.map +0 -1
  205. package/dist/delegation-http/challenge.d.ts +0 -102
  206. package/dist/delegation-http/challenge.d.ts.map +0 -1
  207. package/dist/delegation-http/challenge.js +0 -223
  208. package/dist/delegation-http/challenge.js.map +0 -1
  209. package/dist/delegation-http/consent-anchor-bridge.d.ts +0 -70
  210. package/dist/delegation-http/consent-anchor-bridge.d.ts.map +0 -1
  211. package/dist/delegation-http/consent-anchor-bridge.js +0 -1130
  212. package/dist/delegation-http/consent-anchor-bridge.js.map +0 -1
  213. package/dist/delegation-http/consent-preparation-page.d.ts +0 -18
  214. package/dist/delegation-http/consent-preparation-page.d.ts.map +0 -1
  215. package/dist/delegation-http/consent-preparation-page.js +0 -172
  216. package/dist/delegation-http/consent-preparation-page.js.map +0 -1
  217. package/dist/delegation-http/credential-consent.d.ts +0 -149
  218. package/dist/delegation-http/credential-consent.d.ts.map +0 -1
  219. package/dist/delegation-http/credential-consent.js +0 -338
  220. package/dist/delegation-http/credential-consent.js.map +0 -1
  221. package/dist/delegation-http/native-capabilities.d.ts +0 -4
  222. package/dist/delegation-http/native-capabilities.d.ts.map +0 -1
  223. package/dist/delegation-http/native-capabilities.js +0 -53
  224. package/dist/delegation-http/native-capabilities.js.map +0 -1
  225. package/dist/delegation-http/native-oauth-state.d.ts +0 -28
  226. package/dist/delegation-http/native-oauth-state.d.ts.map +0 -1
  227. package/dist/delegation-http/native-oauth-state.js +0 -146
  228. package/dist/delegation-http/native-oauth-state.js.map +0 -1
  229. package/dist/delegation-http/native-oauth.d.ts +0 -26
  230. package/dist/delegation-http/native-oauth.d.ts.map +0 -1
  231. package/dist/delegation-http/native-oauth.js +0 -401
  232. package/dist/delegation-http/native-oauth.js.map +0 -1
  233. package/dist/delegation-http/native-pickup.d.ts +0 -20
  234. package/dist/delegation-http/native-pickup.d.ts.map +0 -1
  235. package/dist/delegation-http/native-pickup.js +0 -227
  236. package/dist/delegation-http/native-pickup.js.map +0 -1
  237. package/dist/delegation-http/pickup-routes.d.ts +0 -43
  238. package/dist/delegation-http/pickup-routes.d.ts.map +0 -1
  239. package/dist/delegation-http/pickup-routes.js +0 -166
  240. package/dist/delegation-http/pickup-routes.js.map +0 -1
  241. package/dist/delegation-http/register.d.ts +0 -26
  242. package/dist/delegation-http/register.d.ts.map +0 -1
  243. package/dist/delegation-http/register.js +0 -95
  244. package/dist/delegation-http/register.js.map +0 -1
  245. package/dist/delegation-http/registrar-auth.d.ts +0 -24
  246. package/dist/delegation-http/registrar-auth.d.ts.map +0 -1
  247. package/dist/delegation-http/registrar-auth.js +0 -61
  248. package/dist/delegation-http/registrar-auth.js.map +0 -1
  249. package/dist/delegation-http/remote-issuer.d.ts +0 -52
  250. package/dist/delegation-http/remote-issuer.d.ts.map +0 -1
  251. package/dist/delegation-http/remote-issuer.js +0 -324
  252. package/dist/delegation-http/remote-issuer.js.map +0 -1
  253. package/dist/delegation-http/types.d.ts +0 -116
  254. package/dist/delegation-http/types.d.ts.map +0 -1
  255. package/dist/delegation-http/types.js +0 -11
  256. package/dist/delegation-http/types.js.map +0 -1
@@ -0,0 +1,104 @@
1
+ /**
2
+ * Connection auth at the edge: ordinary MCP Authorization.
3
+ *
4
+ * When a deployment turns on `requireTokenToConnect` (config) or
5
+ * `MCPI_REQUIRE_TOKEN_TO_CONNECT` (env), every MCP request needs a valid
6
+ * Bearer credential for this resource. Without one the edge answers the
7
+ * connect-time 401 that sends an OAuth client to the protected-resource
8
+ * metadata. The decision is made on the request headers alone, before the
9
+ * request reaches the agent.
10
+ *
11
+ * It is off unless the deployment turns it on; when off, nothing changes. The
12
+ * per-tool lazy auth gate (./lazy-auth-gate) is separate and unchanged.
13
+ *
14
+ * It applies to every caller, KYA-OS native ones included: requiring a token
15
+ * to connect is the deployment's choice. Per-tool refusals stay tool results.
16
+ *
17
+ * Verdicts:
18
+ * - none or malformed: 401;
19
+ * - expired or invalid (the token's own validity): 401 `invalid_token`;
20
+ * - valid: pass;
21
+ * - refer (revocation, status list, holder binding): pass. The edge cannot
22
+ * decide these; the Durable Object decides them per tool.
23
+ *
24
+ * A verification that cannot run fails closed with 503 and `Retry-After`:
25
+ * the deployment chose to require a token, so a request that could not be
26
+ * checked is not let through.
27
+ *
28
+ * @module @kya-os/mcp-i-cloudflare/delegation-http/connect-gate
29
+ */
30
+ import { InvalidDelegationPresentation } from "../security/presented-delegation";
31
+ import { assessPresentedCredential, extractDelegationVcJwt, } from "./bearer-retry";
32
+ import { lazyAuthResponse } from "./lazy-auth-gate";
33
+ /**
34
+ * Whether the deployment requires a token to connect: the config flag, or the
35
+ * env var set to "true" or "1". Either turns it on; neither turns it off.
36
+ */
37
+ export function requiresTokenToConnect(env, runtimeConfig) {
38
+ return (env.MCPI_REQUIRE_TOKEN_TO_CONNECT === "true" ||
39
+ env.MCPI_REQUIRE_TOKEN_TO_CONNECT === "1" ||
40
+ runtimeConfig.requireTokenToConnect === true);
41
+ }
42
+ /** The connect decision for a verdict. Pure. */
43
+ export function decideConnect(verdict) {
44
+ switch (verdict.kind) {
45
+ case "valid":
46
+ case "refer":
47
+ return { action: "pass" };
48
+ case "expired":
49
+ return {
50
+ action: "unauthorized",
51
+ error: "invalid_token",
52
+ description: "The access token expired",
53
+ scope: [],
54
+ };
55
+ case "invalid":
56
+ return {
57
+ action: "unauthorized",
58
+ error: "invalid_token",
59
+ description: "The access token is invalid",
60
+ scope: [],
61
+ };
62
+ case "none":
63
+ case "malformed":
64
+ return {
65
+ action: "unauthorized",
66
+ description: "Authorization required",
67
+ scope: [],
68
+ };
69
+ }
70
+ }
71
+ function verificationUnavailable() {
72
+ return new Response(JSON.stringify({
73
+ error: "temporarily_unavailable",
74
+ error_description: "The access token could not be verified",
75
+ }), {
76
+ status: 503,
77
+ headers: {
78
+ "Content-Type": "application/json",
79
+ "Cache-Control": "no-store",
80
+ "Retry-After": "3",
81
+ },
82
+ });
83
+ }
84
+ /**
85
+ * Decide a request at the edge. Returns the 401 (or 503) response, or null
86
+ * when the request goes on to the agent. The credential comes from the
87
+ * request headers only.
88
+ */
89
+ export async function applyConnectGate(request, env) {
90
+ let verdict;
91
+ try {
92
+ const token = extractDelegationVcJwt(request);
93
+ verdict = token
94
+ ? await assessPresentedCredential(token, request, env)
95
+ : { kind: "none" };
96
+ }
97
+ catch (error) {
98
+ if (!(error instanceof InvalidDelegationPresentation))
99
+ return verificationUnavailable();
100
+ verdict = { kind: "malformed" };
101
+ }
102
+ return lazyAuthResponse(decideConnect(verdict), new URL(request.url).host);
103
+ }
104
+ //# sourceMappingURL=connect-gate.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"connect-gate.js","sourceRoot":"","sources":["../../src/delegation-http/connect-gate.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AAGH,OAAO,EAAE,6BAA6B,EAAE,MAAM,kCAAkC,CAAC;AAEjF,OAAO,EACL,yBAAyB,EAEzB,sBAAsB,GACvB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAuB,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AAEzE;;;GAGG;AACH,MAAM,UAAU,sBAAsB,CACpC,GAAyD,EACzD,aAAqE;IAErE,OAAO,CACL,GAAG,CAAC,6BAA6B,KAAK,MAAM;QAC5C,GAAG,CAAC,6BAA6B,KAAK,GAAG;QACzC,aAAa,CAAC,qBAAqB,KAAK,IAAI,CAC7C,CAAC;AACJ,CAAC;AAED,gDAAgD;AAChD,MAAM,UAAU,aAAa,CAAC,OAAsB;IAClD,QAAQ,OAAO,CAAC,IAAI,EAAE,CAAC;QACrB,KAAK,OAAO,CAAC;QACb,KAAK,OAAO;YACV,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC;QAC5B,KAAK,SAAS;YACZ,OAAO;gBACL,MAAM,EAAE,cAAc;gBACtB,KAAK,EAAE,eAAe;gBACtB,WAAW,EAAE,0BAA0B;gBACvC,KAAK,EAAE,EAAE;aACV,CAAC;QACJ,KAAK,SAAS;YACZ,OAAO;gBACL,MAAM,EAAE,cAAc;gBACtB,KAAK,EAAE,eAAe;gBACtB,WAAW,EAAE,6BAA6B;gBAC1C,KAAK,EAAE,EAAE;aACV,CAAC;QACJ,KAAK,MAAM,CAAC;QACZ,KAAK,WAAW;YACd,OAAO;gBACL,MAAM,EAAE,cAAc;gBACtB,WAAW,EAAE,wBAAwB;gBACrC,KAAK,EAAE,EAAE;aACV,CAAC;IACN,CAAC;AACH,CAAC;AAED,SAAS,uBAAuB;IAC9B,OAAO,IAAI,QAAQ,CACjB,IAAI,CAAC,SAAS,CAAC;QACb,KAAK,EAAE,yBAAyB;QAChC,iBAAiB,EAAE,wCAAwC;KAC5D,CAAC,EACF;QACE,MAAM,EAAE,GAAG;QACX,OAAO,EAAE;YACP,cAAc,EAAE,kBAAkB;YAClC,eAAe,EAAE,UAAU;YAC3B,aAAa,EAAE,GAAG;SACnB;KACF,CACF,CAAC;AACJ,CAAC;AAED;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,gBAAgB,CACpC,OAAgB,EAChB,GAAkB;IAElB,IAAI,OAAsB,CAAC;IAC3B,IAAI,CAAC;QACH,MAAM,KAAK,GAAG,sBAAsB,CAAC,OAAO,CAAC,CAAC;QAC9C,OAAO,GAAG,KAAK;YACb,CAAC,CAAC,MAAM,yBAAyB,CAAC,KAAK,EAAE,OAAO,EAAE,GAAG,CAAC;YACtD,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC;IACvB,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,CAAC,CAAC,KAAK,YAAY,6BAA6B,CAAC;YACnD,OAAO,uBAAuB,EAAE,CAAC;QACnC,OAAO,GAAG,EAAE,IAAI,EAAE,WAAW,EAAE,CAAC;IAClC,CAAC;IACD,OAAO,gBAAgB,CAAC,aAAa,CAAC,OAAO,CAAC,EAAE,IAAI,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,CAAC;AAC7E,CAAC"}
@@ -1,61 +1,16 @@
1
1
  /**
2
- * draft-kya-http-02 delegation layer — frozen constants.
3
- *
4
- * Wire contract source of truth: the vendored golden fixtures under
5
- * `src/__tests__/fixtures/kya-http/`. These values are byte-significant for §8.1
6
- * challenges, §8.2 worker-served pickup, and the §9 error registry. Do not edit
7
- * without re-vendoring the fixtures from checkpoint.
2
+ * Delegation-http constants.
8
3
  *
9
4
  * @module @kya-os/mcp-i-cloudflare/delegation-http/constants
10
5
  */
11
- /** Worker-served pickup endpoint (§8.2). Hono route pattern with a `:sid` param. */
12
- export declare const PICKUP_ROUTE = "/api/delegation/pickup/:sid";
13
6
  /**
14
- * Consent surface. The legacy consent service owns this path; the kya-http bridge
15
- * only intercepts requests carrying an anchor sid (`?sid=kya-…`) and otherwise
16
- * falls through with `next()`.
7
+ * Consent surface. The legacy consent service owns this path; a native consent
8
+ * link (`?sid=…`) on a native-rail deployment is redirected to the org
9
+ * authorization service, and everything else falls through with `next()`.
17
10
  */
18
11
  export declare const CONSENT_ROUTE = "/consent";
19
- /**
20
- * Anchor-registration endpoint — the SAME path and wire contract as checkpoint's
21
- * bouncer sessions route, so an external PEP that already registers challenge
22
- * anchors against checkpoint (gateway `registration-client.ts`, .NET
23
- * `DelegationAnchorRegistrar`) can be pointed at this worker unmodified. Shape is
24
- * frozen by the vendored `registration.request.json` / `registration.response.json`.
25
- */
26
- export declare const ANCHOR_REGISTRATION_ROUTE = "/api/v1/bouncer/sessions";
27
- /** Raw pickup-token header for token-path holders (hashed before storage). */
28
- export declare const PICKUP_TOKEN_HEADER = "X-KYA-Pickup-Token";
29
12
  /** Fallback delegation header used when `Authorization` is occupied by an OAuth bearer. */
30
13
  export declare const FALLBACK_DELEGATION_HEADER = "KYA-Delegation";
31
- /**
32
- * Param order for the `WWW-Authenticate: Delegation` challenge (§8.1), byte-exact.
33
- * Mirrors `challenge.delegation.full.json` → `conformance.param_order`.
34
- */
35
- export declare const CHALLENGE_PARAM_ORDER: readonly ["realm", "sid", "consent_uri", "pickup_uri", "scope", "min_assurance", "interval", "kind"];
36
- /**
37
- * The `kyaos/*` error registry (§9). Every value MUST be present in the vendored
38
- * `error-codes.json` (asserted by the contract test). Emitters MUST use these exact
39
- * strings.
40
- */
41
- export declare const KYAOS_ERRORS: {
42
- readonly authorizationRequired: "kyaos/authorization-required";
43
- readonly pickupPinned: "kyaos/pickup-pinned";
44
- readonly invalidSession: "kyaos/invalid-session";
45
- readonly pickupExpired: "kyaos/pickup-expired";
46
- readonly expired: "kyaos/expired";
47
- readonly chainBroken: "kyaos/chain-broken";
48
- readonly scopeInsufficient: "kyaos/scope-insufficient";
49
- };
50
- /**
51
- * Internal DO route paths for the pickup-anchor durable object. Dispatched at the
52
- * top of `MCPICloudflareAgent.fetch` (see `agent.ts`) and proxied to via
53
- * `getAnchorStub(env, sid)`.
54
- */
55
- export declare const INTERNAL_PICKUP_CREATE = "/_internal/pickup/create";
56
- export declare const INTERNAL_PICKUP_POLL = "/_internal/pickup/poll";
57
- export declare const INTERNAL_PICKUP_APPROVE = "/_internal/pickup/approve";
58
- export declare const INTERNAL_PICKUP_INFO = "/_internal/pickup/info";
59
14
  /** Native OAuth errors must stay outside legacy callback logging paths. */
60
15
  export declare const NATIVE_OAUTH_STATE_PREFIX = "kya-http-oauth.";
61
16
  //# sourceMappingURL=constants.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../src/delegation-http/constants.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,oFAAoF;AACpF,eAAO,MAAM,YAAY,gCAAgC,CAAC;AAE1D;;;;GAIG;AACH,eAAO,MAAM,aAAa,aAAa,CAAC;AAExC;;;;;;GAMG;AACH,eAAO,MAAM,yBAAyB,6BAA6B,CAAC;AAEpE,8EAA8E;AAC9E,eAAO,MAAM,mBAAmB,uBAAuB,CAAC;AAExD,2FAA2F;AAC3F,eAAO,MAAM,0BAA0B,mBAAmB,CAAC;AAE3D;;;GAGG;AACH,eAAO,MAAM,qBAAqB,sGASxB,CAAC;AAEX;;;;GAIG;AACH,eAAO,MAAM,YAAY;;;;;;;;CAQf,CAAC;AAEX;;;;GAIG;AACH,eAAO,MAAM,sBAAsB,6BAA6B,CAAC;AACjE,eAAO,MAAM,oBAAoB,2BAA2B,CAAC;AAC7D,eAAO,MAAM,uBAAuB,8BAA8B,CAAC;AACnE,eAAO,MAAM,oBAAoB,2BAA2B,CAAC;AAE7D,2EAA2E;AAC3E,eAAO,MAAM,yBAAyB,oBAAoB,CAAC"}
1
+ {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../src/delegation-http/constants.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH;;;;GAIG;AACH,eAAO,MAAM,aAAa,aAAa,CAAC;AAExC,2FAA2F;AAC3F,eAAO,MAAM,0BAA0B,mBAAmB,CAAC;AAE3D,2EAA2E;AAC3E,eAAO,MAAM,yBAAyB,oBAAoB,CAAC"}
@@ -1,70 +1,16 @@
1
1
  /**
2
- * draft-kya-http-02 delegation layer — frozen constants.
3
- *
4
- * Wire contract source of truth: the vendored golden fixtures under
5
- * `src/__tests__/fixtures/kya-http/`. These values are byte-significant for §8.1
6
- * challenges, §8.2 worker-served pickup, and the §9 error registry. Do not edit
7
- * without re-vendoring the fixtures from checkpoint.
2
+ * Delegation-http constants.
8
3
  *
9
4
  * @module @kya-os/mcp-i-cloudflare/delegation-http/constants
10
5
  */
11
- /** Worker-served pickup endpoint (§8.2). Hono route pattern with a `:sid` param. */
12
- export const PICKUP_ROUTE = "/api/delegation/pickup/:sid";
13
6
  /**
14
- * Consent surface. The legacy consent service owns this path; the kya-http bridge
15
- * only intercepts requests carrying an anchor sid (`?sid=kya-…`) and otherwise
16
- * falls through with `next()`.
7
+ * Consent surface. The legacy consent service owns this path; a native consent
8
+ * link (`?sid=…`) on a native-rail deployment is redirected to the org
9
+ * authorization service, and everything else falls through with `next()`.
17
10
  */
18
11
  export const CONSENT_ROUTE = "/consent";
19
- /**
20
- * Anchor-registration endpoint — the SAME path and wire contract as checkpoint's
21
- * bouncer sessions route, so an external PEP that already registers challenge
22
- * anchors against checkpoint (gateway `registration-client.ts`, .NET
23
- * `DelegationAnchorRegistrar`) can be pointed at this worker unmodified. Shape is
24
- * frozen by the vendored `registration.request.json` / `registration.response.json`.
25
- */
26
- export const ANCHOR_REGISTRATION_ROUTE = "/api/v1/bouncer/sessions";
27
- /** Raw pickup-token header for token-path holders (hashed before storage). */
28
- export const PICKUP_TOKEN_HEADER = "X-KYA-Pickup-Token";
29
12
  /** Fallback delegation header used when `Authorization` is occupied by an OAuth bearer. */
30
13
  export const FALLBACK_DELEGATION_HEADER = "KYA-Delegation";
31
- /**
32
- * Param order for the `WWW-Authenticate: Delegation` challenge (§8.1), byte-exact.
33
- * Mirrors `challenge.delegation.full.json` → `conformance.param_order`.
34
- */
35
- export const CHALLENGE_PARAM_ORDER = [
36
- "realm",
37
- "sid",
38
- "consent_uri",
39
- "pickup_uri",
40
- "scope",
41
- "min_assurance",
42
- "interval",
43
- "kind",
44
- ];
45
- /**
46
- * The `kyaos/*` error registry (§9). Every value MUST be present in the vendored
47
- * `error-codes.json` (asserted by the contract test). Emitters MUST use these exact
48
- * strings.
49
- */
50
- export const KYAOS_ERRORS = {
51
- authorizationRequired: "kyaos/authorization-required",
52
- pickupPinned: "kyaos/pickup-pinned",
53
- invalidSession: "kyaos/invalid-session",
54
- pickupExpired: "kyaos/pickup-expired",
55
- expired: "kyaos/expired",
56
- chainBroken: "kyaos/chain-broken",
57
- scopeInsufficient: "kyaos/scope-insufficient",
58
- };
59
- /**
60
- * Internal DO route paths for the pickup-anchor durable object. Dispatched at the
61
- * top of `MCPICloudflareAgent.fetch` (see `agent.ts`) and proxied to via
62
- * `getAnchorStub(env, sid)`.
63
- */
64
- export const INTERNAL_PICKUP_CREATE = "/_internal/pickup/create";
65
- export const INTERNAL_PICKUP_POLL = "/_internal/pickup/poll";
66
- export const INTERNAL_PICKUP_APPROVE = "/_internal/pickup/approve";
67
- export const INTERNAL_PICKUP_INFO = "/_internal/pickup/info";
68
14
  /** Native OAuth errors must stay outside legacy callback logging paths. */
69
15
  export const NATIVE_OAUTH_STATE_PREFIX = "kya-http-oauth.";
70
16
  //# sourceMappingURL=constants.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"constants.js","sourceRoot":"","sources":["../../src/delegation-http/constants.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,oFAAoF;AACpF,MAAM,CAAC,MAAM,YAAY,GAAG,6BAA6B,CAAC;AAE1D;;;;GAIG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,UAAU,CAAC;AAExC;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,yBAAyB,GAAG,0BAA0B,CAAC;AAEpE,8EAA8E;AAC9E,MAAM,CAAC,MAAM,mBAAmB,GAAG,oBAAoB,CAAC;AAExD,2FAA2F;AAC3F,MAAM,CAAC,MAAM,0BAA0B,GAAG,gBAAgB,CAAC;AAE3D;;;GAGG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAG;IACnC,OAAO;IACP,KAAK;IACL,aAAa;IACb,YAAY;IACZ,OAAO;IACP,eAAe;IACf,UAAU;IACV,MAAM;CACE,CAAC;AAEX;;;;GAIG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG;IAC1B,qBAAqB,EAAE,8BAA8B;IACrD,YAAY,EAAE,qBAAqB;IACnC,cAAc,EAAE,uBAAuB;IACvC,aAAa,EAAE,sBAAsB;IACrC,OAAO,EAAE,eAAe;IACxB,WAAW,EAAE,oBAAoB;IACjC,iBAAiB,EAAE,0BAA0B;CACrC,CAAC;AAEX;;;;GAIG;AACH,MAAM,CAAC,MAAM,sBAAsB,GAAG,0BAA0B,CAAC;AACjE,MAAM,CAAC,MAAM,oBAAoB,GAAG,wBAAwB,CAAC;AAC7D,MAAM,CAAC,MAAM,uBAAuB,GAAG,2BAA2B,CAAC;AACnE,MAAM,CAAC,MAAM,oBAAoB,GAAG,wBAAwB,CAAC;AAE7D,2EAA2E;AAC3E,MAAM,CAAC,MAAM,yBAAyB,GAAG,iBAAiB,CAAC"}
1
+ {"version":3,"file":"constants.js","sourceRoot":"","sources":["../../src/delegation-http/constants.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH;;;;GAIG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,UAAU,CAAC;AAExC,2FAA2F;AAC3F,MAAM,CAAC,MAAM,0BAA0B,GAAG,gBAAgB,CAAC;AAE3D,2EAA2E;AAC3E,MAAM,CAAC,MAAM,yBAAyB,GAAG,iBAAiB,CAAC"}
@@ -0,0 +1,26 @@
1
+ /**
2
+ * The native issuer binding Checkpoint provisions for a deployment
3
+ * (`MCPI_NATIVE_HTTP_ISSUER_CONFIG`). It names the org that owns the resource
4
+ * (the one trusted issuer), the project, and the org's authorization and
5
+ * control-plane origins, and carries no private key. A deployment with a
6
+ * valid binding is on the native rail.
7
+ *
8
+ * @module @kya-os/mcp-i-cloudflare/delegation-http/issuer-binding
9
+ */
10
+ export interface RuntimeBinding {
11
+ version: 1;
12
+ projectId: string;
13
+ /** The org that owns this resource: the one trusted issuer. */
14
+ issuerDid: string;
15
+ /**
16
+ * Provisioned by Checkpoint and shape-checked, never used to verify: org
17
+ * keys rotate, so signatures are checked against the key the `kid` names in
18
+ * the org's DID document.
19
+ */
20
+ issuerPublicKey: string;
21
+ authorizationOrigin: string;
22
+ controlPlaneOrigin: string;
23
+ }
24
+ /** Parse and shape-check the binding, or null when it is absent or invalid. */
25
+ export declare function parseNativeIssuerBinding(raw: unknown): RuntimeBinding | null;
26
+ //# sourceMappingURL=issuer-binding.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"issuer-binding.d.ts","sourceRoot":"","sources":["../../src/delegation-http/issuer-binding.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,MAAM,WAAW,cAAc;IAC7B,OAAO,EAAE,CAAC,CAAC;IACX,SAAS,EAAE,MAAM,CAAC;IAClB,+DAA+D;IAC/D,SAAS,EAAE,MAAM,CAAC;IAClB;;;;OAIG;IACH,eAAe,EAAE,MAAM,CAAC;IACxB,mBAAmB,EAAE,MAAM,CAAC;IAC5B,kBAAkB,EAAE,MAAM,CAAC;CAC5B;AAwBD,+EAA+E;AAC/E,wBAAgB,wBAAwB,CAAC,GAAG,EAAE,OAAO,GAAG,cAAc,GAAG,IAAI,CAqB5E"}
@@ -0,0 +1,51 @@
1
+ /**
2
+ * The native issuer binding Checkpoint provisions for a deployment
3
+ * (`MCPI_NATIVE_HTTP_ISSUER_CONFIG`). It names the org that owns the resource
4
+ * (the one trusted issuer), the project, and the org's authorization and
5
+ * control-plane origins, and carries no private key. A deployment with a
6
+ * valid binding is on the native rail.
7
+ *
8
+ * @module @kya-os/mcp-i-cloudflare/delegation-http/issuer-binding
9
+ */
10
+ function decodedLength(encoded) {
11
+ if (typeof encoded !== "string" || !/^[A-Za-z0-9_-]+$/.test(encoded))
12
+ return -1;
13
+ return atob(encoded.replace(/-/g, "+").replace(/_/g, "/")).length;
14
+ }
15
+ function isOrigin(value) {
16
+ if (typeof value !== "string")
17
+ return false;
18
+ try {
19
+ const url = new URL(value);
20
+ return (url.protocol === "https:" &&
21
+ !url.port &&
22
+ !url.username &&
23
+ !url.password &&
24
+ url.origin === value);
25
+ }
26
+ catch {
27
+ return false;
28
+ }
29
+ }
30
+ /** Parse and shape-check the binding, or null when it is absent or invalid. */
31
+ export function parseNativeIssuerBinding(raw) {
32
+ if (typeof raw !== "string")
33
+ return null;
34
+ try {
35
+ const binding = JSON.parse(raw);
36
+ if (!binding ||
37
+ Object.keys(binding).length !== 6 ||
38
+ binding.version !== 1 ||
39
+ !/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(binding.projectId) ||
40
+ !/^did:web:[^/?#]+:orgs:[a-z0-9][a-z0-9-]*$/.test(binding.issuerDid) ||
41
+ decodedLength(binding.issuerPublicKey) !== 32 ||
42
+ !isOrigin(binding.authorizationOrigin) ||
43
+ !isOrigin(binding.controlPlaneOrigin))
44
+ return null;
45
+ return binding;
46
+ }
47
+ catch {
48
+ return null;
49
+ }
50
+ }
51
+ //# sourceMappingURL=issuer-binding.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"issuer-binding.js","sourceRoot":"","sources":["../../src/delegation-http/issuer-binding.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAiBH,SAAS,aAAa,CAAC,OAAgB;IACrC,IAAI,OAAO,OAAO,KAAK,QAAQ,IAAI,CAAC,kBAAkB,CAAC,IAAI,CAAC,OAAO,CAAC;QAClE,OAAO,CAAC,CAAC,CAAC;IACZ,OAAO,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC,CAAC,MAAM,CAAC;AACpE,CAAC;AAED,SAAS,QAAQ,CAAC,KAAc;IAC9B,IAAI,OAAO,KAAK,KAAK,QAAQ;QAAE,OAAO,KAAK,CAAC;IAC5C,IAAI,CAAC;QACH,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,KAAK,CAAC,CAAC;QAC3B,OAAO,CACL,GAAG,CAAC,QAAQ,KAAK,QAAQ;YACzB,CAAC,GAAG,CAAC,IAAI;YACT,CAAC,GAAG,CAAC,QAAQ;YACb,CAAC,GAAG,CAAC,QAAQ;YACb,GAAG,CAAC,MAAM,KAAK,KAAK,CACrB,CAAC;IACJ,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,KAAK,CAAC;IACf,CAAC;AACH,CAAC;AAED,+EAA+E;AAC/E,MAAM,UAAU,wBAAwB,CAAC,GAAY;IACnD,IAAI,OAAO,GAAG,KAAK,QAAQ;QAAE,OAAO,IAAI,CAAC;IACzC,IAAI,CAAC;QACH,MAAM,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAmB,CAAC;QAClD,IACE,CAAC,OAAO;YACR,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,MAAM,KAAK,CAAC;YACjC,OAAO,CAAC,OAAO,KAAK,CAAC;YACrB,CAAC,iEAAiE,CAAC,IAAI,CACrE,OAAO,CAAC,SAAS,CAClB;YACD,CAAC,2CAA2C,CAAC,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC;YACpE,aAAa,CAAC,OAAO,CAAC,eAAe,CAAC,KAAK,EAAE;YAC7C,CAAC,QAAQ,CAAC,OAAO,CAAC,mBAAmB,CAAC;YACtC,CAAC,QAAQ,CAAC,OAAO,CAAC,kBAAkB,CAAC;YAErC,OAAO,IAAI,CAAC;QACd,OAAO,OAAO,CAAC;IACjB,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,IAAI,CAAC;IACd,CAAC;AACH,CAAC"}
@@ -0,0 +1,88 @@
1
+ /**
2
+ * Lazy auth at the edge: the HTTP 401/403 an OAuth client waits for.
3
+ *
4
+ * MCP clients that authorize over OAuth (Claude, the MCP TypeScript SDK) start
5
+ * the flow only when a request is answered 401 with a `WWW-Authenticate`
6
+ * challenge. This gate makes that decision on the PARSED REQUEST, before it
7
+ * reaches the agent. It never inspects a response.
8
+ *
9
+ * A call is gated only when all of these hold:
10
+ * - it is `tools/call` (initialize, tools/list and the rest pass);
11
+ * - it carries no KYA-OS request proof (./request-proof): a native caller
12
+ * never gets an HTTP status, its refusals are tool results;
13
+ * - the tool's served protection has `requiresDelegation: true`. Protection
14
+ * that cannot be resolved passes the call on, so the Durable Object, which
15
+ * is the authority, decides;
16
+ * - that protection names a required scope. One that names none can't be
17
+ * enforced: no credential satisfies it, so the call goes on unchallenged
18
+ * and the Durable Object refuses it (`tool_protection_unenforceable`).
19
+ *
20
+ * For a gated call the verdict on the presented credential decides:
21
+ * - none or malformed: 401;
22
+ * - expired or invalid (the token's own validity): 401 `invalid_token`;
23
+ * - refer (revocation, status list, holder binding, delegation chain): pass;
24
+ * - valid but missing a required scope: 403 `insufficient_scope`;
25
+ * - valid otherwise, holder-bound or not: pass.
26
+ *
27
+ * The credential is the one the pass-through would forward: the headers
28
+ * (`Authorization: Bearer`, `KYA-Delegation`), else `params.delegationToken`.
29
+ *
30
+ * @module @kya-os/mcp-i-cloudflare/delegation-http/lazy-auth-gate
31
+ */
32
+ import { type CardProofSupportGetter } from "../security/card-proof-support";
33
+ import type { CloudflareEnv } from "../types";
34
+ import { type BearerVerdict } from "./bearer-retry";
35
+ /** The part of a tool's served protection the gate reads. */
36
+ export interface GateProtection {
37
+ readonly requiredScopes: readonly string[];
38
+ }
39
+ /** What the gate does with a call. */
40
+ export type LazyAuthAction = {
41
+ action: "pass";
42
+ } | {
43
+ action: "unauthorized";
44
+ /** Set when a credential was presented and refused. */
45
+ error?: "invalid_token";
46
+ description: string;
47
+ scope: string[];
48
+ } | {
49
+ action: "forbidden";
50
+ description: string;
51
+ scope: string[];
52
+ };
53
+ export interface LazyAuthInput {
54
+ /** The JSON-RPC method of the call. */
55
+ method: string | undefined;
56
+ /** Whether the call carries a KYA-OS request proof. */
57
+ hasRequestProof: boolean;
58
+ /** The tool's served protection when it requires delegation; else null. */
59
+ protection: GateProtection | null;
60
+ /** The verdict on the presented credential. */
61
+ verdict: BearerVerdict;
62
+ }
63
+ /** Scopes as RFC 6749 §3.3 tokens: anything else is dropped, repeats too. */
64
+ export declare function scopeTokens(...lists: ReadonlyArray<readonly string[]>): string[];
65
+ /**
66
+ * The calls that can be answered with an HTTP status at all: a `tools/call`
67
+ * without a request proof. Evaluated before anything is fetched.
68
+ */
69
+ export declare function isLazyAuthCandidate(method: string | undefined, hasRequestProof: boolean): boolean;
70
+ /** The gate's decision. Pure: every input is injected. */
71
+ export declare function decideLazyAuth(input: LazyAuthInput): LazyAuthAction;
72
+ /** The 401 or 403 response for a decision; null for a pass. */
73
+ export declare function lazyAuthResponse(decision: LazyAuthAction, routedHost: string): Response | null;
74
+ export interface LazyAuthGateDeps {
75
+ env: CloudflareEnv;
76
+ /**
77
+ * The served protection of `toolName` when it requires delegation, or null
78
+ * when it does not or cannot be resolved (the call then passes on).
79
+ */
80
+ resolveProtection: (toolName: string) => Promise<GateProtection | null>;
81
+ getProofSupport?: CardProofSupportGetter;
82
+ }
83
+ /**
84
+ * Decide a parsed POST at the edge. Returns the 401/403 response, or null when
85
+ * the request goes on to the agent.
86
+ */
87
+ export declare function applyLazyAuthGate(request: Request, deps: LazyAuthGateDeps): Promise<Response | null>;
88
+ //# sourceMappingURL=lazy-auth-gate.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lazy-auth-gate.d.ts","sourceRoot":"","sources":["../../src/delegation-http/lazy-auth-gate.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AAGH,OAAO,EACL,KAAK,sBAAsB,EAE5B,MAAM,gCAAgC,CAAC;AAExC,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAC9C,OAAO,EAEL,KAAK,aAAa,EAEnB,MAAM,gBAAgB,CAAC;AAGxB,6DAA6D;AAC7D,MAAM,WAAW,cAAc;IAC7B,QAAQ,CAAC,cAAc,EAAE,SAAS,MAAM,EAAE,CAAC;CAC5C;AAED,sCAAsC;AACtC,MAAM,MAAM,cAAc,GACtB;IAAE,MAAM,EAAE,MAAM,CAAA;CAAE,GAClB;IACE,MAAM,EAAE,cAAc,CAAC;IACvB,uDAAuD;IACvD,KAAK,CAAC,EAAE,eAAe,CAAC;IACxB,WAAW,EAAE,MAAM,CAAC;IACpB,KAAK,EAAE,MAAM,EAAE,CAAC;CACjB,GACD;IAAE,MAAM,EAAE,WAAW,CAAC;IAAC,WAAW,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,EAAE,CAAA;CAAE,CAAC;AAElE,MAAM,WAAW,aAAa;IAC5B,uCAAuC;IACvC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC;IAC3B,uDAAuD;IACvD,eAAe,EAAE,OAAO,CAAC;IACzB,2EAA2E;IAC3E,UAAU,EAAE,cAAc,GAAG,IAAI,CAAC;IAClC,+CAA+C;IAC/C,OAAO,EAAE,aAAa,CAAC;CACxB;AAOD,6EAA6E;AAC7E,wBAAgB,WAAW,CACzB,GAAG,KAAK,EAAE,aAAa,CAAC,SAAS,MAAM,EAAE,CAAC,GACzC,MAAM,EAAE,CAEV;AAED;;;GAGG;AACH,wBAAgB,mBAAmB,CACjC,MAAM,EAAE,MAAM,GAAG,SAAS,EAC1B,eAAe,EAAE,OAAO,GACvB,OAAO,CAET;AAED,0DAA0D;AAC1D,wBAAgB,cAAc,CAAC,KAAK,EAAE,aAAa,GAAG,cAAc,CA0CnE;AAED,+DAA+D;AAC/D,wBAAgB,gBAAgB,CAC9B,QAAQ,EAAE,cAAc,EACxB,UAAU,EAAE,MAAM,GACjB,QAAQ,GAAG,IAAI,CAyBjB;AAED,MAAM,WAAW,gBAAgB;IAC/B,GAAG,EAAE,aAAa,CAAC;IACnB;;;OAGG;IACH,iBAAiB,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,OAAO,CAAC,cAAc,GAAG,IAAI,CAAC,CAAC;IACxE,eAAe,CAAC,EAAE,sBAAsB,CAAC;CAC1C;AA8DD;;;GAGG;AACH,wBAAsB,iBAAiB,CACrC,OAAO,EAAE,OAAO,EAChB,IAAI,EAAE,gBAAgB,GACrB,OAAO,CAAC,QAAQ,GAAG,IAAI,CAAC,CAiC1B"}
@@ -0,0 +1,203 @@
1
+ /**
2
+ * Lazy auth at the edge: the HTTP 401/403 an OAuth client waits for.
3
+ *
4
+ * MCP clients that authorize over OAuth (Claude, the MCP TypeScript SDK) start
5
+ * the flow only when a request is answered 401 with a `WWW-Authenticate`
6
+ * challenge. This gate makes that decision on the PARSED REQUEST, before it
7
+ * reaches the agent. It never inspects a response.
8
+ *
9
+ * A call is gated only when all of these hold:
10
+ * - it is `tools/call` (initialize, tools/list and the rest pass);
11
+ * - it carries no KYA-OS request proof (./request-proof): a native caller
12
+ * never gets an HTTP status, its refusals are tool results;
13
+ * - the tool's served protection has `requiresDelegation: true`. Protection
14
+ * that cannot be resolved passes the call on, so the Durable Object, which
15
+ * is the authority, decides;
16
+ * - that protection names a required scope. One that names none can't be
17
+ * enforced: no credential satisfies it, so the call goes on unchallenged
18
+ * and the Durable Object refuses it (`tool_protection_unenforceable`).
19
+ *
20
+ * For a gated call the verdict on the presented credential decides:
21
+ * - none or malformed: 401;
22
+ * - expired or invalid (the token's own validity): 401 `invalid_token`;
23
+ * - refer (revocation, status list, holder binding, delegation chain): pass;
24
+ * - valid but missing a required scope: 403 `insufficient_scope`;
25
+ * - valid otherwise, holder-bound or not: pass.
26
+ *
27
+ * The credential is the one the pass-through would forward: the headers
28
+ * (`Authorization: Bearer`, `KYA-Delegation`), else `params.delegationToken`.
29
+ *
30
+ * @module @kya-os/mcp-i-cloudflare/delegation-http/lazy-auth-gate
31
+ */
32
+ import { namesRequiredScope } from "@kya-os/mcp-i-runtime";
33
+ import { getCardProofSupport, } from "../security/card-proof-support";
34
+ import { InvalidDelegationPresentation } from "../security/presented-delegation";
35
+ import { assessPresentedCredential, extractDelegationVcJwt, } from "./bearer-retry";
36
+ import { carriesRequestProof } from "./request-proof";
37
+ const PASS = { action: "pass" };
38
+ /** RFC 6749 §3.3 scope-token: printable ASCII except space, `"` and `\`. */
39
+ const SCOPE_TOKEN = /^[\x21\x23-\x5B\x5D-\x7E]+$/;
40
+ /** Scopes as RFC 6749 §3.3 tokens: anything else is dropped, repeats too. */
41
+ export function scopeTokens(...lists) {
42
+ return [...new Set(lists.flat().filter((scope) => SCOPE_TOKEN.test(scope)))];
43
+ }
44
+ /**
45
+ * The calls that can be answered with an HTTP status at all: a `tools/call`
46
+ * without a request proof. Evaluated before anything is fetched.
47
+ */
48
+ export function isLazyAuthCandidate(method, hasRequestProof) {
49
+ return method === "tools/call" && !hasRequestProof;
50
+ }
51
+ /** The gate's decision. Pure: every input is injected. */
52
+ export function decideLazyAuth(input) {
53
+ const { method, hasRequestProof, protection, verdict } = input;
54
+ if (!isLazyAuthCandidate(method, hasRequestProof) || !protection)
55
+ return PASS;
56
+ // Nothing to check a credential against, so never "satisfied" and never
57
+ // challenged: the Durable Object refuses the call.
58
+ if (!namesRequiredScope(protection.requiredScopes))
59
+ return PASS;
60
+ const required = scopeTokens(protection.requiredScopes);
61
+ switch (verdict.kind) {
62
+ case "none":
63
+ case "malformed":
64
+ return {
65
+ action: "unauthorized",
66
+ description: "Authorization required",
67
+ scope: required,
68
+ };
69
+ case "expired":
70
+ return {
71
+ action: "unauthorized",
72
+ error: "invalid_token",
73
+ description: "The access token expired",
74
+ scope: required,
75
+ };
76
+ case "invalid":
77
+ return {
78
+ action: "unauthorized",
79
+ error: "invalid_token",
80
+ description: "The access token is invalid",
81
+ scope: required,
82
+ };
83
+ case "refer":
84
+ return PASS;
85
+ case "valid": {
86
+ const granted = new Set(verdict.scopes);
87
+ if (protection.requiredScopes.every((scope) => granted.has(scope)))
88
+ return PASS;
89
+ return {
90
+ action: "forbidden",
91
+ description: "The access token lacks a scope this tool requires",
92
+ scope: scopeTokens(verdict.scopes, protection.requiredScopes),
93
+ };
94
+ }
95
+ }
96
+ }
97
+ /** The 401 or 403 response for a decision; null for a pass. */
98
+ export function lazyAuthResponse(decision, routedHost) {
99
+ if (decision.action === "pass")
100
+ return null;
101
+ const status = decision.action === "forbidden" ? 403 : 401;
102
+ // Only a failure of a presented credential or scope is an RFC 6750 error
103
+ // code; a request without a credential carries none in its challenge.
104
+ const challengeError = decision.action === "forbidden" ? "insufficient_scope" : decision.error;
105
+ const error = challengeError ?? "unauthorized";
106
+ const params = [
107
+ ...(challengeError ? [`error="${challengeError}"`] : []),
108
+ ...(challengeError ? [`error_description="${decision.description}"`] : []),
109
+ ...(decision.scope.length ? [`scope="${decision.scope.join(" ")}"`] : []),
110
+ `resource_metadata="https://${routedHost}/.well-known/oauth-protected-resource"`,
111
+ ];
112
+ return new Response(JSON.stringify({ error, error_description: decision.description }), {
113
+ status,
114
+ headers: {
115
+ "Content-Type": "application/json",
116
+ "Cache-Control": "no-store",
117
+ "WWW-Authenticate": `Bearer ${params.join(", ")}`,
118
+ },
119
+ });
120
+ }
121
+ async function parseCall(request) {
122
+ let body;
123
+ try {
124
+ body = await request.clone().json();
125
+ }
126
+ catch {
127
+ return null;
128
+ }
129
+ if (!body || typeof body !== "object" || Array.isArray(body))
130
+ return null;
131
+ const rpc = body;
132
+ const params = rpc.params && typeof rpc.params === "object" && !Array.isArray(rpc.params)
133
+ ? rpc.params
134
+ : {};
135
+ return {
136
+ method: typeof rpc.method === "string" ? rpc.method : undefined,
137
+ toolName: typeof params.name === "string" ? params.name : undefined,
138
+ meta: params._meta,
139
+ bodyToken: params.delegationToken,
140
+ hasBodyToken: Object.prototype.hasOwnProperty.call(params, "delegationToken"),
141
+ };
142
+ }
143
+ /** The verdict on whatever credential the call presents, headers or body. */
144
+ async function assessCall(request, call, env) {
145
+ let headerToken;
146
+ try {
147
+ headerToken = extractDelegationVcJwt(request);
148
+ }
149
+ catch (error) {
150
+ if (!(error instanceof InvalidDelegationPresentation))
151
+ throw error;
152
+ return { kind: "malformed" };
153
+ }
154
+ let token = headerToken;
155
+ if (call.hasBodyToken) {
156
+ if (typeof call.bodyToken !== "string" ||
157
+ !call.bodyToken.trim() ||
158
+ (headerToken !== null && call.bodyToken !== headerToken))
159
+ return { kind: "malformed" };
160
+ token = call.bodyToken;
161
+ }
162
+ if (!token)
163
+ return { kind: "none" };
164
+ return assessPresentedCredential(token, request, env);
165
+ }
166
+ /**
167
+ * Decide a parsed POST at the edge. Returns the 401/403 response, or null when
168
+ * the request goes on to the agent.
169
+ */
170
+ export async function applyLazyAuthGate(request, deps) {
171
+ const call = await parseCall(request);
172
+ if (!call?.toolName)
173
+ return null;
174
+ const hasRequestProof = await carriesRequestProof(request.headers, call.meta, deps.getProofSupport ?? getCardProofSupport);
175
+ if (!isLazyAuthCandidate(call.method, hasRequestProof))
176
+ return null;
177
+ let protection;
178
+ try {
179
+ protection = await deps.resolveProtection(call.toolName);
180
+ }
181
+ catch {
182
+ // Unresolvable protection is the authority's call, not the edge's.
183
+ return null;
184
+ }
185
+ if (!protection)
186
+ return null;
187
+ let verdict;
188
+ try {
189
+ verdict = await assessCall(request, call, deps.env);
190
+ }
191
+ catch {
192
+ // A verification that cannot run is the authority's call, not the edge's.
193
+ return null;
194
+ }
195
+ const decision = decideLazyAuth({
196
+ method: call.method,
197
+ hasRequestProof,
198
+ protection,
199
+ verdict,
200
+ });
201
+ return lazyAuthResponse(decision, new URL(request.url).host);
202
+ }
203
+ //# sourceMappingURL=lazy-auth-gate.js.map