@lightdash-tools/mcp 0.14.0 → 0.16.1
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/README.md +30 -11
- package/dist/auth/oauth-broker/lightdash-token.d.ts +6 -5
- package/dist/auth/oauth-broker/lightdash-token.js +6 -10
- package/dist/auth/oauth-broker/mcp-access-token.d.ts +31 -0
- package/dist/auth/oauth-broker/mcp-access-token.js +109 -0
- package/dist/auth/oauth-broker/mcp-access-token.test.d.ts +1 -0
- package/dist/auth/oauth-broker/mcp-access-token.test.js +61 -0
- package/dist/auth/oauth-broker/pending-store.d.ts +9 -3
- package/dist/auth/oauth-broker/pending-store.js +5 -3
- package/dist/auth/oauth-broker/pending-store.test.js +28 -44
- package/dist/auth/oauth-broker/routes.d.ts +2 -1
- package/dist/auth/oauth-broker/routes.js +97 -45
- package/dist/auth/oauth-broker/routes.test.js +81 -13
- package/dist/auth/resource-server/lightdash-oauth-middleware.d.ts +2 -0
- package/dist/auth/resource-server/lightdash-oauth-middleware.js +58 -37
- package/dist/auth/resource-server/lightdash-oauth-middleware.test.js +59 -57
- package/dist/auth/resource-server/oauth-protected-resource.js +2 -2
- package/dist/bin.js +33 -7
- package/dist/config/env.d.ts +5 -0
- package/dist/config/env.js +6 -1
- package/dist/config/load-mcp-config.d.ts +12 -1
- package/dist/config/load-mcp-config.js +17 -8
- package/dist/config/load-mcp-config.test.js +20 -0
- package/dist/config/prompt-context-policy.d.ts +18 -0
- package/dist/config/prompt-context-policy.js +40 -0
- package/dist/config/prompt-context-policy.test.d.ts +4 -0
- package/dist/config/prompt-context-policy.test.js +35 -0
- package/dist/config/test-mcp-http-config.js +1 -1
- package/dist/governance/mcp-post-factory-context.d.ts +2 -0
- package/dist/http.d.ts +7 -1
- package/dist/http.integration.test.js +57 -22
- package/dist/http.js +10 -1
- package/dist/index.d.ts +6 -1
- package/dist/index.js +6 -3
- package/dist/policy/content-developer.d.ts +5 -0
- package/dist/policy/content-developer.js +25 -1
- package/dist/policy/content-developer.test.js +29 -0
- package/dist/profiles/ai-agent-ops/v1/invariants.d.ts +27 -0
- package/dist/profiles/ai-agent-ops/v1/invariants.js +39 -0
- package/dist/profiles/ai-agent-ops/v1/prompts.d.ts +3 -2
- package/dist/profiles/ai-agent-ops/v1/prompts.js +67 -41
- package/dist/profiles/ai-agent-ops/v1/resources/playbooks.d.ts +2 -1
- package/dist/profiles/ai-agent-ops/v1/resources/playbooks.js +10 -3
- package/dist/profiles/content-developer/v1/invariants.d.ts +69 -0
- package/dist/profiles/content-developer/v1/invariants.js +95 -0
- package/dist/profiles/content-developer/v1/prompt-specs.d.ts +32 -0
- package/dist/profiles/content-developer/v1/prompt-specs.js +56 -0
- package/dist/profiles/content-developer/v1/prompts.d.ts +4 -3
- package/dist/profiles/content-developer/v1/prompts.js +65 -71
- package/dist/profiles/content-developer/v1/prompts.test.js +61 -7
- package/dist/profiles/content-developer/v1/resources/playbooks/core.md +1 -0
- package/dist/profiles/content-developer/v1/resources/playbooks/index.md +11 -8
- package/dist/profiles/content-developer/v1/resources/playbooks/recovery/dashboard-diff.md +15 -0
- package/dist/profiles/content-developer/v1/resources/playbooks/recovery/preview-required.md +14 -0
- package/dist/profiles/content-developer/v1/resources/playbooks/recovery/preview-stale.md +21 -0
- package/dist/profiles/content-developer/v1/resources/playbooks.d.ts +3 -2
- package/dist/profiles/content-developer/v1/resources/playbooks.js +35 -22
- package/dist/profiles/content-governance/v1/invariants.d.ts +27 -0
- package/dist/profiles/content-governance/v1/invariants.js +39 -0
- package/dist/profiles/content-governance/v1/prompts.d.ts +4 -2
- package/dist/profiles/content-governance/v1/prompts.js +31 -19
- package/dist/profiles/content-governance/v1/resources/playbooks.d.ts +2 -1
- package/dist/profiles/content-governance/v1/resources/playbooks.js +8 -9
- package/dist/profiles/content-reader/v1/invariants.d.ts +33 -0
- package/dist/profiles/content-reader/v1/invariants.js +47 -0
- package/dist/profiles/content-reader/v1/prompt-specs.d.ts +18 -0
- package/dist/profiles/content-reader/v1/prompt-specs.js +33 -0
- package/dist/profiles/content-reader/v1/prompts.d.ts +3 -2
- package/dist/profiles/content-reader/v1/prompts.js +69 -76
- package/dist/profiles/content-reader/v1/resources/playbooks.d.ts +2 -1
- package/dist/profiles/content-reader/v1/resources/playbooks.js +10 -11
- package/dist/profiles/data-analyst/v1/invariants.d.ts +21 -0
- package/dist/profiles/data-analyst/v1/invariants.js +31 -0
- package/dist/profiles/data-analyst/v1/prompts.d.ts +3 -2
- package/dist/profiles/data-analyst/v1/prompts.js +17 -9
- package/dist/profiles/data-analyst/v1/resources/playbooks.d.ts +2 -1
- package/dist/profiles/data-analyst/v1/resources/playbooks.js +6 -7
- package/dist/profiles/lib/playbook-resources.d.ts +16 -34
- package/dist/profiles/lib/playbook-resources.js +55 -76
- package/dist/profiles/lib/prompt-context.d.ts +71 -0
- package/dist/profiles/lib/prompt-context.js +205 -0
- package/dist/profiles/lib/prompt-context.test.d.ts +4 -0
- package/dist/profiles/lib/prompt-context.test.js +154 -0
- package/dist/profiles/lib/prompt-invariants.d.ts +20 -0
- package/dist/profiles/lib/prompt-invariants.js +54 -0
- package/dist/profiles/organization-audit/v1/invariants.d.ts +24 -0
- package/dist/profiles/organization-audit/v1/invariants.js +35 -0
- package/dist/profiles/organization-audit/v1/prompts.d.ts +3 -2
- package/dist/profiles/organization-audit/v1/prompts.js +44 -28
- package/dist/profiles/organization-audit/v1/resources/playbooks.d.ts +2 -1
- package/dist/profiles/organization-audit/v1/resources/playbooks.js +10 -3
- package/dist/profiles/semantic-layer/v1/invariants.d.ts +27 -0
- package/dist/profiles/semantic-layer/v1/invariants.js +39 -0
- package/dist/profiles/semantic-layer/v1/prompts.d.ts +3 -2
- package/dist/profiles/semantic-layer/v1/prompts.js +37 -26
- package/dist/profiles/semantic-layer/v1/resources/playbooks.d.ts +2 -4
- package/dist/profiles/semantic-layer/v1/resources/playbooks.js +8 -7
- package/dist/profiles/test-support/prompt-context-budget.test.d.ts +5 -0
- package/dist/profiles/test-support/prompt-context-budget.test.js +72 -0
- package/dist/profiles/types.d.ts +6 -1
- package/dist/server/capabilities.d.ts +3 -0
- package/dist/server/capabilities.js +4 -2
- package/dist/tools/lib/schema-fields.d.ts +4 -0
- package/dist/tools/lib/schema-fields.js +8 -1
- package/dist/tools/project/developer-content-preview.js +1 -1
- package/dist/tools/project/developer-content.js +2 -2
- package/dist/tools/query/reader-tool-helpers.d.ts +2 -2
- package/dist/tools/query/reader-tool-helpers.js +2 -2
- package/dist/tools/query/reader-tool-helpers.test.js +1 -0
- package/dist/tools/shared.d.ts +6 -1
- package/dist/tools/shared.js +12 -2
- package/dist/transports/streamable-http.js +10 -3
- package/package.json +3 -3
|
@@ -2,12 +2,15 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.isOAuthBrokerPath = isOAuthBrokerPath;
|
|
4
4
|
exports.createOAuthBroker = createOAuthBroker;
|
|
5
|
+
const enabled_profiles_js_1 = require("../../config/enabled-profiles.js");
|
|
5
6
|
const env_js_1 = require("../../config/env.js");
|
|
6
7
|
const normalize_url_js_1 = require("../../config/normalize-url.js");
|
|
8
|
+
const public_url_js_1 = require("../../config/public-url.js");
|
|
7
9
|
const http_body_js_1 = require("../../transports/http-body.js");
|
|
8
10
|
const http_response_js_1 = require("../../transports/http-response.js");
|
|
9
11
|
const as_metadata_js_1 = require("./as-metadata.js");
|
|
10
12
|
const lightdash_token_js_1 = require("./lightdash-token.js");
|
|
13
|
+
const mcp_access_token_js_1 = require("./mcp-access-token.js");
|
|
11
14
|
const pending_store_js_1 = require("./pending-store.js");
|
|
12
15
|
const pkce_js_1 = require("./pkce.js");
|
|
13
16
|
/** Schemes that must never be used as OAuth redirect targets. */
|
|
@@ -94,6 +97,10 @@ function isAllowedClientRedirectUri(redirectUri) {
|
|
|
94
97
|
return false;
|
|
95
98
|
}
|
|
96
99
|
}
|
|
100
|
+
function isAllowedMcpResource(config, resource) {
|
|
101
|
+
const publicUrl = (0, public_url_js_1.requirePublicUrl)(config, 'OAuth resource validation');
|
|
102
|
+
return (0, enabled_profiles_js_1.listEnabledProfilePaths)(config.enabledProfiles).some((profilePath) => resource === `${publicUrl}${profilePath}`);
|
|
103
|
+
}
|
|
97
104
|
async function parseRegisteredClientRedirect(query, store) {
|
|
98
105
|
const clientId = query.get('client_id');
|
|
99
106
|
if (!clientId) {
|
|
@@ -139,8 +146,8 @@ async function parseRegisteredClientRedirect(query, store) {
|
|
|
139
146
|
}
|
|
140
147
|
return { ok: true, clientId, redirectUri };
|
|
141
148
|
}
|
|
142
|
-
async function parseAuthorizeRequest(query, store) {
|
|
143
|
-
var _a, _b, _c, _d
|
|
149
|
+
async function parseAuthorizeRequest(query, config, store) {
|
|
150
|
+
var _a, _b, _c, _d;
|
|
144
151
|
const responseType = query.get('response_type');
|
|
145
152
|
if (responseType !== 'code') {
|
|
146
153
|
return {
|
|
@@ -168,6 +175,28 @@ async function parseAuthorizeRequest(query, store) {
|
|
|
168
175
|
},
|
|
169
176
|
};
|
|
170
177
|
}
|
|
178
|
+
const resources = query.getAll('resource');
|
|
179
|
+
if (resources.length !== 1 || resources[0].length === 0) {
|
|
180
|
+
return {
|
|
181
|
+
ok: false,
|
|
182
|
+
status: 400,
|
|
183
|
+
body: {
|
|
184
|
+
error: 'invalid_request',
|
|
185
|
+
error_description: 'Exactly one MCP resource parameter is required',
|
|
186
|
+
},
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
const resource = resources[0];
|
|
190
|
+
if (!isAllowedMcpResource(config, resource)) {
|
|
191
|
+
return {
|
|
192
|
+
ok: false,
|
|
193
|
+
status: 400,
|
|
194
|
+
body: {
|
|
195
|
+
error: 'invalid_target',
|
|
196
|
+
error_description: 'resource must identify an enabled MCP profile endpoint',
|
|
197
|
+
},
|
|
198
|
+
};
|
|
199
|
+
}
|
|
171
200
|
return {
|
|
172
201
|
ok: true,
|
|
173
202
|
value: {
|
|
@@ -175,8 +204,8 @@ async function parseAuthorizeRequest(query, store) {
|
|
|
175
204
|
redirectUri: registered.redirectUri,
|
|
176
205
|
clientState: (_c = query.get('state')) !== null && _c !== void 0 ? _c : undefined,
|
|
177
206
|
codeChallenge,
|
|
178
|
-
resource
|
|
179
|
-
scope: (
|
|
207
|
+
resource,
|
|
208
|
+
scope: (_d = query.get('scope')) !== null && _d !== void 0 ? _d : undefined,
|
|
180
209
|
},
|
|
181
210
|
};
|
|
182
211
|
}
|
|
@@ -185,7 +214,7 @@ async function handleAuthorize(req, res, config, store) {
|
|
|
185
214
|
res.writeHead(405, { Allow: 'GET' }).end();
|
|
186
215
|
return;
|
|
187
216
|
}
|
|
188
|
-
const parsed = await parseAuthorizeRequest(readQuery(req), store);
|
|
217
|
+
const parsed = await parseAuthorizeRequest(readQuery(req), config, store);
|
|
189
218
|
if (!parsed.ok) {
|
|
190
219
|
(0, http_response_js_1.sendJson)(res, parsed.status, parsed.body);
|
|
191
220
|
return;
|
|
@@ -197,17 +226,16 @@ async function handleAuthorize(req, res, config, store) {
|
|
|
197
226
|
clientState,
|
|
198
227
|
codeChallenge,
|
|
199
228
|
codeChallengeMethod: 'S256',
|
|
229
|
+
resource,
|
|
200
230
|
scope,
|
|
201
231
|
});
|
|
202
232
|
if (!pending) {
|
|
203
233
|
(0, http_response_js_1.sendJson)(res, 503, Object.assign({}, AS_BUSY));
|
|
204
234
|
return;
|
|
205
235
|
}
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
resource,
|
|
210
|
-
}));
|
|
236
|
+
// The MCP resource/scope belong to the client→broker leg. Do not forward them
|
|
237
|
+
// to Lightdash, whose OAuth client and protected resource are distinct.
|
|
238
|
+
redirect(res, (0, lightdash_token_js_1.buildLightdashAuthorizeUrl)(config, { state: pending.brokerState }));
|
|
211
239
|
}
|
|
212
240
|
function redirectToClient(res, redirectUri, params) {
|
|
213
241
|
const target = new URL(redirectUri);
|
|
@@ -251,12 +279,13 @@ async function handleCallback(req, res, config, store) {
|
|
|
251
279
|
}
|
|
252
280
|
try {
|
|
253
281
|
const tokens = await (0, lightdash_token_js_1.exchangeLightdashAuthorizationCode)(config, code);
|
|
254
|
-
//
|
|
282
|
+
// The downstream credential remains server-side. The later MCP token endpoint
|
|
283
|
+
// wraps it in an authenticated-encrypted, resource-bound broker token.
|
|
284
|
+
// Refresh tokens are deliberately neither persisted nor returned.
|
|
255
285
|
const issued = await store.issueCode(pending, {
|
|
256
286
|
accessToken: tokens.access_token,
|
|
257
287
|
expiresIn: tokens.expires_in,
|
|
258
288
|
tokenType: tokens.token_type,
|
|
259
|
-
scope: tokens.scope,
|
|
260
289
|
});
|
|
261
290
|
if (!issued) {
|
|
262
291
|
redirectToClient(res, pending.redirectUri, Object.assign(Object.assign({}, AS_BUSY), { state: pending.clientState }));
|
|
@@ -276,10 +305,9 @@ async function handleCallback(req, res, config, store) {
|
|
|
276
305
|
});
|
|
277
306
|
}
|
|
278
307
|
}
|
|
279
|
-
|
|
308
|
+
function parseTokenGrantRequest(params) {
|
|
280
309
|
var _a;
|
|
281
|
-
|
|
282
|
-
if (grantType !== 'authorization_code') {
|
|
310
|
+
if (params.get('grant_type') !== 'authorization_code') {
|
|
283
311
|
return {
|
|
284
312
|
status: 400,
|
|
285
313
|
body: {
|
|
@@ -292,44 +320,50 @@ async function validateTokenGrant(params, store) {
|
|
|
292
320
|
const redirectUri = params.get('redirect_uri');
|
|
293
321
|
const clientId = params.get('client_id');
|
|
294
322
|
const codeVerifier = (_a = params.get('code_verifier')) !== null && _a !== void 0 ? _a : undefined;
|
|
295
|
-
|
|
323
|
+
const resources = params.getAll('resource');
|
|
324
|
+
const resource = resources.length === 1 ? resources[0] : undefined;
|
|
325
|
+
if (!code || !redirectUri || !clientId || !resource) {
|
|
296
326
|
return {
|
|
297
327
|
status: 400,
|
|
298
328
|
body: {
|
|
299
329
|
error: 'invalid_request',
|
|
300
|
-
error_description: 'code, redirect_uri, and
|
|
330
|
+
error_description: 'code, redirect_uri, client_id, and exactly one resource are required',
|
|
301
331
|
},
|
|
302
332
|
};
|
|
303
333
|
}
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
334
|
+
return { code, redirectUri, clientId, codeVerifier, resource };
|
|
335
|
+
}
|
|
336
|
+
async function restoreInvalidGrant(store, candidate, description) {
|
|
337
|
+
await store.restoreCode(candidate);
|
|
338
|
+
return {
|
|
339
|
+
status: 400,
|
|
340
|
+
body: { error: 'invalid_grant', error_description: description },
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
async function validateTokenGrant(params, store) {
|
|
344
|
+
const request = parseTokenGrantRequest(params);
|
|
345
|
+
if ('status' in request)
|
|
346
|
+
return request;
|
|
347
|
+
// Atomic take first. Restore on validation failure so a bad verifier / redirect /
|
|
348
|
+
// resource does not permanently burn a one-time code.
|
|
349
|
+
const candidate = await store.takeCode(request.code);
|
|
307
350
|
if (!candidate) {
|
|
308
351
|
return {
|
|
309
352
|
status: 400,
|
|
310
353
|
body: { error: 'invalid_grant', error_description: 'Invalid or expired code' },
|
|
311
354
|
};
|
|
312
355
|
}
|
|
313
|
-
if (candidate.redirectUri !== redirectUri) {
|
|
314
|
-
|
|
315
|
-
return {
|
|
316
|
-
status: 400,
|
|
317
|
-
body: { error: 'invalid_grant', error_description: 'redirect_uri mismatch' },
|
|
318
|
-
};
|
|
356
|
+
if (candidate.redirectUri !== request.redirectUri) {
|
|
357
|
+
return restoreInvalidGrant(store, candidate, 'redirect_uri mismatch');
|
|
319
358
|
}
|
|
320
|
-
if (candidate.clientId !== clientId) {
|
|
321
|
-
|
|
322
|
-
return {
|
|
323
|
-
status: 400,
|
|
324
|
-
body: { error: 'invalid_grant', error_description: 'client_id mismatch' },
|
|
325
|
-
};
|
|
359
|
+
if (candidate.clientId !== request.clientId) {
|
|
360
|
+
return restoreInvalidGrant(store, candidate, 'client_id mismatch');
|
|
326
361
|
}
|
|
327
|
-
if (
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
};
|
|
362
|
+
if (candidate.resource !== request.resource) {
|
|
363
|
+
return restoreInvalidGrant(store, candidate, 'resource mismatch');
|
|
364
|
+
}
|
|
365
|
+
if (!(0, pkce_js_1.verifyPkce)(request.codeVerifier, candidate.codeChallenge, candidate.codeChallengeMethod)) {
|
|
366
|
+
return restoreInvalidGrant(store, candidate, 'PKCE verification failed');
|
|
333
367
|
}
|
|
334
368
|
return { issued: candidate };
|
|
335
369
|
}
|
|
@@ -348,12 +382,29 @@ async function handleToken(req, res, config, store) {
|
|
|
348
382
|
return;
|
|
349
383
|
}
|
|
350
384
|
const { issued } = grant;
|
|
351
|
-
(
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
385
|
+
const upstreamLifetimeSeconds = (_a = issued.expiresIn) !== null && _a !== void 0 ? _a : 3600;
|
|
386
|
+
const expiresAtMs = issued.createdAt + upstreamLifetimeSeconds * 1000;
|
|
387
|
+
try {
|
|
388
|
+
const minted = (0, mcp_access_token_js_1.mintMcpAccessToken)(config, {
|
|
389
|
+
lightdashAccessToken: issued.accessToken,
|
|
390
|
+
clientId: issued.clientId,
|
|
391
|
+
resource: issued.resource,
|
|
392
|
+
scope: issued.scope,
|
|
393
|
+
expiresAtMs,
|
|
394
|
+
});
|
|
395
|
+
(0, http_response_js_1.sendJson)(res, 200, {
|
|
396
|
+
access_token: minted.accessToken,
|
|
397
|
+
token_type: 'Bearer',
|
|
398
|
+
expires_in: minted.expiresIn,
|
|
399
|
+
scope: issued.scope,
|
|
400
|
+
});
|
|
401
|
+
}
|
|
402
|
+
catch (_b) {
|
|
403
|
+
(0, http_response_js_1.sendJson)(res, 400, {
|
|
404
|
+
error: 'invalid_grant',
|
|
405
|
+
error_description: 'Upstream Lightdash credential expired before broker token issuance',
|
|
406
|
+
});
|
|
407
|
+
}
|
|
357
408
|
}
|
|
358
409
|
async function handleRegister(req, res, config, store) {
|
|
359
410
|
var _a;
|
|
@@ -414,7 +465,8 @@ function isOAuthBrokerPath(path) {
|
|
|
414
465
|
}
|
|
415
466
|
/**
|
|
416
467
|
* Creates the OAuth broker request handler for co-located AS façade routes.
|
|
417
|
-
* Pending state is process-local
|
|
468
|
+
* Pending authorization/code state is process-local, so `/oauth/*` still needs
|
|
469
|
+
* sticky routing or one replica. Issued MCP access tokens are self-contained.
|
|
418
470
|
*/
|
|
419
471
|
function createOAuthBroker(config, store = new pending_store_js_1.InMemoryOAuthBrokerStore()) {
|
|
420
472
|
return {
|
|
@@ -7,9 +7,11 @@ const vitest_1 = require("vitest");
|
|
|
7
7
|
const test_mcp_http_config_js_1 = require("../../config/test-mcp-http-config.js");
|
|
8
8
|
const as_metadata_js_1 = require("./as-metadata.js");
|
|
9
9
|
const lightdash_token_js_1 = require("./lightdash-token.js");
|
|
10
|
+
const mcp_access_token_js_1 = require("./mcp-access-token.js");
|
|
10
11
|
const pending_store_js_1 = require("./pending-store.js");
|
|
11
12
|
const pkce_js_1 = require("./pkce.js");
|
|
12
13
|
const routes_js_1 = require("./routes.js");
|
|
14
|
+
const RESOURCE = 'https://mcp.example.com/semantic-layer/v1/mcp';
|
|
13
15
|
function baseConfig(overrides = {}) {
|
|
14
16
|
return (0, test_mcp_http_config_js_1.makeTestMcpHttpConfig)(Object.assign({ host: '0.0.0.0', oauthClientId: 'ld-client', oauthClientSecret: new client_1.SecretString('ld-secret'), maxBodyBytes: 1024 * 1024, requiredScopes: [], scopesSupported: [], tokenValidationCacheTtlMs: 10000 }, overrides));
|
|
15
17
|
}
|
|
@@ -62,21 +64,26 @@ function mockReq(method, url, body, contentType = 'application/x-www-form-urlenc
|
|
|
62
64
|
});
|
|
63
65
|
return req;
|
|
64
66
|
}
|
|
67
|
+
function authorizeUrl(clientId, redirectUri, challenge) {
|
|
68
|
+
return (`/oauth/authorize?response_type=code&client_id=${encodeURIComponent(clientId)}` +
|
|
69
|
+
`&redirect_uri=${encodeURIComponent(redirectUri)}` +
|
|
70
|
+
`&code_challenge=${encodeURIComponent(challenge)}&code_challenge_method=S256` +
|
|
71
|
+
`&resource=${encodeURIComponent(RESOURCE)}`);
|
|
72
|
+
}
|
|
65
73
|
(0, vitest_1.describe)('oauth broker helpers', () => {
|
|
66
74
|
(0, vitest_1.afterEach)(() => {
|
|
67
75
|
vitest_1.vi.restoreAllMocks();
|
|
68
76
|
});
|
|
69
|
-
(0, vitest_1.it)('builds Lightdash authorize URL
|
|
70
|
-
const url = (0, lightdash_token_js_1.buildLightdashAuthorizeUrl)(baseConfig(), {
|
|
71
|
-
state: 'broker-state',
|
|
72
|
-
resource: 'https://mcp.example.com/semantic-layer/v1/mcp',
|
|
73
|
-
});
|
|
77
|
+
(0, vitest_1.it)('builds downstream Lightdash authorize URL without MCP resource/scope passthrough', () => {
|
|
78
|
+
const url = (0, lightdash_token_js_1.buildLightdashAuthorizeUrl)(baseConfig(), { state: 'broker-state' });
|
|
74
79
|
const parsed = new URL(url);
|
|
75
80
|
(0, vitest_1.expect)(parsed.origin + parsed.pathname).toBe('https://app.lightdash.cloud/api/v1/oauth/authorize');
|
|
76
81
|
(0, vitest_1.expect)(parsed.searchParams.get('client_id')).toBe('ld-client');
|
|
77
82
|
(0, vitest_1.expect)(parsed.searchParams.get('redirect_uri')).toBe('https://mcp.example.com/oauth/callback');
|
|
78
83
|
(0, vitest_1.expect)(parsed.searchParams.get('state')).toBe('broker-state');
|
|
79
84
|
(0, vitest_1.expect)(parsed.searchParams.get('code_challenge')).toBeNull();
|
|
85
|
+
(0, vitest_1.expect)(parsed.searchParams.get('resource')).toBeNull();
|
|
86
|
+
(0, vitest_1.expect)(parsed.searchParams.get('scope')).toBeNull();
|
|
80
87
|
});
|
|
81
88
|
(0, vitest_1.it)('publishes AS metadata pointing at broker endpoints', () => {
|
|
82
89
|
const metadata = (0, as_metadata_js_1.buildBrokerAuthorizationServerMetadata)(baseConfig());
|
|
@@ -101,6 +108,7 @@ function mockReq(method, url, body, contentType = 'application/x-www-form-urlenc
|
|
|
101
108
|
redirectUri: 'http://127.0.0.1:9999/callback',
|
|
102
109
|
codeChallenge: 'challenge',
|
|
103
110
|
codeChallengeMethod: 'S256',
|
|
111
|
+
resource: RESOURCE,
|
|
104
112
|
});
|
|
105
113
|
(0, vitest_1.expect)(pending).toBeDefined();
|
|
106
114
|
(0, vitest_1.expect)((_a = (await store.takePending(pending.brokerState))) === null || _a === void 0 ? void 0 : _a.clientId).toBe('client-a');
|
|
@@ -110,6 +118,7 @@ function mockReq(method, url, body, contentType = 'application/x-www-form-urlenc
|
|
|
110
118
|
redirectUri: 'http://127.0.0.1:9999/callback',
|
|
111
119
|
codeChallenge: 'challenge',
|
|
112
120
|
codeChallengeMethod: 'S256',
|
|
121
|
+
resource: RESOURCE,
|
|
113
122
|
});
|
|
114
123
|
(0, vitest_1.expect)(pending2).toBeDefined();
|
|
115
124
|
const issued = await store.issueCode(pending2, { accessToken: 'atok' });
|
|
@@ -117,6 +126,7 @@ function mockReq(method, url, body, contentType = 'application/x-www-form-urlenc
|
|
|
117
126
|
(0, vitest_1.expect)(issued).not.toHaveProperty('refreshToken');
|
|
118
127
|
const taken = await store.takeCode(issued.code);
|
|
119
128
|
(0, vitest_1.expect)(taken === null || taken === void 0 ? void 0 : taken.accessToken).toBe('atok');
|
|
129
|
+
(0, vitest_1.expect)(taken === null || taken === void 0 ? void 0 : taken.resource).toBe(RESOURCE);
|
|
120
130
|
(0, vitest_1.expect)(await store.takeCode(issued.code)).toBeUndefined();
|
|
121
131
|
});
|
|
122
132
|
(0, vitest_1.it)('registers clients and binds exact redirect URIs', async () => {
|
|
@@ -137,6 +147,7 @@ function mockReq(method, url, body, contentType = 'application/x-www-form-urlenc
|
|
|
137
147
|
redirectUri: 'http://localhost:8787/callback',
|
|
138
148
|
codeChallenge: challenge,
|
|
139
149
|
codeChallengeMethod: 'S256',
|
|
150
|
+
resource: RESOURCE,
|
|
140
151
|
});
|
|
141
152
|
(0, vitest_1.expect)(pending).toBeDefined();
|
|
142
153
|
const issued = await store.issueCode(pending, { accessToken: 'atok' });
|
|
@@ -181,11 +192,13 @@ function mockReq(method, url, body, contentType = 'application/x-www-form-urlenc
|
|
|
181
192
|
(0, vitest_1.expect)(registered.redirect_uris).toEqual([redirectUri]);
|
|
182
193
|
const challenge = (0, node_crypto_1.createHash)('sha256').update('verifier').digest('base64url');
|
|
183
194
|
const okRes = mockRes();
|
|
184
|
-
await broker.handle(mockReq('GET',
|
|
195
|
+
await broker.handle(mockReq('GET', authorizeUrl(registered.client_id, redirectUri, challenge)), okRes, '/oauth/authorize');
|
|
185
196
|
(0, vitest_1.expect)(okRes.statusCode).toBe(302);
|
|
186
|
-
|
|
197
|
+
const upstream = new URL(String(okRes.headers.Location));
|
|
198
|
+
(0, vitest_1.expect)(upstream.pathname).toContain('/api/v1/oauth/authorize');
|
|
199
|
+
(0, vitest_1.expect)(upstream.searchParams.get('resource')).toBeNull();
|
|
187
200
|
const badRes = mockRes();
|
|
188
|
-
await broker.handle(mockReq('GET',
|
|
201
|
+
await broker.handle(mockReq('GET', authorizeUrl(registered.client_id, 'https://attacker.example/cb', challenge)), badRes, '/oauth/authorize');
|
|
189
202
|
(0, vitest_1.expect)(badRes.statusCode).toBe(400);
|
|
190
203
|
(0, vitest_1.expect)(badRes.body.error).toBe('invalid_request');
|
|
191
204
|
});
|
|
@@ -193,23 +206,41 @@ function mockReq(method, url, body, contentType = 'application/x-www-form-urlenc
|
|
|
193
206
|
const broker = (0, routes_js_1.createOAuthBroker)(baseConfig());
|
|
194
207
|
const challenge = (0, node_crypto_1.createHash)('sha256').update('verifier').digest('base64url');
|
|
195
208
|
const res = mockRes();
|
|
196
|
-
await broker.handle(mockReq('GET',
|
|
209
|
+
await broker.handle(mockReq('GET', authorizeUrl('unknown', 'https://app.example/cb', challenge)), res, '/oauth/authorize');
|
|
197
210
|
(0, vitest_1.expect)(res.statusCode).toBe(400);
|
|
198
211
|
(0, vitest_1.expect)(res.body.error).toBe('invalid_client');
|
|
199
212
|
});
|
|
200
|
-
(0, vitest_1.it)('
|
|
213
|
+
(0, vitest_1.it)('requires a valid enabled MCP resource at authorize', async () => {
|
|
201
214
|
const broker = (0, routes_js_1.createOAuthBroker)(baseConfig());
|
|
202
215
|
const redirectUri = 'http://127.0.0.1:8787/callback';
|
|
216
|
+
const registerRes = mockRes();
|
|
217
|
+
await broker.handle(mockReq('POST', '/oauth/register', `redirect_uris=${encodeURIComponent(redirectUri)}`), registerRes, '/oauth/register');
|
|
218
|
+
const { client_id: clientId } = registerRes.body;
|
|
219
|
+
const challenge = (0, node_crypto_1.createHash)('sha256').update('verifier').digest('base64url');
|
|
220
|
+
const missingRes = mockRes();
|
|
221
|
+
await broker.handle(mockReq('GET', `/oauth/authorize?response_type=code&client_id=${encodeURIComponent(clientId)}&redirect_uri=${encodeURIComponent(redirectUri)}&code_challenge=${challenge}&code_challenge_method=S256`), missingRes, '/oauth/authorize');
|
|
222
|
+
(0, vitest_1.expect)(missingRes.statusCode).toBe(400);
|
|
223
|
+
(0, vitest_1.expect)(missingRes.body.error).toBe('invalid_request');
|
|
224
|
+
const wrongRes = mockRes();
|
|
225
|
+
await broker.handle(mockReq('GET', authorizeUrl(clientId, redirectUri, challenge).replace(encodeURIComponent(RESOURCE), encodeURIComponent('https://attacker.example/mcp'))), wrongRes, '/oauth/authorize');
|
|
226
|
+
(0, vitest_1.expect)(wrongRes.statusCode).toBe(400);
|
|
227
|
+
(0, vitest_1.expect)(wrongRes.body.error).toBe('invalid_target');
|
|
228
|
+
});
|
|
229
|
+
(0, vitest_1.it)('returns an MCP-issued resource-bound token and never exposes Lightdash tokens', async () => {
|
|
230
|
+
const config = baseConfig();
|
|
231
|
+
const broker = (0, routes_js_1.createOAuthBroker)(config);
|
|
232
|
+
const redirectUri = 'http://127.0.0.1:8787/callback';
|
|
203
233
|
const verifier = 'test-verifier-value-1234567890';
|
|
204
234
|
const challenge = (0, node_crypto_1.createHash)('sha256').update(verifier).digest('base64url');
|
|
205
235
|
const registerRes = mockRes();
|
|
206
236
|
await broker.handle(mockReq('POST', '/oauth/register', `redirect_uris=${encodeURIComponent(redirectUri)}`), registerRes, '/oauth/register');
|
|
207
237
|
const { client_id: clientId } = registerRes.body;
|
|
208
238
|
const authorizeRes = mockRes();
|
|
209
|
-
await broker.handle(mockReq('GET',
|
|
239
|
+
await broker.handle(mockReq('GET', authorizeUrl(clientId, redirectUri, challenge)), authorizeRes, '/oauth/authorize');
|
|
210
240
|
const ldAuthorize = new URL(String(authorizeRes.headers.Location));
|
|
211
241
|
const brokerState = ldAuthorize.searchParams.get('state');
|
|
212
242
|
(0, vitest_1.expect)(brokerState).toBeTruthy();
|
|
243
|
+
(0, vitest_1.expect)(ldAuthorize.searchParams.get('resource')).toBeNull();
|
|
213
244
|
vitest_1.vi.stubGlobal('fetch', vitest_1.vi.fn().mockResolvedValue({
|
|
214
245
|
ok: true,
|
|
215
246
|
json: async () => ({
|
|
@@ -217,7 +248,7 @@ function mockReq(method, url, body, contentType = 'application/x-www-form-urlenc
|
|
|
217
248
|
refresh_token: 'ld-refresh-should-not-leak',
|
|
218
249
|
expires_in: 3600,
|
|
219
250
|
token_type: 'Bearer',
|
|
220
|
-
scope: '
|
|
251
|
+
scope: 'lightdash-upstream-scope-must-not-become-mcp-scope',
|
|
221
252
|
}),
|
|
222
253
|
}));
|
|
223
254
|
const callbackRes = mockRes();
|
|
@@ -233,10 +264,47 @@ function mockReq(method, url, body, contentType = 'application/x-www-form-urlenc
|
|
|
233
264
|
redirect_uri: redirectUri,
|
|
234
265
|
client_id: clientId,
|
|
235
266
|
code_verifier: verifier,
|
|
267
|
+
resource: RESOURCE,
|
|
236
268
|
}).toString()), tokenRes, '/oauth/token');
|
|
237
269
|
(0, vitest_1.expect)(tokenRes.statusCode).toBe(200);
|
|
238
270
|
const tokenBody = tokenRes.body;
|
|
239
|
-
(0, vitest_1.expect)(tokenBody.access_token).toBe('ld-access');
|
|
271
|
+
(0, vitest_1.expect)(tokenBody.access_token).not.toBe('ld-access');
|
|
272
|
+
(0, vitest_1.expect)(String(tokenBody.access_token)).toMatch(/^ldmcp1\./);
|
|
273
|
+
(0, vitest_1.expect)(String(tokenBody.access_token)).not.toContain('ld-access');
|
|
240
274
|
(0, vitest_1.expect)(tokenBody).not.toHaveProperty('refresh_token');
|
|
275
|
+
(0, vitest_1.expect)(tokenBody.scope).toBeUndefined();
|
|
276
|
+
const decrypted = (0, mcp_access_token_js_1.verifyMcpAccessToken)(config, String(tokenBody.access_token), RESOURCE);
|
|
277
|
+
(0, vitest_1.expect)(decrypted).toMatchObject({
|
|
278
|
+
lightdashAccessToken: 'ld-access',
|
|
279
|
+
clientId,
|
|
280
|
+
resource: RESOURCE,
|
|
281
|
+
});
|
|
282
|
+
});
|
|
283
|
+
(0, vitest_1.it)('binds the token request to the same resource as the authorization request', async () => {
|
|
284
|
+
const store = new pending_store_js_1.InMemoryOAuthBrokerStore();
|
|
285
|
+
const verifier = 'test-verifier-value-1234567890';
|
|
286
|
+
const challenge = (0, node_crypto_1.createHash)('sha256').update(verifier).digest('base64url');
|
|
287
|
+
const pending = await store.createPending({
|
|
288
|
+
clientId: 'client-a',
|
|
289
|
+
redirectUri: 'http://127.0.0.1:8787/callback',
|
|
290
|
+
codeChallenge: challenge,
|
|
291
|
+
codeChallengeMethod: 'S256',
|
|
292
|
+
resource: RESOURCE,
|
|
293
|
+
});
|
|
294
|
+
const issued = await store.issueCode(pending, { accessToken: 'ld-access', expiresIn: 3600 });
|
|
295
|
+
const broker = (0, routes_js_1.createOAuthBroker)(baseConfig(), store);
|
|
296
|
+
const res = mockRes();
|
|
297
|
+
await broker.handle(mockReq('POST', '/oauth/token', new URLSearchParams({
|
|
298
|
+
grant_type: 'authorization_code',
|
|
299
|
+
code: issued.code,
|
|
300
|
+
redirect_uri: pending.redirectUri,
|
|
301
|
+
client_id: pending.clientId,
|
|
302
|
+
code_verifier: verifier,
|
|
303
|
+
resource: 'https://mcp.example.com/content-governance/v1/mcp',
|
|
304
|
+
}).toString()), res, '/oauth/token');
|
|
305
|
+
(0, vitest_1.expect)(res.statusCode).toBe(400);
|
|
306
|
+
(0, vitest_1.expect)(res.body.error).toBe('invalid_grant');
|
|
307
|
+
(0, vitest_1.expect)(res.body.error_description).toBe('resource mismatch');
|
|
308
|
+
(0, vitest_1.expect)(await store.getCode(issued.code)).toBeDefined();
|
|
241
309
|
});
|
|
242
310
|
});
|
|
@@ -3,8 +3,10 @@ import type { McpHttpConfig } from '../../config/load-mcp-config.js';
|
|
|
3
3
|
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
4
4
|
export interface OAuthAuthSuccess {
|
|
5
5
|
ok: true;
|
|
6
|
+
/** Server-held downstream Lightdash credential. Never sourced directly from the MCP bearer. */
|
|
6
7
|
accessToken: string;
|
|
7
8
|
user: ValidatedLightdashUser;
|
|
9
|
+
/** MCP authorization scopes carried by the broker-issued token. */
|
|
8
10
|
scopes: string[] | undefined;
|
|
9
11
|
}
|
|
10
12
|
export interface OAuthAuthFailure {
|
|
@@ -5,6 +5,7 @@ exports.authenticateLightdashOAuth = authenticateLightdashOAuth;
|
|
|
5
5
|
exports.writeOAuthAuthFailure = writeOAuthAuthFailure;
|
|
6
6
|
const http_response_js_1 = require("../../transports/http-response.js");
|
|
7
7
|
const bearer_js_1 = require("../bearer.js");
|
|
8
|
+
const mcp_access_token_js_1 = require("../oauth-broker/mcp-access-token.js");
|
|
8
9
|
const lightdash_token_validation_js_1 = require("./lightdash-token-validation.js");
|
|
9
10
|
const oauth_protected_resource_js_1 = require("./oauth-protected-resource.js");
|
|
10
11
|
const token_scopes_js_1 = require("./token-scopes.js");
|
|
@@ -24,37 +25,70 @@ function buildBearerRequiredFailure(config, mcpPath) {
|
|
|
24
25
|
wwwAuthenticate: (0, www_authenticate_js_1.buildWwwAuthenticateHeader)({ resourceMetadataUrl, scope }),
|
|
25
26
|
};
|
|
26
27
|
}
|
|
28
|
+
function parseBrokerScopes(scope) {
|
|
29
|
+
if (!scope)
|
|
30
|
+
return undefined;
|
|
31
|
+
const scopes = scope.split(/\s+/).filter(Boolean);
|
|
32
|
+
return scopes.length > 0 ? scopes : undefined;
|
|
33
|
+
}
|
|
34
|
+
function invalidMcpTokenFailure(resourceMetadataUrl, scope) {
|
|
35
|
+
const description = 'Invalid, expired, or wrong-audience MCP access token';
|
|
36
|
+
return {
|
|
37
|
+
ok: false,
|
|
38
|
+
status: 401,
|
|
39
|
+
body: {
|
|
40
|
+
error: 'invalid_token',
|
|
41
|
+
error_description: description,
|
|
42
|
+
},
|
|
43
|
+
wwwAuthenticate: (0, www_authenticate_js_1.buildWwwAuthenticateHeader)({
|
|
44
|
+
resourceMetadataUrl,
|
|
45
|
+
scope,
|
|
46
|
+
error: 'invalid_token',
|
|
47
|
+
errorDescription: description,
|
|
48
|
+
}),
|
|
49
|
+
};
|
|
50
|
+
}
|
|
27
51
|
async function authenticateLightdashOAuth(req, config, mcpPath) {
|
|
28
52
|
const resourceMetadataUrl = (0, oauth_protected_resource_js_1.getProtectedResourceMetadataPathUrl)(config, mcpPath);
|
|
53
|
+
const expectedResource = (0, oauth_protected_resource_js_1.buildOAuthProtectedResourceMetadata)(config, mcpPath).resource;
|
|
29
54
|
const scope = config.requiredScopes.join(' ');
|
|
30
55
|
const token = (0, bearer_js_1.extractBearerToken)(req);
|
|
31
56
|
if (!token) {
|
|
32
57
|
return buildBearerRequiredFailure(config, mcpPath);
|
|
33
58
|
}
|
|
59
|
+
// The MCP resource server accepts only tokens minted by its co-located authorization
|
|
60
|
+
// server and bound to this exact profile resource. Raw Lightdash tokens fail here and
|
|
61
|
+
// are never passed through to the downstream API.
|
|
62
|
+
const brokerToken = (0, mcp_access_token_js_1.verifyMcpAccessToken)(config, token, expectedResource);
|
|
63
|
+
if (!brokerToken) {
|
|
64
|
+
return invalidMcpTokenFailure(resourceMetadataUrl, scope);
|
|
65
|
+
}
|
|
66
|
+
const scopes = parseBrokerScopes(brokerToken.scope);
|
|
67
|
+
if (config.requiredScopes.length > 0 && !(0, token_scopes_js_1.hasRequiredScopes)(scopes !== null && scopes !== void 0 ? scopes : [], config.requiredScopes)) {
|
|
68
|
+
const missingScopes = config.requiredScopes.filter((requiredScope) => !(scopes !== null && scopes !== void 0 ? scopes : []).includes(requiredScope));
|
|
69
|
+
return {
|
|
70
|
+
ok: false,
|
|
71
|
+
status: 403,
|
|
72
|
+
body: {
|
|
73
|
+
error: 'insufficient_scope',
|
|
74
|
+
error_description: `Missing required OAuth scopes: ${missingScopes.join(', ')}`,
|
|
75
|
+
},
|
|
76
|
+
wwwAuthenticate: (0, www_authenticate_js_1.buildWwwAuthenticateHeader)({
|
|
77
|
+
resourceMetadataUrl,
|
|
78
|
+
scope: config.requiredScopes.join(' '),
|
|
79
|
+
error: 'insufficient_scope',
|
|
80
|
+
errorDescription: `Missing required OAuth scopes: ${missingScopes.join(', ')}`,
|
|
81
|
+
}),
|
|
82
|
+
};
|
|
83
|
+
}
|
|
34
84
|
try {
|
|
35
|
-
const user = await (0, lightdash_token_validation_js_1.validateLightdashAccessToken)(config,
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
return {
|
|
43
|
-
ok: false,
|
|
44
|
-
status: 403,
|
|
45
|
-
body: {
|
|
46
|
-
error: 'insufficient_scope',
|
|
47
|
-
error_description: `Missing required OAuth scopes: ${missingScopes.join(', ')}`,
|
|
48
|
-
},
|
|
49
|
-
wwwAuthenticate: (0, www_authenticate_js_1.buildWwwAuthenticateHeader)({
|
|
50
|
-
resourceMetadataUrl,
|
|
51
|
-
scope: config.requiredScopes.join(' '),
|
|
52
|
-
error: 'insufficient_scope',
|
|
53
|
-
errorDescription: `Missing required OAuth scopes: ${missingScopes.join(', ')}`,
|
|
54
|
-
}),
|
|
55
|
-
};
|
|
56
|
-
}
|
|
57
|
-
return { ok: true, accessToken: token, user, scopes };
|
|
85
|
+
const user = await (0, lightdash_token_validation_js_1.validateLightdashAccessToken)(config, brokerToken.lightdashAccessToken);
|
|
86
|
+
return {
|
|
87
|
+
ok: true,
|
|
88
|
+
accessToken: brokerToken.lightdashAccessToken,
|
|
89
|
+
user,
|
|
90
|
+
scopes,
|
|
91
|
+
};
|
|
58
92
|
}
|
|
59
93
|
catch (error) {
|
|
60
94
|
if (error instanceof token_validation_error_js_1.TokenValidationError && error.reason === 'upstream_unavailable') {
|
|
@@ -71,20 +105,7 @@ async function authenticateLightdashOAuth(req, config, mcpPath) {
|
|
|
71
105
|
headers,
|
|
72
106
|
};
|
|
73
107
|
}
|
|
74
|
-
return
|
|
75
|
-
ok: false,
|
|
76
|
-
status: 401,
|
|
77
|
-
body: {
|
|
78
|
-
error: 'invalid_token',
|
|
79
|
-
error_description: 'Invalid or expired Lightdash access token',
|
|
80
|
-
},
|
|
81
|
-
wwwAuthenticate: (0, www_authenticate_js_1.buildWwwAuthenticateHeader)({
|
|
82
|
-
resourceMetadataUrl,
|
|
83
|
-
scope,
|
|
84
|
-
error: 'invalid_token',
|
|
85
|
-
errorDescription: 'Invalid or expired Lightdash access token',
|
|
86
|
-
}),
|
|
87
|
-
};
|
|
108
|
+
return invalidMcpTokenFailure(resourceMetadataUrl, scope);
|
|
88
109
|
}
|
|
89
110
|
}
|
|
90
111
|
function writeOAuthAuthFailure(res, failure) {
|