@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,704 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for OAuthService error handling
|
|
3
|
+
*
|
|
4
|
+
* Covers:
|
|
5
|
+
* - GitHub 200-status error responses
|
|
6
|
+
* - Proxy mode failures
|
|
7
|
+
* - Missing access_token handling
|
|
8
|
+
* - Provider configuration errors
|
|
9
|
+
* - Token refresh failures
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { describe, it, expect, beforeEach, vi } from "vitest";
|
|
13
|
+
import { OAuthService } from "../../services/oauth-service";
|
|
14
|
+
import type { FetchProvider } from "../../providers/base";
|
|
15
|
+
import type { OAuthConfigService } from "../../services/oauth-config.service";
|
|
16
|
+
import type { OAuthConfig, OAuthProvider } from "@kya-os/contracts/config";
|
|
17
|
+
|
|
18
|
+
// Helper to create a proper mock Response
|
|
19
|
+
function createMockResponse(options: {
|
|
20
|
+
ok: boolean;
|
|
21
|
+
status?: number;
|
|
22
|
+
body?: unknown;
|
|
23
|
+
}): Response {
|
|
24
|
+
const { ok, status = ok ? 200 : 400, body } = options;
|
|
25
|
+
const jsonBody = body !== undefined ? JSON.stringify(body) : "";
|
|
26
|
+
|
|
27
|
+
return {
|
|
28
|
+
ok,
|
|
29
|
+
status,
|
|
30
|
+
text: vi.fn().mockResolvedValue(jsonBody),
|
|
31
|
+
json: vi.fn().mockResolvedValue(body),
|
|
32
|
+
clone: vi.fn().mockReturnThis(),
|
|
33
|
+
} as unknown as Response;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
describe("OAuthService Error Handling", () => {
|
|
37
|
+
let service: OAuthService;
|
|
38
|
+
let mockFetch: ReturnType<typeof vi.fn>;
|
|
39
|
+
let mockFetchProvider: FetchProvider;
|
|
40
|
+
let mockConfigService: OAuthConfigService;
|
|
41
|
+
let mockLogger: ReturnType<typeof vi.fn>;
|
|
42
|
+
|
|
43
|
+
const projectId = "test-project-123";
|
|
44
|
+
const agentShieldApiUrl = "https://kya.vouched.id";
|
|
45
|
+
const agentShieldApiKey = "test-api-key";
|
|
46
|
+
|
|
47
|
+
// Mock provider configurations
|
|
48
|
+
const pkceProvider: OAuthProvider = {
|
|
49
|
+
clientId: "github-client-id",
|
|
50
|
+
authorizationUrl: "https://github.com/login/oauth/authorize",
|
|
51
|
+
tokenUrl: "https://github.com/login/oauth/access_token",
|
|
52
|
+
supportsPKCE: true,
|
|
53
|
+
proxyMode: false,
|
|
54
|
+
scopes: ["read:user", "user:email"],
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
const proxyProvider: OAuthProvider = {
|
|
58
|
+
clientId: "google-client-id",
|
|
59
|
+
authorizationUrl: "https://accounts.google.com/o/oauth2/v2/auth",
|
|
60
|
+
tokenUrl: "https://oauth2.googleapis.com/token",
|
|
61
|
+
supportsPKCE: false,
|
|
62
|
+
proxyMode: true,
|
|
63
|
+
scopes: ["profile", "email"],
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
const mockOAuthConfig: OAuthConfig = {
|
|
67
|
+
enabled: true,
|
|
68
|
+
providers: {
|
|
69
|
+
github: pkceProvider,
|
|
70
|
+
google: proxyProvider,
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
beforeEach(() => {
|
|
75
|
+
vi.clearAllMocks();
|
|
76
|
+
mockFetch = vi.fn();
|
|
77
|
+
mockLogger = vi.fn();
|
|
78
|
+
|
|
79
|
+
mockFetchProvider = {
|
|
80
|
+
fetch: mockFetch as unknown as typeof globalThis.fetch,
|
|
81
|
+
resolveDID: vi.fn(),
|
|
82
|
+
fetchStatusList: vi.fn(),
|
|
83
|
+
fetchDelegationChain: vi.fn(),
|
|
84
|
+
} as unknown as FetchProvider;
|
|
85
|
+
|
|
86
|
+
mockConfigService = {
|
|
87
|
+
getOAuthConfig: vi.fn().mockResolvedValue(mockOAuthConfig),
|
|
88
|
+
refreshConfig: vi.fn(),
|
|
89
|
+
clearCache: vi.fn(),
|
|
90
|
+
} as unknown as OAuthConfigService;
|
|
91
|
+
|
|
92
|
+
service = new OAuthService({
|
|
93
|
+
configService: mockConfigService,
|
|
94
|
+
fetchProvider: mockFetchProvider,
|
|
95
|
+
agentShieldApiUrl,
|
|
96
|
+
agentShieldApiKey,
|
|
97
|
+
projectId,
|
|
98
|
+
logger: mockLogger,
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
describe("GitHub 200-Status Errors", () => {
|
|
103
|
+
it("should detect error in 200 response (GitHub OAuth pattern)", async () => {
|
|
104
|
+
// GitHub returns errors with 200 status code
|
|
105
|
+
mockFetch.mockResolvedValueOnce(
|
|
106
|
+
createMockResponse({
|
|
107
|
+
ok: true,
|
|
108
|
+
body: {
|
|
109
|
+
error: "bad_verification_code",
|
|
110
|
+
error_description: "The code passed is incorrect or expired.",
|
|
111
|
+
error_uri:
|
|
112
|
+
"https://docs.github.com/apps/managing-oauth-apps/troubleshooting-oauth-app-access-token-request-errors/#bad-verification-code",
|
|
113
|
+
},
|
|
114
|
+
})
|
|
115
|
+
);
|
|
116
|
+
|
|
117
|
+
await expect(
|
|
118
|
+
service.exchangeToken(
|
|
119
|
+
"github",
|
|
120
|
+
"invalid-code",
|
|
121
|
+
"verifier123",
|
|
122
|
+
"http://localhost/callback"
|
|
123
|
+
)
|
|
124
|
+
).rejects.toThrow("The code passed is incorrect or expired.");
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it("should use error field when error_description is missing", async () => {
|
|
128
|
+
mockFetch.mockResolvedValueOnce(
|
|
129
|
+
createMockResponse({
|
|
130
|
+
ok: true,
|
|
131
|
+
body: {
|
|
132
|
+
error: "invalid_request",
|
|
133
|
+
// No error_description
|
|
134
|
+
},
|
|
135
|
+
})
|
|
136
|
+
);
|
|
137
|
+
|
|
138
|
+
await expect(
|
|
139
|
+
service.exchangeToken(
|
|
140
|
+
"github",
|
|
141
|
+
"code",
|
|
142
|
+
"verifier",
|
|
143
|
+
"http://localhost/callback"
|
|
144
|
+
)
|
|
145
|
+
).rejects.toThrow("invalid_request");
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
it("should log GitHub 200-status error details", async () => {
|
|
149
|
+
mockFetch.mockResolvedValueOnce(
|
|
150
|
+
createMockResponse({
|
|
151
|
+
ok: true,
|
|
152
|
+
body: {
|
|
153
|
+
error: "unsupported_grant_type",
|
|
154
|
+
error_description: "Grant type not supported",
|
|
155
|
+
},
|
|
156
|
+
})
|
|
157
|
+
);
|
|
158
|
+
|
|
159
|
+
try {
|
|
160
|
+
await service.exchangeToken(
|
|
161
|
+
"github",
|
|
162
|
+
"code",
|
|
163
|
+
"verifier",
|
|
164
|
+
"http://localhost/callback"
|
|
165
|
+
);
|
|
166
|
+
} catch {
|
|
167
|
+
// Expected
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
expect(mockLogger).toHaveBeenCalledWith(
|
|
171
|
+
expect.stringContaining("Token exchange returned error (200 status)"),
|
|
172
|
+
expect.objectContaining({
|
|
173
|
+
error: "unsupported_grant_type",
|
|
174
|
+
errorDescription: "Grant type not supported",
|
|
175
|
+
})
|
|
176
|
+
);
|
|
177
|
+
});
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
describe("Missing Access Token", () => {
|
|
181
|
+
it("should throw when access_token is missing from response", async () => {
|
|
182
|
+
mockFetch.mockResolvedValueOnce(
|
|
183
|
+
createMockResponse({
|
|
184
|
+
ok: true,
|
|
185
|
+
body: {
|
|
186
|
+
// Missing access_token
|
|
187
|
+
token_type: "Bearer",
|
|
188
|
+
expires_in: 3600,
|
|
189
|
+
},
|
|
190
|
+
})
|
|
191
|
+
);
|
|
192
|
+
|
|
193
|
+
await expect(
|
|
194
|
+
service.exchangeToken(
|
|
195
|
+
"github",
|
|
196
|
+
"code",
|
|
197
|
+
"verifier",
|
|
198
|
+
"http://localhost/callback"
|
|
199
|
+
)
|
|
200
|
+
).rejects.toThrow("Token response missing access_token");
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it("should log missing access_token details", async () => {
|
|
204
|
+
mockFetch.mockResolvedValueOnce(
|
|
205
|
+
createMockResponse({
|
|
206
|
+
ok: true,
|
|
207
|
+
body: {
|
|
208
|
+
token_type: "Bearer",
|
|
209
|
+
// Missing access_token
|
|
210
|
+
},
|
|
211
|
+
})
|
|
212
|
+
);
|
|
213
|
+
|
|
214
|
+
try {
|
|
215
|
+
await service.exchangeToken(
|
|
216
|
+
"github",
|
|
217
|
+
"code",
|
|
218
|
+
"verifier",
|
|
219
|
+
"http://localhost/callback"
|
|
220
|
+
);
|
|
221
|
+
} catch {
|
|
222
|
+
// Expected
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
expect(mockLogger).toHaveBeenCalledWith(
|
|
226
|
+
expect.stringContaining("Token response missing access_token"),
|
|
227
|
+
expect.objectContaining({
|
|
228
|
+
responseKeys: expect.arrayContaining(["token_type"]),
|
|
229
|
+
})
|
|
230
|
+
);
|
|
231
|
+
});
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
describe("HTTP Error Responses", () => {
|
|
235
|
+
it("should handle non-200 response from OAuth provider", async () => {
|
|
236
|
+
mockFetch.mockResolvedValueOnce(
|
|
237
|
+
createMockResponse({
|
|
238
|
+
ok: false,
|
|
239
|
+
status: 400,
|
|
240
|
+
body: {
|
|
241
|
+
error: "invalid_grant",
|
|
242
|
+
error_description: "Authorization code has been used",
|
|
243
|
+
},
|
|
244
|
+
})
|
|
245
|
+
);
|
|
246
|
+
|
|
247
|
+
await expect(
|
|
248
|
+
service.exchangeToken(
|
|
249
|
+
"github",
|
|
250
|
+
"used-code",
|
|
251
|
+
"verifier",
|
|
252
|
+
"http://localhost/callback"
|
|
253
|
+
)
|
|
254
|
+
).rejects.toThrow(
|
|
255
|
+
"Token exchange failed: Authorization code has been used (400)"
|
|
256
|
+
);
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
it("should handle 401 unauthorized from OAuth provider", async () => {
|
|
260
|
+
mockFetch.mockResolvedValueOnce(
|
|
261
|
+
createMockResponse({
|
|
262
|
+
ok: false,
|
|
263
|
+
status: 401,
|
|
264
|
+
body: {
|
|
265
|
+
error: "invalid_client",
|
|
266
|
+
error_description: "Client authentication failed",
|
|
267
|
+
},
|
|
268
|
+
})
|
|
269
|
+
);
|
|
270
|
+
|
|
271
|
+
await expect(
|
|
272
|
+
service.exchangeToken(
|
|
273
|
+
"github",
|
|
274
|
+
"code",
|
|
275
|
+
"verifier",
|
|
276
|
+
"http://localhost/callback"
|
|
277
|
+
)
|
|
278
|
+
).rejects.toThrow("Client authentication failed");
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
it("should handle non-JSON error response gracefully", async () => {
|
|
282
|
+
const mockResponse = {
|
|
283
|
+
ok: false,
|
|
284
|
+
status: 500,
|
|
285
|
+
text: vi.fn().mockResolvedValue("Internal Server Error"),
|
|
286
|
+
json: vi.fn().mockRejectedValue(new SyntaxError("Unexpected token")),
|
|
287
|
+
} as unknown as Response;
|
|
288
|
+
|
|
289
|
+
mockFetch.mockResolvedValueOnce(mockResponse);
|
|
290
|
+
|
|
291
|
+
await expect(
|
|
292
|
+
service.exchangeToken(
|
|
293
|
+
"github",
|
|
294
|
+
"code",
|
|
295
|
+
"verifier",
|
|
296
|
+
"http://localhost/callback"
|
|
297
|
+
)
|
|
298
|
+
).rejects.toThrow("Internal Server Error");
|
|
299
|
+
});
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
describe("Provider Configuration Errors", () => {
|
|
303
|
+
it("should throw when provider not configured", async () => {
|
|
304
|
+
await expect(
|
|
305
|
+
service.exchangeToken(
|
|
306
|
+
"unknown-provider",
|
|
307
|
+
"code",
|
|
308
|
+
"verifier",
|
|
309
|
+
"http://localhost/callback"
|
|
310
|
+
)
|
|
311
|
+
).rejects.toThrow('Provider "unknown-provider" not configured');
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
it("should throw when PKCE provider missing code_verifier", async () => {
|
|
315
|
+
await expect(
|
|
316
|
+
service.exchangeToken(
|
|
317
|
+
"github",
|
|
318
|
+
"code",
|
|
319
|
+
undefined,
|
|
320
|
+
"http://localhost/callback"
|
|
321
|
+
)
|
|
322
|
+
).rejects.toThrow('Provider "github" requires PKCE code_verifier');
|
|
323
|
+
});
|
|
324
|
+
|
|
325
|
+
it("should throw when provider configuration is invalid", async () => {
|
|
326
|
+
// Provider with neither PKCE nor proxy mode
|
|
327
|
+
mockConfigService.getOAuthConfig = vi.fn().mockResolvedValue({
|
|
328
|
+
enabled: true,
|
|
329
|
+
providers: {
|
|
330
|
+
invalid: {
|
|
331
|
+
clientId: "test",
|
|
332
|
+
authorizationUrl: "https://example.com/auth",
|
|
333
|
+
tokenUrl: "https://example.com/token",
|
|
334
|
+
supportsPKCE: false,
|
|
335
|
+
proxyMode: false,
|
|
336
|
+
scopes: [],
|
|
337
|
+
},
|
|
338
|
+
},
|
|
339
|
+
});
|
|
340
|
+
|
|
341
|
+
await expect(
|
|
342
|
+
service.exchangeToken(
|
|
343
|
+
"invalid",
|
|
344
|
+
"code",
|
|
345
|
+
"verifier",
|
|
346
|
+
"http://localhost/callback"
|
|
347
|
+
)
|
|
348
|
+
).rejects.toThrow("must support PKCE or use proxy mode");
|
|
349
|
+
});
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
describe("Proxy Mode Errors", () => {
|
|
353
|
+
it("should handle proxy endpoint failure", async () => {
|
|
354
|
+
mockFetch.mockResolvedValueOnce(
|
|
355
|
+
createMockResponse({
|
|
356
|
+
ok: false,
|
|
357
|
+
status: 502,
|
|
358
|
+
body: {
|
|
359
|
+
error: "upstream_error",
|
|
360
|
+
error_description: "Failed to reach OAuth provider",
|
|
361
|
+
},
|
|
362
|
+
})
|
|
363
|
+
);
|
|
364
|
+
|
|
365
|
+
await expect(
|
|
366
|
+
service.exchangeToken(
|
|
367
|
+
"google",
|
|
368
|
+
"code",
|
|
369
|
+
undefined,
|
|
370
|
+
"http://localhost/callback"
|
|
371
|
+
)
|
|
372
|
+
).rejects.toThrow(
|
|
373
|
+
"Proxy token exchange failed: Failed to reach OAuth provider (502)"
|
|
374
|
+
);
|
|
375
|
+
});
|
|
376
|
+
|
|
377
|
+
it("should handle proxy returning missing access_token", async () => {
|
|
378
|
+
mockFetch.mockResolvedValueOnce(
|
|
379
|
+
createMockResponse({
|
|
380
|
+
ok: true,
|
|
381
|
+
body: {
|
|
382
|
+
// Missing access_token in response
|
|
383
|
+
data: {},
|
|
384
|
+
},
|
|
385
|
+
})
|
|
386
|
+
);
|
|
387
|
+
|
|
388
|
+
await expect(
|
|
389
|
+
service.exchangeToken(
|
|
390
|
+
"google",
|
|
391
|
+
"code",
|
|
392
|
+
undefined,
|
|
393
|
+
"http://localhost/callback"
|
|
394
|
+
)
|
|
395
|
+
).rejects.toThrow("Proxy token response missing access_token");
|
|
396
|
+
});
|
|
397
|
+
|
|
398
|
+
it("should send correct headers to proxy endpoint", async () => {
|
|
399
|
+
mockFetch.mockResolvedValueOnce(
|
|
400
|
+
createMockResponse({
|
|
401
|
+
ok: true,
|
|
402
|
+
body: {
|
|
403
|
+
data: {
|
|
404
|
+
access_token: "proxy-token",
|
|
405
|
+
expires_in: 3600,
|
|
406
|
+
},
|
|
407
|
+
},
|
|
408
|
+
})
|
|
409
|
+
);
|
|
410
|
+
|
|
411
|
+
await service.exchangeToken(
|
|
412
|
+
"google",
|
|
413
|
+
"auth-code",
|
|
414
|
+
undefined,
|
|
415
|
+
"http://localhost/callback"
|
|
416
|
+
);
|
|
417
|
+
|
|
418
|
+
const call = mockFetch.mock.calls[0];
|
|
419
|
+
expect(call[0]).toContain("/api/v1/oauth/token");
|
|
420
|
+
expect(call[1].headers["Content-Type"]).toBe("application/json");
|
|
421
|
+
expect(call[1].headers["Authorization"]).toBe(
|
|
422
|
+
`Bearer ${agentShieldApiKey}`
|
|
423
|
+
);
|
|
424
|
+
});
|
|
425
|
+
|
|
426
|
+
it("should include code_verifier for PKCE-capable proxy providers", async () => {
|
|
427
|
+
// Provider that supports PKCE but uses proxy mode
|
|
428
|
+
mockConfigService.getOAuthConfig = vi.fn().mockResolvedValue({
|
|
429
|
+
enabled: true,
|
|
430
|
+
providers: {
|
|
431
|
+
hybrid: {
|
|
432
|
+
clientId: "test",
|
|
433
|
+
authorizationUrl: "https://example.com/auth",
|
|
434
|
+
tokenUrl: "https://example.com/token",
|
|
435
|
+
supportsPKCE: true,
|
|
436
|
+
proxyMode: true,
|
|
437
|
+
scopes: [],
|
|
438
|
+
},
|
|
439
|
+
},
|
|
440
|
+
});
|
|
441
|
+
|
|
442
|
+
mockFetch.mockResolvedValueOnce(
|
|
443
|
+
createMockResponse({
|
|
444
|
+
ok: true,
|
|
445
|
+
body: {
|
|
446
|
+
access_token: "hybrid-token",
|
|
447
|
+
expires_in: 3600,
|
|
448
|
+
},
|
|
449
|
+
})
|
|
450
|
+
);
|
|
451
|
+
|
|
452
|
+
await service.exchangeToken(
|
|
453
|
+
"hybrid",
|
|
454
|
+
"code",
|
|
455
|
+
"pkce-verifier",
|
|
456
|
+
"http://localhost/callback"
|
|
457
|
+
);
|
|
458
|
+
|
|
459
|
+
const call = mockFetch.mock.calls[0];
|
|
460
|
+
const body = JSON.parse(call[1].body);
|
|
461
|
+
expect(body.code_verifier).toBe("pkce-verifier");
|
|
462
|
+
});
|
|
463
|
+
|
|
464
|
+
it("should NOT include code_verifier for non-PKCE proxy providers", async () => {
|
|
465
|
+
mockFetch.mockResolvedValueOnce(
|
|
466
|
+
createMockResponse({
|
|
467
|
+
ok: true,
|
|
468
|
+
body: {
|
|
469
|
+
data: {
|
|
470
|
+
access_token: "proxy-token",
|
|
471
|
+
expires_in: 3600,
|
|
472
|
+
},
|
|
473
|
+
},
|
|
474
|
+
})
|
|
475
|
+
);
|
|
476
|
+
|
|
477
|
+
await service.exchangeToken(
|
|
478
|
+
"google",
|
|
479
|
+
"code",
|
|
480
|
+
"unused-verifier",
|
|
481
|
+
"http://localhost/callback"
|
|
482
|
+
);
|
|
483
|
+
|
|
484
|
+
const call = mockFetch.mock.calls[0];
|
|
485
|
+
const body = JSON.parse(call[1].body);
|
|
486
|
+
expect(body.code_verifier).toBeUndefined();
|
|
487
|
+
});
|
|
488
|
+
});
|
|
489
|
+
|
|
490
|
+
describe("Token Refresh Errors", () => {
|
|
491
|
+
it("should return null when provider not found for refresh", async () => {
|
|
492
|
+
const result = await service.refreshToken(
|
|
493
|
+
"unknown-provider",
|
|
494
|
+
"refresh-token"
|
|
495
|
+
);
|
|
496
|
+
expect(result).toBeNull();
|
|
497
|
+
});
|
|
498
|
+
|
|
499
|
+
it("should log provider not found for refresh", async () => {
|
|
500
|
+
await service.refreshToken("unknown-provider", "refresh-token");
|
|
501
|
+
|
|
502
|
+
expect(mockLogger).toHaveBeenCalledWith(
|
|
503
|
+
expect.stringContaining("Provider not found for refresh"),
|
|
504
|
+
expect.objectContaining({ provider: "unknown-provider" })
|
|
505
|
+
);
|
|
506
|
+
});
|
|
507
|
+
});
|
|
508
|
+
|
|
509
|
+
describe("Successful Token Exchange", () => {
|
|
510
|
+
it("should return valid tokens on successful PKCE exchange", async () => {
|
|
511
|
+
mockFetch.mockResolvedValueOnce(
|
|
512
|
+
createMockResponse({
|
|
513
|
+
ok: true,
|
|
514
|
+
body: {
|
|
515
|
+
access_token: "gho_abc123",
|
|
516
|
+
token_type: "Bearer",
|
|
517
|
+
scope: "read:user,user:email",
|
|
518
|
+
expires_in: 28800, // 8 hours
|
|
519
|
+
},
|
|
520
|
+
})
|
|
521
|
+
);
|
|
522
|
+
|
|
523
|
+
const tokens = await service.exchangeToken(
|
|
524
|
+
"github",
|
|
525
|
+
"auth-code",
|
|
526
|
+
"verifier",
|
|
527
|
+
"http://localhost/callback"
|
|
528
|
+
);
|
|
529
|
+
|
|
530
|
+
expect(tokens.access_token).toBe("gho_abc123");
|
|
531
|
+
expect(tokens.token_type).toBe("Bearer");
|
|
532
|
+
expect(tokens.scope).toBe("read:user,user:email");
|
|
533
|
+
expect(tokens.expires_in).toBe(28800);
|
|
534
|
+
expect(tokens.expires_at).toBeGreaterThan(Date.now());
|
|
535
|
+
});
|
|
536
|
+
|
|
537
|
+
it("should handle response with optional refresh_token", async () => {
|
|
538
|
+
mockFetch.mockResolvedValueOnce(
|
|
539
|
+
createMockResponse({
|
|
540
|
+
ok: true,
|
|
541
|
+
body: {
|
|
542
|
+
access_token: "access-token",
|
|
543
|
+
refresh_token: "refresh-token",
|
|
544
|
+
expires_in: 3600,
|
|
545
|
+
},
|
|
546
|
+
})
|
|
547
|
+
);
|
|
548
|
+
|
|
549
|
+
const tokens = await service.exchangeToken(
|
|
550
|
+
"github",
|
|
551
|
+
"code",
|
|
552
|
+
"verifier",
|
|
553
|
+
"http://localhost/callback"
|
|
554
|
+
);
|
|
555
|
+
|
|
556
|
+
expect(tokens.access_token).toBe("access-token");
|
|
557
|
+
expect(tokens.refresh_token).toBe("refresh-token");
|
|
558
|
+
});
|
|
559
|
+
|
|
560
|
+
it("should handle proxy response with data wrapper", async () => {
|
|
561
|
+
mockFetch.mockResolvedValueOnce(
|
|
562
|
+
createMockResponse({
|
|
563
|
+
ok: true,
|
|
564
|
+
body: {
|
|
565
|
+
data: {
|
|
566
|
+
access_token: "wrapped-token",
|
|
567
|
+
expires_in: 7200,
|
|
568
|
+
},
|
|
569
|
+
},
|
|
570
|
+
})
|
|
571
|
+
);
|
|
572
|
+
|
|
573
|
+
const tokens = await service.exchangeToken(
|
|
574
|
+
"google",
|
|
575
|
+
"code",
|
|
576
|
+
undefined,
|
|
577
|
+
"http://localhost/callback"
|
|
578
|
+
);
|
|
579
|
+
|
|
580
|
+
expect(tokens.access_token).toBe("wrapped-token");
|
|
581
|
+
expect(tokens.expires_in).toBe(7200);
|
|
582
|
+
});
|
|
583
|
+
|
|
584
|
+
it("should handle proxy response without data wrapper", async () => {
|
|
585
|
+
mockFetch.mockResolvedValueOnce(
|
|
586
|
+
createMockResponse({
|
|
587
|
+
ok: true,
|
|
588
|
+
body: {
|
|
589
|
+
access_token: "direct-token",
|
|
590
|
+
expires_in: 3600,
|
|
591
|
+
},
|
|
592
|
+
})
|
|
593
|
+
);
|
|
594
|
+
|
|
595
|
+
const tokens = await service.exchangeToken(
|
|
596
|
+
"google",
|
|
597
|
+
"code",
|
|
598
|
+
undefined,
|
|
599
|
+
"http://localhost/callback"
|
|
600
|
+
);
|
|
601
|
+
|
|
602
|
+
expect(tokens.access_token).toBe("direct-token");
|
|
603
|
+
});
|
|
604
|
+
|
|
605
|
+
it("should default expires_in to 3600 when not provided", async () => {
|
|
606
|
+
mockFetch.mockResolvedValueOnce(
|
|
607
|
+
createMockResponse({
|
|
608
|
+
ok: true,
|
|
609
|
+
body: {
|
|
610
|
+
access_token: "token-no-expiry",
|
|
611
|
+
// No expires_in
|
|
612
|
+
},
|
|
613
|
+
})
|
|
614
|
+
);
|
|
615
|
+
|
|
616
|
+
const tokens = await service.exchangeToken(
|
|
617
|
+
"github",
|
|
618
|
+
"code",
|
|
619
|
+
"verifier",
|
|
620
|
+
"http://localhost/callback"
|
|
621
|
+
);
|
|
622
|
+
|
|
623
|
+
expect(tokens.expires_in).toBe(3600);
|
|
624
|
+
});
|
|
625
|
+
|
|
626
|
+
it("should default token_type to Bearer when not provided", async () => {
|
|
627
|
+
mockFetch.mockResolvedValueOnce(
|
|
628
|
+
createMockResponse({
|
|
629
|
+
ok: true,
|
|
630
|
+
body: {
|
|
631
|
+
access_token: "token-no-type",
|
|
632
|
+
// No token_type
|
|
633
|
+
},
|
|
634
|
+
})
|
|
635
|
+
);
|
|
636
|
+
|
|
637
|
+
const tokens = await service.exchangeToken(
|
|
638
|
+
"github",
|
|
639
|
+
"code",
|
|
640
|
+
"verifier",
|
|
641
|
+
"http://localhost/callback"
|
|
642
|
+
);
|
|
643
|
+
|
|
644
|
+
expect(tokens.token_type).toBe("Bearer");
|
|
645
|
+
});
|
|
646
|
+
});
|
|
647
|
+
|
|
648
|
+
describe("Client Secret Handling", () => {
|
|
649
|
+
it("should include client_secret when provider has one", async () => {
|
|
650
|
+
// Add client_secret to mock config
|
|
651
|
+
mockConfigService.getOAuthConfig = vi.fn().mockResolvedValue({
|
|
652
|
+
enabled: true,
|
|
653
|
+
providers: {
|
|
654
|
+
github: {
|
|
655
|
+
...pkceProvider,
|
|
656
|
+
clientSecret: "github-secret-123",
|
|
657
|
+
},
|
|
658
|
+
},
|
|
659
|
+
});
|
|
660
|
+
|
|
661
|
+
mockFetch.mockResolvedValueOnce(
|
|
662
|
+
createMockResponse({
|
|
663
|
+
ok: true,
|
|
664
|
+
body: {
|
|
665
|
+
access_token: "token-with-secret",
|
|
666
|
+
},
|
|
667
|
+
})
|
|
668
|
+
);
|
|
669
|
+
|
|
670
|
+
await service.exchangeToken(
|
|
671
|
+
"github",
|
|
672
|
+
"code",
|
|
673
|
+
"verifier",
|
|
674
|
+
"http://localhost/callback"
|
|
675
|
+
);
|
|
676
|
+
|
|
677
|
+
const call = mockFetch.mock.calls[0];
|
|
678
|
+
const body = new URLSearchParams(call[1].body);
|
|
679
|
+
expect(body.get("client_secret")).toBe("github-secret-123");
|
|
680
|
+
});
|
|
681
|
+
|
|
682
|
+
it("should NOT include client_secret when provider doesn't have one", async () => {
|
|
683
|
+
mockFetch.mockResolvedValueOnce(
|
|
684
|
+
createMockResponse({
|
|
685
|
+
ok: true,
|
|
686
|
+
body: {
|
|
687
|
+
access_token: "token-without-secret",
|
|
688
|
+
},
|
|
689
|
+
})
|
|
690
|
+
);
|
|
691
|
+
|
|
692
|
+
await service.exchangeToken(
|
|
693
|
+
"github",
|
|
694
|
+
"code",
|
|
695
|
+
"verifier",
|
|
696
|
+
"http://localhost/callback"
|
|
697
|
+
);
|
|
698
|
+
|
|
699
|
+
const call = mockFetch.mock.calls[0];
|
|
700
|
+
const body = new URLSearchParams(call[1].body);
|
|
701
|
+
expect(body.has("client_secret")).toBe(false);
|
|
702
|
+
});
|
|
703
|
+
});
|
|
704
|
+
});
|