@kya-os/mcp-i-core 1.2.3-canary.6 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude/settings.local.json +9 -0
- package/.turbo/turbo-build.log +4 -0
- package/.turbo/turbo-test$colon$coverage.log +4514 -0
- package/.turbo/turbo-test.log +2973 -0
- package/COMPLIANCE_IMPROVEMENT_REPORT.md +483 -0
- package/Composer 3.md +615 -0
- package/GPT-5.md +1169 -0
- package/OPUS-plan.md +352 -0
- package/PHASE_3_AND_4.1_SUMMARY.md +585 -0
- package/PHASE_3_SUMMARY.md +317 -0
- package/PHASE_4.1.3_SUMMARY.md +428 -0
- package/PHASE_4.1_COMPLETE.md +525 -0
- package/PHASE_4_USER_DID_IDENTITY_LINKING_PLAN.md +1240 -0
- package/SCHEMA_COMPLIANCE_REPORT.md +275 -0
- package/TEST_PLAN.md +571 -0
- package/coverage/coverage-final.json +57 -0
- package/dist/__tests__/utils/mock-providers.d.ts +1 -2
- package/dist/__tests__/utils/mock-providers.d.ts.map +1 -1
- package/dist/__tests__/utils/mock-providers.js.map +1 -1
- package/dist/cache/oauth-config-cache.d.ts +69 -0
- package/dist/cache/oauth-config-cache.d.ts.map +1 -0
- package/dist/cache/oauth-config-cache.js +76 -0
- package/dist/cache/oauth-config-cache.js.map +1 -0
- package/dist/identity/idp-token-resolver.d.ts +53 -0
- package/dist/identity/idp-token-resolver.d.ts.map +1 -0
- package/dist/identity/idp-token-resolver.js +108 -0
- package/dist/identity/idp-token-resolver.js.map +1 -0
- package/dist/identity/idp-token-storage.interface.d.ts +42 -0
- package/dist/identity/idp-token-storage.interface.d.ts.map +1 -0
- package/dist/identity/idp-token-storage.interface.js +12 -0
- package/dist/identity/idp-token-storage.interface.js.map +1 -0
- package/dist/identity/user-did-manager.d.ts +39 -1
- package/dist/identity/user-did-manager.d.ts.map +1 -1
- package/dist/identity/user-did-manager.js +69 -3
- package/dist/identity/user-did-manager.js.map +1 -1
- package/dist/index.d.ts +22 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +39 -1
- package/dist/index.js.map +1 -1
- package/dist/runtime/audit-logger.d.ts +37 -0
- package/dist/runtime/audit-logger.d.ts.map +1 -0
- package/dist/runtime/audit-logger.js +9 -0
- package/dist/runtime/audit-logger.js.map +1 -0
- package/dist/runtime/base.d.ts +58 -2
- package/dist/runtime/base.d.ts.map +1 -1
- package/dist/runtime/base.js +266 -11
- package/dist/runtime/base.js.map +1 -1
- package/dist/services/access-control.service.d.ts.map +1 -1
- package/dist/services/access-control.service.js +200 -35
- package/dist/services/access-control.service.js.map +1 -1
- package/dist/services/authorization/authorization-registry.d.ts +29 -0
- package/dist/services/authorization/authorization-registry.d.ts.map +1 -0
- package/dist/services/authorization/authorization-registry.js +57 -0
- package/dist/services/authorization/authorization-registry.js.map +1 -0
- package/dist/services/authorization/types.d.ts +53 -0
- package/dist/services/authorization/types.d.ts.map +1 -0
- package/dist/services/authorization/types.js +10 -0
- package/dist/services/authorization/types.js.map +1 -0
- package/dist/services/batch-delegation.service.d.ts +53 -0
- package/dist/services/batch-delegation.service.d.ts.map +1 -0
- package/dist/services/batch-delegation.service.js +95 -0
- package/dist/services/batch-delegation.service.js.map +1 -0
- package/dist/services/oauth-config.service.d.ts +53 -0
- package/dist/services/oauth-config.service.d.ts.map +1 -0
- package/dist/services/oauth-config.service.js +117 -0
- package/dist/services/oauth-config.service.js.map +1 -0
- package/dist/services/oauth-provider-registry.d.ts +77 -0
- package/dist/services/oauth-provider-registry.d.ts.map +1 -0
- package/dist/services/oauth-provider-registry.js +112 -0
- package/dist/services/oauth-provider-registry.js.map +1 -0
- package/dist/services/oauth-service.d.ts +77 -0
- package/dist/services/oauth-service.d.ts.map +1 -0
- package/dist/services/oauth-service.js +348 -0
- package/dist/services/oauth-service.js.map +1 -0
- package/dist/services/oauth-token-retrieval.service.d.ts +49 -0
- package/dist/services/oauth-token-retrieval.service.d.ts.map +1 -0
- package/dist/services/oauth-token-retrieval.service.js +150 -0
- package/dist/services/oauth-token-retrieval.service.js.map +1 -0
- package/dist/services/provider-resolver.d.ts +48 -0
- package/dist/services/provider-resolver.d.ts.map +1 -0
- package/dist/services/provider-resolver.js +120 -0
- package/dist/services/provider-resolver.js.map +1 -0
- package/dist/services/provider-validator.d.ts +55 -0
- package/dist/services/provider-validator.d.ts.map +1 -0
- package/dist/services/provider-validator.js +135 -0
- package/dist/services/provider-validator.js.map +1 -0
- package/dist/services/tool-context-builder.d.ts +57 -0
- package/dist/services/tool-context-builder.d.ts.map +1 -0
- package/dist/services/tool-context-builder.js +125 -0
- package/dist/services/tool-context-builder.js.map +1 -0
- package/dist/services/tool-protection.service.d.ts +87 -10
- package/dist/services/tool-protection.service.d.ts.map +1 -1
- package/dist/services/tool-protection.service.js +282 -112
- package/dist/services/tool-protection.service.js.map +1 -1
- package/dist/types/oauth-required-error.d.ts +40 -0
- package/dist/types/oauth-required-error.d.ts.map +1 -0
- package/dist/types/oauth-required-error.js +40 -0
- package/dist/types/oauth-required-error.js.map +1 -0
- package/dist/utils/did-helpers.d.ts +33 -0
- package/dist/utils/did-helpers.d.ts.map +1 -1
- package/dist/utils/did-helpers.js +40 -0
- package/dist/utils/did-helpers.js.map +1 -1
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.d.ts.map +1 -1
- package/dist/utils/index.js +1 -0
- package/dist/utils/index.js.map +1 -1
- package/docs/API_REFERENCE.md +1362 -0
- package/docs/COMPLIANCE_MATRIX.md +691 -0
- package/docs/STATUSLIST2021_GUIDE.md +696 -0
- package/docs/W3C_VC_DELEGATION_GUIDE.md +710 -0
- package/package.json +24 -50
- package/scripts/audit-compliance.ts +724 -0
- package/src/__tests__/cache/tool-protection-cache.test.ts +640 -0
- package/src/__tests__/config/provider-runtime-config.test.ts +309 -0
- package/src/__tests__/delegation-e2e.test.ts +690 -0
- package/src/__tests__/identity/user-did-manager.test.ts +213 -0
- package/src/__tests__/index.test.ts +56 -0
- package/src/__tests__/integration/full-flow.test.ts +776 -0
- package/src/__tests__/integration.test.ts +281 -0
- package/src/__tests__/providers/base.test.ts +173 -0
- package/src/__tests__/providers/memory.test.ts +319 -0
- package/src/__tests__/regression/phase2-regression.test.ts +427 -0
- package/src/__tests__/runtime/audit-logger.test.ts +154 -0
- package/src/__tests__/runtime/base-extensions.test.ts +593 -0
- package/src/__tests__/runtime/base.test.ts +869 -0
- package/src/__tests__/runtime/delegation-flow.test.ts +164 -0
- package/src/__tests__/runtime/proof-client-did.test.ts +375 -0
- package/src/__tests__/runtime/route-interception.test.ts +686 -0
- package/src/__tests__/runtime/tool-protection-enforcement.test.ts +908 -0
- package/src/__tests__/services/agentshield-integration.test.ts +784 -0
- package/src/__tests__/services/provider-resolver-edge-cases.test.ts +487 -0
- package/src/__tests__/services/tool-protection-oauth-provider.test.ts +480 -0
- package/src/__tests__/services/tool-protection.service.test.ts +1366 -0
- package/src/__tests__/utils/mock-providers.ts +340 -0
- package/src/cache/oauth-config-cache.d.ts +69 -0
- package/src/cache/oauth-config-cache.d.ts.map +1 -0
- package/src/cache/oauth-config-cache.js +71 -0
- package/src/cache/oauth-config-cache.js.map +1 -0
- package/src/cache/oauth-config-cache.ts +123 -0
- package/src/cache/tool-protection-cache.ts +171 -0
- package/src/compliance/EXAMPLE.md +412 -0
- package/src/compliance/__tests__/schema-verifier.test.ts +797 -0
- package/src/compliance/index.ts +8 -0
- package/src/compliance/schema-registry.ts +460 -0
- package/src/compliance/schema-verifier.ts +708 -0
- package/src/config/__tests__/remote-config.spec.ts +268 -0
- package/src/config/remote-config.ts +174 -0
- package/src/config.ts +309 -0
- package/src/delegation/__tests__/audience-validator.test.ts +112 -0
- package/src/delegation/__tests__/bitstring.test.ts +346 -0
- package/src/delegation/__tests__/cascading-revocation.test.ts +628 -0
- package/src/delegation/__tests__/delegation-graph.test.ts +584 -0
- package/src/delegation/__tests__/utils.test.ts +152 -0
- package/src/delegation/__tests__/vc-issuer.test.ts +442 -0
- package/src/delegation/__tests__/vc-verifier.test.ts +922 -0
- package/src/delegation/audience-validator.ts +52 -0
- package/src/delegation/bitstring.ts +278 -0
- package/src/delegation/cascading-revocation.ts +370 -0
- package/src/delegation/delegation-graph.ts +299 -0
- package/src/delegation/index.ts +14 -0
- package/src/delegation/statuslist-manager.ts +353 -0
- package/src/delegation/storage/__tests__/memory-graph-storage.test.ts +366 -0
- package/src/delegation/storage/__tests__/memory-statuslist-storage.test.ts +228 -0
- package/src/delegation/storage/index.ts +9 -0
- package/src/delegation/storage/memory-graph-storage.ts +178 -0
- package/src/delegation/storage/memory-statuslist-storage.ts +77 -0
- package/src/delegation/utils.ts +42 -0
- package/src/delegation/vc-issuer.ts +232 -0
- package/src/delegation/vc-verifier.ts +568 -0
- package/src/identity/idp-token-resolver.ts +147 -0
- package/src/identity/idp-token-storage.interface.ts +59 -0
- package/src/identity/user-did-manager.ts +370 -0
- package/src/index.ts +260 -0
- package/src/providers/base.d.ts +91 -0
- package/src/providers/base.d.ts.map +1 -0
- package/src/providers/base.js +38 -0
- package/src/providers/base.js.map +1 -0
- package/src/providers/base.ts +96 -0
- package/src/providers/memory.ts +142 -0
- package/src/runtime/audit-logger.ts +39 -0
- package/src/runtime/base.ts +1329 -0
- package/src/services/__tests__/access-control.integration.test.ts +443 -0
- package/src/services/__tests__/access-control.proof-response-validation.test.ts +578 -0
- package/src/services/__tests__/access-control.service.test.ts +970 -0
- package/src/services/__tests__/batch-delegation.service.test.ts +351 -0
- package/src/services/__tests__/crypto.service.test.ts +531 -0
- package/src/services/__tests__/oauth-provider-registry.test.ts +142 -0
- package/src/services/__tests__/proof-verifier.integration.test.ts +485 -0
- package/src/services/__tests__/proof-verifier.test.ts +489 -0
- package/src/services/__tests__/provider-resolution.integration.test.ts +198 -0
- package/src/services/__tests__/provider-resolver.test.ts +217 -0
- package/src/services/__tests__/storage.service.test.ts +358 -0
- package/src/services/access-control.service.ts +990 -0
- package/src/services/authorization/authorization-registry.ts +66 -0
- package/src/services/authorization/types.ts +71 -0
- package/src/services/batch-delegation.service.ts +137 -0
- package/src/services/crypto.service.ts +302 -0
- package/src/services/errors.ts +76 -0
- package/src/services/index.ts +9 -0
- package/src/services/oauth-config.service.d.ts +53 -0
- package/src/services/oauth-config.service.d.ts.map +1 -0
- package/src/services/oauth-config.service.js +113 -0
- package/src/services/oauth-config.service.js.map +1 -0
- package/src/services/oauth-config.service.ts +166 -0
- package/src/services/oauth-provider-registry.d.ts +57 -0
- package/src/services/oauth-provider-registry.d.ts.map +1 -0
- package/src/services/oauth-provider-registry.js +73 -0
- package/src/services/oauth-provider-registry.js.map +1 -0
- package/src/services/oauth-provider-registry.ts +123 -0
- package/src/services/oauth-service.ts +510 -0
- package/src/services/oauth-token-retrieval.service.ts +245 -0
- package/src/services/proof-verifier.ts +478 -0
- package/src/services/provider-resolver.d.ts +48 -0
- package/src/services/provider-resolver.d.ts.map +1 -0
- package/src/services/provider-resolver.js +106 -0
- package/src/services/provider-resolver.js.map +1 -0
- package/src/services/provider-resolver.ts +144 -0
- package/src/services/provider-validator.ts +170 -0
- package/src/services/storage.service.ts +566 -0
- package/src/services/tool-context-builder.ts +172 -0
- package/src/services/tool-protection.service.ts +958 -0
- package/src/types/oauth-required-error.ts +63 -0
- package/src/types/tool-protection.ts +155 -0
- package/src/utils/__tests__/did-helpers.test.ts +101 -0
- package/src/utils/base64.ts +148 -0
- package/src/utils/cors.ts +83 -0
- package/src/utils/did-helpers.ts +150 -0
- package/src/utils/index.ts +8 -0
- package/src/utils/storage-keys.ts +278 -0
- package/tsconfig.json +21 -0
- package/vitest.config.ts +56 -0
|
@@ -0,0 +1,686 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Route Interception Tests
|
|
3
|
+
*
|
|
4
|
+
* Tests for route interception, resume tokens, and tool call resumption.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
|
8
|
+
import { MCPIRuntimeBase } from '../../runtime/base';
|
|
9
|
+
import { ProviderRuntimeConfig } from '../../config';
|
|
10
|
+
import { createMockProviders, MockClockProvider } from '../utils/mock-providers';
|
|
11
|
+
import { DelegationRequiredError } from '../../types/tool-protection';
|
|
12
|
+
|
|
13
|
+
describe('MCPIRuntimeBase - Route Interception', () => {
|
|
14
|
+
let runtime: MCPIRuntimeBase;
|
|
15
|
+
let config: ProviderRuntimeConfig;
|
|
16
|
+
let mockProviders: ReturnType<typeof createMockProviders>;
|
|
17
|
+
let mockToolProtectionService: {
|
|
18
|
+
checkToolProtection: ReturnType<typeof vi.fn>;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
beforeEach(async () => {
|
|
22
|
+
vi.clearAllMocks();
|
|
23
|
+
mockProviders = createMockProviders();
|
|
24
|
+
|
|
25
|
+
mockToolProtectionService = {
|
|
26
|
+
checkToolProtection: vi.fn()
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
config = {
|
|
30
|
+
...mockProviders,
|
|
31
|
+
environment: 'development',
|
|
32
|
+
session: {
|
|
33
|
+
timestampSkewSeconds: 120,
|
|
34
|
+
ttlMinutes: 30
|
|
35
|
+
},
|
|
36
|
+
audit: {
|
|
37
|
+
enabled: false
|
|
38
|
+
},
|
|
39
|
+
toolProtectionService: mockToolProtectionService as any
|
|
40
|
+
};
|
|
41
|
+
runtime = new MCPIRuntimeBase(config);
|
|
42
|
+
await runtime.initialize();
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
describe('generateResumeToken', () => {
|
|
46
|
+
it('should generate unique resume tokens for different calls', async () => {
|
|
47
|
+
const call1 = {
|
|
48
|
+
toolName: 'tool1',
|
|
49
|
+
args: { arg: 'value1' },
|
|
50
|
+
sessionId: 'session1',
|
|
51
|
+
timestamp: Date.now(),
|
|
52
|
+
expiresAt: Date.now() + 1800000
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const call2 = {
|
|
56
|
+
toolName: 'tool2',
|
|
57
|
+
args: { arg: 'value2' },
|
|
58
|
+
sessionId: 'session2',
|
|
59
|
+
timestamp: Date.now(),
|
|
60
|
+
expiresAt: Date.now() + 1800000
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
const generateToken = (runtime as any).generateResumeToken.bind(runtime);
|
|
64
|
+
const token1 = generateToken(call1);
|
|
65
|
+
const token2 = generateToken(call2);
|
|
66
|
+
|
|
67
|
+
expect(token1).toBeDefined();
|
|
68
|
+
expect(token2).toBeDefined();
|
|
69
|
+
expect(token1).not.toBe(token2);
|
|
70
|
+
expect(token1).toMatch(/^resume_/);
|
|
71
|
+
expect(token2).toMatch(/^resume_/);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it('should generate different tokens due to timestamp component', async () => {
|
|
75
|
+
const call = {
|
|
76
|
+
toolName: 'tool1',
|
|
77
|
+
args: { arg: 'value' },
|
|
78
|
+
sessionId: 'session1',
|
|
79
|
+
timestamp: 1000,
|
|
80
|
+
expiresAt: 1801000
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
const generateToken = (runtime as any).generateResumeToken.bind(runtime);
|
|
84
|
+
|
|
85
|
+
// Generate multiple tokens to verify they differ when generated at different times
|
|
86
|
+
// Note: generateResumeToken uses Date.now() internally, so tokens will differ
|
|
87
|
+
// unless generated in the exact same millisecond (very rare)
|
|
88
|
+
const tokens = new Set<string>();
|
|
89
|
+
for (let i = 0; i < 10; i++) {
|
|
90
|
+
const token = generateToken(call);
|
|
91
|
+
tokens.add(token);
|
|
92
|
+
// Small delay to ensure different timestamps
|
|
93
|
+
await new Promise(resolve => setTimeout(resolve, 1));
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// Verify token format: resume_{hash}_{timestamp}
|
|
97
|
+
const firstToken = Array.from(tokens)[0];
|
|
98
|
+
expect(firstToken).toMatch(/^resume_[a-z0-9]+_[a-z0-9]+$/);
|
|
99
|
+
|
|
100
|
+
const parts = firstToken.split('_');
|
|
101
|
+
expect(parts.length).toBe(3);
|
|
102
|
+
expect(parts[0]).toBe('resume');
|
|
103
|
+
|
|
104
|
+
// Verify hash part is consistent for same call context
|
|
105
|
+
const hashPart = parts[1];
|
|
106
|
+
for (const token of tokens) {
|
|
107
|
+
const tokenParts = token.split('_');
|
|
108
|
+
expect(tokenParts[1]).toBe(hashPart); // Same hash for same call
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// Most tokens should be unique (allowing for edge case of same millisecond)
|
|
112
|
+
// If all 10 tokens are unique, we're confident the timestamp component works
|
|
113
|
+
expect(tokens.size).toBeGreaterThan(1); // At least some should differ
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it('should include call context in token generation', async () => {
|
|
117
|
+
const call1 = {
|
|
118
|
+
toolName: 'tool1',
|
|
119
|
+
args: { arg: 'value1' },
|
|
120
|
+
sessionId: 'session1',
|
|
121
|
+
timestamp: 1000,
|
|
122
|
+
expiresAt: 1801000
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
const call2 = {
|
|
126
|
+
toolName: 'tool1',
|
|
127
|
+
args: { arg: 'value2' }, // Different args
|
|
128
|
+
sessionId: 'session1',
|
|
129
|
+
timestamp: 1000,
|
|
130
|
+
expiresAt: 1801000
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
const generateToken = (runtime as any).generateResumeToken.bind(runtime);
|
|
134
|
+
const token1 = generateToken(call1);
|
|
135
|
+
const token2 = generateToken(call2);
|
|
136
|
+
|
|
137
|
+
// Different args should produce different tokens
|
|
138
|
+
expect(token1).not.toBe(token2);
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
describe('buildConsentUrl', () => {
|
|
143
|
+
it('should build consent URL with tool and scopes', () => {
|
|
144
|
+
const session = {
|
|
145
|
+
id: 'session123',
|
|
146
|
+
agentDid: 'did:key:zagent123',
|
|
147
|
+
audience: 'https://client.example.com'
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
const buildUrl = (runtime as any).buildConsentUrl.bind(runtime);
|
|
151
|
+
const url = buildUrl(
|
|
152
|
+
'protectedTool',
|
|
153
|
+
['files:write', 'files:read'],
|
|
154
|
+
session,
|
|
155
|
+
'resume_token_123'
|
|
156
|
+
);
|
|
157
|
+
|
|
158
|
+
expect(url).toContain('https://kya.vouched.id/bouncer/consent');
|
|
159
|
+
expect(url).toContain('tool=protectedTool');
|
|
160
|
+
expect(url).toContain('scopes=files%3Awrite%2Cfiles%3Aread');
|
|
161
|
+
expect(url).toContain('session_id=session123');
|
|
162
|
+
expect(url).toContain('agent_did=did%3Akey%3Azagent123');
|
|
163
|
+
expect(url).toContain('resume_token=resume_token_123');
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
it('should build consent URL without resume token if not provided', () => {
|
|
167
|
+
const session = {
|
|
168
|
+
id: 'session123',
|
|
169
|
+
agentDid: 'did:key:zagent123',
|
|
170
|
+
audience: 'https://client.example.com'
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
const buildUrl = (runtime as any).buildConsentUrl.bind(runtime);
|
|
174
|
+
const url = buildUrl(
|
|
175
|
+
'protectedTool',
|
|
176
|
+
['files:write'],
|
|
177
|
+
session
|
|
178
|
+
);
|
|
179
|
+
|
|
180
|
+
expect(url).toContain('https://kya.vouched.id/bouncer/consent');
|
|
181
|
+
expect(url).not.toContain('resume_token=');
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
it('should handle missing session fields gracefully', () => {
|
|
185
|
+
const buildUrl = (runtime as any).buildConsentUrl.bind(runtime);
|
|
186
|
+
const url = buildUrl(
|
|
187
|
+
'protectedTool',
|
|
188
|
+
['files:write'],
|
|
189
|
+
{},
|
|
190
|
+
'resume_token_123'
|
|
191
|
+
);
|
|
192
|
+
|
|
193
|
+
expect(url).toContain('https://kya.vouched.id/bouncer/consent');
|
|
194
|
+
expect(url).toContain('tool=protectedTool');
|
|
195
|
+
expect(url).toContain('session_id=');
|
|
196
|
+
expect(url).toContain('agent_did=');
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
it('should URL encode special characters in scopes', () => {
|
|
200
|
+
const session = {
|
|
201
|
+
id: 'session123',
|
|
202
|
+
agentDid: 'did:key:zagent123'
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
const buildUrl = (runtime as any).buildConsentUrl.bind(runtime);
|
|
206
|
+
const url = buildUrl(
|
|
207
|
+
'protectedTool',
|
|
208
|
+
['files:write', 'scope with spaces', 'scope:with:colons'],
|
|
209
|
+
session
|
|
210
|
+
);
|
|
211
|
+
|
|
212
|
+
expect(url).toContain('scopes=files%3Awrite%2Cscope+with+spaces%2Cscope%3Awith%3Acolons');
|
|
213
|
+
});
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
describe('intercepted calls storage', () => {
|
|
217
|
+
const mockHandler = vi.fn().mockResolvedValue({ result: 'success' });
|
|
218
|
+
const session = {
|
|
219
|
+
id: 'session123',
|
|
220
|
+
audience: 'https://client.example.com',
|
|
221
|
+
nonce: 'test-nonce'
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
it('should store intercepted call when delegation required', async () => {
|
|
225
|
+
mockToolProtectionService.checkToolProtection.mockResolvedValue({
|
|
226
|
+
requiresDelegation: true,
|
|
227
|
+
requiredScopes: ['files:write']
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
try {
|
|
231
|
+
await runtime.processToolCall(
|
|
232
|
+
'protectedTool',
|
|
233
|
+
{ arg: 'value' },
|
|
234
|
+
mockHandler,
|
|
235
|
+
session
|
|
236
|
+
);
|
|
237
|
+
expect.fail('Should have thrown DelegationRequiredError');
|
|
238
|
+
} catch (error: any) {
|
|
239
|
+
expect(error.resumeToken).toBeDefined();
|
|
240
|
+
|
|
241
|
+
// Check that intercepted call is stored
|
|
242
|
+
const interceptedCalls = (runtime as any).interceptedCalls;
|
|
243
|
+
const storedCall = interceptedCalls.get(error.resumeToken);
|
|
244
|
+
expect(storedCall).toBeDefined();
|
|
245
|
+
expect(storedCall.toolName).toBe('protectedTool');
|
|
246
|
+
expect(storedCall.args).toEqual({ arg: 'value' });
|
|
247
|
+
expect(storedCall.sessionId).toBe('session123');
|
|
248
|
+
}
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
it('should set expiry time for intercepted calls', async () => {
|
|
252
|
+
const clock = mockProviders.clockProvider as MockClockProvider;
|
|
253
|
+
const currentTime = Date.now();
|
|
254
|
+
clock.setTime(currentTime);
|
|
255
|
+
|
|
256
|
+
mockToolProtectionService.checkToolProtection.mockResolvedValue({
|
|
257
|
+
requiresDelegation: true,
|
|
258
|
+
requiredScopes: ['files:write']
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
try {
|
|
262
|
+
await runtime.processToolCall(
|
|
263
|
+
'protectedTool',
|
|
264
|
+
{ arg: 'value' },
|
|
265
|
+
mockHandler,
|
|
266
|
+
session
|
|
267
|
+
);
|
|
268
|
+
expect.fail('Should have thrown DelegationRequiredError');
|
|
269
|
+
} catch (error: any) {
|
|
270
|
+
const interceptedCalls = (runtime as any).interceptedCalls;
|
|
271
|
+
const storedCall = interceptedCalls.get(error.resumeToken);
|
|
272
|
+
expect(storedCall.expiresAt).toBe(currentTime + 1800000); // 30 minutes
|
|
273
|
+
}
|
|
274
|
+
});
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
describe('resumeToolCall', () => {
|
|
278
|
+
const mockHandler = vi.fn().mockResolvedValue({ result: 'success' });
|
|
279
|
+
let session: any;
|
|
280
|
+
let resumeToken: string;
|
|
281
|
+
|
|
282
|
+
beforeEach(async () => {
|
|
283
|
+
// Create a handshake session first
|
|
284
|
+
const handshakeResponse = await runtime.handleHandshake({
|
|
285
|
+
clientDid: 'did:key:zclient123',
|
|
286
|
+
audience: 'https://client.example.com'
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
session = {
|
|
290
|
+
id: handshakeResponse.sessionId, // Use actual session ID from handshake
|
|
291
|
+
audience: 'https://client.example.com',
|
|
292
|
+
nonce: 'test-nonce'
|
|
293
|
+
};
|
|
294
|
+
|
|
295
|
+
// Intercept a tool call
|
|
296
|
+
mockToolProtectionService.checkToolProtection.mockResolvedValue({
|
|
297
|
+
requiresDelegation: true,
|
|
298
|
+
requiredScopes: ['files:write']
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
try {
|
|
302
|
+
await runtime.processToolCall(
|
|
303
|
+
'protectedTool',
|
|
304
|
+
{ arg: 'value' },
|
|
305
|
+
mockHandler,
|
|
306
|
+
session
|
|
307
|
+
);
|
|
308
|
+
expect.fail('Should have thrown DelegationRequiredError');
|
|
309
|
+
} catch (error: any) {
|
|
310
|
+
resumeToken = error.resumeToken;
|
|
311
|
+
}
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
it('should resume tool call with valid resume token', async () => {
|
|
315
|
+
mockToolProtectionService.checkToolProtection.mockResolvedValue({
|
|
316
|
+
requiresDelegation: true,
|
|
317
|
+
requiredScopes: ['files:write']
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
const result = await runtime.resumeToolCall(
|
|
321
|
+
resumeToken,
|
|
322
|
+
mockHandler,
|
|
323
|
+
'delegation-token-123'
|
|
324
|
+
);
|
|
325
|
+
|
|
326
|
+
expect(mockHandler).toHaveBeenCalledWith({ arg: 'value' });
|
|
327
|
+
expect(result).toEqual({ result: 'success' });
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
it('should throw error for invalid resume token', async () => {
|
|
331
|
+
await expect(
|
|
332
|
+
runtime.resumeToolCall(
|
|
333
|
+
'invalid-resume-token',
|
|
334
|
+
mockHandler,
|
|
335
|
+
'delegation-token-123'
|
|
336
|
+
)
|
|
337
|
+
).rejects.toThrow('Invalid or expired resume token');
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
it('should throw error for expired resume token', async () => {
|
|
341
|
+
const clock = mockProviders.clockProvider as MockClockProvider;
|
|
342
|
+
|
|
343
|
+
// Advance time past expiry (30 minutes + 1 second)
|
|
344
|
+
clock.setTime(Date.now() + (30 * 60 * 1000) + 1000);
|
|
345
|
+
|
|
346
|
+
await expect(
|
|
347
|
+
runtime.resumeToolCall(
|
|
348
|
+
resumeToken,
|
|
349
|
+
mockHandler,
|
|
350
|
+
'delegation-token-123'
|
|
351
|
+
)
|
|
352
|
+
).rejects.toThrow('Resume token expired');
|
|
353
|
+
});
|
|
354
|
+
|
|
355
|
+
it('should throw error if session not found', async () => {
|
|
356
|
+
// Create intercepted call with non-existent session ID
|
|
357
|
+
const call = {
|
|
358
|
+
toolName: 'protectedTool',
|
|
359
|
+
args: { arg: 'value' },
|
|
360
|
+
sessionId: 'non-existent-session',
|
|
361
|
+
timestamp: Date.now(),
|
|
362
|
+
expiresAt: Date.now() + 1800000
|
|
363
|
+
};
|
|
364
|
+
|
|
365
|
+
const generateToken = (runtime as any).generateResumeToken.bind(runtime);
|
|
366
|
+
const invalidToken = generateToken(call);
|
|
367
|
+
|
|
368
|
+
// Manually store the intercepted call
|
|
369
|
+
(runtime as any).interceptedCalls.set(invalidToken, call);
|
|
370
|
+
|
|
371
|
+
await expect(
|
|
372
|
+
runtime.resumeToolCall(
|
|
373
|
+
invalidToken,
|
|
374
|
+
mockHandler,
|
|
375
|
+
'delegation-token-123'
|
|
376
|
+
)
|
|
377
|
+
).rejects.toThrow('Session not found');
|
|
378
|
+
});
|
|
379
|
+
|
|
380
|
+
it('should add delegation token to session when resuming', async () => {
|
|
381
|
+
mockToolProtectionService.checkToolProtection.mockResolvedValue({
|
|
382
|
+
requiresDelegation: true,
|
|
383
|
+
requiredScopes: ['files:write']
|
|
384
|
+
});
|
|
385
|
+
|
|
386
|
+
await runtime.resumeToolCall(
|
|
387
|
+
resumeToken,
|
|
388
|
+
mockHandler,
|
|
389
|
+
'delegation-token-123'
|
|
390
|
+
);
|
|
391
|
+
|
|
392
|
+
// Verify that processToolCall was called with session containing delegationToken
|
|
393
|
+
expect(mockHandler).toHaveBeenCalled();
|
|
394
|
+
});
|
|
395
|
+
|
|
396
|
+
it('should clean up intercepted call after successful resumption', async () => {
|
|
397
|
+
mockToolProtectionService.checkToolProtection.mockResolvedValue({
|
|
398
|
+
requiresDelegation: true,
|
|
399
|
+
requiredScopes: ['files:write']
|
|
400
|
+
});
|
|
401
|
+
|
|
402
|
+
await runtime.resumeToolCall(
|
|
403
|
+
resumeToken,
|
|
404
|
+
mockHandler,
|
|
405
|
+
'delegation-token-123'
|
|
406
|
+
);
|
|
407
|
+
|
|
408
|
+
// Call should be removed from storage
|
|
409
|
+
const interceptedCalls = (runtime as any).interceptedCalls;
|
|
410
|
+
expect(interceptedCalls.has(resumeToken)).toBe(false);
|
|
411
|
+
});
|
|
412
|
+
|
|
413
|
+
it('should not clean up intercepted call if resumption fails', async () => {
|
|
414
|
+
const errorHandler = vi.fn().mockRejectedValue(new Error('Handler failed'));
|
|
415
|
+
|
|
416
|
+
try {
|
|
417
|
+
await runtime.resumeToolCall(
|
|
418
|
+
resumeToken,
|
|
419
|
+
errorHandler,
|
|
420
|
+
'delegation-token-123'
|
|
421
|
+
);
|
|
422
|
+
expect.fail('Should have thrown error');
|
|
423
|
+
} catch (error) {
|
|
424
|
+
// Expected
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
// Call should still be in storage (can retry)
|
|
428
|
+
const interceptedCalls = (runtime as any).interceptedCalls;
|
|
429
|
+
expect(interceptedCalls.has(resumeToken)).toBe(true);
|
|
430
|
+
});
|
|
431
|
+
});
|
|
432
|
+
|
|
433
|
+
describe('cleanupExpiredInterceptedCalls', () => {
|
|
434
|
+
it('should remove expired intercepted calls', async () => {
|
|
435
|
+
const clock = mockProviders.clockProvider as MockClockProvider;
|
|
436
|
+
const currentTime = Date.now();
|
|
437
|
+
clock.setTime(currentTime);
|
|
438
|
+
|
|
439
|
+
const expiredCall = {
|
|
440
|
+
toolName: 'expiredTool',
|
|
441
|
+
args: { arg: 'value' },
|
|
442
|
+
sessionId: 'session123',
|
|
443
|
+
timestamp: currentTime - 1000,
|
|
444
|
+
expiresAt: currentTime - 500 // Already expired
|
|
445
|
+
};
|
|
446
|
+
|
|
447
|
+
const validCall = {
|
|
448
|
+
toolName: 'validTool',
|
|
449
|
+
args: { arg: 'value' },
|
|
450
|
+
sessionId: 'session123',
|
|
451
|
+
timestamp: currentTime,
|
|
452
|
+
expiresAt: currentTime + 1800000 // Not expired
|
|
453
|
+
};
|
|
454
|
+
|
|
455
|
+
const generateToken = (runtime as any).generateResumeToken.bind(runtime);
|
|
456
|
+
const expiredToken = generateToken(expiredCall);
|
|
457
|
+
const validToken = generateToken(validCall);
|
|
458
|
+
|
|
459
|
+
const interceptedCalls = (runtime as any).interceptedCalls;
|
|
460
|
+
interceptedCalls.set(expiredToken, expiredCall);
|
|
461
|
+
interceptedCalls.set(validToken, validCall);
|
|
462
|
+
|
|
463
|
+
// Trigger cleanup
|
|
464
|
+
const cleanup = (runtime as any).cleanupExpiredInterceptedCalls.bind(runtime);
|
|
465
|
+
cleanup();
|
|
466
|
+
|
|
467
|
+
expect(interceptedCalls.has(expiredToken)).toBe(false);
|
|
468
|
+
expect(interceptedCalls.has(validToken)).toBe(true);
|
|
469
|
+
});
|
|
470
|
+
|
|
471
|
+
it('should be called automatically when intercepting calls', async () => {
|
|
472
|
+
const clock = mockProviders.clockProvider as MockClockProvider;
|
|
473
|
+
const currentTime = Date.now();
|
|
474
|
+
clock.setTime(currentTime);
|
|
475
|
+
|
|
476
|
+
// Add an expired call
|
|
477
|
+
const expiredCall = {
|
|
478
|
+
toolName: 'expiredTool',
|
|
479
|
+
args: { arg: 'value' },
|
|
480
|
+
sessionId: 'session123',
|
|
481
|
+
timestamp: currentTime - 1000,
|
|
482
|
+
expiresAt: currentTime - 500
|
|
483
|
+
};
|
|
484
|
+
|
|
485
|
+
const generateToken = (runtime as any).generateResumeToken.bind(runtime);
|
|
486
|
+
const expiredToken = generateToken(expiredCall);
|
|
487
|
+
|
|
488
|
+
const interceptedCalls = (runtime as any).interceptedCalls;
|
|
489
|
+
interceptedCalls.set(expiredToken, expiredCall);
|
|
490
|
+
|
|
491
|
+
// Intercept a new call (should trigger cleanup)
|
|
492
|
+
mockToolProtectionService.checkToolProtection.mockResolvedValue({
|
|
493
|
+
requiresDelegation: true,
|
|
494
|
+
requiredScopes: ['files:write']
|
|
495
|
+
});
|
|
496
|
+
|
|
497
|
+
try {
|
|
498
|
+
await runtime.processToolCall(
|
|
499
|
+
'newTool',
|
|
500
|
+
{ arg: 'value' },
|
|
501
|
+
vi.fn(),
|
|
502
|
+
{ id: 'session123' }
|
|
503
|
+
);
|
|
504
|
+
expect.fail('Should have thrown DelegationRequiredError');
|
|
505
|
+
} catch (error) {
|
|
506
|
+
// Expected
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
// Expired call should be cleaned up
|
|
510
|
+
expect(interceptedCalls.has(expiredToken)).toBe(false);
|
|
511
|
+
});
|
|
512
|
+
});
|
|
513
|
+
|
|
514
|
+
describe('integration: full interception flow', () => {
|
|
515
|
+
it('should complete full flow: intercept -> authorize -> resume', async () => {
|
|
516
|
+
const mockHandler = vi.fn().mockResolvedValue({ result: 'success' });
|
|
517
|
+
const session = {
|
|
518
|
+
id: 'session123',
|
|
519
|
+
audience: 'https://client.example.com',
|
|
520
|
+
nonce: 'test-nonce'
|
|
521
|
+
};
|
|
522
|
+
|
|
523
|
+
// Step 1: Create session
|
|
524
|
+
const handshakeResponse = await runtime.handleHandshake({
|
|
525
|
+
clientDid: 'did:key:zclient123',
|
|
526
|
+
audience: 'https://client.example.com'
|
|
527
|
+
});
|
|
528
|
+
|
|
529
|
+
const sessionWithId = {
|
|
530
|
+
id: handshakeResponse.sessionId,
|
|
531
|
+
audience: 'https://client.example.com',
|
|
532
|
+
nonce: 'test-nonce'
|
|
533
|
+
};
|
|
534
|
+
|
|
535
|
+
// Step 2: Intercept tool call
|
|
536
|
+
mockToolProtectionService.checkToolProtection.mockResolvedValue({
|
|
537
|
+
requiresDelegation: true,
|
|
538
|
+
requiredScopes: ['files:write']
|
|
539
|
+
});
|
|
540
|
+
|
|
541
|
+
let resumeToken: string;
|
|
542
|
+
try {
|
|
543
|
+
await runtime.processToolCall(
|
|
544
|
+
'protectedTool',
|
|
545
|
+
{ arg: 'value' },
|
|
546
|
+
mockHandler,
|
|
547
|
+
sessionWithId
|
|
548
|
+
);
|
|
549
|
+
expect.fail('Should have thrown DelegationRequiredError');
|
|
550
|
+
} catch (error: any) {
|
|
551
|
+
resumeToken = error.resumeToken;
|
|
552
|
+
expect(error).toBeInstanceOf(DelegationRequiredError);
|
|
553
|
+
expect(error.consentUrl).toBeDefined();
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
// Step 3: User authorizes (simulated by having delegation token)
|
|
557
|
+
// Step 4: Resume tool call
|
|
558
|
+
const result = await runtime.resumeToolCall(
|
|
559
|
+
resumeToken!,
|
|
560
|
+
mockHandler,
|
|
561
|
+
'delegation-token-123'
|
|
562
|
+
);
|
|
563
|
+
|
|
564
|
+
// Step 5: Verify successful execution
|
|
565
|
+
expect(mockHandler).toHaveBeenCalledWith({ arg: 'value' });
|
|
566
|
+
expect(result).toEqual({ result: 'success' });
|
|
567
|
+
|
|
568
|
+
// Step 6: Verify cleanup
|
|
569
|
+
const interceptedCalls = (runtime as any).interceptedCalls;
|
|
570
|
+
expect(interceptedCalls.has(resumeToken!)).toBe(false);
|
|
571
|
+
});
|
|
572
|
+
});
|
|
573
|
+
|
|
574
|
+
describe('edge cases', () => {
|
|
575
|
+
it('should handle resume token reuse attempt', async () => {
|
|
576
|
+
const mockHandler = vi.fn().mockResolvedValue({ result: 'success' });
|
|
577
|
+
|
|
578
|
+
// Create session first
|
|
579
|
+
const handshakeResponse = await runtime.handleHandshake({
|
|
580
|
+
clientDid: 'did:key:zclient123',
|
|
581
|
+
audience: 'https://client.example.com'
|
|
582
|
+
});
|
|
583
|
+
|
|
584
|
+
const sessionWithId = {
|
|
585
|
+
id: handshakeResponse.sessionId,
|
|
586
|
+
audience: 'https://client.example.com',
|
|
587
|
+
nonce: 'test-nonce'
|
|
588
|
+
};
|
|
589
|
+
|
|
590
|
+
mockToolProtectionService.checkToolProtection.mockResolvedValue({
|
|
591
|
+
requiresDelegation: true,
|
|
592
|
+
requiredScopes: ['files:write']
|
|
593
|
+
});
|
|
594
|
+
|
|
595
|
+
let resumeToken: string;
|
|
596
|
+
try {
|
|
597
|
+
await runtime.processToolCall(
|
|
598
|
+
'protectedTool',
|
|
599
|
+
{ arg: 'value' },
|
|
600
|
+
mockHandler,
|
|
601
|
+
sessionWithId
|
|
602
|
+
);
|
|
603
|
+
expect.fail('Should have thrown DelegationRequiredError');
|
|
604
|
+
} catch (error: any) {
|
|
605
|
+
resumeToken = error.resumeToken;
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
// First resume should succeed
|
|
609
|
+
await runtime.resumeToolCall(
|
|
610
|
+
resumeToken!,
|
|
611
|
+
mockHandler,
|
|
612
|
+
'delegation-token-123'
|
|
613
|
+
);
|
|
614
|
+
|
|
615
|
+
// Second resume should fail (token already consumed)
|
|
616
|
+
await expect(
|
|
617
|
+
runtime.resumeToolCall(
|
|
618
|
+
resumeToken!,
|
|
619
|
+
mockHandler,
|
|
620
|
+
'delegation-token-123'
|
|
621
|
+
)
|
|
622
|
+
).rejects.toThrow('Invalid or expired resume token');
|
|
623
|
+
});
|
|
624
|
+
|
|
625
|
+
it('should handle complex nested args in intercepted calls', async () => {
|
|
626
|
+
const mockHandler = vi.fn().mockResolvedValue({ result: 'success' });
|
|
627
|
+
const session = {
|
|
628
|
+
id: 'session123',
|
|
629
|
+
audience: 'https://client.example.com',
|
|
630
|
+
nonce: 'test-nonce'
|
|
631
|
+
};
|
|
632
|
+
|
|
633
|
+
const handshakeResponse = await runtime.handleHandshake({
|
|
634
|
+
clientDid: 'did:key:zclient123',
|
|
635
|
+
audience: 'https://client.example.com'
|
|
636
|
+
});
|
|
637
|
+
|
|
638
|
+
const sessionWithId = {
|
|
639
|
+
id: handshakeResponse.sessionId,
|
|
640
|
+
audience: 'https://client.example.com',
|
|
641
|
+
nonce: 'test-nonce'
|
|
642
|
+
};
|
|
643
|
+
|
|
644
|
+
mockToolProtectionService.checkToolProtection.mockResolvedValue({
|
|
645
|
+
requiresDelegation: true,
|
|
646
|
+
requiredScopes: ['files:write']
|
|
647
|
+
});
|
|
648
|
+
|
|
649
|
+
const complexArgs = {
|
|
650
|
+
nested: {
|
|
651
|
+
deeply: {
|
|
652
|
+
nested: {
|
|
653
|
+
value: 'test',
|
|
654
|
+
array: [1, 2, 3],
|
|
655
|
+
object: { key: 'value' }
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
},
|
|
659
|
+
array: [{ id: 1 }, { id: 2 }]
|
|
660
|
+
};
|
|
661
|
+
|
|
662
|
+
let resumeToken: string;
|
|
663
|
+
try {
|
|
664
|
+
await runtime.processToolCall(
|
|
665
|
+
'protectedTool',
|
|
666
|
+
complexArgs,
|
|
667
|
+
mockHandler,
|
|
668
|
+
sessionWithId
|
|
669
|
+
);
|
|
670
|
+
expect.fail('Should have thrown DelegationRequiredError');
|
|
671
|
+
} catch (error: any) {
|
|
672
|
+
resumeToken = error.resumeToken;
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
const result = await runtime.resumeToolCall(
|
|
676
|
+
resumeToken!,
|
|
677
|
+
mockHandler,
|
|
678
|
+
'delegation-token-123'
|
|
679
|
+
);
|
|
680
|
+
|
|
681
|
+
expect(mockHandler).toHaveBeenCalledWith(complexArgs);
|
|
682
|
+
expect(result).toEqual({ result: 'success' });
|
|
683
|
+
});
|
|
684
|
+
});
|
|
685
|
+
});
|
|
686
|
+
|