@kya-os/mcp-i-core 1.2.3-canary.7 → 1.3.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 (225) hide show
  1. package/.claude/settings.local.json +9 -0
  2. package/.turbo/turbo-build.log +4 -0
  3. package/.turbo/turbo-test.log +2979 -0
  4. package/COMPLIANCE_IMPROVEMENT_REPORT.md +483 -0
  5. package/Composer 3.md +615 -0
  6. package/GPT-5.md +1169 -0
  7. package/OPUS-plan.md +352 -0
  8. package/PHASE_3_AND_4.1_SUMMARY.md +585 -0
  9. package/PHASE_3_SUMMARY.md +317 -0
  10. package/PHASE_4.1.3_SUMMARY.md +428 -0
  11. package/PHASE_4.1_COMPLETE.md +525 -0
  12. package/PHASE_4_USER_DID_IDENTITY_LINKING_PLAN.md +1240 -0
  13. package/SCHEMA_COMPLIANCE_REPORT.md +275 -0
  14. package/TEST_PLAN.md +571 -0
  15. package/coverage/coverage-final.json +57 -0
  16. package/dist/__tests__/utils/mock-providers.d.ts +1 -2
  17. package/dist/__tests__/utils/mock-providers.d.ts.map +1 -1
  18. package/dist/__tests__/utils/mock-providers.js.map +1 -1
  19. package/dist/cache/oauth-config-cache.d.ts +69 -0
  20. package/dist/cache/oauth-config-cache.d.ts.map +1 -0
  21. package/dist/cache/oauth-config-cache.js +76 -0
  22. package/dist/cache/oauth-config-cache.js.map +1 -0
  23. package/dist/identity/idp-token-resolver.d.ts +53 -0
  24. package/dist/identity/idp-token-resolver.d.ts.map +1 -0
  25. package/dist/identity/idp-token-resolver.js +108 -0
  26. package/dist/identity/idp-token-resolver.js.map +1 -0
  27. package/dist/identity/idp-token-storage.interface.d.ts +42 -0
  28. package/dist/identity/idp-token-storage.interface.d.ts.map +1 -0
  29. package/dist/identity/idp-token-storage.interface.js +12 -0
  30. package/dist/identity/idp-token-storage.interface.js.map +1 -0
  31. package/dist/identity/user-did-manager.d.ts +39 -1
  32. package/dist/identity/user-did-manager.d.ts.map +1 -1
  33. package/dist/identity/user-did-manager.js +69 -3
  34. package/dist/identity/user-did-manager.js.map +1 -1
  35. package/dist/index.d.ts +22 -0
  36. package/dist/index.d.ts.map +1 -1
  37. package/dist/index.js +39 -1
  38. package/dist/index.js.map +1 -1
  39. package/dist/runtime/audit-logger.d.ts +37 -0
  40. package/dist/runtime/audit-logger.d.ts.map +1 -0
  41. package/dist/runtime/audit-logger.js +9 -0
  42. package/dist/runtime/audit-logger.js.map +1 -0
  43. package/dist/runtime/base.d.ts +58 -2
  44. package/dist/runtime/base.d.ts.map +1 -1
  45. package/dist/runtime/base.js +266 -11
  46. package/dist/runtime/base.js.map +1 -1
  47. package/dist/services/access-control.service.d.ts.map +1 -1
  48. package/dist/services/access-control.service.js +200 -35
  49. package/dist/services/access-control.service.js.map +1 -1
  50. package/dist/services/authorization/authorization-registry.d.ts +29 -0
  51. package/dist/services/authorization/authorization-registry.d.ts.map +1 -0
  52. package/dist/services/authorization/authorization-registry.js +57 -0
  53. package/dist/services/authorization/authorization-registry.js.map +1 -0
  54. package/dist/services/authorization/types.d.ts +53 -0
  55. package/dist/services/authorization/types.d.ts.map +1 -0
  56. package/dist/services/authorization/types.js +10 -0
  57. package/dist/services/authorization/types.js.map +1 -0
  58. package/dist/services/batch-delegation.service.d.ts +53 -0
  59. package/dist/services/batch-delegation.service.d.ts.map +1 -0
  60. package/dist/services/batch-delegation.service.js +95 -0
  61. package/dist/services/batch-delegation.service.js.map +1 -0
  62. package/dist/services/oauth-config.service.d.ts +53 -0
  63. package/dist/services/oauth-config.service.d.ts.map +1 -0
  64. package/dist/services/oauth-config.service.js +119 -0
  65. package/dist/services/oauth-config.service.js.map +1 -0
  66. package/dist/services/oauth-provider-registry.d.ts +88 -0
  67. package/dist/services/oauth-provider-registry.d.ts.map +1 -0
  68. package/dist/services/oauth-provider-registry.js +128 -0
  69. package/dist/services/oauth-provider-registry.js.map +1 -0
  70. package/dist/services/oauth-service.d.ts +77 -0
  71. package/dist/services/oauth-service.d.ts.map +1 -0
  72. package/dist/services/oauth-service.js +348 -0
  73. package/dist/services/oauth-service.js.map +1 -0
  74. package/dist/services/oauth-token-retrieval.service.d.ts +49 -0
  75. package/dist/services/oauth-token-retrieval.service.d.ts.map +1 -0
  76. package/dist/services/oauth-token-retrieval.service.js +150 -0
  77. package/dist/services/oauth-token-retrieval.service.js.map +1 -0
  78. package/dist/services/provider-resolver.d.ts +48 -0
  79. package/dist/services/provider-resolver.d.ts.map +1 -0
  80. package/dist/services/provider-resolver.js +121 -0
  81. package/dist/services/provider-resolver.js.map +1 -0
  82. package/dist/services/provider-validator.d.ts +55 -0
  83. package/dist/services/provider-validator.d.ts.map +1 -0
  84. package/dist/services/provider-validator.js +135 -0
  85. package/dist/services/provider-validator.js.map +1 -0
  86. package/dist/services/tool-context-builder.d.ts +57 -0
  87. package/dist/services/tool-context-builder.d.ts.map +1 -0
  88. package/dist/services/tool-context-builder.js +125 -0
  89. package/dist/services/tool-context-builder.js.map +1 -0
  90. package/dist/services/tool-protection.service.d.ts +87 -10
  91. package/dist/services/tool-protection.service.d.ts.map +1 -1
  92. package/dist/services/tool-protection.service.js +282 -112
  93. package/dist/services/tool-protection.service.js.map +1 -1
  94. package/dist/types/oauth-required-error.d.ts +40 -0
  95. package/dist/types/oauth-required-error.d.ts.map +1 -0
  96. package/dist/types/oauth-required-error.js +40 -0
  97. package/dist/types/oauth-required-error.js.map +1 -0
  98. package/dist/utils/did-helpers.d.ts +33 -0
  99. package/dist/utils/did-helpers.d.ts.map +1 -1
  100. package/dist/utils/did-helpers.js +40 -0
  101. package/dist/utils/did-helpers.js.map +1 -1
  102. package/dist/utils/index.d.ts +1 -0
  103. package/dist/utils/index.d.ts.map +1 -1
  104. package/dist/utils/index.js +1 -0
  105. package/dist/utils/index.js.map +1 -1
  106. package/docs/API_REFERENCE.md +1362 -0
  107. package/docs/COMPLIANCE_MATRIX.md +691 -0
  108. package/docs/STATUSLIST2021_GUIDE.md +696 -0
  109. package/docs/W3C_VC_DELEGATION_GUIDE.md +710 -0
  110. package/package.json +24 -50
  111. package/scripts/audit-compliance.ts +724 -0
  112. package/src/__tests__/cache/tool-protection-cache.test.ts +640 -0
  113. package/src/__tests__/config/provider-runtime-config.test.ts +309 -0
  114. package/src/__tests__/delegation-e2e.test.ts +690 -0
  115. package/src/__tests__/identity/user-did-manager.test.ts +213 -0
  116. package/src/__tests__/index.test.ts +56 -0
  117. package/src/__tests__/integration/full-flow.test.ts +776 -0
  118. package/src/__tests__/integration.test.ts +281 -0
  119. package/src/__tests__/providers/base.test.ts +173 -0
  120. package/src/__tests__/providers/memory.test.ts +319 -0
  121. package/src/__tests__/regression/phase2-regression.test.ts +429 -0
  122. package/src/__tests__/runtime/audit-logger.test.ts +154 -0
  123. package/src/__tests__/runtime/base-extensions.test.ts +593 -0
  124. package/src/__tests__/runtime/base.test.ts +869 -0
  125. package/src/__tests__/runtime/delegation-flow.test.ts +164 -0
  126. package/src/__tests__/runtime/proof-client-did.test.ts +375 -0
  127. package/src/__tests__/runtime/route-interception.test.ts +686 -0
  128. package/src/__tests__/runtime/tool-protection-enforcement.test.ts +908 -0
  129. package/src/__tests__/services/agentshield-integration.test.ts +784 -0
  130. package/src/__tests__/services/provider-resolver-edge-cases.test.ts +591 -0
  131. package/src/__tests__/services/tool-protection-oauth-provider.test.ts +480 -0
  132. package/src/__tests__/services/tool-protection.service.test.ts +1366 -0
  133. package/src/__tests__/utils/mock-providers.ts +340 -0
  134. package/src/cache/oauth-config-cache.d.ts +69 -0
  135. package/src/cache/oauth-config-cache.d.ts.map +1 -0
  136. package/src/cache/oauth-config-cache.js.map +1 -0
  137. package/src/cache/oauth-config-cache.ts +123 -0
  138. package/src/cache/tool-protection-cache.ts +171 -0
  139. package/src/compliance/EXAMPLE.md +412 -0
  140. package/src/compliance/__tests__/schema-verifier.test.ts +797 -0
  141. package/src/compliance/index.ts +8 -0
  142. package/src/compliance/schema-registry.ts +460 -0
  143. package/src/compliance/schema-verifier.ts +708 -0
  144. package/src/config/__tests__/remote-config.spec.ts +268 -0
  145. package/src/config/remote-config.ts +174 -0
  146. package/src/config.ts +309 -0
  147. package/src/delegation/__tests__/audience-validator.test.ts +112 -0
  148. package/src/delegation/__tests__/bitstring.test.ts +346 -0
  149. package/src/delegation/__tests__/cascading-revocation.test.ts +628 -0
  150. package/src/delegation/__tests__/delegation-graph.test.ts +584 -0
  151. package/src/delegation/__tests__/utils.test.ts +152 -0
  152. package/src/delegation/__tests__/vc-issuer.test.ts +442 -0
  153. package/src/delegation/__tests__/vc-verifier.test.ts +922 -0
  154. package/src/delegation/audience-validator.ts +52 -0
  155. package/src/delegation/bitstring.ts +278 -0
  156. package/src/delegation/cascading-revocation.ts +370 -0
  157. package/src/delegation/delegation-graph.ts +299 -0
  158. package/src/delegation/index.ts +14 -0
  159. package/src/delegation/statuslist-manager.ts +353 -0
  160. package/src/delegation/storage/__tests__/memory-graph-storage.test.ts +366 -0
  161. package/src/delegation/storage/__tests__/memory-statuslist-storage.test.ts +228 -0
  162. package/src/delegation/storage/index.ts +9 -0
  163. package/src/delegation/storage/memory-graph-storage.ts +178 -0
  164. package/src/delegation/storage/memory-statuslist-storage.ts +77 -0
  165. package/src/delegation/utils.ts +42 -0
  166. package/src/delegation/vc-issuer.ts +232 -0
  167. package/src/delegation/vc-verifier.ts +568 -0
  168. package/src/identity/idp-token-resolver.ts +147 -0
  169. package/src/identity/idp-token-storage.interface.ts +59 -0
  170. package/src/identity/user-did-manager.ts +370 -0
  171. package/src/index.ts +260 -0
  172. package/src/providers/base.d.ts +91 -0
  173. package/src/providers/base.d.ts.map +1 -0
  174. package/src/providers/base.js.map +1 -0
  175. package/src/providers/base.ts +96 -0
  176. package/src/providers/memory.ts +142 -0
  177. package/src/runtime/audit-logger.ts +39 -0
  178. package/src/runtime/base.ts +1329 -0
  179. package/src/services/__tests__/access-control.integration.test.ts +443 -0
  180. package/src/services/__tests__/access-control.proof-response-validation.test.ts +578 -0
  181. package/src/services/__tests__/access-control.service.test.ts +970 -0
  182. package/src/services/__tests__/batch-delegation.service.test.ts +351 -0
  183. package/src/services/__tests__/crypto.service.test.ts +531 -0
  184. package/src/services/__tests__/oauth-provider-registry.test.ts +142 -0
  185. package/src/services/__tests__/proof-verifier.integration.test.ts +485 -0
  186. package/src/services/__tests__/proof-verifier.test.ts +489 -0
  187. package/src/services/__tests__/provider-resolution.integration.test.ts +202 -0
  188. package/src/services/__tests__/provider-resolver.test.ts +213 -0
  189. package/src/services/__tests__/storage.service.test.ts +358 -0
  190. package/src/services/access-control.service.ts +990 -0
  191. package/src/services/authorization/authorization-registry.ts +66 -0
  192. package/src/services/authorization/types.ts +71 -0
  193. package/src/services/batch-delegation.service.ts +137 -0
  194. package/src/services/crypto.service.ts +302 -0
  195. package/src/services/errors.ts +76 -0
  196. package/src/services/index.ts +9 -0
  197. package/src/services/oauth-config.service.d.ts +53 -0
  198. package/src/services/oauth-config.service.d.ts.map +1 -0
  199. package/src/services/oauth-config.service.js.map +1 -0
  200. package/src/services/oauth-config.service.ts +169 -0
  201. package/src/services/oauth-provider-registry.d.ts +57 -0
  202. package/src/services/oauth-provider-registry.d.ts.map +1 -0
  203. package/src/services/oauth-provider-registry.js.map +1 -0
  204. package/src/services/oauth-provider-registry.ts +141 -0
  205. package/src/services/oauth-service.ts +510 -0
  206. package/src/services/oauth-token-retrieval.service.ts +245 -0
  207. package/src/services/proof-verifier.ts +478 -0
  208. package/src/services/provider-resolver.d.ts +48 -0
  209. package/src/services/provider-resolver.d.ts.map +1 -0
  210. package/src/services/provider-resolver.js.map +1 -0
  211. package/src/services/provider-resolver.ts +146 -0
  212. package/src/services/provider-validator.ts +170 -0
  213. package/src/services/storage.service.ts +566 -0
  214. package/src/services/tool-context-builder.ts +172 -0
  215. package/src/services/tool-protection.service.ts +958 -0
  216. package/src/types/oauth-required-error.ts +63 -0
  217. package/src/types/tool-protection.ts +155 -0
  218. package/src/utils/__tests__/did-helpers.test.ts +101 -0
  219. package/src/utils/base64.ts +148 -0
  220. package/src/utils/cors.ts +83 -0
  221. package/src/utils/did-helpers.ts +150 -0
  222. package/src/utils/index.ts +8 -0
  223. package/src/utils/storage-keys.ts +278 -0
  224. package/tsconfig.json +21 -0
  225. package/vitest.config.ts +56 -0
