@kya-os/mcp-i-cloudflare 1.6.1 → 1.6.2-canary.clientinfo.20251125192351
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/dist/adapter.d.ts +2 -1
- package/dist/adapter.d.ts.map +1 -1
- package/dist/adapter.js +14 -2
- package/dist/adapter.js.map +1 -1
- package/dist/agent.d.ts +6 -0
- package/dist/agent.d.ts.map +1 -1
- package/dist/agent.js +32 -0
- package/dist/agent.js.map +1 -1
- package/dist/app.d.ts.map +1 -1
- package/dist/app.js +76 -9
- package/dist/app.js.map +1 -1
- package/dist/utils/client-info.d.ts +69 -0
- package/dist/utils/client-info.d.ts.map +1 -0
- package/dist/utils/client-info.js +178 -0
- package/dist/utils/client-info.js.map +1 -0
- package/dist/utils/error-formatter.d.ts +103 -0
- package/dist/utils/error-formatter.d.ts.map +1 -0
- package/dist/utils/error-formatter.js +245 -0
- package/dist/utils/error-formatter.js.map +1 -0
- package/dist/utils/initialize-context.d.ts +91 -0
- package/dist/utils/initialize-context.d.ts.map +1 -0
- package/dist/utils/initialize-context.js +169 -0
- package/dist/utils/initialize-context.js.map +1 -0
- package/dist/utils/oauth-identity.d.ts +58 -0
- package/dist/utils/oauth-identity.d.ts.map +1 -0
- package/dist/utils/oauth-identity.js +215 -0
- package/dist/utils/oauth-identity.js.map +1 -0
- package/package.json +1 -1
- package/dist/services/oauth-service.d.ts +0 -66
- package/dist/services/oauth-service.d.ts.map +0 -1
- package/dist/services/oauth-service.js +0 -223
- package/dist/services/oauth-service.js.map +0 -1
- package/dist/services/tool-context-builder.d.ts +0 -55
- package/dist/services/tool-context-builder.d.ts.map +0 -1
- package/dist/services/tool-context-builder.js +0 -124
- package/dist/services/tool-context-builder.js.map +0 -1
- package/dist/types/tool-context.d.ts +0 -35
- package/dist/types/tool-context.d.ts.map +0 -1
- package/dist/types/tool-context.js +0 -13
- package/dist/types/tool-context.js.map +0 -1
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OAuth Identity Utilities
|
|
3
|
+
*
|
|
4
|
+
* Handles OAuth identity extraction and validation from HTTP requests.
|
|
5
|
+
* This module consolidates OAuth-related logic that was previously in the adapter.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Extract OAuth identity from request cookies
|
|
9
|
+
*
|
|
10
|
+
* @param request - HTTP Request object
|
|
11
|
+
* @returns OAuthIdentity or null if not found/invalid
|
|
12
|
+
*/
|
|
13
|
+
export function extractOAuthIdentityFromRequest(request) {
|
|
14
|
+
try {
|
|
15
|
+
const cookieHeader = request.headers.get("Cookie");
|
|
16
|
+
if (!cookieHeader)
|
|
17
|
+
return null;
|
|
18
|
+
const cookies = cookieHeader.split("; ").map((c) => c.trim());
|
|
19
|
+
const oauthCookie = cookies.find((c) => c.startsWith("oauth_identity="));
|
|
20
|
+
if (!oauthCookie)
|
|
21
|
+
return null;
|
|
22
|
+
// Extract cookie value properly handling cases where value contains '='
|
|
23
|
+
// Find the first '=' which separates key from value, then take everything after it
|
|
24
|
+
const equalsIndex = oauthCookie.indexOf("=");
|
|
25
|
+
if (equalsIndex === -1)
|
|
26
|
+
return null;
|
|
27
|
+
const cookieValue = oauthCookie.substring(equalsIndex + 1);
|
|
28
|
+
const parsed = JSON.parse(decodeURIComponent(cookieValue));
|
|
29
|
+
// ✅ SECURITY: Validate OAuth identity format and content
|
|
30
|
+
const validationResult = validateOAuthIdentity(parsed);
|
|
31
|
+
if (!validationResult.valid) {
|
|
32
|
+
console.warn("[OAuth] ⚠️ OAuth identity validation failed:", validationResult.reason, { parsed });
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
return parsed;
|
|
36
|
+
}
|
|
37
|
+
catch (error) {
|
|
38
|
+
console.warn("[OAuth] Failed to extract OAuth identity from cookies:", error);
|
|
39
|
+
}
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Validate OAuth identity format and content
|
|
44
|
+
*
|
|
45
|
+
* Ensures:
|
|
46
|
+
* - Provider is non-empty string (1-50 chars)
|
|
47
|
+
* - Subject is non-empty string (1-255 chars)
|
|
48
|
+
* - Provider matches expected format (alphanumeric, hyphens, underscores)
|
|
49
|
+
* - Subject matches expected format (non-empty, reasonable length)
|
|
50
|
+
*
|
|
51
|
+
* @param identity - Parsed OAuth identity object
|
|
52
|
+
* @returns Validation result
|
|
53
|
+
*/
|
|
54
|
+
export function validateOAuthIdentity(identity) {
|
|
55
|
+
// Check if identity is an object
|
|
56
|
+
if (!identity || typeof identity !== "object") {
|
|
57
|
+
return { valid: false, reason: "OAuth identity must be an object" };
|
|
58
|
+
}
|
|
59
|
+
const oauth = identity;
|
|
60
|
+
// Validate provider
|
|
61
|
+
if (!oauth.provider || typeof oauth.provider !== "string") {
|
|
62
|
+
return {
|
|
63
|
+
valid: false,
|
|
64
|
+
reason: "OAuth provider is required and must be a string",
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
const provider = oauth.provider.trim();
|
|
68
|
+
if (provider.length === 0) {
|
|
69
|
+
return { valid: false, reason: "OAuth provider cannot be empty" };
|
|
70
|
+
}
|
|
71
|
+
if (provider.length > 50) {
|
|
72
|
+
return {
|
|
73
|
+
valid: false,
|
|
74
|
+
reason: "OAuth provider must be 50 characters or less",
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
// Provider format: alphanumeric, hyphens, underscores, dots (e.g., "google", "microsoft", "github", "custom-provider")
|
|
78
|
+
const providerPattern = /^[a-zA-Z0-9._-]+$/;
|
|
79
|
+
if (!providerPattern.test(provider)) {
|
|
80
|
+
return {
|
|
81
|
+
valid: false,
|
|
82
|
+
reason: `OAuth provider must match pattern [a-zA-Z0-9._-]: "${provider}"`,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
// Validate subject
|
|
86
|
+
if (!oauth.subject || typeof oauth.subject !== "string") {
|
|
87
|
+
return {
|
|
88
|
+
valid: false,
|
|
89
|
+
reason: "OAuth subject is required and must be a string",
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
const subject = oauth.subject.trim();
|
|
93
|
+
if (subject.length === 0) {
|
|
94
|
+
return { valid: false, reason: "OAuth subject cannot be empty" };
|
|
95
|
+
}
|
|
96
|
+
if (subject.length > 255) {
|
|
97
|
+
return {
|
|
98
|
+
valid: false,
|
|
99
|
+
reason: "OAuth subject must be 255 characters or less",
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
// Subject format: non-empty, reasonable characters (allows most Unicode, but prevents control chars)
|
|
103
|
+
// OAuth subjects can be numeric IDs, email-like strings, or other identifiers
|
|
104
|
+
const subjectPattern = /^[\S]+$/; // At least one non-whitespace character
|
|
105
|
+
if (!subjectPattern.test(subject)) {
|
|
106
|
+
return {
|
|
107
|
+
valid: false,
|
|
108
|
+
reason: `OAuth subject contains invalid characters: "${subject.substring(0, 20)}..."`,
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
// Validate optional email if present
|
|
112
|
+
if (oauth.email !== undefined) {
|
|
113
|
+
if (typeof oauth.email !== "string") {
|
|
114
|
+
return {
|
|
115
|
+
valid: false,
|
|
116
|
+
reason: "OAuth email must be a string if provided",
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
const email = oauth.email.trim();
|
|
120
|
+
if (email.length > 0) {
|
|
121
|
+
// Basic email format validation
|
|
122
|
+
const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
123
|
+
if (!emailPattern.test(email)) {
|
|
124
|
+
return {
|
|
125
|
+
valid: false,
|
|
126
|
+
reason: `OAuth email format invalid: "${email}"`,
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
if (email.length > 255) {
|
|
130
|
+
return {
|
|
131
|
+
valid: false,
|
|
132
|
+
reason: "OAuth email must be 255 characters or less",
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
// Validate optional name if present
|
|
138
|
+
if (oauth.name !== undefined) {
|
|
139
|
+
if (typeof oauth.name !== "string") {
|
|
140
|
+
return {
|
|
141
|
+
valid: false,
|
|
142
|
+
reason: "OAuth name must be a string if provided",
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
if (oauth.name.length > 255) {
|
|
146
|
+
return {
|
|
147
|
+
valid: false,
|
|
148
|
+
reason: "OAuth name must be 255 characters or less",
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return { valid: true };
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Lookup User DID from OAuth identity mapping
|
|
156
|
+
*
|
|
157
|
+
* @param oauthIdentity OAuth identity to lookup
|
|
158
|
+
* @param delegationStorage KV namespace for storage
|
|
159
|
+
* @returns User DID or null if not found
|
|
160
|
+
*/
|
|
161
|
+
export async function lookupUserDidFromOAuth(oauthIdentity, delegationStorage) {
|
|
162
|
+
if (!delegationStorage || !oauthIdentity?.provider || !oauthIdentity?.subject) {
|
|
163
|
+
return null;
|
|
164
|
+
}
|
|
165
|
+
try {
|
|
166
|
+
const { STORAGE_KEYS } = await import("../constants/storage-keys");
|
|
167
|
+
const oauthKey = STORAGE_KEYS.oauthIdentity(oauthIdentity.provider, oauthIdentity.subject);
|
|
168
|
+
const userDid = await delegationStorage.get(oauthKey, "text");
|
|
169
|
+
if (userDid) {
|
|
170
|
+
console.log("[OAuth] ✅ Retrieved persistent userDid from OAuth mapping:", {
|
|
171
|
+
provider: oauthIdentity.provider,
|
|
172
|
+
userDid: userDid.slice(0, 20) + "...",
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
return userDid;
|
|
176
|
+
}
|
|
177
|
+
catch (error) {
|
|
178
|
+
console.warn("[OAuth] Failed to lookup userDid from OAuth mapping:", error);
|
|
179
|
+
return null;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Create a redacted OAuth identity for storage (PII protection)
|
|
184
|
+
*
|
|
185
|
+
* @param oauthIdentity Original OAuth identity
|
|
186
|
+
* @returns Redacted identity safe for storage
|
|
187
|
+
*/
|
|
188
|
+
export function redactOAuthIdentityForStorage(oauthIdentity) {
|
|
189
|
+
if (!oauthIdentity) {
|
|
190
|
+
return undefined;
|
|
191
|
+
}
|
|
192
|
+
return {
|
|
193
|
+
provider: oauthIdentity.provider,
|
|
194
|
+
subjectHash: oauthIdentity.subject.substring(0, 8), // Redact full subject
|
|
195
|
+
// Don't store email, name, or full subject for PII protection
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Test if an OAuth cookie value is valid
|
|
200
|
+
* Helper function for testing and debugging
|
|
201
|
+
*
|
|
202
|
+
* @param cookieValue Encoded cookie value
|
|
203
|
+
* @returns true if valid OAuth identity, false otherwise
|
|
204
|
+
*/
|
|
205
|
+
export function isValidOAuthCookie(cookieValue) {
|
|
206
|
+
try {
|
|
207
|
+
const parsed = JSON.parse(decodeURIComponent(cookieValue));
|
|
208
|
+
const result = validateOAuthIdentity(parsed);
|
|
209
|
+
return result.valid;
|
|
210
|
+
}
|
|
211
|
+
catch {
|
|
212
|
+
return false;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
//# sourceMappingURL=oauth-identity.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"oauth-identity.js","sourceRoot":"","sources":["../../src/utils/oauth-identity.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAYH;;;;;GAKG;AACH,MAAM,UAAU,+BAA+B,CAAC,OAAgB;IAC9D,IAAI,CAAC;QACH,MAAM,YAAY,GAAG,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;QACnD,IAAI,CAAC,YAAY;YAAE,OAAO,IAAI,CAAC;QAE/B,MAAM,OAAO,GAAG,YAAY,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CA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C;IAC/E,IAAI,CAAC,aAAa,EAAE,CAAC;QACnB,OAAO,SAAS,CAAC;IACnB,CAAC;IAED,OAAO;QACL,QAAQ,EAAE,aAAa,CAAC,QAAQ;QAChC,WAAW,EAAE,aAAa,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,sBAAsB;QAC1E,8DAA8D;KAC/D,CAAC;AACJ,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,kBAAkB,CAAC,WAAmB;IACpD,IAAI,CAAC;QACH,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,kBAAkB,CAAC,WAAW,CAAC,CAAC,CAAC;QAC3D,MAAM,MAAM,GAAG,qBAAqB,CAAC,MAAM,CAAC,CAAC;QAC7C,OAAO,MAAM,CAAC,KAAK,CAAC;IACtB,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,KAAK,CAAC;IACf,CAAC;AACH,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,66 +0,0 @@
|
|
|
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-cloudflare
|
|
8
|
-
*/
|
|
9
|
-
import type { OAuthConfigService } from "@kya-os/mcp-i-core";
|
|
10
|
-
import type { IdpTokens } from "@kya-os/contracts/config";
|
|
11
|
-
export interface OAuthServiceConfig {
|
|
12
|
-
/** OAuth config service for fetching provider configurations */
|
|
13
|
-
configService: OAuthConfigService;
|
|
14
|
-
/** Project ID for fetching OAuth config */
|
|
15
|
-
projectId: string;
|
|
16
|
-
/** Optional logger callback for diagnostics */
|
|
17
|
-
logger?: (message: string, data?: unknown) => void;
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* Service for OAuth token exchange and refresh
|
|
21
|
-
*/
|
|
22
|
-
export declare class OAuthService {
|
|
23
|
-
private config;
|
|
24
|
-
constructor(config: OAuthServiceConfig);
|
|
25
|
-
/**
|
|
26
|
-
* Exchange authorization code for IDP tokens using PKCE
|
|
27
|
-
*
|
|
28
|
-
* For PKCE providers: Exchanges code directly with OAuth provider (no client secret)
|
|
29
|
-
* For proxy mode: Exchanges code via AgentShield API
|
|
30
|
-
*
|
|
31
|
-
* @param provider - OAuth provider name (e.g., "github", "google")
|
|
32
|
-
* @param code - Authorization code from OAuth callback
|
|
33
|
-
* @param codeVerifier - PKCE code verifier (must match code_challenge from authorization)
|
|
34
|
-
* @param redirectUri - Redirect URI used in authorization request
|
|
35
|
-
* @returns IDP tokens (access_token, refresh_token, expires_at, etc.)
|
|
36
|
-
*/
|
|
37
|
-
exchangeToken(provider: string, code: string, codeVerifier: string, redirectUri: string): Promise<IdpTokens>;
|
|
38
|
-
/**
|
|
39
|
-
* Exchange token directly with OAuth provider using PKCE
|
|
40
|
-
*/
|
|
41
|
-
private exchangeTokenPKCE;
|
|
42
|
-
/**
|
|
43
|
-
* Exchange token via AgentShield proxy (for providers that require proxy mode)
|
|
44
|
-
*/
|
|
45
|
-
private exchangeTokenProxy;
|
|
46
|
-
/**
|
|
47
|
-
* Refresh IDP access token using refresh token
|
|
48
|
-
*
|
|
49
|
-
* For PKCE providers: Refreshes directly with OAuth provider
|
|
50
|
-
* For proxy mode: Refreshes via AgentShield API
|
|
51
|
-
*
|
|
52
|
-
* @param provider - OAuth provider name
|
|
53
|
-
* @param refreshToken - Refresh token from previous token exchange
|
|
54
|
-
* @returns New IDP tokens or null if refresh failed
|
|
55
|
-
*/
|
|
56
|
-
refreshToken(provider: string, refreshToken: string): Promise<IdpTokens | null>;
|
|
57
|
-
/**
|
|
58
|
-
* Refresh token directly with OAuth provider using PKCE
|
|
59
|
-
*/
|
|
60
|
-
private refreshTokenPKCE;
|
|
61
|
-
/**
|
|
62
|
-
* Refresh token via AgentShield proxy
|
|
63
|
-
*/
|
|
64
|
-
private refreshTokenProxy;
|
|
65
|
-
}
|
|
66
|
-
//# sourceMappingURL=oauth-service.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"oauth-service.d.ts","sourceRoot":"","sources":["../../src/services/oauth-service.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,oBAAoB,CAAC;AAC7D,OAAO,KAAK,EAAE,SAAS,EAAiB,MAAM,0BAA0B,CAAC;AAEzE,MAAM,WAAW,kBAAkB;IACjC,gEAAgE;IAChE,aAAa,EAAE,kBAAkB,CAAC;IAElC,2CAA2C;IAC3C,SAAS,EAAE,MAAM,CAAC;IAElB,+CAA+C;IAC/C,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,OAAO,KAAK,IAAI,CAAC;CACpD;AAED;;GAEG;AACH,qBAAa,YAAY;IACvB,OAAO,CAAC,MAAM,CAEZ;gBAEU,MAAM,EAAE,kBAAkB;IAQtC;;;;;;;;;;;OAWG;IACG,aAAa,CACjB,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,MAAM,EACZ,YAAY,EAAE,MAAM,EACpB,WAAW,EAAE,MAAM,GAClB,OAAO,CAAC,SAAS,CAAC;IA2CrB;;OAEG;YACW,iBAAiB;IA8E/B;;OAEG;YACW,kBAAkB;IAiBhC;;;;;;;;;OASG;IACG,YAAY,CAChB,QAAQ,EAAE,MAAM,EAChB,YAAY,EAAE,MAAM,GACnB,OAAO,CAAC,SAAS,GAAG,IAAI,CAAC;IA2B5B;;OAEG;YACW,gBAAgB;IAiE9B;;OAEG;YACW,iBAAiB;CAShC"}
|
|
@@ -1,223 +0,0 @@
|
|
|
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-cloudflare
|
|
8
|
-
*/
|
|
9
|
-
/**
|
|
10
|
-
* Service for OAuth token exchange and refresh
|
|
11
|
-
*/
|
|
12
|
-
export class OAuthService {
|
|
13
|
-
config;
|
|
14
|
-
constructor(config) {
|
|
15
|
-
this.config = {
|
|
16
|
-
configService: config.configService,
|
|
17
|
-
projectId: config.projectId,
|
|
18
|
-
logger: config.logger || (() => { }),
|
|
19
|
-
};
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* Exchange authorization code for IDP tokens using PKCE
|
|
23
|
-
*
|
|
24
|
-
* For PKCE providers: Exchanges code directly with OAuth provider (no client secret)
|
|
25
|
-
* For proxy mode: Exchanges code via AgentShield API
|
|
26
|
-
*
|
|
27
|
-
* @param provider - OAuth provider name (e.g., "github", "google")
|
|
28
|
-
* @param code - Authorization code from OAuth callback
|
|
29
|
-
* @param codeVerifier - PKCE code verifier (must match code_challenge from authorization)
|
|
30
|
-
* @param redirectUri - Redirect URI used in authorization request
|
|
31
|
-
* @returns IDP tokens (access_token, refresh_token, expires_at, etc.)
|
|
32
|
-
*/
|
|
33
|
-
async exchangeToken(provider, code, codeVerifier, redirectUri) {
|
|
34
|
-
// Fetch provider config
|
|
35
|
-
const oauthConfig = await this.config.configService.getOAuthConfig(this.config.projectId);
|
|
36
|
-
const providerConfig = oauthConfig.providers[provider];
|
|
37
|
-
if (!providerConfig) {
|
|
38
|
-
throw new Error(`Provider "${provider}" not configured for project "${this.config.projectId}"`);
|
|
39
|
-
}
|
|
40
|
-
// Check if provider supports PKCE
|
|
41
|
-
if (!providerConfig.supportsPKCE) {
|
|
42
|
-
throw new Error(`Provider "${provider}" does not support PKCE. Only PKCE providers are supported in Phase 1.`);
|
|
43
|
-
}
|
|
44
|
-
// For PKCE providers, exchange directly with OAuth provider
|
|
45
|
-
if (providerConfig.supportsPKCE && !providerConfig.proxyMode) {
|
|
46
|
-
return this.exchangeTokenPKCE(providerConfig, code, codeVerifier, redirectUri);
|
|
47
|
-
}
|
|
48
|
-
// For proxy mode, exchange via AgentShield
|
|
49
|
-
if (providerConfig.proxyMode) {
|
|
50
|
-
return this.exchangeTokenProxy(providerConfig, code, codeVerifier, redirectUri);
|
|
51
|
-
}
|
|
52
|
-
throw new Error(`Provider "${provider}" configuration is invalid: must support PKCE or use proxy mode`);
|
|
53
|
-
}
|
|
54
|
-
/**
|
|
55
|
-
* Exchange token directly with OAuth provider using PKCE
|
|
56
|
-
*/
|
|
57
|
-
async exchangeTokenPKCE(providerConfig, code, codeVerifier, redirectUri) {
|
|
58
|
-
this.config.logger("[OAuthService] Exchanging token with PKCE", {
|
|
59
|
-
provider: providerConfig.authorizationUrl,
|
|
60
|
-
tokenUrl: providerConfig.tokenUrl,
|
|
61
|
-
});
|
|
62
|
-
const response = await fetch(providerConfig.tokenUrl, {
|
|
63
|
-
method: "POST",
|
|
64
|
-
headers: {
|
|
65
|
-
"Content-Type": "application/x-www-form-urlencoded",
|
|
66
|
-
Accept: "application/json",
|
|
67
|
-
},
|
|
68
|
-
body: new URLSearchParams({
|
|
69
|
-
grant_type: "authorization_code",
|
|
70
|
-
code,
|
|
71
|
-
redirect_uri: redirectUri,
|
|
72
|
-
client_id: providerConfig.clientId,
|
|
73
|
-
code_verifier: codeVerifier,
|
|
74
|
-
}),
|
|
75
|
-
});
|
|
76
|
-
if (!response.ok) {
|
|
77
|
-
const errorText = await response.text().catch(() => "Unknown error");
|
|
78
|
-
let errorData;
|
|
79
|
-
try {
|
|
80
|
-
errorData = JSON.parse(errorText);
|
|
81
|
-
}
|
|
82
|
-
catch {
|
|
83
|
-
errorData = { error: errorText };
|
|
84
|
-
}
|
|
85
|
-
const errorMessage = errorData.error_description || errorData.error || errorText;
|
|
86
|
-
this.config.logger("[OAuthService] Token exchange failed", {
|
|
87
|
-
status: response.status,
|
|
88
|
-
error: errorMessage,
|
|
89
|
-
provider: providerConfig.tokenUrl,
|
|
90
|
-
});
|
|
91
|
-
throw new Error(`Token exchange failed: ${errorMessage} (${response.status})`);
|
|
92
|
-
}
|
|
93
|
-
const tokens = await response.json();
|
|
94
|
-
// Validate required fields
|
|
95
|
-
if (!tokens.access_token) {
|
|
96
|
-
throw new Error("Token response missing access_token");
|
|
97
|
-
}
|
|
98
|
-
// Calculate expiration timestamp
|
|
99
|
-
const expiresIn = tokens.expires_in || 3600; // Default 1 hour
|
|
100
|
-
const expiresAt = Date.now() + expiresIn * 1000;
|
|
101
|
-
const idpTokens = {
|
|
102
|
-
access_token: tokens.access_token,
|
|
103
|
-
refresh_token: tokens.refresh_token,
|
|
104
|
-
expires_in: expiresIn,
|
|
105
|
-
expires_at: expiresAt,
|
|
106
|
-
token_type: tokens.token_type || "Bearer",
|
|
107
|
-
scope: tokens.scope,
|
|
108
|
-
};
|
|
109
|
-
this.config.logger("[OAuthService] Token exchange successful", {
|
|
110
|
-
provider: providerConfig.tokenUrl,
|
|
111
|
-
expiresAt: new Date(expiresAt).toISOString(),
|
|
112
|
-
hasRefreshToken: !!idpTokens.refresh_token,
|
|
113
|
-
});
|
|
114
|
-
return idpTokens;
|
|
115
|
-
}
|
|
116
|
-
/**
|
|
117
|
-
* Exchange token via AgentShield proxy (for providers that require proxy mode)
|
|
118
|
-
*/
|
|
119
|
-
async exchangeTokenProxy(providerConfig, code, codeVerifier, redirectUri) {
|
|
120
|
-
// Get AgentShield API URL from config service
|
|
121
|
-
const oauthConfig = await this.config.configService.getOAuthConfig(this.config.projectId);
|
|
122
|
-
// Note: We need access to baseUrl and apiKey from configService
|
|
123
|
-
// For now, this is a placeholder - will need to pass these through config
|
|
124
|
-
throw new Error("Proxy mode token exchange not yet implemented. Use PKCE mode for Phase 1.");
|
|
125
|
-
}
|
|
126
|
-
/**
|
|
127
|
-
* Refresh IDP access token using refresh token
|
|
128
|
-
*
|
|
129
|
-
* For PKCE providers: Refreshes directly with OAuth provider
|
|
130
|
-
* For proxy mode: Refreshes via AgentShield API
|
|
131
|
-
*
|
|
132
|
-
* @param provider - OAuth provider name
|
|
133
|
-
* @param refreshToken - Refresh token from previous token exchange
|
|
134
|
-
* @returns New IDP tokens or null if refresh failed
|
|
135
|
-
*/
|
|
136
|
-
async refreshToken(provider, refreshToken) {
|
|
137
|
-
// Fetch provider config
|
|
138
|
-
const oauthConfig = await this.config.configService.getOAuthConfig(this.config.projectId);
|
|
139
|
-
const providerConfig = oauthConfig.providers[provider];
|
|
140
|
-
if (!providerConfig) {
|
|
141
|
-
this.config.logger("[OAuthService] Provider not found for refresh", {
|
|
142
|
-
provider,
|
|
143
|
-
});
|
|
144
|
-
return null;
|
|
145
|
-
}
|
|
146
|
-
// For PKCE providers, refresh directly with OAuth provider
|
|
147
|
-
if (providerConfig.supportsPKCE && !providerConfig.proxyMode) {
|
|
148
|
-
return this.refreshTokenPKCE(providerConfig, refreshToken);
|
|
149
|
-
}
|
|
150
|
-
// For proxy mode, refresh via AgentShield
|
|
151
|
-
if (providerConfig.proxyMode) {
|
|
152
|
-
return this.refreshTokenProxy(providerConfig, refreshToken);
|
|
153
|
-
}
|
|
154
|
-
return null;
|
|
155
|
-
}
|
|
156
|
-
/**
|
|
157
|
-
* Refresh token directly with OAuth provider using PKCE
|
|
158
|
-
*/
|
|
159
|
-
async refreshTokenPKCE(providerConfig, refreshToken) {
|
|
160
|
-
this.config.logger("[OAuthService] Refreshing token with PKCE", {
|
|
161
|
-
provider: providerConfig.tokenUrl,
|
|
162
|
-
});
|
|
163
|
-
try {
|
|
164
|
-
const response = await fetch(providerConfig.tokenUrl, {
|
|
165
|
-
method: "POST",
|
|
166
|
-
headers: {
|
|
167
|
-
"Content-Type": "application/x-www-form-urlencoded",
|
|
168
|
-
Accept: "application/json",
|
|
169
|
-
},
|
|
170
|
-
body: new URLSearchParams({
|
|
171
|
-
grant_type: "refresh_token",
|
|
172
|
-
refresh_token: refreshToken,
|
|
173
|
-
client_id: providerConfig.clientId,
|
|
174
|
-
}),
|
|
175
|
-
});
|
|
176
|
-
if (!response.ok) {
|
|
177
|
-
this.config.logger("[OAuthService] Token refresh failed", {
|
|
178
|
-
status: response.status,
|
|
179
|
-
provider: providerConfig.tokenUrl,
|
|
180
|
-
});
|
|
181
|
-
return null;
|
|
182
|
-
}
|
|
183
|
-
const tokens = await response.json();
|
|
184
|
-
if (!tokens.access_token) {
|
|
185
|
-
this.config.logger("[OAuthService] Token refresh response missing access_token");
|
|
186
|
-
return null;
|
|
187
|
-
}
|
|
188
|
-
// Calculate expiration timestamp
|
|
189
|
-
const expiresIn = tokens.expires_in || 3600; // Default 1 hour
|
|
190
|
-
const expiresAt = Date.now() + expiresIn * 1000;
|
|
191
|
-
const idpTokens = {
|
|
192
|
-
access_token: tokens.access_token,
|
|
193
|
-
refresh_token: tokens.refresh_token || refreshToken, // Use new refresh token if provided, otherwise keep old one
|
|
194
|
-
expires_in: expiresIn,
|
|
195
|
-
expires_at: expiresAt,
|
|
196
|
-
token_type: tokens.token_type || "Bearer",
|
|
197
|
-
scope: tokens.scope,
|
|
198
|
-
};
|
|
199
|
-
this.config.logger("[OAuthService] Token refresh successful", {
|
|
200
|
-
provider: providerConfig.tokenUrl,
|
|
201
|
-
expiresAt: new Date(expiresAt).toISOString(),
|
|
202
|
-
});
|
|
203
|
-
return idpTokens;
|
|
204
|
-
}
|
|
205
|
-
catch (error) {
|
|
206
|
-
this.config.logger("[OAuthService] Token refresh error", {
|
|
207
|
-
error: error instanceof Error ? error.message : String(error),
|
|
208
|
-
provider: providerConfig.tokenUrl,
|
|
209
|
-
});
|
|
210
|
-
return null;
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
/**
|
|
214
|
-
* Refresh token via AgentShield proxy
|
|
215
|
-
*/
|
|
216
|
-
async refreshTokenProxy(providerConfig, refreshToken) {
|
|
217
|
-
// Placeholder for proxy mode refresh
|
|
218
|
-
// Will need access to AgentShield API URL and key
|
|
219
|
-
this.config.logger("[OAuthService] Proxy mode refresh not yet implemented");
|
|
220
|
-
return null;
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
//# sourceMappingURL=oauth-service.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"oauth-service.js","sourceRoot":"","sources":["../../src/services/oauth-service.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAgBH;;GAEG;AACH,MAAM,OAAO,YAAY;IACf,MAAM,CAEZ;IAEF,YAAY,MAA0B;QACpC,IAAI,CAAC,MAAM,GAAG;YACZ,aAAa,EAAE,MAAM,CAAC,aAAa;YACnC,SAAS,EAAE,MAAM,CAAC,SAAS;YAC3B,MAAM,EAAE,MAAM,CAAC,MAAM,IAAI,CAAC,GAAG,EAAE,GAAE,CAAC,CAAC;SACpC,CAAC;IACJ,CAAC;IAED;;;;;;;;;;;OAWG;IACH,KAAK,CAAC,aAAa,CACjB,QAAgB,EAChB,IAAY,EACZ,YAAoB,EACpB,WAAmB;QAEnB,wBAAwB;QACxB,MAAM,WAAW,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,cAAc,CAChE,IAAI,CAAC,MAAM,CAAC,SAAS,CACtB,CAAC;QACF,MAAM,cAAc,GAAG,WAAW,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC;QAEvD,IAAI,CAAC,cAAc,EAAE,CAAC;YACpB,MAAM,IAAI,KAAK,CAAC,aAAa,QAAQ,iCAAiC,IAAI,CAAC,MAAM,CAAC,SAAS,GAAG,CAAC,CAAC;QAClG,CAAC;QAED,kCAAkC;QAClC,IAAI,CAAC,cAAc,CAAC,YAAY,EAAE,CAAC;YACjC,MAAM,IAAI,KAAK,CACb,aAAa,QAAQ,wEAAwE,CAC9F,CAAC;QACJ,CAAC;QAED,4DAA4D;QAC5D,IAAI,cAAc,CAAC,YAAY,IAAI,CAAC,cAAc,CAAC,SAAS,EAAE,CAAC;YAC7D,OAAO,IAAI,CAAC,iBAAiB,CAC3B,cAAc,EACd,IAAI,EACJ,YAAY,EAC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QAAQ;gBACzC,KAAK,EAAE,MAAM,CAAC,KAAK;aACpB,CAAC;YAEF,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,yCAAyC,EAAE;gBAC5D,QAAQ,EAAE,cAAc,CAAC,QAAQ;gBACjC,SAAS,EAAE,IAAI,IAAI,CAAC,SAAS,CAAC,CAAC,WAAW,EAAE;aAC7C,CAAC,CAAC;YAEH,OAAO,SAAS,CAAC;QACnB,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,oCAAoC,EAAE;gBACvD,KAAK,EAAE,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC;gBAC7D,QAAQ,EAAE,cAAc,CAAC,QAAQ;aAClC,CAAC,CAAC;YACH,OAAO,IAAI,CAAC;QACd,CAAC;IACH,CAAC;IAED;;OAEG;IACK,KAAK,CAAC,iBAAiB,CAC7B,cAA6B,EAC7B,YAAoB;QAEpB,qCAAqC;QACrC,kDAAkD;QAClD,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,uDAAuD,CAAC,CAAC;QAC5E,OAAO,IAAI,CAAC;IACd,CAAC;CACF"}
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Tool Context Builder
|
|
3
|
-
*
|
|
4
|
-
* Builds ToolExecutionContext for tool handlers by resolving IDP tokens
|
|
5
|
-
* based on tool protection configuration and user identity.
|
|
6
|
-
*
|
|
7
|
-
* @package @kya-os/mcp-i-cloudflare
|
|
8
|
-
*/
|
|
9
|
-
import type { ToolExecutionContext } from "../types/tool-context.js";
|
|
10
|
-
import type { IdpTokenResolver } from "@kya-os/mcp-i-core";
|
|
11
|
-
import type { ToolProtection } from "@kya-os/mcp-i-core/types/tool-protection";
|
|
12
|
-
import type { OAuthConfigService } from "@kya-os/mcp-i-core";
|
|
13
|
-
export interface ToolContextBuilderConfig {
|
|
14
|
-
/** IDP token resolver for resolving tokens from User DID */
|
|
15
|
-
tokenResolver: IdpTokenResolver;
|
|
16
|
-
/** OAuth config service for fetching provider configurations */
|
|
17
|
-
configService: OAuthConfigService;
|
|
18
|
-
/** Project ID for fetching OAuth config */
|
|
19
|
-
projectId: string;
|
|
20
|
-
/** Optional logger callback for diagnostics */
|
|
21
|
-
logger?: (message: string, data?: unknown) => void;
|
|
22
|
-
}
|
|
23
|
-
/**
|
|
24
|
-
* Builder for tool execution context
|
|
25
|
-
*
|
|
26
|
-
* Resolves IDP tokens and builds context for tool handlers.
|
|
27
|
-
* Phase 1: Uses configured provider as temporary fallback.
|
|
28
|
-
* Phase 2+: Requires explicit oauthProvider on tool protection.
|
|
29
|
-
*/
|
|
30
|
-
export declare class ToolContextBuilder {
|
|
31
|
-
private config;
|
|
32
|
-
constructor(config: ToolContextBuilderConfig);
|
|
33
|
-
/**
|
|
34
|
-
* Build tool execution context
|
|
35
|
-
*
|
|
36
|
-
* @param toolName - Name of the tool being executed
|
|
37
|
-
* @param userDid - User DID (optional, required for OAuth)
|
|
38
|
-
* @param sessionId - Session ID (optional)
|
|
39
|
-
* @param delegationToken - Delegation token (optional)
|
|
40
|
-
* @param toolProtection - Tool protection configuration (optional)
|
|
41
|
-
* @returns Tool execution context or undefined if not needed
|
|
42
|
-
*/
|
|
43
|
-
buildContext(toolName: string, userDid: string | undefined, sessionId: string | undefined, delegationToken: string | undefined, toolProtection: ToolProtection | null): Promise<ToolExecutionContext | undefined>;
|
|
44
|
-
/**
|
|
45
|
-
* Resolve OAuth provider for a tool
|
|
46
|
-
*
|
|
47
|
-
* Phase 1: Uses configured provider from OAuth config as temporary fallback
|
|
48
|
-
* Phase 2+: Requires explicit oauthProvider on tool protection
|
|
49
|
-
*
|
|
50
|
-
* @param toolProtection - Tool protection configuration
|
|
51
|
-
* @returns Provider name or null if not found
|
|
52
|
-
*/
|
|
53
|
-
private resolveProvider;
|
|
54
|
-
}
|
|
55
|
-
//# sourceMappingURL=tool-context-builder.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"tool-context-builder.d.ts","sourceRoot":"","sources":["../../src/services/tool-context-builder.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,0BAA0B,CAAC;AACrE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAC3D,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,0CAA0C,CAAC;AAE/E,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,oBAAoB,CAAC;AAE7D,MAAM,WAAW,wBAAwB;IACvC,4DAA4D;IAC5D,aAAa,EAAE,gBAAgB,CAAC;IAEhC,gEAAgE;IAChE,aAAa,EAAE,kBAAkB,CAAC;IAElC,2CAA2C;IAC3C,SAAS,EAAE,MAAM,CAAC;IAElB,+CAA+C;IAC/C,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,OAAO,KAAK,IAAI,CAAC;CACpD;AAED;;;;;;GAMG;AACH,qBAAa,kBAAkB;IAC7B,OAAO,CAAC,MAAM,CAEZ;gBAEU,MAAM,EAAE,wBAAwB;IAS5C;;;;;;;;;OASG;IACG,YAAY,CAChB,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE,MAAM,GAAG,SAAS,EAC3B,SAAS,EAAE,MAAM,GAAG,SAAS,EAC7B,eAAe,EAAE,MAAM,GAAG,SAAS,EACnC,cAAc,EAAE,cAAc,GAAG,IAAI,GACpC,OAAO,CAAC,oBAAoB,GAAG,SAAS,CAAC;IAyD5C;;;;;;;;OAQG;YACW,eAAe;CAyC9B"}
|