@kya-os/mcp-i-core 1.3.13 → 1.3.15

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 (255) hide show
  1. package/dist/config/remote-config.js +9 -12
  2. package/dist/runtime/base.d.ts +2 -1
  3. package/dist/runtime/base.js +34 -6
  4. package/dist/services/access-control.service.js +5 -0
  5. package/dist/services/tool-protection.service.js +17 -8
  6. package/package.json +2 -2
  7. package/.turbo/turbo-build.log +0 -4
  8. package/.turbo/turbo-test$colon$coverage.log +0 -4586
  9. package/.turbo/turbo-test.log +0 -4631
  10. package/COMPLIANCE_IMPROVEMENT_REPORT.md +0 -483
  11. package/Composer 3.md +0 -615
  12. package/GPT-5.md +0 -1169
  13. package/OPUS-plan.md +0 -352
  14. package/PHASE_3_AND_4.1_SUMMARY.md +0 -585
  15. package/PHASE_3_SUMMARY.md +0 -317
  16. package/PHASE_4.1.3_SUMMARY.md +0 -428
  17. package/PHASE_4.1_COMPLETE.md +0 -525
  18. package/PHASE_4_USER_DID_IDENTITY_LINKING_PLAN.md +0 -1240
  19. package/SCHEMA_COMPLIANCE_REPORT.md +0 -275
  20. package/TEST_PLAN.md +0 -571
  21. package/coverage/coverage-final.json +0 -60
  22. package/dist/cache/oauth-config-cache.d.ts.map +0 -1
  23. package/dist/cache/oauth-config-cache.js.map +0 -1
  24. package/dist/cache/tool-protection-cache.d.ts.map +0 -1
  25. package/dist/cache/tool-protection-cache.js.map +0 -1
  26. package/dist/compliance/index.d.ts.map +0 -1
  27. package/dist/compliance/index.js.map +0 -1
  28. package/dist/compliance/schema-registry.d.ts.map +0 -1
  29. package/dist/compliance/schema-registry.js.map +0 -1
  30. package/dist/compliance/schema-verifier.d.ts.map +0 -1
  31. package/dist/compliance/schema-verifier.js.map +0 -1
  32. package/dist/config/remote-config.d.ts.map +0 -1
  33. package/dist/config/remote-config.js.map +0 -1
  34. package/dist/config.d.ts.map +0 -1
  35. package/dist/config.js.map +0 -1
  36. package/dist/delegation/audience-validator.d.ts.map +0 -1
  37. package/dist/delegation/audience-validator.js.map +0 -1
  38. package/dist/delegation/bitstring.d.ts.map +0 -1
  39. package/dist/delegation/bitstring.js.map +0 -1
  40. package/dist/delegation/cascading-revocation.d.ts.map +0 -1
  41. package/dist/delegation/cascading-revocation.js.map +0 -1
  42. package/dist/delegation/delegation-graph.d.ts.map +0 -1
  43. package/dist/delegation/delegation-graph.js.map +0 -1
  44. package/dist/delegation/did-key-resolver.d.ts.map +0 -1
  45. package/dist/delegation/did-key-resolver.js.map +0 -1
  46. package/dist/delegation/index.d.ts.map +0 -1
  47. package/dist/delegation/index.js.map +0 -1
  48. package/dist/delegation/statuslist-manager.d.ts.map +0 -1
  49. package/dist/delegation/statuslist-manager.js.map +0 -1
  50. package/dist/delegation/storage/index.d.ts.map +0 -1
  51. package/dist/delegation/storage/index.js.map +0 -1
  52. package/dist/delegation/storage/memory-graph-storage.d.ts.map +0 -1
  53. package/dist/delegation/storage/memory-graph-storage.js.map +0 -1
  54. package/dist/delegation/storage/memory-statuslist-storage.d.ts.map +0 -1
  55. package/dist/delegation/storage/memory-statuslist-storage.js.map +0 -1
  56. package/dist/delegation/utils.d.ts.map +0 -1
  57. package/dist/delegation/utils.js.map +0 -1
  58. package/dist/delegation/vc-issuer.d.ts.map +0 -1
  59. package/dist/delegation/vc-issuer.js.map +0 -1
  60. package/dist/delegation/vc-verifier.d.ts.map +0 -1
  61. package/dist/delegation/vc-verifier.js.map +0 -1
  62. package/dist/identity/idp-token-resolver.d.ts.map +0 -1
  63. package/dist/identity/idp-token-resolver.js.map +0 -1
  64. package/dist/identity/idp-token-storage.interface.d.ts.map +0 -1
  65. package/dist/identity/idp-token-storage.interface.js.map +0 -1
  66. package/dist/identity/user-did-manager.d.ts.map +0 -1
  67. package/dist/identity/user-did-manager.js.map +0 -1
  68. package/dist/index.d.ts.map +0 -1
  69. package/dist/index.js.map +0 -1
  70. package/dist/providers/base.d.ts.map +0 -1
  71. package/dist/providers/base.js.map +0 -1
  72. package/dist/providers/memory.d.ts.map +0 -1
  73. package/dist/providers/memory.js.map +0 -1
  74. package/dist/runtime/audit-logger.d.ts.map +0 -1
  75. package/dist/runtime/audit-logger.js.map +0 -1
  76. package/dist/runtime/base.d.ts.map +0 -1
  77. package/dist/runtime/base.js.map +0 -1
  78. package/dist/services/access-control.service.d.ts.map +0 -1
  79. package/dist/services/access-control.service.js.map +0 -1
  80. package/dist/services/authorization/authorization-registry.d.ts.map +0 -1
  81. package/dist/services/authorization/authorization-registry.js.map +0 -1
  82. package/dist/services/authorization/types.d.ts.map +0 -1
  83. package/dist/services/authorization/types.js.map +0 -1
  84. package/dist/services/batch-delegation.service.d.ts.map +0 -1
  85. package/dist/services/batch-delegation.service.js.map +0 -1
  86. package/dist/services/crypto.service.d.ts.map +0 -1
  87. package/dist/services/crypto.service.js.map +0 -1
  88. package/dist/services/errors.d.ts.map +0 -1
  89. package/dist/services/errors.js.map +0 -1
  90. package/dist/services/index.d.ts.map +0 -1
  91. package/dist/services/index.js.map +0 -1
  92. package/dist/services/oauth-config.service.d.ts.map +0 -1
  93. package/dist/services/oauth-config.service.js.map +0 -1
  94. package/dist/services/oauth-provider-registry.d.ts.map +0 -1
  95. package/dist/services/oauth-provider-registry.js.map +0 -1
  96. package/dist/services/oauth-service.d.ts.map +0 -1
  97. package/dist/services/oauth-service.js.map +0 -1
  98. package/dist/services/oauth-token-retrieval.service.d.ts.map +0 -1
  99. package/dist/services/oauth-token-retrieval.service.js.map +0 -1
  100. package/dist/services/proof-verifier.d.ts.map +0 -1
  101. package/dist/services/proof-verifier.js.map +0 -1
  102. package/dist/services/provider-resolver.d.ts.map +0 -1
  103. package/dist/services/provider-resolver.js.map +0 -1
  104. package/dist/services/provider-validator.d.ts.map +0 -1
  105. package/dist/services/provider-validator.js.map +0 -1
  106. package/dist/services/session-registration.service.d.ts.map +0 -1
  107. package/dist/services/session-registration.service.js.map +0 -1
  108. package/dist/services/storage.service.d.ts.map +0 -1
  109. package/dist/services/storage.service.js.map +0 -1
  110. package/dist/services/tool-context-builder.d.ts.map +0 -1
  111. package/dist/services/tool-context-builder.js.map +0 -1
  112. package/dist/services/tool-protection.service.d.ts.map +0 -1
  113. package/dist/services/tool-protection.service.js.map +0 -1
  114. package/dist/types/oauth-required-error.d.ts.map +0 -1
  115. package/dist/types/oauth-required-error.js.map +0 -1
  116. package/dist/types/tool-protection.d.ts.map +0 -1
  117. package/dist/types/tool-protection.js.map +0 -1
  118. package/dist/utils/base58.d.ts.map +0 -1
  119. package/dist/utils/base58.js.map +0 -1
  120. package/dist/utils/base64.d.ts.map +0 -1
  121. package/dist/utils/base64.js.map +0 -1
  122. package/dist/utils/cors.d.ts.map +0 -1
  123. package/dist/utils/cors.js.map +0 -1
  124. package/dist/utils/did-helpers.d.ts.map +0 -1
  125. package/dist/utils/did-helpers.js.map +0 -1
  126. package/dist/utils/index.d.ts.map +0 -1
  127. package/dist/utils/index.js.map +0 -1
  128. package/dist/utils/storage-keys.d.ts.map +0 -1
  129. package/dist/utils/storage-keys.js.map +0 -1
  130. package/docs/API_REFERENCE.md +0 -1362
  131. package/docs/COMPLIANCE_MATRIX.md +0 -691
  132. package/docs/STATUSLIST2021_GUIDE.md +0 -696
  133. package/docs/W3C_VC_DELEGATION_GUIDE.md +0 -710
  134. package/src/__tests__/cache/tool-protection-cache.test.ts +0 -640
  135. package/src/__tests__/config/provider-runtime-config.test.ts +0 -309
  136. package/src/__tests__/delegation-e2e.test.ts +0 -690
  137. package/src/__tests__/identity/user-did-manager.test.ts +0 -232
  138. package/src/__tests__/index.test.ts +0 -56
  139. package/src/__tests__/integration/full-flow.test.ts +0 -789
  140. package/src/__tests__/integration.test.ts +0 -281
  141. package/src/__tests__/providers/base.test.ts +0 -173
  142. package/src/__tests__/providers/memory.test.ts +0 -319
  143. package/src/__tests__/regression/phase2-regression.test.ts +0 -429
  144. package/src/__tests__/runtime/audit-logger.test.ts +0 -154
  145. package/src/__tests__/runtime/base-extensions.test.ts +0 -595
  146. package/src/__tests__/runtime/base.test.ts +0 -869
  147. package/src/__tests__/runtime/delegation-flow.test.ts +0 -164
  148. package/src/__tests__/runtime/proof-client-did.test.ts +0 -376
  149. package/src/__tests__/runtime/route-interception.test.ts +0 -686
  150. package/src/__tests__/runtime/tool-protection-enforcement.test.ts +0 -908
  151. package/src/__tests__/services/agentshield-integration.test.ts +0 -791
  152. package/src/__tests__/services/cache-busting.test.ts +0 -125
  153. package/src/__tests__/services/oauth-service-pkce.test.ts +0 -556
  154. package/src/__tests__/services/provider-resolver-edge-cases.test.ts +0 -591
  155. package/src/__tests__/services/tool-protection-merged-config.test.ts +0 -485
  156. package/src/__tests__/services/tool-protection-oauth-provider.test.ts +0 -480
  157. package/src/__tests__/services/tool-protection.service.test.ts +0 -1373
  158. package/src/__tests__/utils/mock-providers.ts +0 -340
  159. package/src/cache/oauth-config-cache.d.ts +0 -69
  160. package/src/cache/oauth-config-cache.d.ts.map +0 -1
  161. package/src/cache/oauth-config-cache.js.map +0 -1
  162. package/src/cache/oauth-config-cache.ts +0 -123
  163. package/src/cache/tool-protection-cache.ts +0 -171
  164. package/src/compliance/EXAMPLE.md +0 -412
  165. package/src/compliance/__tests__/schema-verifier.test.ts +0 -797
  166. package/src/compliance/index.ts +0 -8
  167. package/src/compliance/schema-registry.ts +0 -460
  168. package/src/compliance/schema-verifier.ts +0 -708
  169. package/src/config/__tests__/merged-config.spec.ts +0 -445
  170. package/src/config/__tests__/remote-config.spec.ts +0 -268
  171. package/src/config/remote-config.ts +0 -264
  172. package/src/config.ts +0 -312
  173. package/src/delegation/__tests__/audience-validator.test.ts +0 -112
  174. package/src/delegation/__tests__/bitstring.test.ts +0 -346
  175. package/src/delegation/__tests__/cascading-revocation.test.ts +0 -628
  176. package/src/delegation/__tests__/delegation-graph.test.ts +0 -584
  177. package/src/delegation/__tests__/did-key-resolver.test.ts +0 -265
  178. package/src/delegation/__tests__/utils.test.ts +0 -152
  179. package/src/delegation/__tests__/vc-issuer.test.ts +0 -442
  180. package/src/delegation/__tests__/vc-verifier.test.ts +0 -922
  181. package/src/delegation/audience-validator.ts +0 -52
  182. package/src/delegation/bitstring.ts +0 -278
  183. package/src/delegation/cascading-revocation.ts +0 -370
  184. package/src/delegation/delegation-graph.ts +0 -299
  185. package/src/delegation/did-key-resolver.ts +0 -179
  186. package/src/delegation/index.ts +0 -14
  187. package/src/delegation/statuslist-manager.ts +0 -353
  188. package/src/delegation/storage/__tests__/memory-graph-storage.test.ts +0 -366
  189. package/src/delegation/storage/__tests__/memory-statuslist-storage.test.ts +0 -228
  190. package/src/delegation/storage/index.ts +0 -9
  191. package/src/delegation/storage/memory-graph-storage.ts +0 -178
  192. package/src/delegation/storage/memory-statuslist-storage.ts +0 -77
  193. package/src/delegation/utils.ts +0 -221
  194. package/src/delegation/vc-issuer.ts +0 -232
  195. package/src/delegation/vc-verifier.ts +0 -568
  196. package/src/identity/idp-token-resolver.ts +0 -181
  197. package/src/identity/idp-token-storage.interface.ts +0 -94
  198. package/src/identity/user-did-manager.ts +0 -526
  199. package/src/index.ts +0 -310
  200. package/src/providers/base.d.ts +0 -91
  201. package/src/providers/base.d.ts.map +0 -1
  202. package/src/providers/base.js.map +0 -1
  203. package/src/providers/base.ts +0 -96
  204. package/src/providers/memory.ts +0 -142
  205. package/src/runtime/audit-logger.ts +0 -39
  206. package/src/runtime/base.ts +0 -1392
  207. package/src/services/__tests__/access-control.integration.test.ts +0 -443
  208. package/src/services/__tests__/access-control.proof-response-validation.test.ts +0 -578
  209. package/src/services/__tests__/access-control.service.test.ts +0 -970
  210. package/src/services/__tests__/batch-delegation.service.test.ts +0 -351
  211. package/src/services/__tests__/crypto.service.test.ts +0 -531
  212. package/src/services/__tests__/oauth-provider-registry.test.ts +0 -142
  213. package/src/services/__tests__/proof-verifier.integration.test.ts +0 -485
  214. package/src/services/__tests__/proof-verifier.test.ts +0 -489
  215. package/src/services/__tests__/provider-resolution.integration.test.ts +0 -202
  216. package/src/services/__tests__/provider-resolver.test.ts +0 -213
  217. package/src/services/__tests__/storage.service.test.ts +0 -358
  218. package/src/services/access-control.service.ts +0 -990
  219. package/src/services/authorization/authorization-registry.ts +0 -66
  220. package/src/services/authorization/types.ts +0 -71
  221. package/src/services/batch-delegation.service.ts +0 -137
  222. package/src/services/crypto.service.ts +0 -302
  223. package/src/services/errors.ts +0 -76
  224. package/src/services/index.ts +0 -18
  225. package/src/services/oauth-config.service.d.ts +0 -53
  226. package/src/services/oauth-config.service.d.ts.map +0 -1
  227. package/src/services/oauth-config.service.js.map +0 -1
  228. package/src/services/oauth-config.service.ts +0 -192
  229. package/src/services/oauth-provider-registry.d.ts +0 -57
  230. package/src/services/oauth-provider-registry.d.ts.map +0 -1
  231. package/src/services/oauth-provider-registry.js.map +0 -1
  232. package/src/services/oauth-provider-registry.ts +0 -141
  233. package/src/services/oauth-service.ts +0 -544
  234. package/src/services/oauth-token-retrieval.service.ts +0 -245
  235. package/src/services/proof-verifier.ts +0 -478
  236. package/src/services/provider-resolver.d.ts +0 -48
  237. package/src/services/provider-resolver.d.ts.map +0 -1
  238. package/src/services/provider-resolver.js.map +0 -1
  239. package/src/services/provider-resolver.ts +0 -146
  240. package/src/services/provider-validator.ts +0 -170
  241. package/src/services/session-registration.service.ts +0 -251
  242. package/src/services/storage.service.ts +0 -566
  243. package/src/services/tool-context-builder.ts +0 -237
  244. package/src/services/tool-protection.service.ts +0 -1070
  245. package/src/types/oauth-required-error.ts +0 -63
  246. package/src/types/tool-protection.ts +0 -155
  247. package/src/utils/__tests__/did-helpers.test.ts +0 -156
  248. package/src/utils/base58.ts +0 -109
  249. package/src/utils/base64.ts +0 -148
  250. package/src/utils/cors.ts +0 -83
  251. package/src/utils/did-helpers.ts +0 -210
  252. package/src/utils/index.ts +0 -8
  253. package/src/utils/storage-keys.ts +0 -278
  254. package/tsconfig.json +0 -21
  255. package/vitest.config.ts +0 -56