@@ -0,0 +1,340 @@
1
+ /**
2
+ * Mock Providers for Testing
3
+ *
4
+ * These mock implementations allow controlled testing of the runtime
5
+ * and other components that depend on providers.
6
+ */
7
+
8
+ import { vi } from 'vitest';
9
+ import {
10
+ CryptoProvider,
11
+ ClockProvider,
12
+ FetchProvider,
13
+ StorageProvider,
14
+ NonceCacheProvider,
15
+ IdentityProvider,
16
+ AgentIdentity
17
+ } from '../../providers/base';
18
+
19
+ /**
20
+ * Mock Crypto Provider
21
+ */
22
+ export class MockCryptoProvider extends CryptoProvider {
23
+ async sign(data: Uint8Array, privateKey: string): Promise<Uint8Array> {
24
+ // Simple mock signature
25
+ return new Uint8Array([1, 2, 3, 4, 5]);
26
+ }
27
+
28
+ async verify(data: Uint8Array, signature: Uint8Array, publicKey: string): Promise<boolean> {
29
+ // Simple verification - check signature matches expected pattern
30
+ // In real implementation, this would verify cryptographic signature
31
+ return signature.length === 5 && signature[0] === 1 && signature[1] === 2;
32
+ }
33
+
34
+ async generateKeyPair(): Promise<{ privateKey: string; publicKey: string }> {
35
+ // Generate valid base64-encoded keys for testing
36
+ // Ed25519 keys are 32 bytes, so we generate 32 random bytes and encode them
37
+ const randomBytes = new Uint8Array(32);
38
+ for (let i = 0; i < 32; i++) {
39
+ randomBytes[i] = Math.floor(Math.random() * 256);
40
+ }
41
+
42
+ // Convert to base64 using Node.js Buffer (works in Node.js test environment)
43
+ const base64PrivateKey = Buffer.from(randomBytes).toString('base64');
44
+
45
+ // Generate a different public key (Ed25519 public key is also 32 bytes)
46
+ const publicBytes = new Uint8Array(32);
47
+ for (let i = 0; i < 32; i++) {
48
+ publicBytes[i] = Math.floor(Math.random() * 256);
49
+ }
50
+ const base64PublicKey = Buffer.from(publicBytes).toString('base64');
51
+
52
+ return {
53
+ privateKey: base64PrivateKey,
54
+ publicKey: base64PublicKey
55
+ };
56
+ }
57
+
58
+ async hash(data: Uint8Array): Promise<Uint8Array> {
59
+ // Simple mock hash
60
+ return new Uint8Array([9, 8, 7, 6, 5]);
61
+ }
62
+
63
+ async randomBytes(length: number): Promise<Uint8Array> {
64
+ const bytes = new Uint8Array(length);
65
+ for (let i = 0; i < length; i++) {
66
+ bytes[i] = Math.floor(Math.random() * 256);
67
+ }
68
+ return bytes;
69
+ }
70
+ }
71
+
72
+ /**
73
+ * Mock Clock Provider
74
+ */
75
+ export class MockClockProvider extends ClockProvider {
76
+ private currentTime: number = Date.now();
77
+
78
+ setTime(timestamp: number): void {
79
+ this.currentTime = timestamp;
80
+ }
81
+
82
+ now(): number {
83
+ return this.currentTime;
84
+ }
85
+
86
+ isWithinSkew(timestamp: number, skewSeconds: number): boolean {
87
+ const diff = Math.abs(this.currentTime - timestamp);
88
+ return diff <= skewSeconds * 1000;
89
+ }
90
+
91
+ hasExpired(expiresAt: number): boolean {
92
+ return this.currentTime > expiresAt;
93
+ }
94
+
95
+ calculateExpiry(ttlSeconds: number): number {
96
+ return this.currentTime + (ttlSeconds * 1000);
97
+ }
98
+
99
+ format(timestamp: number): string {
100
+ return new Date(timestamp).toISOString();
101
+ }
102
+ }
103
+
104
+ /**
105
+ * Mock Fetch Provider
106
+ */
107
+ export class MockFetchProvider extends FetchProvider {
108
+ private didDocuments: Map<string, any> = new Map();
109
+ private statusLists: Map<string, any> = new Map();
110
+ private delegationChains: Map<string, any[]> = new Map();
111
+ public fetch: (url: string, options?: any) => Promise<Response>;
112
+
113
+ constructor() {
114
+ super();
115
+ // Initialize fetch as a vi.fn() in constructor to ensure it's always mockable
116
+ this.fetch = vi.fn(async (url: string, options?: any): Promise<Response> => {
117
+ // Simple mock Response
118
+ return new Response(JSON.stringify({ url, options }), {
119
+ status: 200,
120
+ headers: { 'Content-Type': 'application/json' }
121
+ });
122
+ }) as any;
123
+ }
124
+
125
+ // Test helpers
126
+ setDIDDocument(did: string, doc: any): void {
127
+ this.didDocuments.set(did, doc);
128
+ }
129
+
130
+ setStatusList(url: string, list: any): void {
131
+ this.statusLists.set(url, list);
132
+ }
133
+
134
+ setDelegationChain(id: string, chain: any[]): void {
135
+ this.delegationChains.set(id, chain);
136
+ }
137
+
138
+ async resolveDID(did: string): Promise<any> {
139
+ const doc = this.didDocuments.get(did);
140
+ if (!doc) {
141
+ throw new Error(`DID ${did} not found`);
142
+ }
143
+ return doc;
144
+ }
145
+
146
+ async fetchStatusList(url: string): Promise<any> {
147
+ const list = this.statusLists.get(url);
148
+ if (!list) {
149
+ throw new Error(`Status list ${url} not found`);
150
+ }
151
+ return list;
152
+ }
153
+
154
+ async fetchDelegationChain(id: string): Promise<any[]> {
155
+ const chain = this.delegationChains.get(id);
156
+ if (!chain) {
157
+ throw new Error(`Delegation chain ${id} not found`);
158
+ }
159
+ return chain;
160
+ }
161
+ }
162
+
163
+ /**
164
+ * Mock Storage Provider
165
+ */
166
+ export class MockStorageProvider extends StorageProvider {
167
+ private store: Map<string, string> = new Map();
168
+
169
+ async get(key: string): Promise<string | null> {
170
+ return this.store.get(key) || null;
171
+ }
172
+
173
+ async set(key: string, value: string): Promise<void> {
174
+ this.store.set(key, value);
175
+ }
176
+
177
+ async delete(key: string): Promise<void> {
178
+ this.store.delete(key);
179
+ }
180
+
181
+ async exists(key: string): Promise<boolean> {
182
+ return this.store.has(key);
183
+ }
184
+
185
+ async list(prefix?: string): Promise<string[]> {
186
+ const keys = Array.from(this.store.keys());
187
+ if (prefix) {
188
+ return keys.filter(k => k.startsWith(prefix));
189
+ }
190
+ return keys;
191
+ }
192
+
193
+ // Test helper
194
+ clear(): void {
195
+ this.store.clear();
196
+ }
197
+ }
198
+
199
+ /**
200
+ * Mock Nonce Cache Provider
201
+ */
202
+ export class MockNonceCacheProvider extends NonceCacheProvider {
203
+ private nonces: Map<string, number> = new Map();
204
+ public cleanupCalled = false;
205
+ public destroyCalled = false;
206
+ private clock?: ClockProvider;
207
+
208
+ setClock(clock: ClockProvider): void {
209
+ this.clock = clock;
210
+ }
211
+
212
+ async has(nonce: string, agentDid?: string): Promise<boolean> {
213
+ const key = agentDid ? `nonce:${agentDid}:${nonce}` : `nonce:${nonce}`;
214
+ const expiry = this.nonces.get(key);
215
+ if (!expiry) return false;
216
+
217
+ const now = this.clock ? this.clock.now() : Date.now();
218
+ if (now > expiry) {
219
+ this.nonces.delete(key);
220
+ return false;
221
+ }
222
+
223
+ return true;
224
+ }
225
+
226
+ async add(nonce: string, ttlSeconds: number, agentDid?: string): Promise<void> {
227
+ const key = agentDid ? `nonce:${agentDid}:${nonce}` : `nonce:${nonce}`;
228
+ // Convert TTL seconds to absolute expiration timestamp for storage
229
+ const now = this.clock ? this.clock.now() : Date.now();
230
+ const expiresAt = now + (ttlSeconds * 1000);
231
+ this.nonces.set(key, expiresAt);
232
+ }
233
+
234
+ async cleanup(): Promise<void> {
235
+ this.cleanupCalled = true;
236
+ const now = this.clock ? this.clock.now() : Date.now();
237
+ for (const [nonce, expiry] of this.nonces) {
238
+ if (now > expiry) {
239
+ this.nonces.delete(nonce);
240
+ }
241
+ }
242
+ }
243
+
244
+ async destroy(): Promise<void> {
245
+ this.destroyCalled = true;
246
+ this.nonces.clear();
247
+ }
248
+
249
+ // Test helper
250
+ clear(): void {
251
+ this.nonces.clear();
252
+ }
253
+
254
+ size(): number {
255
+ return this.nonces.size;
256
+ }
257
+ }
258
+
259
+ /**
260
+ * Mock Identity Provider
261
+ */
262
+ export class MockIdentityProvider extends IdentityProvider {
263
+ private identity?: AgentIdentity;
264
+ public rotateKeysCalled = false;
265
+ public deleteIdentityCalled = false;
266
+ private rotateCount = 0;
267
+
268
+ constructor(identity?: AgentIdentity) {
269
+ super();
270
+ this.identity = identity;
271
+ }
272
+
273
+ async getIdentity(): Promise<AgentIdentity> {
274
+ if (!this.identity) {
275
+ this.identity = {
276
+ did: 'did:key:zmock123',
277
+ kid: 'did:key:zmock123#key-1',
278
+ privateKey: 'mock-private-key',
279
+ publicKey: 'mock-public-key',
280
+ createdAt: new Date().toISOString(),
281
+ type: 'development',
282
+ metadata: { mock: true }
283
+ };
284
+ }
285
+ return this.identity;
286
+ }
287
+
288
+ async saveIdentity(identity: AgentIdentity): Promise<void> {
289
+ this.identity = identity;
290
+ }
291
+
292
+ async rotateKeys(): Promise<AgentIdentity> {
293
+ this.rotateKeysCalled = true;
294
+ this.rotateCount++;
295
+ this.identity = {
296
+ did: `did:key:zmock456-${this.rotateCount}`,
297
+ kid: `did:key:zmock456-${this.rotateCount}#key-1`,
298
+ privateKey: `mock-private-key-rotated-${this.rotateCount}`,
299
+ publicKey: `mock-public-key-rotated-${this.rotateCount}`,
300
+ createdAt: new Date().toISOString(),
301
+ type: 'development',
302
+ metadata: { mock: true, rotated: true, rotateCount: this.rotateCount }
303
+ };
304
+ return this.identity;
305
+ }
306
+
307
+ async deleteIdentity(): Promise<void> {
308
+ this.deleteIdentityCalled = true;
309
+ this.identity = undefined;
310
+ }
311
+
312
+ // Test helper
313
+ setIdentity(identity: AgentIdentity): void {
314
+ this.identity = identity;
315
+ }
316
+ }
317
+
318
+ /**
319
+ * Create a full set of mock providers for testing
320
+ */
321
+ export function createMockProviders() {
322
+ const cryptoProvider = new MockCryptoProvider();
323
+ const clockProvider = new MockClockProvider();
324
+ const fetchProvider = new MockFetchProvider();
325
+ const storageProvider = new MockStorageProvider();
326
+ const nonceCacheProvider = new MockNonceCacheProvider();
327
+ const identityProvider = new MockIdentityProvider();
328
+
329
+ // Connect nonce cache to clock for consistent time
330
+ nonceCacheProvider.setClock(clockProvider);
331
+
332
+ return {
333
+ cryptoProvider,
334
+ clockProvider,
335
+ fetchProvider,
336
+ storageProvider,
337
+ nonceCacheProvider,
338
+ identityProvider
339
+ };
340
+ }
@@ -0,0 +1,69 @@
1
+ /**
2
+ * Platform-agnostic cache interface for OAuth provider configurations
3
+ *
4
+ * This interface allows different runtime adapters to provide their own
5
+ * caching implementations (e.g., in-memory for Node.js, KV for Cloudflare)
6
+ *
7
+ * @package @kya-os/mcp-i-core
8
+ */
9
+ import type { OAuthConfig } from "@kya-os/contracts/config";
10
+ /**
11
+ * Cache interface for storing and retrieving OAuth provider configurations
12
+ */
13
+ export interface OAuthConfigCache {
14
+ /**
15
+ * Retrieve a cached OAuth configuration
16
+ * @param key Cache key (typically projectId)
17
+ * @returns Cached config or null if not found/expired
18
+ */
19
+ get(key: string): Promise<OAuthConfig | null>;
20
+ /**
21
+ * Store an OAuth configuration in cache
22
+ * @param key Cache key (typically projectId)
23
+ * @param value OAuth configuration to cache
24
+ * @param ttl Time-to-live in milliseconds
25
+ */
26
+ set(key: string, value: OAuthConfig, ttl: number): Promise<void>;
27
+ /**
28
+ * Clear all cached entries
29
+ */
30
+ clear(): Promise<void>;
31
+ /**
32
+ * Remove a specific cache entry
33
+ * @param key Cache key to remove
34
+ */
35
+ delete(key: string): Promise<void>;
36
+ }
37
+ /**
38
+ * In-memory cache implementation
39
+ *
40
+ * Suitable for:
41
+ * - Node.js runtimes
42
+ * - Development/testing
43
+ * - Single-instance deployments
44
+ *
45
+ * NOT suitable for:
46
+ * - Multi-instance deployments (cache not shared)
47
+ * - Serverless environments (state not persisted)
48
+ */
49
+ export declare class InMemoryOAuthConfigCache implements OAuthConfigCache {
50
+ private cache;
51
+ get(key: string): Promise<OAuthConfig | null>;
52
+ set(key: string, value: OAuthConfig, ttl: number): Promise<void>;
53
+ clear(): Promise<void>;
54
+ delete(key: string): Promise<void>;
55
+ }
56
+ /**
57
+ * No-op cache implementation (disables caching)
58
+ *
59
+ * Use when:
60
+ * - You want to disable caching entirely
61
+ * - Testing scenarios that require fresh data
62
+ */
63
+ export declare class NoOpOAuthConfigCache implements OAuthConfigCache {
64
+ get(_key: string): Promise<OAuthConfig | null>;
65
+ set(_key: string, _value: OAuthConfig, _ttl: number): Promise<void>;
66
+ clear(): Promise<void>;
67
+ delete(_key: string): Promise<void>;
68
+ }
69
+ //# sourceMappingURL=o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CAAC,GAAG,CAAC,CAAC;IACzB,CAAC;CACF;AAED;;;;;;GAMG;AACH,MAAM,OAAO,oBAAoB;IAC/B,KAAK,CAAC,GAAG,CAAC,IAAY;QACpB,OAAO,IAAI,CAAC;IACd,CAAC;IAED,KAAK,CAAC,GAAG,CACP,IAAY,EACZ,MAAmB,EACnB,IAAY;QAEZ,QAAQ;IACV,CAAC;IAED,KAAK,CAAC,KAAK;QACT,QAAQ;IACV,CAAC;IAED,KAAK,CAAC,MAAM,CAAC,IAAY;QACvB,QAAQ;IACV,CAAC;CACF"}
@@ -0,0 +1,123 @@
1
+ /**
2
+ * Platform-agnostic cache interface for OAuth provider configurations
3
+ *
4
+ * This interface allows different runtime adapters to provide their own
5
+ * caching implementations (e.g., in-memory for Node.js, KV for Cloudflare)
6
+ *
7
+ * @package @kya-os/mcp-i-core
8
+ */
9
+
10
+ import type { OAuthConfig } from "@kya-os/contracts/config";
11
+
12
+ /**
13
+ * Cache interface for storing and retrieving OAuth provider configurations
14
+ */
15
+ export interface OAuthConfigCache {
16
+ /**
17
+ * Retrieve a cached OAuth configuration
18
+ * @param key Cache key (typically projectId)
19
+ * @returns Cached config or null if not found/expired
20
+ */
21
+ get(key: string): Promise<OAuthConfig | null>;
22
+
23
+ /**
24
+ * Store an OAuth configuration in cache
25
+ * @param key Cache key (typically projectId)
26
+ * @param value OAuth configuration to cache
27
+ * @param ttl Time-to-live in milliseconds
28
+ */
29
+ set(key: string, value: OAuthConfig, ttl: number): Promise<void>;
30
+
31
+ /**
32
+ * Clear all cached entries
33
+ */
34
+ clear(): Promise<void>;
35
+
36
+ /**
37
+ * Remove a specific cache entry
38
+ * @param key Cache key to remove
39
+ */
40
+ delete(key: string): Promise<void>;
41
+ }
42
+
43
+ /**
44
+ * In-memory cache implementation
45
+ *
46
+ * Suitable for:
47
+ * - Node.js runtimes
48
+ * - Development/testing
49
+ * - Single-instance deployments
50
+ *
51
+ * NOT suitable for:
52
+ * - Multi-instance deployments (cache not shared)
53
+ * - Serverless environments (state not persisted)
54
+ */
55
+ export class InMemoryOAuthConfigCache implements OAuthConfigCache {
56
+ private cache = new Map<
57
+ string,
58
+ { value: OAuthConfig; expiresAt: number }
59
+ >();
60
+
61
+ async get(key: string): Promise<OAuthConfig | null> {
62
+ const entry = this.cache.get(key);
63
+
64
+ if (!entry) {
65
+ return null;
66
+ }
67
+
68
+ // Check if expired
69
+ if (Date.now() > entry.expiresAt) {
70
+ this.cache.delete(key);
71
+ return null;
72
+ }
73
+
74
+ return entry.value;
75
+ }
76
+
77
+ async set(key: string, value: OAuthConfig, ttl: number): Promise<void> {
78
+ // If TTL is <= 0, don't store (entry would be immediately expired)
79
+ if (ttl <= 0) {
80
+ return;
81
+ }
82
+ const expiresAt = Date.now() + ttl;
83
+ this.cache.set(key, { value, expiresAt });
84
+ }
85
+
86
+ async clear(): Promise<void> {
87
+ this.cache.clear();
88
+ }
89
+
90
+ async delete(key: string): Promise<void> {
91
+ this.cache.delete(key);
92
+ }
93
+ }
94
+
95
+ /**
96
+ * No-op cache implementation (disables caching)
97
+ *
98
+ * Use when:
99
+ * - You want to disable caching entirely
100
+ * - Testing scenarios that require fresh data
101
+ */
102
+ export class NoOpOAuthConfigCache implements OAuthConfigCache {
103
+ async get(_key: string): Promise<OAuthConfig | null> {
104
+ return null;
105
+ }
106
+
107
+ async set(
108
+ _key: string,
109
+ _value: OAuthConfig,
110
+ _ttl: number
111
+ ): Promise<void> {
112
+ // No-op
113
+ }
114
+
115
+ async clear(): Promise<void> {
116
+ // No-op
117
+ }
118
+
119
+ async delete(_key: string): Promise<void> {
120
+ // No-op
121
+ }
122
+ }
123
+
@@ -0,0 +1,171 @@
1
+ /**
2
+ * Platform-agnostic cache interface for tool protection configurations
3
+ *
4
+ * This interface allows different runtime adapters to provide their own
5
+ * caching implementations (e.g., in-memory for Node.js, KV for CloudFlare)
6
+ *
7
+ * @package @kya-os/mcp-i-core
8
+ */
9
+
10
+ import type { ToolProtectionConfig } from '../types/tool-protection.js';
11
+
12
+ /**
13
+ * Cache interface for storing and retrieving tool protection configurations
14
+ */
15
+ export interface ToolProtectionCache {
16
+ /**
17
+ * Retrieve a cached tool protection configuration
18
+ * @param key Cache key (typically projectId)
19
+ * @returns Cached config or null if not found/expired
20
+ */
21
+ get(key: string): Promise<ToolProtectionConfig | null>;
22
+
23
+ /**
24
+ * Store a tool protection configuration in cache
25
+ * @param key Cache key (typically projectId)
26
+ * @param value Tool protection configuration to cache
27
+ * @param ttl Time-to-live in milliseconds
28
+ */
29
+ set(key: string, value: ToolProtectionConfig, ttl: number): Promise<void>;
30
+
31
+ /**
32
+ * Clear all cached entries
33
+ */
34
+ clear(): Promise<void>;
35
+
36
+ /**
37
+ * Remove a specific cache entry
38
+ * @param key Cache key to remove
39
+ */
40
+ delete(key: string): Promise<void>;
41
+ }
42
+
43
+ /**
44
+ * In-memory cache implementation
45
+ *
46
+ * Suitable for:
47
+ * - Node.js runtimes
48
+ * - Development/testing
49
+ * - Single-instance deployments
50
+ *
51
+ * NOT suitable for:
52
+ * - Multi-instance deployments (cache not shared)
53
+ * - Serverless environments (state not persisted)
54
+ */
55
+ export class InMemoryToolProtectionCache implements ToolProtectionCache {
56
+ private cache = new Map<
57
+ string,
58
+ { value: ToolProtectionConfig; expiresAt: number }
59
+ >();
60
+
61
+ async get(key: string): Promise<ToolProtectionConfig | null> {
62
+ const entry = this.cache.get(key);
63
+
64
+ if (!entry) {
65
+ return null;
66
+ }
67
+
68
+ // Check if expired
69
+ if (Date.now() > entry.expiresAt) {
70
+ this.cache.delete(key);
71
+ return null;
72
+ }
73
+
74
+ return entry.value;
75
+ }
76
+
77
+ async set(
78
+ key: string,
79
+ value: ToolProtectionConfig,
80
+ ttl: number
81
+ ): Promise<void> {
82
+ // If TTL is <= 0, don't store (entry would be immediately expired)
83
+ if (ttl <= 0) {
84
+ return;
85
+ }
86
+ const expiresAt = Date.now() + ttl;
87
+ this.cache.set(key, { value, expiresAt });
88
+ }
89
+
90
+ async clear(): Promise<void> {
91
+ this.cache.clear();
92
+ }
93
+
94
+ async delete(key: string): Promise<void> {
95
+ this.cache.delete(key);
96
+ }
97
+
98
+ /**
99
+ * Clean up expired entries (call periodically)
100
+ */
101
+ cleanup(): void {
102
+ const now = Date.now();
103
+ for (const [key, entry] of this.cache.entries()) {
104
+ if (now > entry.expiresAt) {
105
+ this.cache.delete(key);
106
+ }
107
+ }
108
+ }
109
+
110
+ /**
111
+ * Get stale cache entry (including expired entries)
112
+ * Used for fail-safe behavior when API is unavailable
113
+ * This method accesses the internal cache map directly to avoid
114
+ * deletion of expired entries that would occur with get()
115
+ * @param key Cache key
116
+ * @returns Cached config or null if not found
117
+ */
118
+ getStale(key: string): ToolProtectionConfig | null {
119
+ // Access internal cache map directly (not through async get() which deletes expired entries)
120
+ const entry = this.cache.get(key);
121
+ if (!entry) {
122
+ return null;
123
+ }
124
+ // Return value even if expired (getStale is meant for fail-safe behavior)
125
+ return entry.value;
126
+ }
127
+
128
+ /**
129
+ * Get expiration timestamp for a cache entry
130
+ * Used to check if stale cache is within maxStaleCacheAge
131
+ * @param key Cache key
132
+ * @returns Expiration timestamp in milliseconds, or null if not found
133
+ */
134
+ getExpiresAt(key: string): number | null {
135
+ // Access internal cache map directly (not through async get() which deletes expired entries)
136
+ const entry = this.cache.get(key);
137
+ if (!entry) {
138
+ return null;
139
+ }
140
+ return entry.expiresAt;
141
+ }
142
+ }
143
+
144
+ /**
145
+ * No-op cache implementation (disables caching)
146
+ *
147
+ * Use when:
148
+ * - You want to disable caching entirely
149
+ * - Testing scenarios that require fresh data
150
+ */
151
+ export class NoOpToolProtectionCache implements ToolProtectionCache {
152
+ async get(_key: string): Promise<ToolProtectionConfig | null> {
153
+ return null;
154
+ }
155
+
156
+ async set(
157
+ _key: string,
158
+ _value: ToolProtectionConfig,
159
+ _ttl: number
160
+ ): Promise<void> {
161
+ // No-op
162
+ }
163
+
164
+ async clear(): Promise<void> {
165
+ // No-op
166
+ }
167
+
168
+ async delete(_key: string): Promise<void> {
169
+ // No-op
170
+ }
171
+ }