@kya-os/mcp-i-core 1.2.3-canary.7 → 1.3.0-canary.clientinfo.20251126003544
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/.turbo/turbo-test$colon$coverage.log +4239 -0
- package/.turbo/turbo-test.log +2973 -0
- package/COMPLIANCE_IMPROVEMENT_REPORT.md +483 -0
- package/Composer 3.md +615 -0
- package/GPT-5.md +1169 -0
- package/OPUS-plan.md +352 -0
- package/PHASE_3_AND_4.1_SUMMARY.md +585 -0
- package/PHASE_3_SUMMARY.md +317 -0
- package/PHASE_4.1.3_SUMMARY.md +428 -0
- package/PHASE_4.1_COMPLETE.md +525 -0
- package/PHASE_4_USER_DID_IDENTITY_LINKING_PLAN.md +1240 -0
- package/SCHEMA_COMPLIANCE_REPORT.md +275 -0
- package/TEST_PLAN.md +571 -0
- package/coverage/coverage-final.json +57 -0
- package/dist/__tests__/utils/mock-providers.d.ts +1 -2
- package/dist/__tests__/utils/mock-providers.d.ts.map +1 -1
- package/dist/__tests__/utils/mock-providers.js.map +1 -1
- package/dist/cache/oauth-config-cache.d.ts +69 -0
- package/dist/cache/oauth-config-cache.d.ts.map +1 -0
- package/dist/cache/oauth-config-cache.js +76 -0
- package/dist/cache/oauth-config-cache.js.map +1 -0
- package/dist/identity/idp-token-resolver.d.ts +53 -0
- package/dist/identity/idp-token-resolver.d.ts.map +1 -0
- package/dist/identity/idp-token-resolver.js +108 -0
- package/dist/identity/idp-token-resolver.js.map +1 -0
- package/dist/identity/idp-token-storage.interface.d.ts +42 -0
- package/dist/identity/idp-token-storage.interface.d.ts.map +1 -0
- package/dist/identity/idp-token-storage.interface.js +12 -0
- package/dist/identity/idp-token-storage.interface.js.map +1 -0
- package/dist/identity/user-did-manager.d.ts +39 -1
- package/dist/identity/user-did-manager.d.ts.map +1 -1
- package/dist/identity/user-did-manager.js +69 -3
- package/dist/identity/user-did-manager.js.map +1 -1
- package/dist/index.d.ts +24 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +43 -1
- package/dist/index.js.map +1 -1
- package/dist/runtime/audit-logger.d.ts +37 -0
- package/dist/runtime/audit-logger.d.ts.map +1 -0
- package/dist/runtime/audit-logger.js +9 -0
- package/dist/runtime/audit-logger.js.map +1 -0
- package/dist/runtime/base.d.ts +58 -2
- package/dist/runtime/base.d.ts.map +1 -1
- package/dist/runtime/base.js +266 -11
- package/dist/runtime/base.js.map +1 -1
- package/dist/services/access-control.service.d.ts.map +1 -1
- package/dist/services/access-control.service.js +200 -35
- package/dist/services/access-control.service.js.map +1 -1
- package/dist/services/authorization/authorization-registry.d.ts +29 -0
- package/dist/services/authorization/authorization-registry.d.ts.map +1 -0
- package/dist/services/authorization/authorization-registry.js +57 -0
- package/dist/services/authorization/authorization-registry.js.map +1 -0
- package/dist/services/authorization/types.d.ts +53 -0
- package/dist/services/authorization/types.d.ts.map +1 -0
- package/dist/services/authorization/types.js +10 -0
- package/dist/services/authorization/types.js.map +1 -0
- package/dist/services/batch-delegation.service.d.ts +53 -0
- package/dist/services/batch-delegation.service.d.ts.map +1 -0
- package/dist/services/batch-delegation.service.js +95 -0
- package/dist/services/batch-delegation.service.js.map +1 -0
- package/dist/services/index.d.ts +2 -0
- package/dist/services/index.d.ts.map +1 -1
- package/dist/services/index.js +4 -1
- package/dist/services/index.js.map +1 -1
- package/dist/services/oauth-config.service.d.ts +53 -0
- package/dist/services/oauth-config.service.d.ts.map +1 -0
- package/dist/services/oauth-config.service.js +117 -0
- package/dist/services/oauth-config.service.js.map +1 -0
- package/dist/services/oauth-provider-registry.d.ts +77 -0
- package/dist/services/oauth-provider-registry.d.ts.map +1 -0
- package/dist/services/oauth-provider-registry.js +112 -0
- package/dist/services/oauth-provider-registry.js.map +1 -0
- package/dist/services/oauth-service.d.ts +77 -0
- package/dist/services/oauth-service.d.ts.map +1 -0
- package/dist/services/oauth-service.js +348 -0
- package/dist/services/oauth-service.js.map +1 -0
- package/dist/services/oauth-token-retrieval.service.d.ts +49 -0
- package/dist/services/oauth-token-retrieval.service.d.ts.map +1 -0
- package/dist/services/oauth-token-retrieval.service.js +150 -0
- package/dist/services/oauth-token-retrieval.service.js.map +1 -0
- package/dist/services/provider-resolver.d.ts +48 -0
- package/dist/services/provider-resolver.d.ts.map +1 -0
- package/dist/services/provider-resolver.js +120 -0
- package/dist/services/provider-resolver.js.map +1 -0
- package/dist/services/provider-validator.d.ts +55 -0
- package/dist/services/provider-validator.d.ts.map +1 -0
- package/dist/services/provider-validator.js +135 -0
- package/dist/services/provider-validator.js.map +1 -0
- package/dist/services/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 +172 -0
- package/dist/services/session-registration.service.js.map +1 -0
- package/dist/services/tool-context-builder.d.ts +57 -0
- package/dist/services/tool-context-builder.d.ts.map +1 -0
- package/dist/services/tool-context-builder.js +125 -0
- package/dist/services/tool-context-builder.js.map +1 -0
- package/dist/services/tool-protection.service.d.ts +87 -10
- package/dist/services/tool-protection.service.d.ts.map +1 -1
- package/dist/services/tool-protection.service.js +282 -112
- package/dist/services/tool-protection.service.js.map +1 -1
- package/dist/types/oauth-required-error.d.ts +40 -0
- package/dist/types/oauth-required-error.d.ts.map +1 -0
- package/dist/types/oauth-required-error.js +40 -0
- package/dist/types/oauth-required-error.js.map +1 -0
- package/dist/utils/did-helpers.d.ts +33 -0
- package/dist/utils/did-helpers.d.ts.map +1 -1
- package/dist/utils/did-helpers.js +40 -0
- package/dist/utils/did-helpers.js.map +1 -1
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.d.ts.map +1 -1
- package/dist/utils/index.js +1 -0
- package/dist/utils/index.js.map +1 -1
- package/docs/API_REFERENCE.md +1362 -0
- package/docs/COMPLIANCE_MATRIX.md +691 -0
- package/docs/STATUSLIST2021_GUIDE.md +696 -0
- package/docs/W3C_VC_DELEGATION_GUIDE.md +710 -0
- package/package.json +24 -50
- package/scripts/audit-compliance.ts +724 -0
- package/src/__tests__/cache/tool-protection-cache.test.ts +640 -0
- package/src/__tests__/config/provider-runtime-config.test.ts +309 -0
- package/src/__tests__/delegation-e2e.test.ts +690 -0
- package/src/__tests__/identity/user-did-manager.test.ts +213 -0
- package/src/__tests__/index.test.ts +56 -0
- package/src/__tests__/integration/full-flow.test.ts +776 -0
- package/src/__tests__/integration.test.ts +281 -0
- package/src/__tests__/providers/base.test.ts +173 -0
- package/src/__tests__/providers/memory.test.ts +319 -0
- package/src/__tests__/regression/phase2-regression.test.ts +427 -0
- package/src/__tests__/runtime/audit-logger.test.ts +154 -0
- package/src/__tests__/runtime/base-extensions.test.ts +593 -0
- package/src/__tests__/runtime/base.test.ts +869 -0
- package/src/__tests__/runtime/delegation-flow.test.ts +164 -0
- package/src/__tests__/runtime/proof-client-did.test.ts +375 -0
- package/src/__tests__/runtime/route-interception.test.ts +686 -0
- package/src/__tests__/runtime/tool-protection-enforcement.test.ts +908 -0
- package/src/__tests__/services/agentshield-integration.test.ts +784 -0
- package/src/__tests__/services/provider-resolver-edge-cases.test.ts +487 -0
- package/src/__tests__/services/tool-protection-oauth-provider.test.ts +480 -0
- package/src/__tests__/services/tool-protection.service.test.ts +1366 -0
- package/src/__tests__/utils/mock-providers.ts +340 -0
- package/src/cache/oauth-config-cache.d.ts +69 -0
- package/src/cache/oauth-config-cache.d.ts.map +1 -0
- package/src/cache/oauth-config-cache.js +71 -0
- package/src/cache/oauth-config-cache.js.map +1 -0
- package/src/cache/oauth-config-cache.ts +123 -0
- package/src/cache/tool-protection-cache.ts +171 -0
- package/src/compliance/EXAMPLE.md +412 -0
- package/src/compliance/__tests__/schema-verifier.test.ts +797 -0
- package/src/compliance/index.ts +8 -0
- package/src/compliance/schema-registry.ts +460 -0
- package/src/compliance/schema-verifier.ts +708 -0
- package/src/config/__tests__/remote-config.spec.ts +268 -0
- package/src/config/remote-config.ts +174 -0
- package/src/config.ts +309 -0
- package/src/delegation/__tests__/audience-validator.test.ts +112 -0
- package/src/delegation/__tests__/bitstring.test.ts +346 -0
- package/src/delegation/__tests__/cascading-revocation.test.ts +628 -0
- package/src/delegation/__tests__/delegation-graph.test.ts +584 -0
- package/src/delegation/__tests__/utils.test.ts +152 -0
- package/src/delegation/__tests__/vc-issuer.test.ts +442 -0
- package/src/delegation/__tests__/vc-verifier.test.ts +922 -0
- package/src/delegation/audience-validator.ts +52 -0
- package/src/delegation/bitstring.ts +278 -0
- package/src/delegation/cascading-revocation.ts +370 -0
- package/src/delegation/delegation-graph.ts +299 -0
- package/src/delegation/index.ts +14 -0
- package/src/delegation/statuslist-manager.ts +353 -0
- package/src/delegation/storage/__tests__/memory-graph-storage.test.ts +366 -0
- package/src/delegation/storage/__tests__/memory-statuslist-storage.test.ts +228 -0
- package/src/delegation/storage/index.ts +9 -0
- package/src/delegation/storage/memory-graph-storage.ts +178 -0
- package/src/delegation/storage/memory-statuslist-storage.ts +77 -0
- package/src/delegation/utils.ts +42 -0
- package/src/delegation/vc-issuer.ts +232 -0
- package/src/delegation/vc-verifier.ts +568 -0
- package/src/identity/idp-token-resolver.ts +147 -0
- package/src/identity/idp-token-storage.interface.ts +59 -0
- package/src/identity/user-did-manager.ts +370 -0
- package/src/index.ts +271 -0
- package/src/providers/base.d.ts +91 -0
- package/src/providers/base.d.ts.map +1 -0
- package/src/providers/base.js +38 -0
- package/src/providers/base.js.map +1 -0
- package/src/providers/base.ts +96 -0
- package/src/providers/memory.ts +142 -0
- package/src/runtime/audit-logger.ts +39 -0
- package/src/runtime/base.ts +1329 -0
- package/src/services/__tests__/access-control.integration.test.ts +443 -0
- package/src/services/__tests__/access-control.proof-response-validation.test.ts +578 -0
- package/src/services/__tests__/access-control.service.test.ts +970 -0
- package/src/services/__tests__/batch-delegation.service.test.ts +351 -0
- package/src/services/__tests__/crypto.service.test.ts +531 -0
- package/src/services/__tests__/oauth-provider-registry.test.ts +142 -0
- package/src/services/__tests__/proof-verifier.integration.test.ts +485 -0
- package/src/services/__tests__/proof-verifier.test.ts +489 -0
- package/src/services/__tests__/provider-resolution.integration.test.ts +198 -0
- package/src/services/__tests__/provider-resolver.test.ts +217 -0
- package/src/services/__tests__/storage.service.test.ts +358 -0
- package/src/services/access-control.service.ts +990 -0
- package/src/services/authorization/authorization-registry.ts +66 -0
- package/src/services/authorization/types.ts +71 -0
- package/src/services/batch-delegation.service.ts +137 -0
- package/src/services/crypto.service.ts +302 -0
- package/src/services/errors.ts +76 -0
- package/src/services/index.ts +18 -0
- package/src/services/oauth-config.service.d.ts +53 -0
- package/src/services/oauth-config.service.d.ts.map +1 -0
- package/src/services/oauth-config.service.js +113 -0
- package/src/services/oauth-config.service.js.map +1 -0
- package/src/services/oauth-config.service.ts +166 -0
- package/src/services/oauth-provider-registry.d.ts +57 -0
- package/src/services/oauth-provider-registry.d.ts.map +1 -0
- package/src/services/oauth-provider-registry.js +73 -0
- package/src/services/oauth-provider-registry.js.map +1 -0
- package/src/services/oauth-provider-registry.ts +123 -0
- package/src/services/oauth-service.ts +510 -0
- package/src/services/oauth-token-retrieval.service.ts +245 -0
- package/src/services/proof-verifier.ts +478 -0
- package/src/services/provider-resolver.d.ts +48 -0
- package/src/services/provider-resolver.d.ts.map +1 -0
- package/src/services/provider-resolver.js +106 -0
- package/src/services/provider-resolver.js.map +1 -0
- package/src/services/provider-resolver.ts +144 -0
- package/src/services/provider-validator.ts +170 -0
- package/src/services/session-registration.service.ts +251 -0
- package/src/services/storage.service.ts +566 -0
- package/src/services/tool-context-builder.ts +172 -0
- package/src/services/tool-protection.service.ts +958 -0
- package/src/types/oauth-required-error.ts +63 -0
- package/src/types/tool-protection.ts +155 -0
- package/src/utils/__tests__/did-helpers.test.ts +101 -0
- package/src/utils/base64.ts +148 -0
- package/src/utils/cors.ts +83 -0
- package/src/utils/did-helpers.ts +150 -0
- package/src/utils/index.ts +8 -0
- package/src/utils/storage-keys.ts +278 -0
- package/tsconfig.json +21 -0
- package/vitest.config.ts +56 -0
|
@@ -0,0 +1,510 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OAuth Service
|
|
3
|
+
*
|
|
4
|
+
* Handles OAuth token exchange and refresh using PKCE (Proof Key for Code Exchange).
|
|
5
|
+
* Supports both direct PKCE exchange with OAuth providers and proxy mode via AgentShield.
|
|
6
|
+
*
|
|
7
|
+
* @package @kya-os/mcp-i-core
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type { FetchProvider } from "../providers/base.js";
|
|
11
|
+
import type { OAuthConfigService } from "./oauth-config.service.js";
|
|
12
|
+
import type { IdpTokens, OAuthProvider } from "@kya-os/contracts/config";
|
|
13
|
+
|
|
14
|
+
export interface OAuthServiceConfig {
|
|
15
|
+
/** OAuth config service for fetching provider configurations */
|
|
16
|
+
configService: OAuthConfigService;
|
|
17
|
+
|
|
18
|
+
/** Fetch provider for making HTTP requests */
|
|
19
|
+
fetchProvider: FetchProvider;
|
|
20
|
+
|
|
21
|
+
/** AgentShield API URL (for proxy mode) */
|
|
22
|
+
agentShieldApiUrl: string;
|
|
23
|
+
|
|
24
|
+
/** AgentShield API key (for proxy mode) */
|
|
25
|
+
agentShieldApiKey: string;
|
|
26
|
+
|
|
27
|
+
/** Project ID for fetching OAuth config */
|
|
28
|
+
projectId: string;
|
|
29
|
+
|
|
30
|
+
/** Optional logger callback for diagnostics */
|
|
31
|
+
logger?: (message: string, data?: unknown) => void;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Service for OAuth token exchange and refresh
|
|
36
|
+
*/
|
|
37
|
+
export class OAuthService {
|
|
38
|
+
private config: Required<Omit<OAuthServiceConfig, "logger">> & {
|
|
39
|
+
logger: (message: string, data?: unknown) => void;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
constructor(config: OAuthServiceConfig) {
|
|
43
|
+
this.config = {
|
|
44
|
+
configService: config.configService,
|
|
45
|
+
fetchProvider: config.fetchProvider,
|
|
46
|
+
agentShieldApiUrl: config.agentShieldApiUrl,
|
|
47
|
+
agentShieldApiKey: config.agentShieldApiKey,
|
|
48
|
+
projectId: config.projectId,
|
|
49
|
+
logger: config.logger || (() => {}),
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Exchange authorization code for IDP tokens using PKCE
|
|
55
|
+
*
|
|
56
|
+
* For PKCE providers: Exchanges code directly with OAuth provider (no client secret)
|
|
57
|
+
* For proxy mode: Exchanges code via AgentShield API
|
|
58
|
+
*
|
|
59
|
+
* @param provider - OAuth provider name (e.g., "github", "google")
|
|
60
|
+
* @param code - Authorization code from OAuth callback
|
|
61
|
+
* @param codeVerifier - PKCE code verifier (optional for non-PKCE providers in proxy mode)
|
|
62
|
+
* @param redirectUri - Redirect URI used in authorization request
|
|
63
|
+
* @returns IDP tokens (access_token, refresh_token, expires_at, etc.)
|
|
64
|
+
*/
|
|
65
|
+
async exchangeToken(
|
|
66
|
+
provider: string,
|
|
67
|
+
code: string,
|
|
68
|
+
codeVerifier?: string,
|
|
69
|
+
redirectUri?: string
|
|
70
|
+
): Promise<IdpTokens> {
|
|
71
|
+
// Fetch provider config
|
|
72
|
+
const oauthConfig = await this.config.configService.getOAuthConfig(
|
|
73
|
+
this.config.projectId
|
|
74
|
+
);
|
|
75
|
+
const providerConfig = oauthConfig.providers[provider];
|
|
76
|
+
|
|
77
|
+
if (!providerConfig) {
|
|
78
|
+
throw new Error(`Provider "${provider}" not configured for project "${this.config.projectId}"`);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// For PKCE providers, require codeVerifier
|
|
82
|
+
if (providerConfig.supportsPKCE && !providerConfig.proxyMode) {
|
|
83
|
+
if (!codeVerifier) {
|
|
84
|
+
throw new Error(
|
|
85
|
+
`Provider "${provider}" requires PKCE code_verifier for token exchange`
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
return this.exchangeTokenPKCE(
|
|
89
|
+
providerConfig,
|
|
90
|
+
code,
|
|
91
|
+
codeVerifier,
|
|
92
|
+
redirectUri || ""
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// For proxy mode, codeVerifier is optional (only included if PKCE is supported)
|
|
97
|
+
if (providerConfig.proxyMode) {
|
|
98
|
+
return this.exchangeTokenProxy(
|
|
99
|
+
providerConfig,
|
|
100
|
+
code,
|
|
101
|
+
codeVerifier,
|
|
102
|
+
redirectUri || ""
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
throw new Error(
|
|
107
|
+
`Provider "${provider}" configuration is invalid: must support PKCE or use proxy mode`
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Exchange token directly with OAuth provider using PKCE
|
|
113
|
+
*/
|
|
114
|
+
private async exchangeTokenPKCE(
|
|
115
|
+
providerConfig: OAuthProvider,
|
|
116
|
+
code: string,
|
|
117
|
+
codeVerifier: string,
|
|
118
|
+
redirectUri: string
|
|
119
|
+
): Promise<IdpTokens> {
|
|
120
|
+
this.config.logger("[OAuthService] Exchanging token with PKCE", {
|
|
121
|
+
provider: providerConfig.authorizationUrl,
|
|
122
|
+
tokenUrl: providerConfig.tokenUrl,
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
const response = await this.config.fetchProvider.fetch(providerConfig.tokenUrl, {
|
|
126
|
+
method: "POST",
|
|
127
|
+
headers: {
|
|
128
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
129
|
+
Accept: "application/json",
|
|
130
|
+
},
|
|
131
|
+
body: new URLSearchParams({
|
|
132
|
+
grant_type: "authorization_code",
|
|
133
|
+
code,
|
|
134
|
+
redirect_uri: redirectUri,
|
|
135
|
+
client_id: providerConfig.clientId,
|
|
136
|
+
code_verifier: codeVerifier,
|
|
137
|
+
}).toString(),
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
if (!response.ok) {
|
|
141
|
+
const errorText = await response.text().catch(() => "Unknown error");
|
|
142
|
+
let errorData: any;
|
|
143
|
+
try {
|
|
144
|
+
errorData = JSON.parse(errorText);
|
|
145
|
+
} catch {
|
|
146
|
+
errorData = { error: errorText };
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const errorMessage =
|
|
150
|
+
errorData.error_description || errorData.error || errorText;
|
|
151
|
+
|
|
152
|
+
this.config.logger("[OAuthService] Token exchange failed", {
|
|
153
|
+
status: response.status,
|
|
154
|
+
error: errorMessage,
|
|
155
|
+
provider: providerConfig.tokenUrl,
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
throw new Error(
|
|
159
|
+
`Token exchange failed: ${errorMessage} (${response.status})`
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const tokens = await response.json() as {
|
|
164
|
+
access_token: string;
|
|
165
|
+
refresh_token?: string;
|
|
166
|
+
expires_in?: number;
|
|
167
|
+
token_type?: string;
|
|
168
|
+
scope?: string;
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
// Validate required fields
|
|
172
|
+
if (!tokens.access_token) {
|
|
173
|
+
throw new Error("Token response missing access_token");
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// Calculate expiration timestamp
|
|
177
|
+
const expiresIn = tokens.expires_in || 3600; // Default 1 hour
|
|
178
|
+
const expiresAt = Date.now() + expiresIn * 1000;
|
|
179
|
+
|
|
180
|
+
const idpTokens: IdpTokens = {
|
|
181
|
+
access_token: tokens.access_token,
|
|
182
|
+
refresh_token: tokens.refresh_token,
|
|
183
|
+
expires_in: expiresIn,
|
|
184
|
+
expires_at: expiresAt,
|
|
185
|
+
token_type: tokens.token_type || "Bearer",
|
|
186
|
+
scope: tokens.scope,
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
this.config.logger("[OAuthService] Token exchange successful", {
|
|
190
|
+
provider: providerConfig.tokenUrl,
|
|
191
|
+
expiresAt: new Date(expiresAt).toISOString(),
|
|
192
|
+
hasRefreshToken: !!idpTokens.refresh_token,
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
return idpTokens;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Exchange token via AgentShield proxy (for providers that require proxy mode)
|
|
200
|
+
*
|
|
201
|
+
* Note: For Phase 3 two-step flow, OAuth tokens are retrieved separately via
|
|
202
|
+
* OAuthTokenRetrievalService in the callback handler. This method maintains
|
|
203
|
+
* backward compatibility for direct proxy mode usage.
|
|
204
|
+
*/
|
|
205
|
+
private async exchangeTokenProxy(
|
|
206
|
+
providerConfig: OAuthProvider,
|
|
207
|
+
code: string,
|
|
208
|
+
codeVerifier?: string,
|
|
209
|
+
redirectUri?: string
|
|
210
|
+
): Promise<IdpTokens> {
|
|
211
|
+
// Exchange via AgentShield proxy endpoint
|
|
212
|
+
const proxyUrl = `${this.config.agentShieldApiUrl}/api/v1/oauth/token`;
|
|
213
|
+
|
|
214
|
+
this.config.logger("[OAuthService] Exchanging token via proxy", {
|
|
215
|
+
proxyUrl,
|
|
216
|
+
provider: providerConfig.authorizationUrl,
|
|
217
|
+
hasCodeVerifier: !!codeVerifier,
|
|
218
|
+
supportsPKCE: providerConfig.supportsPKCE,
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
// Build request body - only include code_verifier if PKCE is supported and provided
|
|
222
|
+
const requestBody: Record<string, string> = {
|
|
223
|
+
grant_type: "authorization_code",
|
|
224
|
+
code,
|
|
225
|
+
redirect_uri: redirectUri || "",
|
|
226
|
+
provider: providerConfig.authorizationUrl,
|
|
227
|
+
project_id: this.config.projectId,
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
// Include code_verifier only if provider supports PKCE and verifier is provided
|
|
231
|
+
if (providerConfig.supportsPKCE && codeVerifier) {
|
|
232
|
+
requestBody.code_verifier = codeVerifier;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
const response = await this.config.fetchProvider.fetch(proxyUrl, {
|
|
236
|
+
method: "POST",
|
|
237
|
+
headers: {
|
|
238
|
+
"Content-Type": "application/json",
|
|
239
|
+
Authorization: `Bearer ${this.config.agentShieldApiKey}`,
|
|
240
|
+
},
|
|
241
|
+
body: JSON.stringify(requestBody),
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
if (!response.ok) {
|
|
245
|
+
const errorText = await response.text().catch(() => "Unknown error");
|
|
246
|
+
let errorData: any;
|
|
247
|
+
try {
|
|
248
|
+
errorData = JSON.parse(errorText);
|
|
249
|
+
} catch {
|
|
250
|
+
errorData = { error: errorText };
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
const errorMessage =
|
|
254
|
+
errorData.error_description || errorData.error || errorText;
|
|
255
|
+
|
|
256
|
+
this.config.logger("[OAuthService] Proxy token exchange failed", {
|
|
257
|
+
status: response.status,
|
|
258
|
+
error: errorMessage,
|
|
259
|
+
proxyUrl,
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
throw new Error(
|
|
263
|
+
`Proxy token exchange failed: ${errorMessage} (${response.status})`
|
|
264
|
+
);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
const result = await response.json() as {
|
|
268
|
+
data?: {
|
|
269
|
+
access_token: string;
|
|
270
|
+
refresh_token?: string;
|
|
271
|
+
expires_in?: number;
|
|
272
|
+
token_type?: string;
|
|
273
|
+
scope?: string;
|
|
274
|
+
};
|
|
275
|
+
access_token?: string;
|
|
276
|
+
refresh_token?: string;
|
|
277
|
+
expires_in?: number;
|
|
278
|
+
token_type?: string;
|
|
279
|
+
scope?: string;
|
|
280
|
+
};
|
|
281
|
+
const tokens = result.data || result;
|
|
282
|
+
|
|
283
|
+
// Validate required fields
|
|
284
|
+
if (!tokens.access_token) {
|
|
285
|
+
throw new Error("Proxy token response missing access_token");
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
// Calculate expiration timestamp
|
|
289
|
+
const expiresIn = tokens.expires_in || 3600; // Default 1 hour
|
|
290
|
+
const expiresAt = Date.now() + expiresIn * 1000;
|
|
291
|
+
|
|
292
|
+
const idpTokens: IdpTokens = {
|
|
293
|
+
access_token: tokens.access_token,
|
|
294
|
+
refresh_token: tokens.refresh_token,
|
|
295
|
+
expires_in: expiresIn,
|
|
296
|
+
expires_at: expiresAt,
|
|
297
|
+
token_type: tokens.token_type || "Bearer",
|
|
298
|
+
scope: tokens.scope,
|
|
299
|
+
};
|
|
300
|
+
|
|
301
|
+
this.config.logger("[OAuthService] Proxy token exchange successful", {
|
|
302
|
+
proxyUrl,
|
|
303
|
+
expiresAt: new Date(expiresAt).toISOString(),
|
|
304
|
+
hasRefreshToken: !!idpTokens.refresh_token,
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
return idpTokens;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* Refresh IDP access token using refresh token
|
|
312
|
+
*
|
|
313
|
+
* For PKCE providers: Refreshes directly with OAuth provider
|
|
314
|
+
* For proxy mode: Refreshes via AgentShield API
|
|
315
|
+
*
|
|
316
|
+
* @param provider - OAuth provider name
|
|
317
|
+
* @param refreshToken - Refresh token from previous token exchange
|
|
318
|
+
* @returns New IDP tokens or null if refresh failed
|
|
319
|
+
*/
|
|
320
|
+
async refreshToken(
|
|
321
|
+
provider: string,
|
|
322
|
+
refreshToken: string
|
|
323
|
+
): Promise<IdpTokens | null> {
|
|
324
|
+
// Fetch provider config
|
|
325
|
+
const oauthConfig = await this.config.configService.getOAuthConfig(
|
|
326
|
+
this.config.projectId
|
|
327
|
+
);
|
|
328
|
+
const providerConfig = oauthConfig.providers[provider];
|
|
329
|
+
|
|
330
|
+
if (!providerConfig) {
|
|
331
|
+
this.config.logger("[OAuthService] Provider not found for refresh", {
|
|
332
|
+
provider,
|
|
333
|
+
});
|
|
334
|
+
return null;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
// For PKCE providers, refresh directly with OAuth provider
|
|
338
|
+
if (providerConfig.supportsPKCE && !providerConfig.proxyMode) {
|
|
339
|
+
return this.refreshTokenPKCE(providerConfig, refreshToken);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
// For proxy mode, refresh via AgentShield
|
|
343
|
+
if (providerConfig.proxyMode) {
|
|
344
|
+
return this.refreshTokenProxy(providerConfig, refreshToken);
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
return null;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
/**
|
|
351
|
+
* Refresh token directly with OAuth provider using PKCE
|
|
352
|
+
*/
|
|
353
|
+
private async refreshTokenPKCE(
|
|
354
|
+
providerConfig: OAuthProvider,
|
|
355
|
+
refreshToken: string
|
|
356
|
+
): Promise<IdpTokens | null> {
|
|
357
|
+
this.config.logger("[OAuthService] Refreshing token with PKCE", {
|
|
358
|
+
provider: providerConfig.tokenUrl,
|
|
359
|
+
});
|
|
360
|
+
|
|
361
|
+
try {
|
|
362
|
+
const response = await this.config.fetchProvider.fetch(providerConfig.tokenUrl, {
|
|
363
|
+
method: "POST",
|
|
364
|
+
headers: {
|
|
365
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
366
|
+
Accept: "application/json",
|
|
367
|
+
},
|
|
368
|
+
body: new URLSearchParams({
|
|
369
|
+
grant_type: "refresh_token",
|
|
370
|
+
refresh_token: refreshToken,
|
|
371
|
+
client_id: providerConfig.clientId,
|
|
372
|
+
}).toString(),
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
if (!response.ok) {
|
|
376
|
+
this.config.logger("[OAuthService] Token refresh failed", {
|
|
377
|
+
status: response.status,
|
|
378
|
+
provider: providerConfig.tokenUrl,
|
|
379
|
+
});
|
|
380
|
+
return null;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
const tokens = await response.json() as {
|
|
384
|
+
access_token: string;
|
|
385
|
+
refresh_token?: string;
|
|
386
|
+
expires_in?: number;
|
|
387
|
+
token_type?: string;
|
|
388
|
+
scope?: string;
|
|
389
|
+
};
|
|
390
|
+
|
|
391
|
+
if (!tokens.access_token) {
|
|
392
|
+
this.config.logger("[OAuthService] Token refresh response missing access_token");
|
|
393
|
+
return null;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
// Calculate expiration timestamp
|
|
397
|
+
const expiresIn = tokens.expires_in || 3600; // Default 1 hour
|
|
398
|
+
const expiresAt = Date.now() + expiresIn * 1000;
|
|
399
|
+
|
|
400
|
+
const idpTokens: IdpTokens = {
|
|
401
|
+
access_token: tokens.access_token,
|
|
402
|
+
refresh_token: tokens.refresh_token || refreshToken, // Use new refresh token if provided, otherwise keep old one
|
|
403
|
+
expires_in: expiresIn,
|
|
404
|
+
expires_at: expiresAt,
|
|
405
|
+
token_type: tokens.token_type || "Bearer",
|
|
406
|
+
scope: tokens.scope,
|
|
407
|
+
};
|
|
408
|
+
|
|
409
|
+
this.config.logger("[OAuthService] Token refresh successful", {
|
|
410
|
+
provider: providerConfig.tokenUrl,
|
|
411
|
+
expiresAt: new Date(expiresAt).toISOString(),
|
|
412
|
+
});
|
|
413
|
+
|
|
414
|
+
return idpTokens;
|
|
415
|
+
} catch (error) {
|
|
416
|
+
this.config.logger("[OAuthService] Token refresh error", {
|
|
417
|
+
error: error instanceof Error ? error.message : String(error),
|
|
418
|
+
provider: providerConfig.tokenUrl,
|
|
419
|
+
});
|
|
420
|
+
return null;
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
/**
|
|
425
|
+
* Refresh token via AgentShield proxy
|
|
426
|
+
*/
|
|
427
|
+
private async refreshTokenProxy(
|
|
428
|
+
providerConfig: OAuthProvider,
|
|
429
|
+
refreshToken: string
|
|
430
|
+
): Promise<IdpTokens | null> {
|
|
431
|
+
const proxyUrl = `${this.config.agentShieldApiUrl}/api/v1/oauth/token`;
|
|
432
|
+
|
|
433
|
+
this.config.logger("[OAuthService] Refreshing token via proxy", {
|
|
434
|
+
proxyUrl,
|
|
435
|
+
provider: providerConfig.authorizationUrl,
|
|
436
|
+
});
|
|
437
|
+
|
|
438
|
+
try {
|
|
439
|
+
const response = await this.config.fetchProvider.fetch(proxyUrl, {
|
|
440
|
+
method: "POST",
|
|
441
|
+
headers: {
|
|
442
|
+
"Content-Type": "application/json",
|
|
443
|
+
Authorization: `Bearer ${this.config.agentShieldApiKey}`,
|
|
444
|
+
},
|
|
445
|
+
body: JSON.stringify({
|
|
446
|
+
grant_type: "refresh_token",
|
|
447
|
+
refresh_token: refreshToken,
|
|
448
|
+
provider: providerConfig.authorizationUrl,
|
|
449
|
+
project_id: this.config.projectId,
|
|
450
|
+
}),
|
|
451
|
+
});
|
|
452
|
+
|
|
453
|
+
if (!response.ok) {
|
|
454
|
+
this.config.logger("[OAuthService] Proxy token refresh failed", {
|
|
455
|
+
status: response.status,
|
|
456
|
+
proxyUrl,
|
|
457
|
+
});
|
|
458
|
+
return null;
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
const result = await response.json() as {
|
|
462
|
+
data?: {
|
|
463
|
+
access_token: string;
|
|
464
|
+
refresh_token?: string;
|
|
465
|
+
expires_in?: number;
|
|
466
|
+
token_type?: string;
|
|
467
|
+
scope?: string;
|
|
468
|
+
};
|
|
469
|
+
access_token?: string;
|
|
470
|
+
refresh_token?: string;
|
|
471
|
+
expires_in?: number;
|
|
472
|
+
token_type?: string;
|
|
473
|
+
scope?: string;
|
|
474
|
+
};
|
|
475
|
+
const tokens = result.data || result;
|
|
476
|
+
|
|
477
|
+
if (!tokens.access_token) {
|
|
478
|
+
this.config.logger("[OAuthService] Proxy token refresh response missing access_token");
|
|
479
|
+
return null;
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
// Calculate expiration timestamp
|
|
483
|
+
const expiresIn = tokens.expires_in || 3600; // Default 1 hour
|
|
484
|
+
const expiresAt = Date.now() + expiresIn * 1000;
|
|
485
|
+
|
|
486
|
+
const idpTokens: IdpTokens = {
|
|
487
|
+
access_token: tokens.access_token,
|
|
488
|
+
refresh_token: tokens.refresh_token || refreshToken,
|
|
489
|
+
expires_in: expiresIn,
|
|
490
|
+
expires_at: expiresAt,
|
|
491
|
+
token_type: tokens.token_type || "Bearer",
|
|
492
|
+
scope: tokens.scope,
|
|
493
|
+
};
|
|
494
|
+
|
|
495
|
+
this.config.logger("[OAuthService] Proxy token refresh successful", {
|
|
496
|
+
proxyUrl,
|
|
497
|
+
expiresAt: new Date(expiresAt).toISOString(),
|
|
498
|
+
});
|
|
499
|
+
|
|
500
|
+
return idpTokens;
|
|
501
|
+
} catch (error) {
|
|
502
|
+
this.config.logger("[OAuthService] Proxy token refresh error", {
|
|
503
|
+
error: error instanceof Error ? error.message : String(error),
|
|
504
|
+
proxyUrl,
|
|
505
|
+
});
|
|
506
|
+
return null;
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
|