package/src/utils/cors.ts DELETED
@@ -1,83 +0,0 @@
1
- /**
2
- * CORS Header Utilities
3
- *
4
- * Centralized CORS header management for MCP-I services.
5
- * Includes Vary: Origin for cache optimization when origin scoping is implemented.
6
- */
7
-
8
- /**
9
- * Type-safe CORS headers
10
- * Compatible with Response headers and HeadersInit
11
- */
12
- export type CORSHeaders = Record<string, string>;
13
-
14
- /**
15
- * Standard CORS headers for well-known endpoints (.well-known/*)
16
- * Includes Vary: Origin for future cache optimization when origin scoping is added
17
- */
18
- export const WELL_KNOWN_CORS_HEADERS: CORSHeaders = {
19
- 'Access-Control-Allow-Origin': '*',
20
- 'Vary': 'Origin'
21
- };
22
-
23
- /**
24
- * CORS headers for MCP protocol responses
25
- * Includes exposed headers for session management
26
- */
27
- export const MCP_CORS_HEADERS: CORSHeaders = {
28
- 'Access-Control-Allow-Origin': '*',
29
- 'Access-Control-Expose-Headers': 'mcp-session-id',
30
- 'Vary': 'Origin'
31
- };
32
-
33
- /**
34
- * CORS preflight headers for OPTIONS requests
35
- * Comprehensive header allowlist for MCP protocol
36
- */
37
- export const PREFLIGHT_CORS_HEADERS: CORSHeaders = {
38
- 'Access-Control-Allow-Origin': '*',
39
- 'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
40
- 'Access-Control-Allow-Headers': 'Content-Type, Authorization, mcp-session-id, mcp-protocol-version',
41
- 'Vary': 'Origin'
42
- };
43
-
44
- /**
45
- * CORS headers for OAuth endpoints
46
- * Includes additional headers required for OAuth 2.0 flows
47
- */
48
- export const OAUTH_CORS_HEADERS: CORSHeaders = {
49
- 'Access-Control-Allow-Origin': '*',
50
- 'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
51
- 'Access-Control-Allow-Headers': 'Content-Type, Authorization, Accept, mcp-protocol-version',
52
- 'Access-Control-Expose-Headers': 'Content-Type',
53
- 'Vary': 'Origin'
54
- };
55
-
56
- /**
57
- * Merge CORS headers with existing headers
58
- * Ensures CORS headers take precedence
59
- */
60
- export function mergeCORSHeaders(
61
- existingHeaders: Record<string, string>,
62
- corsHeaders: CORSHeaders = WELL_KNOWN_CORS_HEADERS
63
- ): Record<string, string> {
64
- return {
65
- ...existingHeaders,
66
- ...corsHeaders
67
- };
68
- }
69
-
70
- /**
71
- * Apply CORS headers to an Express Response object
72
- * For use with Express middleware
73
- */
74
- export function applyCORSHeaders(
75
- res: { setHeader: (name: string, value: string) => void },
76
- corsHeaders: CORSHeaders = MCP_CORS_HEADERS
77
- ): void {
78
- Object.entries(corsHeaders).forEach(([key, value]) => {
79
- if (value !== undefined) {
80
- res.setHeader(key, value);
81
- }
82
- });
83
- }
@@ -1,210 +0,0 @@
1
- /**
2
- * DID Validation and Helper Utilities
3
- *
4
- * Centralized utilities for DID validation, normalization, and handling.
5
- * Promotes DRY principle and consistency across the codebase.
6
- *
7
- * @package @kya-os/mcp-i-core/utils
8
- */
9
-
10
- import { base58Encode } from "./base58";
11
-
12
- /**
13
- * Check if a string is a valid DID format
14
- *
15
- * @param did - String to validate
16
- * @returns true if string starts with "did:"
17
- *
18
- * @example
19
- * ```typescript
20
- * isValidDid("did:key:z6Mk...") // true
21
- * isValidDid("not-a-did") // false
22
- * ```
23
- */
24
- export function isValidDid(did: string): boolean {
25
- return typeof did === "string" && did.startsWith("did:");
26
- }
27
-
28
- /**
29
- * Get the DID method from a DID string
30
- *
31
- * @param did - DID string
32
- * @returns DID method (e.g., "key", "web") or null if invalid
33
- *
34
- * @example
35
- * ```typescript
36
- * getDidMethod("did:key:z6Mk...") // "key"
37
- * getDidMethod("did:web:example.com") // "web"
38
- * getDidMethod("invalid") // null
39
- * ```
40
- */
41
- export function getDidMethod(did: string): string | null {
42
- if (!isValidDid(did)) {
43
- return null;
44
- }
45
- const match = did.match(/^did:([^:]+):/);
46
- return match ? match[1] : null;
47
- }
48
-
49
- /**
50
- * Normalize a DID string (trim whitespace)
51
- *
52
- * @param did - DID string to normalize
53
- * @returns Normalized DID string
54
- *
55
- * @example
56
- * ```typescript
57
- * normalizeDid(" did:key:z6Mk... ") // "did:key:z6Mk..."
58
- * ```
59
- */
60
- export function normalizeDid(did: string): string {
61
- return did.trim();
62
- }
63
-
64
- /**
65
- * Compare two DIDs for equality (case-sensitive)
66
- *
67
- * @param did1 - First DID
68
- * @param did2 - Second DID
69
- * @returns true if DIDs are equal (after normalization)
70
- *
71
- * @example
72
- * ```typescript
73
- * compareDids("did:key:z6Mk...", "did:key:z6Mk...") // true
74
- * compareDids("did:key:z6Mk...", "did:web:example.com") // false
75
- * ```
76
- */
77
- export function compareDids(did1: string, did2: string): boolean {
78
- return normalizeDid(did1) === normalizeDid(did2);
79
- }
80
-
81
- /**
82
- * Extract server DID from config (supports both old and new field names)
83
- *
84
- * Supports backward compatibility by reading both `serverDid` and deprecated `agentDid`.
85
- * Prefers `serverDid` if both are present.
86
- *
87
- * @param config - Config object with identity field
88
- * @returns Server DID string
89
- * @throws Error if neither serverDid nor agentDid is configured
90
- *
91
- * @example
92
- * ```typescript
93
- * // New config
94
- * getServerDid({ identity: { serverDid: "did:web:server.com" } }) // "did:web:server.com"
95
- *
96
- * // Old config (backward compatibility)
97
- * getServerDid({ identity: { agentDid: "did:web:server.com" } }) // "did:web:server.com"
98
- *
99
- * // Prefers serverDid over agentDid
100
- * getServerDid({ identity: { serverDid: "new", agentDid: "old" } }) // "new"
101
- * ```
102
- */
103
- export function getServerDid(config: {
104
- identity: { serverDid?: string; agentDid?: string };
105
- }): string {
106
- const serverDid = config.identity.serverDid || config.identity.agentDid;
107
- if (!serverDid) {
108
- throw new Error("Server DID not configured");
109
- }
110
- return serverDid;
111
- }
112
-
113
- /**
114
- * Extract agent ID from DID
115
- *
116
- * The agent ID is the last component of the DID.
117
- *
118
- * @param did - DID string
119
- * @returns Agent ID (last component of DID)
120
- *
121
- * @example
122
- * ```typescript
123
- * extractAgentId("did:web:knowthat.ai:agents:my-agent") // "my-agent"
124
- * extractAgentId("did:web:localhost:3000:agents:12912feb") // "12912feb"
125
- * extractAgentId("did:key:z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK") // "z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK"
126
- * ```
127
- */
128
- export function extractAgentId(did: string): string {
129
- const parts = did.split(':');
130
- return parts[parts.length - 1];
131
- }
132
-
133
- /**
134
- * Extract agent slug from DID
135
- *
136
- * Agent slug is the same as agent ID - the last component of the DID.
137
- * For DID format: did:web:knowthat.ai:agents:my-agent
138
- * Returns: my-agent
139
- *
140
- * @param did - DID string
141
- * @returns Agent slug (last component of DID)
142
- *
143
- * @example
144
- * ```typescript
145
- * extractAgentSlug("did:web:knowthat.ai:agents:my-agent") // "my-agent"
146
- * extractAgentSlug("did:web:localhost:3000:agents:12912feb") // "12912feb"
147
- * ```
148
- */
149
- export function extractAgentSlug(did: string): string {
150
- return extractAgentId(did);
151
- }
152
-
153
- /**
154
- * Ed25519 multicodec prefix for did:key encoding
155
- * As per https://w3c-ccg.github.io/did-method-key/
156
- */
157
- const ED25519_MULTICODEC_PREFIX = new Uint8Array([0xed, 0x01]);
158
-
159
- /**
160
- * Generate a did:key from Ed25519 public key bytes
161
- *
162
- * Following spec: https://w3c-ccg.github.io/did-method-key/
163
- * Format: did:key:z<multibase-base58btc(<multicodec-ed25519-pub><publicKey>)>
164
- *
165
- * @param publicKeyBytes - Ed25519 public key as Uint8Array (32 bytes)
166
- * @returns did:key string
167
- *
168
- * @example
169
- * ```typescript
170
- * const publicKey = new Uint8Array(32); // 32-byte Ed25519 public key
171
- * const did = generateDidKeyFromBytes(publicKey);
172
- * // did = "did:key:z6Mk..."
173
- * ```
174
- */
175
- export function generateDidKeyFromBytes(publicKeyBytes: Uint8Array): string {
176
- // Combine multicodec prefix + public key
177
- const multicodecKey = new Uint8Array(
178
- ED25519_MULTICODEC_PREFIX.length + publicKeyBytes.length
179
- );
180
- multicodecKey.set(ED25519_MULTICODEC_PREFIX);
181
- multicodecKey.set(publicKeyBytes, ED25519_MULTICODEC_PREFIX.length);
182
-
183
- // Base58-btc encode and add multibase prefix 'z'
184
- const base58Encoded = base58Encode(multicodecKey);
185
- return `did:key:z${base58Encoded}`;
186
- }
187
-
188
- /**
189
- * Generate a did:key from base64-encoded Ed25519 public key
190
- *
191
- * Convenience wrapper around generateDidKeyFromBytes for base64-encoded keys.
192
- *
193
- * @param publicKeyBase64 - Ed25519 public key as base64 string
194
- * @returns did:key string
195
- *
196
- * @example
197
- * ```typescript
198
- * const publicKeyBase64 = "...base64 encoded key...";
199
- * const did = generateDidKeyFromBase64(publicKeyBase64);
200
- * // did = "did:key:z6Mk..."
201
- * ```
202
- */
203
- export function generateDidKeyFromBase64(publicKeyBase64: string): string {
204
- // Decode base64 to bytes
205
- const publicKeyBytes = Uint8Array.from(atob(publicKeyBase64), (c) =>
206
- c.charCodeAt(0)
207
- );
208
- return generateDidKeyFromBytes(publicKeyBytes);
209
- }
210
-
@@ -1,8 +0,0 @@
1
- /**
2
- * Utility exports
3
- */
4
-
5
- export * from "./cors";
6
- export * from "./base64";
7
- export * from "./storage-keys";
8
- export * from "./did-helpers";
@@ -1,278 +0,0 @@
1
- /**
2
- * Storage Key Migration Utilities
3
- *
4
- * Provides utilities for migrating from old storage key formats to new composite formats.
5
- * This supports Phase 3 Task 2 (StorageService) and Phase 4 (User DID identity linking).
6
- *
7
- * @package @kya-os/mcp-i-core
8
- */
9
-
10
- /**
11
- * Legacy storage key format (agent-only, causes multi-tenant conflicts)
12
- * Format: `agent:${agentDid}:delegation`
13
- */
14
- export function legacyDelegationKey(agentDid: string): string {
15
- return `agent:${agentDid}:delegation`;
16
- }
17
-
18
- /**
19
- * New composite storage key format (user+agent scoped, prevents conflicts)
20
- * Format: `delegation:user:${userDid}:agent:${agentDid}:project:${projectId}`
21
- *
22
- * Note: projectId is optional for backward compatibility
23
- */
24
- export function compositeDelegationKey(
25
- userDid: string,
26
- agentDid: string,
27
- projectId?: string
28
- ): string {
29
- if (projectId) {
30
- return `delegation:user:${userDid}:agent:${agentDid}:project:${projectId}`;
31
- }
32
- return `delegation:user:${userDid}:agent:${agentDid}`;
33
- }
34
-
35
- /**
36
- * Session cache key format
37
- * Format: `session:${sessionId}`
38
- */
39
- export function sessionKey(sessionId: string): string {
40
- return `session:${sessionId}`;
41
- }
42
-
43
- /**
44
- * User DID storage key format
45
- * Format: `userDid:oauth:${provider}:${subject}`
46
- */
47
- export function userDidKey(provider: string, subject: string): string {
48
- return `userDid:oauth:${provider}:${subject}`;
49
- }
50
-
51
- /**
52
- * OAuth identity mapping key format
53
- * Format: `oauth:${provider}:${subject}`
54
- */
55
- export function oauthIdentityKey(provider: string, subject: string): string {
56
- return `oauth:${provider}:${subject}`;
57
- }
58
-
59
- /**
60
- * Verification cache key format
61
- * Format: `verified:${tokenHash}`
62
- */
63
- export function verificationCacheKey(tokenHash: string): string {
64
- return `verified:${tokenHash}`;
65
- }
66
-
67
- /**
68
- * Nonce tracking key format
69
- * Format: `nonce:${nonce}`
70
- */
71
- export function nonceKey(nonce: string): string {
72
- return `nonce:${nonce}`;
73
- }
74
-
75
- /**
76
- * Storage key migration result
77
- */
78
- export interface MigrationResult {
79
- /** Number of keys migrated */
80
- migrated: number;
81
-
82
- /** Number of keys that failed to migrate */
83
- failed: number;
84
-
85
- /** List of migrated key pairs (old -> new) */
86
- migrations: Array<{ oldKey: string; newKey: string }>;
87
-
88
- /** List of errors encountered */
89
- errors: Array<{ key: string; error: string }>;
90
- }
91
-
92
- /**
93
- * Storage provider interface for migration operations
94
- *
95
- * Matches the base StorageProvider abstract class contract.
96
- */
97
- export interface StorageProvider {
98
- get(key: string): Promise<string | null>;
99
- set(key: string, value: string): Promise<void>;
100
- delete(key: string): Promise<void>;
101
- exists(key: string): Promise<boolean>;
102
- list(prefix?: string): Promise<string[]>;
103
- }
104
-
105
- /**
106
- * Migrate delegation keys from legacy format to composite format
107
- *
108
- * This function:
109
- * 1. Finds all legacy keys (`agent:${did}:delegation`)
110
- * 2. Attempts to extract userDid from session data or OAuth mappings
111
- * 3. Creates new composite keys (`delegation:user:${userDid}:agent:${agentDid}`)
112
- * 4. Copies values to new keys
113
- * 5. Optionally deletes old keys (dry-run mode available)
114
- *
115
- * @param storage - Storage provider instance
116
- * @param options - Migration options
117
- * @returns Migration result with statistics
118
- */
119
- export async function migrateDelegationKeys(
120
- storage: StorageProvider,
121
- options: {
122
- /** If true, only report what would be migrated without making changes */
123
- dryRun?: boolean;
124
-
125
- /** If true, delete old keys after successful migration */
126
- deleteOldKeys?: boolean;
127
-
128
- /** Optional userDid resolver function (if not provided, attempts to extract from session) */
129
- resolveUserDid?: (agentDid: string, sessionId?: string) => Promise<string | null>;
130
- } = {}
131
- ): Promise<MigrationResult> {
132
- const result: MigrationResult = {
133
- migrated: 0,
134
- failed: 0,
135
- migrations: [],
136
- errors: [],
137
- };
138
-
139
- try {
140
- // Find all legacy delegation keys
141
- const legacyKeys = await storage.list('agent:');
142
- const delegationKeys = legacyKeys.filter((key) =>
143
- key.match(/^agent:[^:]+:delegation$/)
144
- );
145
-
146
- console.log(`Found ${delegationKeys.length} legacy delegation keys to migrate`);
147
-
148
- for (const oldKey of delegationKeys) {
149
- try {
150
- // Extract agentDid from key: `agent:${agentDid}:delegation`
151
- const match = oldKey.match(/^agent:([^:]+):delegation$/);
152
- if (!match) {
153
- result.errors.push({
154
- key: oldKey,
155
- error: 'Invalid legacy key format',
156
- });
157
- result.failed++;
158
- continue;
159
- }
160
-
161
- const agentDid = match[1];
162
-
163
- // Get the value from old key
164
- const value = await storage.get(oldKey);
165
- if (!value) {
166
- // Key exists but has no value - skip
167
- continue;
168
- }
169
-
170
- // Try to resolve userDid
171
- let userDid: string | null = null;
172
- let sessionId: string | undefined = undefined;
173
-
174
- // First, attempt to extract from session data to get both userDid and sessionId
175
- const sessionKeys = await storage.list('session:');
176
- for (const sessionKey of sessionKeys) {
177
- const sessionData = await storage.get(sessionKey);
178
- if (sessionData) {
179
- try {
180
- const parsed = JSON.parse(sessionData);
181
- if (parsed.userDid && parsed.agentDid === agentDid) {
182
- userDid = parsed.userDid;
183
- // Extract sessionId from key: `session:${sessionId}`
184
- const sessionMatch = sessionKey.match(/^session:(.+)$/);
185
- if (sessionMatch) {
186
- sessionId = sessionMatch[1];
187
- }
188
- break;
189
- }
190
- } catch {
191
- // Not JSON, skip
192
- }
193
- }
194
- }
195
-
196
- // If custom resolver provided, use it (with sessionId context if available)
197
- if (options.resolveUserDid) {
198
- const resolvedUserDid = await options.resolveUserDid(agentDid, sessionId);
199
- // Use resolved userDid if available, otherwise fall back to extracted one
200
- if (resolvedUserDid) {
201
- userDid = resolvedUserDid;
202
- }
203
- }
204
-
205
- if (!userDid) {
206
- // Cannot migrate without userDid - skip for now
207
- result.errors.push({
208
- key: oldKey,
209
- error: 'Cannot resolve userDid - skipping migration',
210
- });
211
- result.failed++;
212
- continue;
213
- }
214
-
215
- // Create new composite key
216
- const newKey = compositeDelegationKey(userDid, agentDid);
217
-
218
- if (options.dryRun) {
219
- // Just record what would be migrated
220
- result.migrations.push({ oldKey, newKey });
221
- result.migrated++;
222
- } else {
223
- // Copy value to new key
224
- await storage.set(newKey, value);
225
- result.migrations.push({ oldKey, newKey });
226
- result.migrated++;
227
-
228
- // Optionally delete old key
229
- if (options.deleteOldKeys) {
230
- await storage.delete(oldKey);
231
- }
232
- }
233
- } catch (error) {
234
- result.errors.push({
235
- key: oldKey,
236
- error: error instanceof Error ? error.message : String(error),
237
- });
238
- result.failed++;
239
- }
240
- }
241
- } catch (error) {
242
- result.errors.push({
243
- key: 'migration',
244
- error: error instanceof Error ? error.message : String(error),
245
- });
246
- }
247
-
248
- return result;
249
- }
250
-
251
- /**
252
- * Storage key constants for consistent namespace management
253
- *
254
- * These match the Phase 4 storage key architecture.
255
- */
256
- export const STORAGE_KEYS = {
257
- /** User DID storage (persistent - 90 days) */
258
- userDid: userDidKey,
259
-
260
- /** OAuth identity mapping (persistent - 90 days) */
261
- oauthIdentity: oauthIdentityKey,
262
-
263
- /** User+Agent delegation tokens (persistent - 7 days) */
264
- delegation: compositeDelegationKey,
265
-
266
- /** Session cache (temporary - 30 minutes) */
267
- session: sessionKey,
268
-
269
- /** Legacy delegation format (deprecated - 24 hours) */
270
- legacyDelegation: legacyDelegationKey,
271
-
272
- /** Verification cache (temporary - 5 minutes) */
273
- verificationCache: verificationCacheKey,
274
-
275
- /** Nonce tracking (temporary - 5 minutes) */
276
- nonce: nonceKey,
277
- } as const;
278
-
package/tsconfig.json DELETED
@@ -1,21 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "ES2022",
4
- "module": "Node16",
5
- "lib": ["ES2022"],
6
- "outDir": "./dist",
7
- "rootDir": "./src",
8
- "strict": true,
9
- "esModuleInterop": true,
10
- "skipLibCheck": true,
11
- "forceConsistentCasingInFileNames": true,
12
- "declaration": true,
13
- "declarationMap": true,
14
- "sourceMap": true,
15
- "moduleResolution": "node16",
16
- "resolveJsonModule": true,
17
- "allowSyntheticDefaultImports": true
18
- },
19
- "include": ["src/**/*"],
20
- "exclude": ["node_modules", "dist", "**/*.test.ts", "**/*.spec.ts", "**/__tests__/**"]
21
- }
package/vitest.config.ts DELETED
@@ -1,56 +0,0 @@
1
- import { defineConfig } from "vitest/config";
2
- import path from "path";
3
-
4
- export default defineConfig({
5
- resolve: {
6
- alias: {
7
- // Map contracts subpath exports to their actual dist paths for vitest resolution
8
- // These aliases work for both source files and bundled code in node_modules
9
- "@kya-os/contracts/proof": path.resolve(__dirname, "../contracts/dist/proof/index.js"),
10
- "@kya-os/contracts/delegation": path.resolve(__dirname, "../contracts/dist/delegation/index.js"),
11
- "@kya-os/contracts/agentshield-api": path.resolve(__dirname, "../contracts/dist/agentshield-api/index.js"),
12
- "@kya-os/contracts/config": path.resolve(__dirname, "../contracts/dist/config/index.js"),
13
- "@kya-os/contracts/tool-protection": path.resolve(__dirname, "../contracts/dist/tool-protection/index.js"),
14
- "@kya-os/contracts/well-known": path.resolve(__dirname, "../contracts/dist/well-known/index.js"),
15
- "@kya-os/contracts/runtime": path.resolve(__dirname, "../contracts/dist/runtime/index.js"),
16
- "@kya-os/contracts/handshake": path.resolve(__dirname, "../contracts/dist/handshake.js"),
17
- "@kya-os/contracts/test": path.resolve(__dirname, "../contracts/dist/test.js"),
18
- "@kya-os/contracts": path.resolve(__dirname, "../contracts/dist/index.js"),
19
- },
20
- // Ensure aliases are resolved before node_modules
21
- dedupe: ["@kya-os/contracts"],
22
- // Force resolution to use workspace contracts
23
- conditions: ["node", "import", "require"],
24
- },
25
- // Force vitest to inline contracts and mcp-i-core packages so aliases work
26
- // This ensures that bundled code in node_modules can resolve contracts subpath exports
27
- server: {
28
- deps: {
29
- inline: ["@kya-os/contracts", "@kya-os/mcp-i-core"],
30
- },
31
- },
32
- test: {
33
- globals: true,
34
- environment: "node",
35
- coverage: {
36
- provider: "v8",
37
- reporter: ["json", "text-summary"],
38
- include: ["src/**/*.ts"],
39
- exclude: [
40
- "src/**/__tests__/**",
41
- "src/**/*.test.ts",
42
- "dist/**",
43
- "node_modules/**",
44
- ],
45
- reportsDirectory: "./coverage",
46
- clean: true,
47
- // Coverage thresholds removed - will be re-enabled when coverage improves
48
- // thresholds: {
49
- // lines: 80,
50
- // branches: 70,
51
- // functions: 80,
52
- // statements: 80,
53
- // },
54
- },
55
- },
56
- });