@kya-os/mcp-i-runtime 1.0.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/.turbo/turbo-build.log +4 -0
- package/dist/auth/handshake.d.ts +136 -0
- package/dist/auth/handshake.d.ts.map +1 -0
- package/dist/auth/handshake.js +276 -0
- package/dist/auth/handshake.js.map +1 -0
- package/dist/auth/index.d.ts +6 -0
- package/dist/auth/index.d.ts.map +1 -0
- package/dist/auth/index.js +12 -0
- package/dist/auth/index.js.map +1 -0
- package/dist/auth/types.d.ts +52 -0
- package/dist/auth/types.d.ts.map +1 -0
- package/dist/auth/types.js +10 -0
- package/dist/auth/types.js.map +1 -0
- 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/cache/tool-protection-cache.d.ts +102 -0
- package/dist/cache/tool-protection-cache.d.ts.map +1 -0
- package/dist/cache/tool-protection-cache.js +131 -0
- package/dist/cache/tool-protection-cache.js.map +1 -0
- package/dist/compliance/index.d.ts +8 -0
- package/dist/compliance/index.d.ts.map +1 -0
- package/dist/compliance/index.js +24 -0
- package/dist/compliance/index.js.map +1 -0
- package/dist/compliance/schema-registry.d.ts +50 -0
- package/dist/compliance/schema-registry.d.ts.map +1 -0
- package/dist/compliance/schema-registry.js +438 -0
- package/dist/compliance/schema-registry.js.map +1 -0
- package/dist/compliance/schema-verifier.d.ts +110 -0
- package/dist/compliance/schema-verifier.d.ts.map +1 -0
- package/dist/compliance/schema-verifier.js +518 -0
- package/dist/compliance/schema-verifier.js.map +1 -0
- package/dist/config/remote-config.d.ts +188 -0
- package/dist/config/remote-config.d.ts.map +1 -0
- package/dist/config/remote-config.js +268 -0
- package/dist/config/remote-config.js.map +1 -0
- package/dist/config.d.ts +182 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +148 -0
- package/dist/config.js.map +1 -0
- package/dist/identity/idp-token-resolver.d.ts +69 -0
- package/dist/identity/idp-token-resolver.d.ts.map +1 -0
- package/dist/identity/idp-token-resolver.js +136 -0
- package/dist/identity/idp-token-resolver.js.map +1 -0
- package/dist/identity/idp-token-storage.interface.d.ts +80 -0
- package/dist/identity/idp-token-storage.interface.d.ts.map +1 -0
- package/dist/identity/idp-token-storage.interface.js +14 -0
- package/dist/identity/idp-token-storage.interface.js.map +1 -0
- package/dist/identity/user-did-manager.d.ts +228 -0
- package/dist/identity/user-did-manager.d.ts.map +1 -0
- package/dist/identity/user-did-manager.js +356 -0
- package/dist/identity/user-did-manager.js.map +1 -0
- package/dist/index.d.ts +69 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +193 -0
- package/dist/index.js.map +1 -0
- package/dist/logging/index.d.ts +5 -0
- package/dist/logging/index.d.ts.map +1 -0
- package/dist/logging/index.js +10 -0
- package/dist/logging/index.js.map +1 -0
- package/dist/logging/logger.d.ts +33 -0
- package/dist/logging/logger.d.ts.map +1 -0
- package/dist/logging/logger.js +104 -0
- package/dist/logging/logger.js.map +1 -0
- package/dist/proof/generator.d.ts +109 -0
- package/dist/proof/generator.d.ts.map +1 -0
- package/dist/proof/generator.js +236 -0
- package/dist/proof/generator.js.map +1 -0
- package/dist/proof/index.d.ts +5 -0
- package/dist/proof/index.d.ts.map +1 -0
- package/dist/proof/index.js +11 -0
- package/dist/proof/index.js.map +1 -0
- package/dist/providers/base.d.ts +95 -0
- package/dist/providers/base.d.ts.map +1 -0
- package/dist/providers/base.js +47 -0
- package/dist/providers/base.js.map +1 -0
- package/dist/providers/memory.d.ts +42 -0
- package/dist/providers/memory.d.ts.map +1 -0
- package/dist/providers/memory.js +121 -0
- package/dist/providers/memory.js.map +1 -0
- 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 +392 -0
- package/dist/runtime/base.d.ts.map +1 -0
- package/dist/runtime/base.js +1527 -0
- package/dist/runtime/base.js.map +1 -0
- package/dist/runtime/dif-core.d.ts +33 -0
- package/dist/runtime/dif-core.d.ts.map +1 -0
- package/dist/runtime/dif-core.js +14 -0
- package/dist/runtime/dif-core.js.map +1 -0
- package/dist/runtime/ext-apps-constants.d.ts +20 -0
- package/dist/runtime/ext-apps-constants.d.ts.map +1 -0
- package/dist/runtime/ext-apps-constants.js +23 -0
- package/dist/runtime/ext-apps-constants.js.map +1 -0
- package/dist/services/access-control.service.d.ts +138 -0
- package/dist/services/access-control.service.d.ts.map +1 -0
- package/dist/services/access-control.service.js +721 -0
- package/dist/services/access-control.service.js.map +1 -0
- 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 +58 -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/crypto.service.d.ts +69 -0
- package/dist/services/crypto.service.d.ts.map +1 -0
- package/dist/services/crypto.service.js +225 -0
- package/dist/services/crypto.service.js.map +1 -0
- package/dist/services/delegation-error-formatter.d.ts +124 -0
- package/dist/services/delegation-error-formatter.d.ts.map +1 -0
- package/dist/services/delegation-error-formatter.js +214 -0
- package/dist/services/delegation-error-formatter.js.map +1 -0
- package/dist/services/errors.d.ts +49 -0
- package/dist/services/errors.d.ts.map +1 -0
- package/dist/services/errors.js +66 -0
- package/dist/services/errors.js.map +1 -0
- package/dist/services/index.d.ts +9 -0
- package/dist/services/index.d.ts.map +1 -0
- package/dist/services/index.js +15 -0
- package/dist/services/index.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 +141 -0
- package/dist/services/oauth-config.service.js.map +1 -0
- package/dist/services/oauth-provider-registry.d.ts +88 -0
- package/dist/services/oauth-provider-registry.d.ts.map +1 -0
- package/dist/services/oauth-provider-registry.js +128 -0
- package/dist/services/oauth-provider-registry.js.map +1 -0
- package/dist/services/oauth-service.d.ts +122 -0
- package/dist/services/oauth-service.d.ts.map +1 -0
- package/dist/services/oauth-service.js +442 -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/policy.service.d.ts +130 -0
- package/dist/services/policy.service.d.ts.map +1 -0
- package/dist/services/policy.service.js +399 -0
- package/dist/services/policy.service.js.map +1 -0
- package/dist/services/proof-verifier.d.ts +103 -0
- package/dist/services/proof-verifier.d.ts.map +1 -0
- package/dist/services/proof-verifier.js +190 -0
- package/dist/services/proof-verifier.js.map +1 -0
- package/dist/services/provider-resolver.d.ts +89 -0
- package/dist/services/provider-resolver.d.ts.map +1 -0
- package/dist/services/provider-resolver.js +196 -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/session-registration.service.d.ts +80 -0
- package/dist/services/session-registration.service.d.ts.map +1 -0
- package/dist/services/session-registration.service.js +174 -0
- package/dist/services/session-registration.service.js.map +1 -0
- package/dist/services/storage.service.d.ts +116 -0
- package/dist/services/storage.service.d.ts.map +1 -0
- package/dist/services/storage.service.js +411 -0
- package/dist/services/storage.service.js.map +1 -0
- package/dist/services/tool-context-builder.d.ts +74 -0
- package/dist/services/tool-context-builder.d.ts.map +1 -0
- package/dist/services/tool-context-builder.js +215 -0
- package/dist/services/tool-context-builder.js.map +1 -0
- package/dist/services/tool-protection.service.d.ts +209 -0
- package/dist/services/tool-protection.service.d.ts.map +1 -0
- package/dist/services/tool-protection.service.js +752 -0
- package/dist/services/tool-protection.service.js.map +1 -0
- package/dist/session/index.d.ts +5 -0
- package/dist/session/index.d.ts.map +1 -0
- package/dist/session/index.js +11 -0
- package/dist/session/index.js.map +1 -0
- package/dist/session/manager.d.ts +118 -0
- package/dist/session/manager.d.ts.map +1 -0
- package/dist/session/manager.js +285 -0
- package/dist/session/manager.js.map +1 -0
- package/dist/types/oauth-required-error.d.ts +46 -0
- package/dist/types/oauth-required-error.d.ts.map +1 -0
- package/dist/types/oauth-required-error.js +42 -0
- package/dist/types/oauth-required-error.js.map +1 -0
- package/dist/types/tool-protection.d.ts +144 -0
- package/dist/types/tool-protection.d.ts.map +1 -0
- package/dist/types/tool-protection.js +48 -0
- package/dist/types/tool-protection.js.map +1 -0
- package/dist/utils/base58.d.ts +31 -0
- package/dist/utils/base58.d.ts.map +1 -0
- package/dist/utils/base58.js +103 -0
- package/dist/utils/base58.js.map +1 -0
- package/dist/utils/base64.d.ts +32 -0
- package/dist/utils/base64.d.ts.map +1 -0
- package/dist/utils/base64.js +172 -0
- package/dist/utils/base64.js.map +1 -0
- package/dist/utils/cors.d.ts +52 -0
- package/dist/utils/cors.d.ts.map +1 -0
- package/dist/utils/cors.js +79 -0
- package/dist/utils/cors.js.map +1 -0
- package/dist/utils/did-helpers.d.ts +168 -0
- package/dist/utils/did-helpers.d.ts.map +1 -0
- package/dist/utils/did-helpers.js +222 -0
- package/dist/utils/did-helpers.js.map +1 -0
- package/dist/utils/index.d.ts +8 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +24 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/storage-keys.d.ts +120 -0
- package/dist/utils/storage-keys.d.ts.map +1 -0
- package/dist/utils/storage-keys.js +217 -0
- package/dist/utils/storage-keys.js.map +1 -0
- package/dist/wire-format.d.ts +62 -0
- package/dist/wire-format.d.ts.map +1 -0
- package/dist/wire-format.js +89 -0
- package/dist/wire-format.js.map +1 -0
- package/package.json +83 -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__/config.test.ts +307 -0
- package/src/__tests__/identity/user-did-manager.test.ts +232 -0
- package/src/__tests__/index.test.ts +56 -0
- package/src/__tests__/integration/full-flow.test.ts +779 -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 +406 -0
- package/src/__tests__/runtime/audit-logger.test.ts +154 -0
- package/src/__tests__/runtime/base-extensions.test.ts +595 -0
- package/src/__tests__/runtime/base.test.ts +1328 -0
- package/src/__tests__/runtime/delegation-flow.test.ts +159 -0
- package/src/__tests__/runtime/process-tool-call-security.test.ts +1024 -0
- package/src/__tests__/runtime/proof-client-did.test.ts +376 -0
- package/src/__tests__/runtime/provider-selection.test.ts +292 -0
- package/src/__tests__/runtime/route-interception.test.ts +670 -0
- package/src/__tests__/runtime/tool-protection-enforcement.test.ts +989 -0
- package/src/__tests__/services/access-control-retry.test.ts +595 -0
- package/src/__tests__/services/agentshield-integration.test.ts +709 -0
- package/src/__tests__/services/cache-busting.test.ts +125 -0
- package/src/__tests__/services/oauth-service-errors.test.ts +704 -0
- package/src/__tests__/services/oauth-service-pkce.test.ts +556 -0
- package/src/__tests__/services/oauth-service-secret-resolution.test.ts +349 -0
- package/src/__tests__/services/provider-resolver-edge-cases.test.ts +591 -0
- package/src/__tests__/services/tool-protection-failsafe.test.ts +683 -0
- package/src/__tests__/services/tool-protection-merged-config.test.ts +664 -0
- package/src/__tests__/services/tool-protection-oauth-provider.test.ts +300 -0
- package/src/__tests__/services/tool-protection.service.test.ts +1201 -0
- package/src/__tests__/utils/mock-providers.ts +340 -0
- package/src/__tests__/wire-format.test.ts +123 -0
- package/src/auth/__tests__/resume-token.test.ts +42 -0
- package/src/auth/handshake.ts +476 -0
- package/src/auth/index.ts +15 -0
- package/src/auth/types.ts +68 -0
- package/src/cache/__tests__/oauth-config-cache.test.ts +215 -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.map +1 -0
- package/src/cache/oauth-config-cache.ts +123 -0
- package/src/cache/tool-protection-cache.ts +184 -0
- package/src/compliance/EXAMPLE.md +412 -0
- package/src/compliance/__tests__/schema-verifier.test.ts +791 -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__/merged-config.spec.ts +445 -0
- package/src/config/__tests__/remote-config-edge-cases.test.ts +424 -0
- package/src/config/__tests__/remote-config-helpers.test.ts +280 -0
- package/src/config/__tests__/remote-config.spec.ts +265 -0
- package/src/config/remote-config.ts +368 -0
- package/src/config.ts +312 -0
- package/src/identity/__tests__/idp-token-resolver.test.ts +340 -0
- package/src/identity/__tests__/user-did-manager.test.ts +487 -0
- package/src/identity/idp-token-resolver.ts +181 -0
- package/src/identity/idp-token-storage.interface.ts +102 -0
- package/src/identity/user-did-manager.ts +592 -0
- package/src/index.ts +331 -0
- package/src/logging/__tests__/logger.test.ts +366 -0
- package/src/logging/index.ts +10 -0
- package/src/logging/logger.ts +112 -0
- package/src/proof/__tests__/crypto-parity.test.ts +202 -0
- package/src/proof/__tests__/proof-generator.test.ts +484 -0
- package/src/proof/generator.ts +347 -0
- package/src/proof/index.ts +12 -0
- package/src/providers/base.d.ts +91 -0
- package/src/providers/base.d.ts.map +1 -0
- package/src/providers/base.js.map +1 -0
- package/src/providers/base.ts +100 -0
- package/src/providers/memory.ts +142 -0
- package/src/runtime/__tests__/dif-esm-bridge.test.ts +33 -0
- package/src/runtime/__tests__/ext-apps-consent.test.ts +334 -0
- package/src/runtime/audit-logger.ts +39 -0
- package/src/runtime/base.ts +2100 -0
- package/src/runtime/dif-core.ts +39 -0
- package/src/runtime/ext-apps-constants.ts +20 -0
- package/src/services/__tests__/access-control.integration.test.ts +441 -0
- package/src/services/__tests__/access-control.proof-response-validation.test.ts +578 -0
- package/src/services/__tests__/access-control.service.test.ts +1132 -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__/delegation-error-formatter.test.ts +643 -0
- package/src/services/__tests__/oauth-config.service.test.ts +359 -0
- package/src/services/__tests__/oauth-provider-registry.test.ts +142 -0
- package/src/services/__tests__/oauth-token-retrieval.service.test.ts +365 -0
- package/src/services/__tests__/proof-verifier.integration.test.ts +494 -0
- package/src/services/__tests__/proof-verifier.test.ts +489 -0
- package/src/services/__tests__/provider-resolution.integration.test.ts +202 -0
- package/src/services/__tests__/provider-resolver.test.ts +213 -0
- package/src/services/__tests__/provider-validator.test.ts +370 -0
- package/src/services/__tests__/session-registration.service.test.ts +312 -0
- package/src/services/__tests__/storage.service.test.ts +358 -0
- package/src/services/__tests__/tool-context-builder.test.ts +438 -0
- package/src/services/access-control.service.ts +1048 -0
- package/src/services/authorization/authorization-registry.ts +67 -0
- package/src/services/authorization/types.ts +71 -0
- package/src/services/batch-delegation.service.ts +136 -0
- package/src/services/crypto.service.ts +296 -0
- package/src/services/delegation-error-formatter.ts +299 -0
- package/src/services/errors.ts +76 -0
- package/src/services/index.ts +34 -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.map +1 -0
- package/src/services/oauth-config.service.ts +192 -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.map +1 -0
- package/src/services/oauth-provider-registry.ts +141 -0
- package/src/services/oauth-service.ts +652 -0
- package/src/services/oauth-token-retrieval.service.ts +245 -0
- package/src/services/policy.service.ts +527 -0
- package/src/services/proof-verifier.ts +295 -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.map +1 -0
- package/src/services/provider-resolver.ts +236 -0
- package/src/services/provider-validator.ts +170 -0
- package/src/services/session-registration.service.ts +259 -0
- package/src/services/storage.service.ts +566 -0
- package/src/services/tool-context-builder.ts +285 -0
- package/src/services/tool-protection.service.ts +971 -0
- package/src/session/__tests__/session-manager.test.ts +327 -0
- package/src/session/index.ts +10 -0
- package/src/session/manager.ts +380 -0
- package/src/types/__tests__/oauth-required-error.test.ts +79 -0
- package/src/types/__tests__/tool-protection.test.ts +121 -0
- package/src/types/oauth-required-error.ts +71 -0
- package/src/types/tool-protection.ts +179 -0
- package/src/utils/__tests__/base58.test.ts +281 -0
- package/src/utils/__tests__/base64.test.ts +239 -0
- package/src/utils/__tests__/cors.test.ts +184 -0
- package/src/utils/__tests__/did-helpers.test.ts +156 -0
- package/src/utils/__tests__/storage-keys.test.ts +372 -0
- package/src/utils/base58.ts +109 -0
- package/src/utils/base64.ts +185 -0
- package/src/utils/cors.ts +93 -0
- package/src/utils/did-helpers.ts +229 -0
- package/src/utils/index.ts +8 -0
- package/src/utils/storage-keys.ts +285 -0
- package/src/wire-format.ts +114 -0
- package/tsconfig.json +21 -0
- package/vitest.config.mts +57 -0
|
@@ -0,0 +1,1132 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unit Tests for AccessControlApiService
|
|
3
|
+
*
|
|
4
|
+
* Comprehensive test coverage for the access control API service.
|
|
5
|
+
* Tests all methods (fetchConfig, verifyDelegation, submitProofs) with
|
|
6
|
+
* success cases, error cases, retry logic, and validation.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { describe, it, expect, beforeEach, vi } from "vitest";
|
|
10
|
+
import {
|
|
11
|
+
AccessControlApiService,
|
|
12
|
+
authorizationMatches,
|
|
13
|
+
} from "../access-control.service.js";
|
|
14
|
+
import { AgentShieldAPIError } from "@kya-os/contracts/agentshield-api";
|
|
15
|
+
import type { FetchProvider } from "../../providers/base.js";
|
|
16
|
+
import type {
|
|
17
|
+
VerifyDelegationRequest,
|
|
18
|
+
ToolProtectionConfigAPIResponse,
|
|
19
|
+
ProofSubmissionRequest,
|
|
20
|
+
} from "@kya-os/contracts/agentshield-api";
|
|
21
|
+
|
|
22
|
+
describe("AccessControlApiService", () => {
|
|
23
|
+
let service: AccessControlApiService;
|
|
24
|
+
let mockFetchProvider: FetchProvider;
|
|
25
|
+
let mockLoggerFn: ReturnType<typeof vi.fn>;
|
|
26
|
+
let mockSleepFn: ReturnType<typeof vi.fn>;
|
|
27
|
+
|
|
28
|
+
beforeEach(() => {
|
|
29
|
+
mockLoggerFn = vi.fn();
|
|
30
|
+
mockSleepFn = vi.fn().mockResolvedValue(undefined);
|
|
31
|
+
|
|
32
|
+
mockFetchProvider = {
|
|
33
|
+
resolveDID: vi.fn(),
|
|
34
|
+
fetchStatusList: vi.fn(),
|
|
35
|
+
fetchDelegationChain: vi.fn(),
|
|
36
|
+
fetch: vi.fn(),
|
|
37
|
+
} as unknown as FetchProvider;
|
|
38
|
+
|
|
39
|
+
service = new AccessControlApiService({
|
|
40
|
+
baseUrl: "https://api.example.com",
|
|
41
|
+
apiKey: "test-api-key",
|
|
42
|
+
fetchProvider: mockFetchProvider,
|
|
43
|
+
logger: mockLoggerFn as (message: string, data?: unknown) => void,
|
|
44
|
+
sleepProvider: mockSleepFn as (ms: number) => Promise<void>,
|
|
45
|
+
retryConfig: {
|
|
46
|
+
maxRetries: 2,
|
|
47
|
+
initialDelayMs: 10,
|
|
48
|
+
maxDelayMs: 100,
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
describe("fetchConfig", () => {
|
|
54
|
+
it("should fetch config successfully", async () => {
|
|
55
|
+
const mockResponse: ToolProtectionConfigAPIResponse = {
|
|
56
|
+
success: true,
|
|
57
|
+
data: {
|
|
58
|
+
agent_did: "did:key:z123",
|
|
59
|
+
tools: {
|
|
60
|
+
tool1: {
|
|
61
|
+
scopes: ["scope1"],
|
|
62
|
+
requires_delegation: true,
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
metadata: {
|
|
67
|
+
requestId: "test-request-id",
|
|
68
|
+
timestamp: new Date().toISOString(),
|
|
69
|
+
},
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
(mockFetchProvider.fetch as ReturnType<typeof vi.fn>).mockResolvedValue(
|
|
73
|
+
new Response(JSON.stringify(mockResponse), {
|
|
74
|
+
status: 200,
|
|
75
|
+
headers: { "Content-Type": "application/json" },
|
|
76
|
+
})
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
const result = await service.fetchConfig({ agentDid: "did:key:z123" });
|
|
80
|
+
|
|
81
|
+
expect(result).toEqual(mockResponse);
|
|
82
|
+
expect(mockFetchProvider.fetch).toHaveBeenCalledWith(
|
|
83
|
+
"https://api.example.com/api/v1/bouncer/config?agent_did=did%3Akey%3Az123",
|
|
84
|
+
expect.objectContaining({
|
|
85
|
+
method: "GET",
|
|
86
|
+
headers: expect.objectContaining({
|
|
87
|
+
Authorization: "Bearer test-api-key",
|
|
88
|
+
"Content-Type": "application/json",
|
|
89
|
+
}),
|
|
90
|
+
})
|
|
91
|
+
);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it("should handle 404 error", async () => {
|
|
95
|
+
const errorResponse = {
|
|
96
|
+
success: false,
|
|
97
|
+
error: {
|
|
98
|
+
code: "config_not_found",
|
|
99
|
+
message: "Config not found",
|
|
100
|
+
},
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
(mockFetchProvider.fetch as ReturnType<typeof vi.fn>).mockResolvedValue(
|
|
104
|
+
new Response(JSON.stringify(errorResponse), {
|
|
105
|
+
status: 404,
|
|
106
|
+
headers: { "Content-Type": "application/json" },
|
|
107
|
+
})
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
await expect(
|
|
111
|
+
service.fetchConfig({ agentDid: "did:key:z123" })
|
|
112
|
+
).rejects.toThrow(AgentShieldAPIError);
|
|
113
|
+
|
|
114
|
+
const metrics = service.getMetrics();
|
|
115
|
+
expect(metrics.errorCount).toBe(1);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it("should retry on 500 error", async () => {
|
|
119
|
+
const mockResponse: ToolProtectionConfigAPIResponse = {
|
|
120
|
+
success: true,
|
|
121
|
+
data: {
|
|
122
|
+
agent_did: "did:key:z123",
|
|
123
|
+
tools: {},
|
|
124
|
+
},
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
// First call fails with 500, second succeeds
|
|
128
|
+
(mockFetchProvider.fetch as ReturnType<typeof vi.fn>)
|
|
129
|
+
.mockResolvedValueOnce(
|
|
130
|
+
new Response("Internal Server Error", { status: 500 })
|
|
131
|
+
)
|
|
132
|
+
.mockResolvedValueOnce(
|
|
133
|
+
new Response(JSON.stringify(mockResponse), {
|
|
134
|
+
status: 200,
|
|
135
|
+
headers: { "Content-Type": "application/json" },
|
|
136
|
+
})
|
|
137
|
+
);
|
|
138
|
+
|
|
139
|
+
const result = await service.fetchConfig({ agentDid: "did:key:z123" });
|
|
140
|
+
|
|
141
|
+
expect(result).toEqual(mockResponse);
|
|
142
|
+
expect(mockFetchProvider.fetch).toHaveBeenCalledTimes(2);
|
|
143
|
+
expect(mockSleepFn).toHaveBeenCalled();
|
|
144
|
+
expect(service.getMetrics().retryCount).toBe(1);
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it("should handle invalid JSON response", async () => {
|
|
148
|
+
(mockFetchProvider.fetch as ReturnType<typeof vi.fn>)
|
|
149
|
+
.mockResolvedValueOnce(
|
|
150
|
+
new Response("invalid json", {
|
|
151
|
+
status: 200,
|
|
152
|
+
headers: { "Content-Type": "application/json" },
|
|
153
|
+
})
|
|
154
|
+
)
|
|
155
|
+
.mockResolvedValueOnce(
|
|
156
|
+
new Response("invalid json", {
|
|
157
|
+
status: 200,
|
|
158
|
+
headers: { "Content-Type": "application/json" },
|
|
159
|
+
})
|
|
160
|
+
);
|
|
161
|
+
|
|
162
|
+
await expect(
|
|
163
|
+
service.fetchConfig({ agentDid: "did:key:z123" })
|
|
164
|
+
).rejects.toThrow(AgentShieldAPIError);
|
|
165
|
+
|
|
166
|
+
const error = await service
|
|
167
|
+
.fetchConfig({ agentDid: "did:key:z123" })
|
|
168
|
+
.catch((e) => e);
|
|
169
|
+
expect(error).toBeInstanceOf(AgentShieldAPIError);
|
|
170
|
+
expect(error.code).toBe("invalid_response");
|
|
171
|
+
});
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
describe("verifyDelegation", () => {
|
|
175
|
+
it("should verify delegation successfully", async () => {
|
|
176
|
+
const request: VerifyDelegationRequest = {
|
|
177
|
+
agent_did: "did:key:z123",
|
|
178
|
+
scopes: ["scope1", "scope2"],
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
const mockResponse = {
|
|
182
|
+
success: true,
|
|
183
|
+
data: {
|
|
184
|
+
valid: true,
|
|
185
|
+
delegation_id: "123e4567-e89b-12d3-a456-426614174000", // Valid UUID format
|
|
186
|
+
credential: {
|
|
187
|
+
agent_did: "did:key:z123",
|
|
188
|
+
user_did: "did:key:zUser123", // REQUIRED: User DID
|
|
189
|
+
scopes: ["scope1", "scope2"],
|
|
190
|
+
issued_at: Math.floor(Date.now() / 1000), // Unix timestamp (positive integer)
|
|
191
|
+
created_at: Math.floor(Date.now() / 1000), // Unix timestamp (positive integer)
|
|
192
|
+
authorization: { type: "oauth", provider: "github" }, // REQUIRED: Authorization method
|
|
193
|
+
},
|
|
194
|
+
},
|
|
195
|
+
metadata: {
|
|
196
|
+
requestId: "test-request-id",
|
|
197
|
+
timestamp: new Date().toISOString(),
|
|
198
|
+
},
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
(mockFetchProvider.fetch as ReturnType<typeof vi.fn>).mockResolvedValue(
|
|
202
|
+
new Response(JSON.stringify(mockResponse), {
|
|
203
|
+
status: 200,
|
|
204
|
+
headers: { "Content-Type": "application/json" },
|
|
205
|
+
})
|
|
206
|
+
);
|
|
207
|
+
|
|
208
|
+
const result = await service.verifyDelegation(request);
|
|
209
|
+
|
|
210
|
+
expect(result.data.valid).toBe(true);
|
|
211
|
+
expect(mockFetchProvider.fetch).toHaveBeenCalledWith(
|
|
212
|
+
"https://api.example.com/api/v1/bouncer/delegations/verify",
|
|
213
|
+
expect.objectContaining({
|
|
214
|
+
method: "POST",
|
|
215
|
+
body: JSON.stringify({
|
|
216
|
+
agent_did: "did:key:z123",
|
|
217
|
+
scopes: ["scope1", "scope2"],
|
|
218
|
+
}),
|
|
219
|
+
})
|
|
220
|
+
);
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
it("should omit scopes field when scopes is undefined (truly optional)", async () => {
|
|
224
|
+
const request: VerifyDelegationRequest = {
|
|
225
|
+
agent_did: "did:key:z123",
|
|
226
|
+
// scopes is intentionally omitted
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
const mockResponse = {
|
|
230
|
+
success: true,
|
|
231
|
+
data: { valid: true },
|
|
232
|
+
metadata: {
|
|
233
|
+
requestId: "test-request-id",
|
|
234
|
+
timestamp: new Date().toISOString(),
|
|
235
|
+
},
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
(mockFetchProvider.fetch as ReturnType<typeof vi.fn>).mockResolvedValue(
|
|
239
|
+
new Response(JSON.stringify(mockResponse), {
|
|
240
|
+
status: 200,
|
|
241
|
+
headers: { "Content-Type": "application/json" },
|
|
242
|
+
})
|
|
243
|
+
);
|
|
244
|
+
|
|
245
|
+
try {
|
|
246
|
+
await service.verifyDelegation(request);
|
|
247
|
+
} catch (error: any) {
|
|
248
|
+
// Log validation errors and mock logger calls for debugging
|
|
249
|
+
if (error.details?.zodErrors) {
|
|
250
|
+
console.log(
|
|
251
|
+
"Validation errors:",
|
|
252
|
+
JSON.stringify(error.details.zodErrors, null, 2)
|
|
253
|
+
);
|
|
254
|
+
}
|
|
255
|
+
// Check mock logger calls
|
|
256
|
+
const loggerCalls = mockLoggerFn.mock.calls;
|
|
257
|
+
const debugCall = loggerCalls.find((call: any[]) =>
|
|
258
|
+
call[0]?.includes("Request body debug")
|
|
259
|
+
);
|
|
260
|
+
if (debugCall) {
|
|
261
|
+
console.log("Debug log:", JSON.stringify(debugCall[1], null, 2));
|
|
262
|
+
}
|
|
263
|
+
throw error;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
const callArgs = (mockFetchProvider.fetch as ReturnType<typeof vi.fn>)
|
|
267
|
+
.mock.calls[0];
|
|
268
|
+
const requestBody = JSON.parse(callArgs[1].body as string);
|
|
269
|
+
|
|
270
|
+
// Verify scopes field is not included in request body when undefined
|
|
271
|
+
expect(requestBody).not.toHaveProperty("scopes");
|
|
272
|
+
expect(requestBody.agent_did).toBe("did:key:z123");
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
it("should include scopes field when scopes is provided", async () => {
|
|
276
|
+
const request: VerifyDelegationRequest = {
|
|
277
|
+
agent_did: "did:key:z123",
|
|
278
|
+
scopes: ["scope1", "scope2"],
|
|
279
|
+
};
|
|
280
|
+
|
|
281
|
+
const mockResponse = {
|
|
282
|
+
success: true,
|
|
283
|
+
data: { valid: true },
|
|
284
|
+
metadata: {
|
|
285
|
+
requestId: "test-request-id",
|
|
286
|
+
timestamp: new Date().toISOString(),
|
|
287
|
+
},
|
|
288
|
+
};
|
|
289
|
+
|
|
290
|
+
(mockFetchProvider.fetch as ReturnType<typeof vi.fn>).mockResolvedValue(
|
|
291
|
+
new Response(JSON.stringify(mockResponse), {
|
|
292
|
+
status: 200,
|
|
293
|
+
headers: { "Content-Type": "application/json" },
|
|
294
|
+
})
|
|
295
|
+
);
|
|
296
|
+
|
|
297
|
+
await service.verifyDelegation(request);
|
|
298
|
+
|
|
299
|
+
const callArgs = (mockFetchProvider.fetch as ReturnType<typeof vi.fn>)
|
|
300
|
+
.mock.calls[0];
|
|
301
|
+
const requestBody = JSON.parse(callArgs[1].body as string);
|
|
302
|
+
|
|
303
|
+
// Verify scopes field is included when provided
|
|
304
|
+
expect(requestBody.scopes).toEqual(["scope1", "scope2"]);
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
it("should include delegation_token from context", async () => {
|
|
308
|
+
const request: VerifyDelegationRequest = {
|
|
309
|
+
agent_did: "did:key:z123",
|
|
310
|
+
scopes: ["scope1"],
|
|
311
|
+
};
|
|
312
|
+
|
|
313
|
+
const mockResponse = {
|
|
314
|
+
success: true,
|
|
315
|
+
data: { valid: true },
|
|
316
|
+
metadata: {
|
|
317
|
+
requestId: "test-request-id",
|
|
318
|
+
timestamp: new Date().toISOString(),
|
|
319
|
+
},
|
|
320
|
+
};
|
|
321
|
+
|
|
322
|
+
(mockFetchProvider.fetch as ReturnType<typeof vi.fn>).mockResolvedValue(
|
|
323
|
+
new Response(JSON.stringify(mockResponse), {
|
|
324
|
+
status: 200,
|
|
325
|
+
headers: { "Content-Type": "application/json" },
|
|
326
|
+
})
|
|
327
|
+
);
|
|
328
|
+
|
|
329
|
+
await service.verifyDelegation(request, {
|
|
330
|
+
delegationToken: "token-123",
|
|
331
|
+
credentialJwt: "jwt-123",
|
|
332
|
+
});
|
|
333
|
+
|
|
334
|
+
expect(mockFetchProvider.fetch).toHaveBeenCalledWith(
|
|
335
|
+
expect.any(String),
|
|
336
|
+
expect.objectContaining({
|
|
337
|
+
body: expect.stringContaining("delegation_token"),
|
|
338
|
+
})
|
|
339
|
+
);
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
it("should handle validation error", async () => {
|
|
343
|
+
const invalidRequest = {
|
|
344
|
+
agent_did: "", // Invalid: empty string
|
|
345
|
+
scopes: [],
|
|
346
|
+
} as VerifyDelegationRequest;
|
|
347
|
+
|
|
348
|
+
// Mock fetch twice since we call verifyDelegation twice
|
|
349
|
+
(mockFetchProvider.fetch as ReturnType<typeof vi.fn>)
|
|
350
|
+
.mockResolvedValueOnce(
|
|
351
|
+
new Response(
|
|
352
|
+
JSON.stringify({
|
|
353
|
+
success: false,
|
|
354
|
+
error: {
|
|
355
|
+
code: "validation_error",
|
|
356
|
+
message: "Invalid request",
|
|
357
|
+
},
|
|
358
|
+
}),
|
|
359
|
+
{
|
|
360
|
+
status: 400,
|
|
361
|
+
headers: { "Content-Type": "application/json" },
|
|
362
|
+
}
|
|
363
|
+
)
|
|
364
|
+
)
|
|
365
|
+
.mockResolvedValueOnce(
|
|
366
|
+
new Response(
|
|
367
|
+
JSON.stringify({
|
|
368
|
+
success: false,
|
|
369
|
+
error: {
|
|
370
|
+
code: "validation_error",
|
|
371
|
+
message: "Invalid request",
|
|
372
|
+
},
|
|
373
|
+
}),
|
|
374
|
+
{
|
|
375
|
+
status: 400,
|
|
376
|
+
headers: { "Content-Type": "application/json" },
|
|
377
|
+
}
|
|
378
|
+
)
|
|
379
|
+
);
|
|
380
|
+
|
|
381
|
+
await expect(service.verifyDelegation(invalidRequest)).rejects.toThrow(
|
|
382
|
+
AgentShieldAPIError
|
|
383
|
+
);
|
|
384
|
+
|
|
385
|
+
const error = await service
|
|
386
|
+
.verifyDelegation(invalidRequest)
|
|
387
|
+
.catch((e) => e);
|
|
388
|
+
expect(error.code).toBe("validation_error");
|
|
389
|
+
});
|
|
390
|
+
|
|
391
|
+
it("should handle 401 authentication error", async () => {
|
|
392
|
+
const request: VerifyDelegationRequest = {
|
|
393
|
+
agent_did: "did:key:z123",
|
|
394
|
+
scopes: ["scope1"],
|
|
395
|
+
};
|
|
396
|
+
|
|
397
|
+
// Mock fetch twice since we call verifyDelegation twice
|
|
398
|
+
(mockFetchProvider.fetch as ReturnType<typeof vi.fn>)
|
|
399
|
+
.mockResolvedValueOnce(
|
|
400
|
+
new Response(
|
|
401
|
+
JSON.stringify({
|
|
402
|
+
success: false,
|
|
403
|
+
error: {
|
|
404
|
+
code: "authentication_failed",
|
|
405
|
+
message: "Invalid API key",
|
|
406
|
+
},
|
|
407
|
+
}),
|
|
408
|
+
{
|
|
409
|
+
status: 401,
|
|
410
|
+
headers: { "Content-Type": "application/json" },
|
|
411
|
+
}
|
|
412
|
+
)
|
|
413
|
+
)
|
|
414
|
+
.mockResolvedValueOnce(
|
|
415
|
+
new Response(
|
|
416
|
+
JSON.stringify({
|
|
417
|
+
success: false,
|
|
418
|
+
error: {
|
|
419
|
+
code: "authentication_failed",
|
|
420
|
+
message: "Invalid API key",
|
|
421
|
+
},
|
|
422
|
+
}),
|
|
423
|
+
{
|
|
424
|
+
status: 401,
|
|
425
|
+
headers: { "Content-Type": "application/json" },
|
|
426
|
+
}
|
|
427
|
+
)
|
|
428
|
+
);
|
|
429
|
+
|
|
430
|
+
await expect(service.verifyDelegation(request)).rejects.toThrow(
|
|
431
|
+
AgentShieldAPIError
|
|
432
|
+
);
|
|
433
|
+
|
|
434
|
+
const error = await service.verifyDelegation(request).catch((e) => e);
|
|
435
|
+
expect(error.code).toBe("authentication_failed");
|
|
436
|
+
});
|
|
437
|
+
});
|
|
438
|
+
|
|
439
|
+
describe("submitProofs", () => {
|
|
440
|
+
it("should submit proofs successfully", async () => {
|
|
441
|
+
const request: ProofSubmissionRequest = {
|
|
442
|
+
session_id: "123e4567-e89b-12d3-a456-426614174000", // Valid UUID
|
|
443
|
+
delegation_id: null, // Explicitly set to null
|
|
444
|
+
proofs: [
|
|
445
|
+
{
|
|
446
|
+
jws: "header.payload.signature",
|
|
447
|
+
meta: {
|
|
448
|
+
did: "did:key:z123",
|
|
449
|
+
kid: "did:key:z123#key-1",
|
|
450
|
+
ts: Math.floor(Date.now() / 1000), // Unix timestamp in seconds
|
|
451
|
+
nonce: "nonce-123",
|
|
452
|
+
audience: "mcp-client",
|
|
453
|
+
sessionId: "session-123",
|
|
454
|
+
requestHash: "sha256:" + "a".repeat(64),
|
|
455
|
+
responseHash: "sha256:" + "b".repeat(64),
|
|
456
|
+
},
|
|
457
|
+
},
|
|
458
|
+
],
|
|
459
|
+
};
|
|
460
|
+
|
|
461
|
+
const mockResponse = {
|
|
462
|
+
// Direct ProofSubmissionResponse (not wrapped)
|
|
463
|
+
success: true,
|
|
464
|
+
accepted: 1,
|
|
465
|
+
rejected: 0,
|
|
466
|
+
outcomes: {
|
|
467
|
+
success: 1,
|
|
468
|
+
failed: 0,
|
|
469
|
+
blocked: 0,
|
|
470
|
+
error: 0,
|
|
471
|
+
},
|
|
472
|
+
};
|
|
473
|
+
|
|
474
|
+
(mockFetchProvider.fetch as ReturnType<typeof vi.fn>).mockResolvedValue(
|
|
475
|
+
new Response(JSON.stringify(mockResponse), {
|
|
476
|
+
status: 200,
|
|
477
|
+
headers: { "Content-Type": "application/json" },
|
|
478
|
+
})
|
|
479
|
+
);
|
|
480
|
+
|
|
481
|
+
const result = await service.submitProofs(request);
|
|
482
|
+
|
|
483
|
+
expect(result.accepted).toBe(1);
|
|
484
|
+
expect(result.rejected).toBe(0);
|
|
485
|
+
expect(mockFetchProvider.fetch).toHaveBeenCalledWith(
|
|
486
|
+
"https://api.example.com/api/v1/bouncer/proofs",
|
|
487
|
+
expect.objectContaining({
|
|
488
|
+
method: "POST",
|
|
489
|
+
body: JSON.stringify(request),
|
|
490
|
+
})
|
|
491
|
+
);
|
|
492
|
+
});
|
|
493
|
+
|
|
494
|
+
it("should handle all_proofs_rejected error gracefully", async () => {
|
|
495
|
+
const request: ProofSubmissionRequest = {
|
|
496
|
+
session_id: "123e4567-e89b-12d3-a456-426614174000", // Valid UUID
|
|
497
|
+
delegation_id: null, // Explicitly set to null
|
|
498
|
+
proofs: [
|
|
499
|
+
{
|
|
500
|
+
jws: "header.payload.signature",
|
|
501
|
+
meta: {
|
|
502
|
+
did: "did:key:z123",
|
|
503
|
+
kid: "did:key:z123#key-1",
|
|
504
|
+
ts: Math.floor(Date.now() / 1000), // Unix timestamp in seconds
|
|
505
|
+
nonce: "nonce-123",
|
|
506
|
+
audience: "mcp-client",
|
|
507
|
+
sessionId: "session-123",
|
|
508
|
+
requestHash: "sha256:" + "a".repeat(64),
|
|
509
|
+
responseHash: "sha256:" + "b".repeat(64),
|
|
510
|
+
},
|
|
511
|
+
},
|
|
512
|
+
],
|
|
513
|
+
};
|
|
514
|
+
|
|
515
|
+
const errorResponse = {
|
|
516
|
+
success: false,
|
|
517
|
+
error: {
|
|
518
|
+
code: "all_proofs_rejected",
|
|
519
|
+
message: "All proofs rejected",
|
|
520
|
+
details: {
|
|
521
|
+
rejected: 1,
|
|
522
|
+
errors: [
|
|
523
|
+
{
|
|
524
|
+
proof_index: 0,
|
|
525
|
+
error: {
|
|
526
|
+
code: "invalid_signature",
|
|
527
|
+
message: "Invalid signature",
|
|
528
|
+
},
|
|
529
|
+
},
|
|
530
|
+
],
|
|
531
|
+
},
|
|
532
|
+
},
|
|
533
|
+
};
|
|
534
|
+
|
|
535
|
+
(mockFetchProvider.fetch as ReturnType<typeof vi.fn>).mockResolvedValue(
|
|
536
|
+
new Response(JSON.stringify(errorResponse), {
|
|
537
|
+
status: 400,
|
|
538
|
+
headers: { "Content-Type": "application/json" },
|
|
539
|
+
})
|
|
540
|
+
);
|
|
541
|
+
|
|
542
|
+
const result = await service.submitProofs(request);
|
|
543
|
+
|
|
544
|
+
// ProofSubmissionResponse has a success field
|
|
545
|
+
expect(result.success).toBe(false);
|
|
546
|
+
expect(result.accepted).toBe(0);
|
|
547
|
+
expect(result.rejected).toBe(1);
|
|
548
|
+
expect(result.errors).toBeDefined();
|
|
549
|
+
});
|
|
550
|
+
|
|
551
|
+
it("should handle wrapped response format", async () => {
|
|
552
|
+
const request: ProofSubmissionRequest = {
|
|
553
|
+
session_id: "123e4567-e89b-12d3-a456-426614174000", // Valid UUID
|
|
554
|
+
delegation_id: null, // Explicitly set to null
|
|
555
|
+
proofs: [
|
|
556
|
+
{
|
|
557
|
+
jws: "header.payload.signature",
|
|
558
|
+
meta: {
|
|
559
|
+
did: "did:key:z123",
|
|
560
|
+
kid: "did:key:z123#key-1",
|
|
561
|
+
ts: Math.floor(Date.now() / 1000), // Unix timestamp in seconds
|
|
562
|
+
nonce: "nonce-123",
|
|
563
|
+
audience: "mcp-client",
|
|
564
|
+
sessionId: "session-123",
|
|
565
|
+
requestHash: "sha256:" + "a".repeat(64),
|
|
566
|
+
responseHash: "sha256:" + "b".repeat(64),
|
|
567
|
+
},
|
|
568
|
+
},
|
|
569
|
+
],
|
|
570
|
+
};
|
|
571
|
+
|
|
572
|
+
const wrappedResponse = {
|
|
573
|
+
success: true,
|
|
574
|
+
data: {
|
|
575
|
+
// ProofSubmissionResponse has a success field
|
|
576
|
+
success: true,
|
|
577
|
+
accepted: 1,
|
|
578
|
+
rejected: 0,
|
|
579
|
+
outcomes: {
|
|
580
|
+
success: 1,
|
|
581
|
+
failed: 0,
|
|
582
|
+
blocked: 0,
|
|
583
|
+
error: 0,
|
|
584
|
+
},
|
|
585
|
+
},
|
|
586
|
+
metadata: {
|
|
587
|
+
requestId: "test-request-id",
|
|
588
|
+
timestamp: new Date().toISOString(),
|
|
589
|
+
},
|
|
590
|
+
};
|
|
591
|
+
|
|
592
|
+
(mockFetchProvider.fetch as ReturnType<typeof vi.fn>).mockResolvedValue(
|
|
593
|
+
new Response(JSON.stringify(wrappedResponse), {
|
|
594
|
+
status: 200,
|
|
595
|
+
headers: { "Content-Type": "application/json" },
|
|
596
|
+
})
|
|
597
|
+
);
|
|
598
|
+
|
|
599
|
+
const result = await service.submitProofs(request);
|
|
600
|
+
|
|
601
|
+
expect(result.accepted).toBe(1);
|
|
602
|
+
});
|
|
603
|
+
|
|
604
|
+
it("should handle invalid response format with detailed error logging", async () => {
|
|
605
|
+
const request: ProofSubmissionRequest = {
|
|
606
|
+
session_id: "123e4567-e89b-12d3-a456-426614174000",
|
|
607
|
+
delegation_id: null,
|
|
608
|
+
proofs: [
|
|
609
|
+
{
|
|
610
|
+
jws: "header.payload.signature",
|
|
611
|
+
meta: {
|
|
612
|
+
did: "did:key:z123",
|
|
613
|
+
kid: "did:key:z123#key-1",
|
|
614
|
+
ts: Math.floor(Date.now() / 1000),
|
|
615
|
+
nonce: "nonce-123",
|
|
616
|
+
audience: "mcp-client",
|
|
617
|
+
sessionId: "session-123",
|
|
618
|
+
requestHash: "sha256:" + "a".repeat(64),
|
|
619
|
+
responseHash: "sha256:" + "b".repeat(64),
|
|
620
|
+
},
|
|
621
|
+
},
|
|
622
|
+
],
|
|
623
|
+
};
|
|
624
|
+
|
|
625
|
+
// Invalid response: missing required fields (accepted, rejected, outcomes)
|
|
626
|
+
const invalidResponse = {
|
|
627
|
+
success: true,
|
|
628
|
+
message: "Proof submitted", // Wrong format - missing required fields
|
|
629
|
+
};
|
|
630
|
+
|
|
631
|
+
const consoleErrorSpy = vi
|
|
632
|
+
.spyOn(console, "error")
|
|
633
|
+
.mockImplementation(() => {});
|
|
634
|
+
|
|
635
|
+
(mockFetchProvider.fetch as ReturnType<typeof vi.fn>).mockResolvedValue(
|
|
636
|
+
new Response(JSON.stringify(invalidResponse), {
|
|
637
|
+
status: 200,
|
|
638
|
+
headers: { "Content-Type": "application/json" },
|
|
639
|
+
})
|
|
640
|
+
);
|
|
641
|
+
|
|
642
|
+
const error = await service.submitProofs(request).catch((e) => e);
|
|
643
|
+
expect(error).toBeInstanceOf(AgentShieldAPIError);
|
|
644
|
+
expect((error as AgentShieldAPIError).code).toBe("invalid_response");
|
|
645
|
+
expect((error as AgentShieldAPIError).message).toBe(
|
|
646
|
+
"Response validation failed"
|
|
647
|
+
);
|
|
648
|
+
|
|
649
|
+
// Verify detailed error logging
|
|
650
|
+
expect(consoleErrorSpy).toHaveBeenCalled();
|
|
651
|
+
const errorCall = consoleErrorSpy.mock.calls.find((call) =>
|
|
652
|
+
call[0]?.includes("Response validation failed")
|
|
653
|
+
);
|
|
654
|
+
expect(errorCall).toBeDefined();
|
|
655
|
+
expect(errorCall?.[1]).toHaveProperty("zodErrors");
|
|
656
|
+
expect(errorCall?.[1]).toHaveProperty("responseData");
|
|
657
|
+
|
|
658
|
+
consoleErrorSpy.mockRestore();
|
|
659
|
+
});
|
|
660
|
+
|
|
661
|
+
it("should handle response with missing outcomes field (outcomes is optional)", async () => {
|
|
662
|
+
const request: ProofSubmissionRequest = {
|
|
663
|
+
session_id: "123e4567-e89b-12d3-a456-426614174000",
|
|
664
|
+
delegation_id: null,
|
|
665
|
+
proofs: [
|
|
666
|
+
{
|
|
667
|
+
jws: "header.payload.signature",
|
|
668
|
+
meta: {
|
|
669
|
+
did: "did:key:z123",
|
|
670
|
+
kid: "did:key:z123#key-1",
|
|
671
|
+
ts: Math.floor(Date.now() / 1000),
|
|
672
|
+
nonce: "nonce-123",
|
|
673
|
+
audience: "mcp-client",
|
|
674
|
+
sessionId: "session-123",
|
|
675
|
+
requestHash: "sha256:" + "a".repeat(64),
|
|
676
|
+
responseHash: "sha256:" + "b".repeat(64),
|
|
677
|
+
},
|
|
678
|
+
},
|
|
679
|
+
],
|
|
680
|
+
};
|
|
681
|
+
|
|
682
|
+
// Test 1: Response WITHOUT outcomes field (valid - outcomes is optional)
|
|
683
|
+
const responseWithoutOutcomes = {
|
|
684
|
+
success: true,
|
|
685
|
+
accepted: 1,
|
|
686
|
+
rejected: 0,
|
|
687
|
+
// Missing outcomes field - this is now valid
|
|
688
|
+
};
|
|
689
|
+
|
|
690
|
+
(
|
|
691
|
+
mockFetchProvider.fetch as ReturnType<typeof vi.fn>
|
|
692
|
+
).mockResolvedValueOnce(
|
|
693
|
+
new Response(JSON.stringify(responseWithoutOutcomes), {
|
|
694
|
+
status: 200,
|
|
695
|
+
headers: { "Content-Type": "application/json" },
|
|
696
|
+
})
|
|
697
|
+
);
|
|
698
|
+
|
|
699
|
+
const result1 = await service.submitProofs(request);
|
|
700
|
+
expect(result1).toEqual({
|
|
701
|
+
success: true,
|
|
702
|
+
accepted: 1,
|
|
703
|
+
rejected: 0,
|
|
704
|
+
outcomes: undefined, // outcomes is optional
|
|
705
|
+
});
|
|
706
|
+
|
|
707
|
+
// Test 2: Response WITH outcomes field (also valid)
|
|
708
|
+
const responseWithOutcomes = {
|
|
709
|
+
success: true,
|
|
710
|
+
accepted: 1,
|
|
711
|
+
rejected: 0,
|
|
712
|
+
outcomes: {
|
|
713
|
+
success: 1,
|
|
714
|
+
failed: 0,
|
|
715
|
+
blocked: 0,
|
|
716
|
+
error: 0,
|
|
717
|
+
},
|
|
718
|
+
};
|
|
719
|
+
|
|
720
|
+
(
|
|
721
|
+
mockFetchProvider.fetch as ReturnType<typeof vi.fn>
|
|
722
|
+
).mockResolvedValueOnce(
|
|
723
|
+
new Response(JSON.stringify(responseWithOutcomes), {
|
|
724
|
+
status: 200,
|
|
725
|
+
headers: { "Content-Type": "application/json" },
|
|
726
|
+
})
|
|
727
|
+
);
|
|
728
|
+
|
|
729
|
+
const result2 = await service.submitProofs(request);
|
|
730
|
+
expect(result2).toEqual({
|
|
731
|
+
success: true,
|
|
732
|
+
accepted: 1,
|
|
733
|
+
rejected: 0,
|
|
734
|
+
outcomes: {
|
|
735
|
+
success: 1,
|
|
736
|
+
failed: 0,
|
|
737
|
+
blocked: 0,
|
|
738
|
+
error: 0,
|
|
739
|
+
},
|
|
740
|
+
});
|
|
741
|
+
|
|
742
|
+
// Test 3: Response with empty outcomes object (also valid)
|
|
743
|
+
const responseWithEmptyOutcomes = {
|
|
744
|
+
success: true,
|
|
745
|
+
accepted: 1,
|
|
746
|
+
rejected: 0,
|
|
747
|
+
outcomes: {},
|
|
748
|
+
};
|
|
749
|
+
|
|
750
|
+
(
|
|
751
|
+
mockFetchProvider.fetch as ReturnType<typeof vi.fn>
|
|
752
|
+
).mockResolvedValueOnce(
|
|
753
|
+
new Response(JSON.stringify(responseWithEmptyOutcomes), {
|
|
754
|
+
status: 200,
|
|
755
|
+
headers: { "Content-Type": "application/json" },
|
|
756
|
+
})
|
|
757
|
+
);
|
|
758
|
+
|
|
759
|
+
const result3 = await service.submitProofs(request);
|
|
760
|
+
expect(result3).toEqual({
|
|
761
|
+
success: true,
|
|
762
|
+
accepted: 1,
|
|
763
|
+
rejected: 0,
|
|
764
|
+
outcomes: {},
|
|
765
|
+
});
|
|
766
|
+
});
|
|
767
|
+
|
|
768
|
+
it("should handle wrapped response with invalid data structure", async () => {
|
|
769
|
+
const request: ProofSubmissionRequest = {
|
|
770
|
+
session_id: "123e4567-e89b-12d3-a456-426614174000",
|
|
771
|
+
delegation_id: null,
|
|
772
|
+
proofs: [
|
|
773
|
+
{
|
|
774
|
+
jws: "header.payload.signature",
|
|
775
|
+
meta: {
|
|
776
|
+
did: "did:key:z123",
|
|
777
|
+
kid: "did:key:z123#key-1",
|
|
778
|
+
ts: Math.floor(Date.now() / 1000),
|
|
779
|
+
nonce: "nonce-123",
|
|
780
|
+
audience: "mcp-client",
|
|
781
|
+
sessionId: "session-123",
|
|
782
|
+
requestHash: "sha256:" + "a".repeat(64),
|
|
783
|
+
responseHash: "sha256:" + "b".repeat(64),
|
|
784
|
+
},
|
|
785
|
+
},
|
|
786
|
+
],
|
|
787
|
+
};
|
|
788
|
+
|
|
789
|
+
// Wrapped response but data is invalid
|
|
790
|
+
const invalidWrappedResponse = {
|
|
791
|
+
success: true,
|
|
792
|
+
data: {
|
|
793
|
+
// Missing required fields
|
|
794
|
+
message: "Invalid format",
|
|
795
|
+
},
|
|
796
|
+
};
|
|
797
|
+
|
|
798
|
+
(mockFetchProvider.fetch as ReturnType<typeof vi.fn>).mockResolvedValue(
|
|
799
|
+
new Response(JSON.stringify(invalidWrappedResponse), {
|
|
800
|
+
status: 200,
|
|
801
|
+
headers: { "Content-Type": "application/json" },
|
|
802
|
+
})
|
|
803
|
+
);
|
|
804
|
+
|
|
805
|
+
const error = await service.submitProofs(request).catch((e) => e);
|
|
806
|
+
expect(error).toBeInstanceOf(AgentShieldAPIError);
|
|
807
|
+
expect((error as AgentShieldAPIError).code).toBe("invalid_response");
|
|
808
|
+
});
|
|
809
|
+
|
|
810
|
+
it("should validate request schema", async () => {
|
|
811
|
+
const invalidRequest = {
|
|
812
|
+
session_id: "", // Invalid: empty string
|
|
813
|
+
proofs: [],
|
|
814
|
+
} as ProofSubmissionRequest;
|
|
815
|
+
|
|
816
|
+
await expect(service.submitProofs(invalidRequest)).rejects.toThrow(
|
|
817
|
+
AgentShieldAPIError
|
|
818
|
+
);
|
|
819
|
+
|
|
820
|
+
const error = await service.submitProofs(invalidRequest).catch((e) => e);
|
|
821
|
+
expect(error.code).toBe("validation_error");
|
|
822
|
+
});
|
|
823
|
+
});
|
|
824
|
+
|
|
825
|
+
describe("retry logic", () => {
|
|
826
|
+
it("should retry on network errors", async () => {
|
|
827
|
+
const mockResponse = {
|
|
828
|
+
success: true,
|
|
829
|
+
data: {
|
|
830
|
+
agent_did: "did:key:z123",
|
|
831
|
+
tools: {},
|
|
832
|
+
},
|
|
833
|
+
metadata: {
|
|
834
|
+
requestId: "test-request-id",
|
|
835
|
+
timestamp: new Date().toISOString(),
|
|
836
|
+
},
|
|
837
|
+
};
|
|
838
|
+
|
|
839
|
+
// First call throws network error, second succeeds
|
|
840
|
+
(mockFetchProvider.fetch as ReturnType<typeof vi.fn>)
|
|
841
|
+
.mockRejectedValueOnce(new TypeError("fetch failed"))
|
|
842
|
+
.mockResolvedValueOnce(
|
|
843
|
+
new Response(JSON.stringify(mockResponse), {
|
|
844
|
+
status: 200,
|
|
845
|
+
headers: { "Content-Type": "application/json" },
|
|
846
|
+
})
|
|
847
|
+
);
|
|
848
|
+
|
|
849
|
+
const result = await service.fetchConfig({ agentDid: "did:key:z123" });
|
|
850
|
+
|
|
851
|
+
expect(result).toBeDefined();
|
|
852
|
+
expect(mockFetchProvider.fetch).toHaveBeenCalledTimes(2);
|
|
853
|
+
expect(service.getMetrics().retryCount).toBe(1);
|
|
854
|
+
});
|
|
855
|
+
|
|
856
|
+
it("should not retry on 400 errors", async () => {
|
|
857
|
+
(mockFetchProvider.fetch as ReturnType<typeof vi.fn>).mockResolvedValue(
|
|
858
|
+
new Response(
|
|
859
|
+
JSON.stringify({
|
|
860
|
+
success: false,
|
|
861
|
+
error: {
|
|
862
|
+
code: "validation_error",
|
|
863
|
+
message: "Invalid request",
|
|
864
|
+
},
|
|
865
|
+
}),
|
|
866
|
+
{
|
|
867
|
+
status: 400,
|
|
868
|
+
headers: { "Content-Type": "application/json" },
|
|
869
|
+
}
|
|
870
|
+
)
|
|
871
|
+
);
|
|
872
|
+
|
|
873
|
+
await expect(
|
|
874
|
+
service.fetchConfig({ agentDid: "did:key:z123" })
|
|
875
|
+
).rejects.toThrow(AgentShieldAPIError);
|
|
876
|
+
|
|
877
|
+
expect(mockFetchProvider.fetch).toHaveBeenCalledTimes(1);
|
|
878
|
+
expect(mockSleepFn).not.toHaveBeenCalled();
|
|
879
|
+
});
|
|
880
|
+
|
|
881
|
+
it("should respect maxRetries limit", async () => {
|
|
882
|
+
// All calls fail with 500 - need to mock multiple times for retries
|
|
883
|
+
(mockFetchProvider.fetch as ReturnType<typeof vi.fn>)
|
|
884
|
+
.mockResolvedValueOnce(
|
|
885
|
+
new Response("Internal Server Error", { status: 500 })
|
|
886
|
+
)
|
|
887
|
+
.mockResolvedValueOnce(
|
|
888
|
+
new Response("Internal Server Error", { status: 500 })
|
|
889
|
+
)
|
|
890
|
+
.mockResolvedValueOnce(
|
|
891
|
+
new Response("Internal Server Error", { status: 500 })
|
|
892
|
+
);
|
|
893
|
+
|
|
894
|
+
await expect(
|
|
895
|
+
service.fetchConfig({ agentDid: "did:key:z123" })
|
|
896
|
+
).rejects.toThrow();
|
|
897
|
+
|
|
898
|
+
// Should retry maxRetries times (2) + initial attempt = 3 total
|
|
899
|
+
expect(mockFetchProvider.fetch).toHaveBeenCalledTimes(3);
|
|
900
|
+
expect(service.getMetrics().retryCount).toBe(2);
|
|
901
|
+
expect(service.getMetrics().errorCount).toBe(1);
|
|
902
|
+
});
|
|
903
|
+
});
|
|
904
|
+
|
|
905
|
+
describe("metrics", () => {
|
|
906
|
+
it("should track successful requests", async () => {
|
|
907
|
+
const mockResponse = {
|
|
908
|
+
success: true,
|
|
909
|
+
data: {
|
|
910
|
+
agent_did: "did:key:z123",
|
|
911
|
+
tools: {},
|
|
912
|
+
},
|
|
913
|
+
metadata: {
|
|
914
|
+
requestId: "test-request-id",
|
|
915
|
+
timestamp: new Date().toISOString(),
|
|
916
|
+
},
|
|
917
|
+
};
|
|
918
|
+
|
|
919
|
+
(mockFetchProvider.fetch as ReturnType<typeof vi.fn>).mockResolvedValue(
|
|
920
|
+
new Response(JSON.stringify(mockResponse), {
|
|
921
|
+
status: 200,
|
|
922
|
+
headers: { "Content-Type": "application/json" },
|
|
923
|
+
})
|
|
924
|
+
);
|
|
925
|
+
|
|
926
|
+
await service.fetchConfig({ agentDid: "did:key:z123" });
|
|
927
|
+
|
|
928
|
+
const metrics = service.getMetrics();
|
|
929
|
+
expect(metrics.successCount).toBe(1);
|
|
930
|
+
expect(metrics.errorCount).toBe(0);
|
|
931
|
+
});
|
|
932
|
+
|
|
933
|
+
it("should reset metrics", async () => {
|
|
934
|
+
const mockResponse = {
|
|
935
|
+
success: true,
|
|
936
|
+
data: {
|
|
937
|
+
agent_did: "did:key:z123",
|
|
938
|
+
tools: {},
|
|
939
|
+
},
|
|
940
|
+
metadata: {
|
|
941
|
+
requestId: "test-request-id",
|
|
942
|
+
timestamp: new Date().toISOString(),
|
|
943
|
+
},
|
|
944
|
+
};
|
|
945
|
+
|
|
946
|
+
(mockFetchProvider.fetch as ReturnType<typeof vi.fn>).mockResolvedValue(
|
|
947
|
+
new Response(JSON.stringify(mockResponse), {
|
|
948
|
+
status: 200,
|
|
949
|
+
headers: { "Content-Type": "application/json" },
|
|
950
|
+
})
|
|
951
|
+
);
|
|
952
|
+
|
|
953
|
+
await service.fetchConfig({ agentDid: "did:key:z123" });
|
|
954
|
+
service.resetMetrics();
|
|
955
|
+
|
|
956
|
+
const metrics = service.getMetrics();
|
|
957
|
+
expect(metrics.successCount).toBe(0);
|
|
958
|
+
expect(metrics.errorCount).toBe(0);
|
|
959
|
+
expect(metrics.retryCount).toBe(0);
|
|
960
|
+
});
|
|
961
|
+
});
|
|
962
|
+
|
|
963
|
+
describe("correlation ID", () => {
|
|
964
|
+
it("should include correlation ID in headers", async () => {
|
|
965
|
+
const mockResponse = {
|
|
966
|
+
success: true,
|
|
967
|
+
data: {
|
|
968
|
+
agent_did: "did:key:z123",
|
|
969
|
+
tools: {},
|
|
970
|
+
},
|
|
971
|
+
metadata: {
|
|
972
|
+
requestId: "test-request-id",
|
|
973
|
+
timestamp: new Date().toISOString(),
|
|
974
|
+
},
|
|
975
|
+
};
|
|
976
|
+
|
|
977
|
+
(mockFetchProvider.fetch as ReturnType<typeof vi.fn>).mockResolvedValue(
|
|
978
|
+
new Response(JSON.stringify(mockResponse), {
|
|
979
|
+
status: 200,
|
|
980
|
+
headers: { "Content-Type": "application/json" },
|
|
981
|
+
})
|
|
982
|
+
);
|
|
983
|
+
|
|
984
|
+
await service.fetchConfig({ agentDid: "did:key:z123" });
|
|
985
|
+
|
|
986
|
+
const callArgs = (mockFetchProvider.fetch as ReturnType<typeof vi.fn>)
|
|
987
|
+
.mock.calls[0];
|
|
988
|
+
const headers = callArgs[1].headers;
|
|
989
|
+
expect(headers["X-Request-ID"]).toBeDefined();
|
|
990
|
+
expect(typeof headers["X-Request-ID"]).toBe("string");
|
|
991
|
+
});
|
|
992
|
+
});
|
|
993
|
+
});
|
|
994
|
+
|
|
995
|
+
describe("authorizationMatches", () => {
|
|
996
|
+
describe("when toolAuth is undefined", () => {
|
|
997
|
+
it("should return true (no tool auth requirement)", () => {
|
|
998
|
+
const delegationAuth = { type: "oauth", provider: "github" };
|
|
999
|
+
expect(authorizationMatches(delegationAuth, undefined)).toBe(true);
|
|
1000
|
+
});
|
|
1001
|
+
});
|
|
1002
|
+
|
|
1003
|
+
describe("type matching", () => {
|
|
1004
|
+
it("should return false when types do not match", () => {
|
|
1005
|
+
const delegationAuth = { type: "oauth", provider: "github" };
|
|
1006
|
+
const toolAuth = { type: "verifiable_credential", credentialType: "api-key" };
|
|
1007
|
+
expect(authorizationMatches(delegationAuth, toolAuth)).toBe(false);
|
|
1008
|
+
});
|
|
1009
|
+
|
|
1010
|
+
it("should return true when types match and no provider/credentialType specified", () => {
|
|
1011
|
+
const delegationAuth = { type: "oauth", provider: "github" };
|
|
1012
|
+
const toolAuth = { type: "oauth" };
|
|
1013
|
+
expect(authorizationMatches(delegationAuth, toolAuth)).toBe(true);
|
|
1014
|
+
});
|
|
1015
|
+
});
|
|
1016
|
+
|
|
1017
|
+
describe("OAuth provider matching", () => {
|
|
1018
|
+
it("should return true when providers match", () => {
|
|
1019
|
+
const delegationAuth = { type: "oauth", provider: "github" };
|
|
1020
|
+
const toolAuth = { type: "oauth", provider: "github" };
|
|
1021
|
+
expect(authorizationMatches(delegationAuth, toolAuth)).toBe(true);
|
|
1022
|
+
});
|
|
1023
|
+
|
|
1024
|
+
it("should return false when providers do not match", () => {
|
|
1025
|
+
const delegationAuth = { type: "oauth", provider: "github" };
|
|
1026
|
+
const toolAuth = { type: "oauth", provider: "google" };
|
|
1027
|
+
expect(authorizationMatches(delegationAuth, toolAuth)).toBe(false);
|
|
1028
|
+
});
|
|
1029
|
+
|
|
1030
|
+
it("should return true when toolAuth.provider is undefined (accepts any provider)", () => {
|
|
1031
|
+
const delegationAuth = { type: "oauth", provider: "github" };
|
|
1032
|
+
const toolAuth = { type: "oauth" }; // provider is undefined
|
|
1033
|
+
expect(authorizationMatches(delegationAuth, toolAuth)).toBe(true);
|
|
1034
|
+
});
|
|
1035
|
+
|
|
1036
|
+
it("should return true when delegationAuth.provider is undefined and toolAuth.provider is undefined", () => {
|
|
1037
|
+
const delegationAuth = { type: "oauth" }; // provider is undefined
|
|
1038
|
+
const toolAuth = { type: "oauth" }; // provider is undefined
|
|
1039
|
+
expect(authorizationMatches(delegationAuth, toolAuth)).toBe(true);
|
|
1040
|
+
});
|
|
1041
|
+
|
|
1042
|
+
it("should return false when delegationAuth.provider is undefined but toolAuth requires specific provider", () => {
|
|
1043
|
+
const delegationAuth = { type: "oauth" }; // provider is undefined
|
|
1044
|
+
const toolAuth = { type: "oauth", provider: "github" };
|
|
1045
|
+
expect(authorizationMatches(delegationAuth, toolAuth)).toBe(false);
|
|
1046
|
+
});
|
|
1047
|
+
});
|
|
1048
|
+
|
|
1049
|
+
describe("verifiable_credential type matching", () => {
|
|
1050
|
+
it("should return true when credentialTypes match", () => {
|
|
1051
|
+
const delegationAuth = { type: "verifiable_credential", credentialType: "api-key" };
|
|
1052
|
+
const toolAuth = { type: "verifiable_credential", credentialType: "api-key" };
|
|
1053
|
+
expect(authorizationMatches(delegationAuth, toolAuth)).toBe(true);
|
|
1054
|
+
});
|
|
1055
|
+
|
|
1056
|
+
it("should return false when credentialTypes do not match", () => {
|
|
1057
|
+
const delegationAuth = { type: "verifiable_credential", credentialType: "api-key" };
|
|
1058
|
+
const toolAuth = { type: "verifiable_credential", credentialType: "bearer-token" };
|
|
1059
|
+
expect(authorizationMatches(delegationAuth, toolAuth)).toBe(false);
|
|
1060
|
+
});
|
|
1061
|
+
|
|
1062
|
+
it("should return true when toolAuth.credentialType is undefined (accepts any type)", () => {
|
|
1063
|
+
const delegationAuth = { type: "verifiable_credential", credentialType: "api-key" };
|
|
1064
|
+
const toolAuth = { type: "verifiable_credential" }; // credentialType is undefined
|
|
1065
|
+
expect(authorizationMatches(delegationAuth, toolAuth)).toBe(true);
|
|
1066
|
+
});
|
|
1067
|
+
|
|
1068
|
+
it("should return true when both credentialTypes are undefined", () => {
|
|
1069
|
+
const delegationAuth = { type: "verifiable_credential" }; // credentialType is undefined
|
|
1070
|
+
const toolAuth = { type: "verifiable_credential" }; // credentialType is undefined
|
|
1071
|
+
expect(authorizationMatches(delegationAuth, toolAuth)).toBe(true);
|
|
1072
|
+
});
|
|
1073
|
+
|
|
1074
|
+
it("should return false when delegationAuth.credentialType is undefined but toolAuth requires specific type", () => {
|
|
1075
|
+
const delegationAuth = { type: "verifiable_credential" }; // credentialType is undefined
|
|
1076
|
+
const toolAuth = { type: "verifiable_credential", credentialType: "api-key" };
|
|
1077
|
+
expect(authorizationMatches(delegationAuth, toolAuth)).toBe(false);
|
|
1078
|
+
});
|
|
1079
|
+
});
|
|
1080
|
+
|
|
1081
|
+
describe("none type", () => {
|
|
1082
|
+
it("should return true when both are none type", () => {
|
|
1083
|
+
const delegationAuth = { type: "none" };
|
|
1084
|
+
const toolAuth = { type: "none" };
|
|
1085
|
+
expect(authorizationMatches(delegationAuth, toolAuth)).toBe(true);
|
|
1086
|
+
});
|
|
1087
|
+
});
|
|
1088
|
+
|
|
1089
|
+
describe("cross-type compatibility (credential to oauth)", () => {
|
|
1090
|
+
it("should return true when credential delegation matches oauth tool with same provider", () => {
|
|
1091
|
+
// Credential providers (like email/password) create delegations with type: 'credential'
|
|
1092
|
+
// but tool protection may specify type: 'oauth' with provider: 'credentials'
|
|
1093
|
+
const delegationAuth = {
|
|
1094
|
+
type: "credential",
|
|
1095
|
+
credentialType: "credentials",
|
|
1096
|
+
};
|
|
1097
|
+
const toolAuth = { type: "oauth", provider: "credentials" };
|
|
1098
|
+
expect(authorizationMatches(delegationAuth, toolAuth)).toBe(true);
|
|
1099
|
+
});
|
|
1100
|
+
|
|
1101
|
+
it("should return false when credential type does not match oauth provider", () => {
|
|
1102
|
+
const delegationAuth = { type: "credential", credentialType: "api-key" };
|
|
1103
|
+
const toolAuth = { type: "oauth", provider: "credentials" };
|
|
1104
|
+
expect(authorizationMatches(delegationAuth, toolAuth)).toBe(false);
|
|
1105
|
+
});
|
|
1106
|
+
|
|
1107
|
+
it("should return false when credential delegation has no credentialType", () => {
|
|
1108
|
+
const delegationAuth = { type: "credential" };
|
|
1109
|
+
const toolAuth = { type: "oauth", provider: "credentials" };
|
|
1110
|
+
expect(authorizationMatches(delegationAuth, toolAuth)).toBe(false);
|
|
1111
|
+
});
|
|
1112
|
+
|
|
1113
|
+
it("should return false when oauth tool has no provider", () => {
|
|
1114
|
+
const delegationAuth = {
|
|
1115
|
+
type: "credential",
|
|
1116
|
+
credentialType: "credentials",
|
|
1117
|
+
};
|
|
1118
|
+
const toolAuth = { type: "oauth" };
|
|
1119
|
+
// No provider specified - can't determine if credential type matches
|
|
1120
|
+
expect(authorizationMatches(delegationAuth, toolAuth)).toBe(false);
|
|
1121
|
+
});
|
|
1122
|
+
|
|
1123
|
+
it("should work for any matching provider name (e.g., hardwareworld)", () => {
|
|
1124
|
+
const delegationAuth = {
|
|
1125
|
+
type: "credential",
|
|
1126
|
+
credentialType: "hardwareworld",
|
|
1127
|
+
};
|
|
1128
|
+
const toolAuth = { type: "oauth", provider: "hardwareworld" };
|
|
1129
|
+
expect(authorizationMatches(delegationAuth, toolAuth)).toBe(true);
|
|
1130
|
+
});
|
|
1131
|
+
});
|
|
1132
|
+
});
|