@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
@@ -1,178 +0,0 @@
1
- /**
2
- * In-Memory Delegation Graph Storage Provider
3
- *
4
- * Memory-based implementation for testing and development.
5
- * NOT suitable for production (no persistence).
6
- *
7
- * SOLID: Implements DelegationGraphStorageProvider interface
8
- */
9
-
10
- import type {
11
- DelegationGraphStorageProvider,
12
- DelegationNode,
13
- } from '../delegation-graph';
14
-
15
- /**
16
- * Memory-based Delegation Graph storage
17
- *
18
- * Stores delegation nodes in memory with efficient graph queries.
19
- * Useful for:
20
- * - Unit tests
21
- * - Integration tests
22
- * - Development/debugging
23
- * - Examples
24
- */
25
- export class MemoryDelegationGraphStorage
26
- implements DelegationGraphStorageProvider
27
- {
28
- private nodes = new Map<string, DelegationNode>();
29
-
30
- /**
31
- * Get a delegation node by ID
32
- */
33
- async getNode(delegationId: string): Promise<DelegationNode | null> {
34
- return this.nodes.get(delegationId) || null;
35
- }
36
-
37
- /**
38
- * Save a delegation node
39
- */
40
- async setNode(node: DelegationNode): Promise<void> {
41
- this.nodes.set(node.id, node);
42
- }
43
-
44
- /**
45
- * Get all children of a delegation
46
- */
47
- async getChildren(delegationId: string): Promise<DelegationNode[]> {
48
- const parent = this.nodes.get(delegationId);
49
- if (!parent) return [];
50
-
51
- return parent.children
52
- .map((childId) => this.nodes.get(childId))
53
- .filter((node): node is DelegationNode => node !== undefined);
54
- }
55
-
56
- /**
57
- * Get the full chain from root to this delegation
58
- */
59
- async getChain(delegationId: string): Promise<DelegationNode[]> {
60
- const chain: DelegationNode[] = [];
61
- let currentId: string | null = delegationId;
62
-
63
- // Walk up the tree to root
64
- while (currentId) {
65
- const node = this.nodes.get(currentId);
66
- if (!node) break;
67
-
68
- chain.unshift(node); // Add to front (root first)
69
- currentId = node.parentId;
70
- }
71
-
72
- return chain;
73
- }
74
-
75
- /**
76
- * Get all descendants (children, grandchildren, etc.)
77
- *
78
- * Uses BFS for efficiency.
79
- */
80
- async getDescendants(delegationId: string): Promise<DelegationNode[]> {
81
- const descendants: DelegationNode[] = [];
82
- const queue: string[] = [delegationId];
83
- const visited = new Set<string>();
84
-
85
- while (queue.length > 0) {
86
- const currentId = queue.shift()!;
87
-
88
- // Skip if already visited (prevent infinite loops)
89
- if (visited.has(currentId)) continue;
90
- visited.add(currentId);
91
-
92
- const node = this.nodes.get(currentId);
93
- if (!node) continue;
94
-
95
- // Add children to queue
96
- for (const childId of node.children) {
97
- if (!visited.has(childId)) {
98
- queue.push(childId);
99
-
100
- const childNode = this.nodes.get(childId);
101
- if (childNode) {
102
- descendants.push(childNode);
103
- }
104
- }
105
- }
106
- }
107
-
108
- return descendants;
109
- }
110
-
111
- /**
112
- * Delete a node
113
- */
114
- async deleteNode(delegationId: string): Promise<void> {
115
- this.nodes.delete(delegationId);
116
- }
117
-
118
- /**
119
- * Clear all data (for testing)
120
- */
121
- clear(): void {
122
- this.nodes.clear();
123
- }
124
-
125
- /**
126
- * Get all node IDs (for testing)
127
- */
128
- getAllNodeIds(): string[] {
129
- return Array.from(this.nodes.keys());
130
- }
131
-
132
- /**
133
- * Get graph statistics (for testing/debugging)
134
- */
135
- getStats(): {
136
- totalNodes: number;
137
- rootNodes: number;
138
- leafNodes: number;
139
- maxDepth: number;
140
- } {
141
- const nodes = Array.from(this.nodes.values());
142
-
143
- const rootNodes = nodes.filter((n) => n.parentId === null).length;
144
- const leafNodes = nodes.filter((n) => n.children.length === 0).length;
145
-
146
- // Calculate max depth
147
- let maxDepth = 0;
148
- for (const node of nodes) {
149
- const chain = this.getChainSync(node.id);
150
- maxDepth = Math.max(maxDepth, chain.length - 1);
151
- }
152
-
153
- return {
154
- totalNodes: nodes.length,
155
- rootNodes,
156
- leafNodes,
157
- maxDepth,
158
- };
159
- }
160
-
161
- /**
162
- * Synchronous chain retrieval (for stats)
163
- */
164
- private getChainSync(delegationId: string): DelegationNode[] {
165
- const chain: DelegationNode[] = [];
166
- let currentId: string | null = delegationId;
167
-
168
- while (currentId) {
169
- const node = this.nodes.get(currentId);
170
- if (!node) break;
171
-
172
- chain.unshift(node);
173
- currentId = node.parentId;
174
- }
175
-
176
- return chain;
177
- }
178
- }
@@ -1,77 +0,0 @@
1
- /**
2
- * In-Memory StatusList Storage Provider
3
- *
4
- * Memory-based implementation for testing and development.
5
- * NOT suitable for production (no persistence).
6
- *
7
- * SOLID: Implements StatusListStorageProvider interface
8
- */
9
-
10
- import type { StatusList2021Credential } from '@kya-os/contracts';
11
- import type { StatusListStorageProvider } from '../statuslist-manager';
12
-
13
- /**
14
- * Memory-based StatusList storage
15
- *
16
- * Stores status lists in memory. Thread-safe index allocation.
17
- * Useful for:
18
- * - Unit tests
19
- * - Integration tests
20
- * - Development/debugging
21
- * - Examples
22
- */
23
- export class MemoryStatusListStorage implements StatusListStorageProvider {
24
- private statusLists = new Map<string, StatusList2021Credential>();
25
- private indexCounters = new Map<string, number>();
26
-
27
- /**
28
- * Get a status list credential by ID
29
- */
30
- async getStatusList(
31
- statusListId: string
32
- ): Promise<StatusList2021Credential | null> {
33
- return this.statusLists.get(statusListId) || null;
34
- }
35
-
36
- /**
37
- * Save a status list credential
38
- */
39
- async setStatusList(
40
- statusListId: string,
41
- credential: StatusList2021Credential
42
- ): Promise<void> {
43
- this.statusLists.set(statusListId, credential);
44
- }
45
-
46
- /**
47
- * Allocate a new index (thread-safe)
48
- */
49
- async allocateIndex(statusListId: string): Promise<number> {
50
- const current = this.indexCounters.get(statusListId) || 0;
51
- const allocated = current;
52
- this.indexCounters.set(statusListId, current + 1);
53
- return allocated;
54
- }
55
-
56
- /**
57
- * Get current index count (for testing)
58
- */
59
- getIndexCount(statusListId: string): number {
60
- return this.indexCounters.get(statusListId) || 0;
61
- }
62
-
63
- /**
64
- * Clear all data (for testing)
65
- */
66
- clear(): void {
67
- this.statusLists.clear();
68
- this.indexCounters.clear();
69
- }
70
-
71
- /**
72
- * Get all status list IDs (for testing)
73
- */
74
- getAllStatusListIds(): string[] {
75
- return Array.from(this.statusLists.keys());
76
- }
77
- }
@@ -1,221 +0,0 @@
1
- /**
2
- * Delegation Utilities
3
- *
4
- * Shared utility functions for delegation credential operations.
5
- * Following DRY (Don't Repeat Yourself) principle.
6
- */
7
-
8
- import { base64urlEncodeFromString } from '../utils/base64';
9
-
10
- /**
11
- * JSON canonicalization (RFC 8785)
12
- *
13
- * Creates a deterministic representation of JSON for signing.
14
- * Per W3C VC spec, canonicalization ensures identical VCs produce identical signatures.
15
- *
16
- * DRY: Single implementation shared across vc-issuer and statuslist-manager.
17
- *
18
- * @param obj - The object to canonicalize
19
- * @returns Canonical JSON string
20
- */
21
- export function canonicalizeJSON(obj: any): string {
22
- if (obj === null) return 'null';
23
- if (typeof obj === 'boolean') return obj.toString();
24
- if (typeof obj === 'number') {
25
- if (!isFinite(obj)) {
26
- throw new Error('Cannot canonicalize non-finite number');
27
- }
28
- return JSON.stringify(obj);
29
- }
30
- if (typeof obj === 'string') return JSON.stringify(obj);
31
- if (Array.isArray(obj)) {
32
- const elements = obj.map((item) => canonicalizeJSON(item));
33
- return '[' + elements.join(',') + ']';
34
- }
35
- if (typeof obj === 'object') {
36
- const keys = Object.keys(obj).sort();
37
- const pairs = keys.map((key) => {
38
- const value = canonicalizeJSON(obj[key]);
39
- return JSON.stringify(key) + ':' + value;
40
- });
41
- return '{' + pairs.join(',') + '}';
42
- }
43
- throw new Error(`Cannot canonicalize type: ${typeof obj}`);
44
- }
45
-
46
- /**
47
- * JWT Header for EdDSA (Ed25519) signed credentials
48
- */
49
- export interface VCJWTHeader {
50
- alg: 'EdDSA';
51
- typ: 'JWT';
52
- kid?: string;
53
- }
54
-
55
- /**
56
- * VC-JWT Payload structure
57
- *
58
- * Per W3C VC-JWT spec, the VC is embedded in the JWT claims.
59
- * Standard claims (iss, sub, exp, iat, jti) are derived from the VC.
60
- */
61
- export interface VCJWTPayload {
62
- /** Issuer DID (from vc.issuer) */
63
- iss: string;
64
- /** Subject DID (from vc.credentialSubject.id) */
65
- sub?: string;
66
- /** Expiration time (from vc.expirationDate) */
67
- exp?: number;
68
- /** Issued at time (from vc.issuanceDate) */
69
- iat?: number;
70
- /** JWT ID (from vc.id) */
71
- jti?: string;
72
- /** The complete VC (without proof) */
73
- vc: Record<string, unknown>;
74
- }
75
-
76
- /**
77
- * Options for encoding a VC as JWT
78
- */
79
- export interface EncodeVCAsJWTOptions {
80
- /** Key ID for the JWT header */
81
- keyId?: string;
82
- }
83
-
84
- /**
85
- * Create unsigned JWT parts (header + payload) for a VC
86
- *
87
- * Prepares the VC for signing by extracting standard claims and
88
- * encoding the header and payload as base64url strings.
89
- *
90
- * @param vc - The Verifiable Credential (without proof)
91
- * @param options - Encoding options
92
- * @returns Object with encoded parts and signing input
93
- */
94
- export function createUnsignedVCJWT(
95
- vc: Record<string, unknown>,
96
- options: EncodeVCAsJWTOptions = {}
97
- ): {
98
- header: VCJWTHeader;
99
- payload: VCJWTPayload;
100
- encodedHeader: string;
101
- encodedPayload: string;
102
- signingInput: string;
103
- } {
104
- // Create JWT header
105
- const header: VCJWTHeader = {
106
- alg: 'EdDSA',
107
- typ: 'JWT',
108
- };
109
- if (options.keyId) {
110
- header.kid = options.keyId;
111
- }
112
-
113
- // Extract standard claims from VC
114
- const issuer = typeof vc.issuer === 'string' ? vc.issuer : (vc.issuer as Record<string, unknown>)?.id as string;
115
- const subject = (vc.credentialSubject as Record<string, unknown>)?.id as string | undefined;
116
-
117
- // Parse dates to Unix timestamps
118
- let exp: number | undefined;
119
- let iat: number | undefined;
120
-
121
- if (vc.expirationDate && typeof vc.expirationDate === 'string') {
122
- exp = Math.floor(new Date(vc.expirationDate).getTime() / 1000);
123
- }
124
- if (vc.issuanceDate && typeof vc.issuanceDate === 'string') {
125
- iat = Math.floor(new Date(vc.issuanceDate).getTime() / 1000);
126
- }
127
-
128
- // Remove proof from VC for JWT payload (signature is in JWT itself)
129
- const vcWithoutProof = { ...vc };
130
- delete vcWithoutProof.proof;
131
-
132
- // Build JWT payload
133
- const payload: VCJWTPayload = {
134
- iss: issuer,
135
- vc: vcWithoutProof,
136
- };
137
-
138
- if (subject) payload.sub = subject;
139
- if (exp) payload.exp = exp;
140
- if (iat) payload.iat = iat;
141
- if (vc.id && typeof vc.id === 'string') payload.jti = vc.id;
142
-
143
- // Encode header and payload
144
- const encodedHeader = base64urlEncodeFromString(JSON.stringify(header));
145
- const encodedPayload = base64urlEncodeFromString(JSON.stringify(payload));
146
- const signingInput = `${encodedHeader}.${encodedPayload}`;
147
-
148
- return {
149
- header,
150
- payload,
151
- encodedHeader,
152
- encodedPayload,
153
- signingInput,
154
- };
155
- }
156
-
157
- /**
158
- * Complete a JWT with a signature
159
- *
160
- * Takes the signing input and a base64url-encoded signature to create the final JWT.
161
- *
162
- * @param signingInput - The header.payload string that was signed
163
- * @param signature - Base64url-encoded signature
164
- * @returns Complete JWT string (header.payload.signature)
165
- */
166
- export function completeVCJWT(signingInput: string, signature: string): string {
167
- return `${signingInput}.${signature}`;
168
- }
169
-
170
- /**
171
- * Parse a VC-JWT and extract the VC
172
- *
173
- * Does NOT verify the signature - use with a verification function.
174
- *
175
- * @param jwt - The JWT string
176
- * @returns Parsed JWT parts
177
- */
178
- export function parseVCJWT(jwt: string): {
179
- header: VCJWTHeader;
180
- payload: VCJWTPayload;
181
- signature: string;
182
- signingInput: string;
183
- } | null {
184
- const parts = jwt.split('.');
185
- if (parts.length !== 3) {
186
- return null;
187
- }
188
-
189
- try {
190
- // Decode header and payload
191
- const headerJson = base64urlDecodeToString(parts[0]);
192
- const payloadJson = base64urlDecodeToString(parts[1]);
193
-
194
- const header = JSON.parse(headerJson) as VCJWTHeader;
195
- const payload = JSON.parse(payloadJson) as VCJWTPayload;
196
-
197
- return {
198
- header,
199
- payload,
200
- signature: parts[2],
201
- signingInput: `${parts[0]}.${parts[1]}`,
202
- };
203
- } catch {
204
- return null;
205
- }
206
- }
207
-
208
- /**
209
- * Decode base64url string to string (internal helper)
210
- */
211
- function base64urlDecodeToString(input: string): string {
212
- // Add padding if needed
213
- const padded = input + '='.repeat((4 - input.length % 4) % 4);
214
- const base64 = padded.replace(/-/g, '+').replace(/_/g, '/');
215
-
216
- if (typeof atob !== 'undefined') {
217
- return atob(base64);
218
- }
219
-
220
- return Buffer.from(base64, 'base64').toString('utf-8');
221
- }
@@ -1,232 +0,0 @@
1
- /**
2
- * Delegation Credential Issuer (Platform-Agnostic)
3
- *
4
- * Issues W3C Verifiable Credentials for delegations with Ed25519 signatures.
5
- * Follows the Python POC design (Delegation-Service.md:136-163) where
6
- * delegations are issued AS W3C VCs.
7
- *
8
- * Related Spec: MCP-I §4.1, §4.2, W3C VC Data Model 1.1
9
- * Python Reference: Delegation-Service.md
10
- */
11
-
12
- import type {
13
- DelegationCredential,
14
- DelegationRecord,
15
- CredentialStatus,
16
- Proof,
17
- } from '@kya-os/contracts';
18
- import { wrapDelegationAsVC } from '@kya-os/contracts';
19
- import { canonicalizeJSON } from './utils';
20
-
21
- /**
22
- * Options for issuing a delegation credential
23
- */
24
- export interface IssueDelegationOptions {
25
- /** VC ID (optional, will be generated if not provided) */
26
- id?: string;
27
-
28
- /** Issuance date (optional, defaults to now) */
29
- issuanceDate?: string;
30
-
31
- /** Expiration date (optional, derived from constraints if not provided) */
32
- expirationDate?: string;
33
-
34
- /** Credential status for StatusList2021 (optional) */
35
- credentialStatus?: CredentialStatus;
36
-
37
- /** Additional context URIs (optional) */
38
- additionalContexts?: string[];
39
- }
40
-
41
- /**
42
- * Signing function interface
43
- *
44
- * Platform-specific implementations provide this function to sign VCs.
45
- * For example:
46
- * - Node.js: Uses jose library with importPKCS8
47
- * - Cloudflare: Uses Web Crypto API
48
- */
49
- export interface VCSigningFunction {
50
- /**
51
- * Sign a canonicalized VC
52
- *
53
- * @param canonicalVC - The canonical JSON string to sign
54
- * @param issuerDid - The DID of the issuer
55
- * @param kid - The key ID
56
- * @returns Ed25519Signature2020 proof
57
- */
58
- (canonicalVC: string, issuerDid: string, kid: string): Promise<Proof>;
59
- }
60
-
61
- /**
62
- * Identity provider interface
63
- *
64
- * Platform-specific implementations provide identity details.
65
- */
66
- export interface IdentityProvider {
67
- /** Get the DID of this identity */
68
- getDid(): string;
69
-
70
- /** Get the key ID of this identity */
71
- getKeyId(): string;
72
-
73
- /** Get the private key (base64 encoded) */
74
- getPrivateKey(): string;
75
- }
76
-
77
- /**
78
- * Delegation Credential Issuer (Platform-Agnostic)
79
- *
80
- * Issues W3C Verifiable Credentials for delegations.
81
- * Per Python POC (Delegation-Service.md:136-146):
82
- * - Every delegation MUST be issued as a VC
83
- * - VC is signed with Ed25519 (Ed25519Signature2020)
84
- * - StatusList2021 support for efficient revocation
85
- */
86
- export class DelegationCredentialIssuer {
87
- constructor(
88
- private identity: IdentityProvider,
89
- private signingFunction: VCSigningFunction
90
- ) {}
91
-
92
- /**
93
- * Issue a delegation credential
94
- *
95
- * Creates a W3C Verifiable Credential from a delegation record.
96
- * Signs it with Ed25519 and returns the complete DelegationCredential.
97
- *
98
- * @param delegation - The delegation record to issue as a VC
99
- * @param options - Issuance options
100
- * @returns Signed DelegationCredential
101
- */
102
- async issueDelegationCredential(
103
- delegation: DelegationRecord,
104
- options: IssueDelegationOptions = {}
105
- ): Promise<DelegationCredential> {
106
- // Step 1: Create unsigned VC
107
- let unsignedVC = wrapDelegationAsVC(delegation, {
108
- id: options.id,
109
- issuanceDate: options.issuanceDate,
110
- expirationDate: options.expirationDate,
111
- credentialStatus: options.credentialStatus,
112
- });
113
-
114
- // Add additional contexts if provided
115
- if (options.additionalContexts && options.additionalContexts.length > 0) {
116
- const existingContexts = unsignedVC['@context'] as Array<
117
- string | Record<string, any>
118
- >;
119
- unsignedVC = {
120
- ...unsignedVC,
121
- '@context': [...existingContexts, ...options.additionalContexts],
122
- };
123
- }
124
-
125
- // Step 2: Canonicalize VC (for signing)
126
- const canonicalVC = this.canonicalizeVC(unsignedVC);
127
-
128
- // Step 3: Sign with Ed25519 using platform-specific signing function
129
- const proof = await this.signingFunction(
130
- canonicalVC,
131
- this.identity.getDid(),
132
- this.identity.getKeyId()
133
- );
134
-
135
- // Step 4: Return signed VC
136
- return {
137
- ...unsignedVC,
138
- proof,
139
- } as DelegationCredential;
140
- }
141
-
142
- /**
143
- * Create a delegation record and issue it as a VC in one step
144
- *
145
- * Convenience method for creating a new delegation from scratch.
146
- *
147
- * @param params - Delegation parameters
148
- * @param options - Issuance options
149
- * @returns Signed DelegationCredential
150
- */
151
- async createAndIssueDelegation(
152
- params: {
153
- id: string;
154
- issuerDid: string;
155
- subjectDid: string;
156
- controller?: string;
157
- parentId?: string;
158
- constraints: DelegationRecord['constraints'];
159
- status?: DelegationRecord['status'];
160
- metadata?: Record<string, any>;
161
- },
162
- options: IssueDelegationOptions = {}
163
- ): Promise<DelegationCredential> {
164
- const now = Date.now();
165
-
166
- // Create delegation record
167
- const delegation: DelegationRecord = {
168
- id: params.id,
169
- issuerDid: params.issuerDid,
170
- subjectDid: params.subjectDid,
171
- controller: params.controller,
172
- vcId: options.id || `urn:uuid:${params.id}`,
173
- parentId: params.parentId,
174
- constraints: params.constraints,
175
- signature: '', // Will be filled by VC proof
176
- status: params.status || 'active',
177
- createdAt: now,
178
- metadata: params.metadata,
179
- };
180
-
181
- // Issue as VC
182
- return this.issueDelegationCredential(delegation, options);
183
- }
184
-
185
- /**
186
- * Canonicalize VC for signing
187
- *
188
- * Uses JCS (JSON Canonicalization Scheme, RFC 8785) to create
189
- * a deterministic representation of the VC.
190
- *
191
- * @param vc - The unsigned VC
192
- * @returns Canonical JSON string
193
- */
194
- private canonicalizeVC(vc: Omit<DelegationCredential, 'proof'>): string {
195
- // DRY: Use shared canonicalization utility
196
- return canonicalizeJSON(vc);
197
- }
198
-
199
- /**
200
- * Get issuer DID
201
- *
202
- * @returns The DID of this issuer
203
- */
204
- getIssuerDid(): string {
205
- return this.identity.getDid();
206
- }
207
-
208
- /**
209
- * Get issuer key ID
210
- *
211
- * @returns The key ID of this issuer
212
- */
213
- getIssuerKeyId(): string {
214
- return this.identity.getKeyId();
215
- }
216
- }
217
-
218
- /**
219
- * Create a delegation credential issuer
220
- *
221
- * Convenience factory function.
222
- *
223
- * @param identity - Identity provider
224
- * @param signingFunction - Platform-specific signing function
225
- * @returns DelegationCredentialIssuer instance
226
- */
227
- export function createDelegationIssuer(
228
- identity: IdentityProvider,
229
- signingFunction: VCSigningFunction
230
- ): DelegationCredentialIssuer {
231
- return new DelegationCredentialIssuer(identity, signingFunction);
232
- }