@lightdash-tools/mcp 0.24.0 → 0.25.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/dist/auth/oauth-broker/as-metadata.d.ts +1 -0
- package/dist/auth/oauth-broker/as-metadata.js +5 -1
- package/dist/auth/oauth-broker/client-crypto.d.ts +12 -0
- package/dist/auth/oauth-broker/client-crypto.js +80 -0
- package/dist/auth/oauth-broker/client-directory.d.ts +39 -0
- package/dist/auth/oauth-broker/client-directory.js +104 -0
- package/dist/auth/oauth-broker/client-directory.test.d.ts +1 -0
- package/dist/auth/oauth-broker/client-directory.test.js +134 -0
- package/dist/auth/oauth-broker/lightdash-token.d.ts +8 -0
- package/dist/auth/oauth-broker/lightdash-token.js +72 -8
- package/dist/auth/oauth-broker/mcp-access-token.d.ts +3 -0
- package/dist/auth/oauth-broker/mcp-access-token.js +9 -1
- package/dist/auth/oauth-broker/pending-store.d.ts +16 -4
- package/dist/auth/oauth-broker/pending-store.js +40 -18
- package/dist/auth/oauth-broker/refresh-crypto.d.ts +19 -0
- package/dist/auth/oauth-broker/refresh-crypto.js +70 -0
- package/dist/auth/oauth-broker/refresh-crypto.test.d.ts +1 -0
- package/dist/auth/oauth-broker/refresh-crypto.test.js +40 -0
- package/dist/auth/oauth-broker/refresh-flow.test.d.ts +1 -0
- package/dist/auth/oauth-broker/refresh-flow.test.js +350 -0
- package/dist/auth/oauth-broker/refresh-grant.d.ts +46 -0
- package/dist/auth/oauth-broker/refresh-grant.js +288 -0
- package/dist/auth/oauth-broker/refresh-store.conformance.test.d.ts +1 -0
- package/dist/auth/oauth-broker/refresh-store.conformance.test.js +254 -0
- package/dist/auth/oauth-broker/refresh-store.d.ts +61 -0
- package/dist/auth/oauth-broker/refresh-store.js +124 -0
- package/dist/auth/oauth-broker/routes.d.ts +6 -4
- package/dist/auth/oauth-broker/routes.js +114 -51
- package/dist/auth/oauth-broker/routes.test.js +48 -0
- package/dist/auth/oauth-broker/stores/client-kv.d.ts +46 -0
- package/dist/auth/oauth-broker/stores/client-kv.js +182 -0
- package/dist/auth/oauth-broker/stores/firestore.d.ts +29 -0
- package/dist/auth/oauth-broker/stores/firestore.js +201 -0
- package/dist/auth/oauth-broker/stores/open-client-directory.d.ts +4 -0
- package/dist/auth/oauth-broker/stores/open-client-directory.js +30 -0
- package/dist/auth/oauth-broker/stores/open-refresh-store.d.ts +4 -0
- package/dist/auth/oauth-broker/stores/open-refresh-store.js +24 -0
- package/dist/auth/oauth-broker/stores/postgres.d.ts +13 -0
- package/dist/auth/oauth-broker/stores/postgres.js +193 -0
- package/dist/auth/oauth-broker/stores/redis.d.ts +25 -0
- package/dist/auth/oauth-broker/stores/redis.js +337 -0
- package/dist/auth/resource-server/lightdash-oauth-middleware.d.ts +1 -1
- package/dist/config/env.d.ts +13 -0
- package/dist/config/env.js +14 -1
- package/dist/config/load-mcp-config.d.ts +20 -0
- package/dist/config/load-mcp-config.js +53 -1
- package/dist/config/load-mcp-config.test.js +53 -0
- package/dist/config/oauth-store-key.d.ts +2 -0
- package/dist/config/oauth-store-key.js +17 -0
- package/dist/policy/content-developer.d.ts +4 -1
- package/dist/policy/content-developer.js +7 -3
- package/dist/policy/preview-ledger.d.ts +11 -1
- package/dist/policy/preview-ledger.js +29 -3
- package/dist/policy/rename-rejected.d.ts +9 -0
- package/dist/policy/rename-rejected.js +15 -0
- package/dist/profiles/content-developer/v1/index.js +6 -0
- package/dist/profiles/content-developer/v1/prompt-specs.d.ts +1 -1
- package/dist/profiles/content-developer/v1/resources/playbooks/core.md +15 -0
- package/dist/profiles/content-developer/v1/resources/playbooks/recovery/preview-stale.md +1 -0
- package/dist/profiles/content-reader/v1/prompt-specs.d.ts +1 -1
- package/dist/server/capabilities.d.ts +1 -1
- package/dist/stdio.process.test.js +3 -1
- package/dist/tools/project/content.js +8 -1
- package/dist/tools/project/developer-content.js +1 -1
- package/dist/tools/project/developer-helpers.d.ts +1 -0
- package/dist/tools/project/developer-helpers.test.js +1 -0
- package/dist/tools/project/developer-rename.d.ts +13 -0
- package/dist/tools/project/developer-rename.js +319 -0
- package/dist/tools/project/developer-rename.test.d.ts +5 -0
- package/dist/tools/project/developer-rename.test.js +296 -0
- package/dist/tools/project/reader-content.js +10 -3
- package/dist/tools/project/rename-instruction.d.ts +58 -0
- package/dist/tools/project/rename-instruction.js +104 -0
- package/dist/tools/query/reader-tool-helpers.d.ts +1 -1
- package/dist/tools/query/reader-tool-helpers.js +4 -0
- package/dist/tools/registry.d.ts +1 -1
- package/dist/transports/streamable-http.js +33 -7
- package/package.json +13 -9
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Server-side Lightdash refresh-token store (ADR-0037).
|
|
4
|
+
*
|
|
5
|
+
* `consume` is single-use. A second consume of the same handle is `reused`
|
|
6
|
+
* so the broker can revoke the family. `release` undoes a consume when the
|
|
7
|
+
* upstream call did not finish.
|
|
8
|
+
*/
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.InMemoryRefreshStore = exports.RefreshStoreError = void 0;
|
|
11
|
+
exports.withClose = withClose;
|
|
12
|
+
exports.wrapStoreError = wrapStoreError;
|
|
13
|
+
exports.ignoreRefreshStoreError = ignoreRefreshStoreError;
|
|
14
|
+
exports.sessionEndMs = sessionEndMs;
|
|
15
|
+
exports.storeErrorCode = storeErrorCode;
|
|
16
|
+
class RefreshStoreError extends Error {
|
|
17
|
+
constructor(message, options) {
|
|
18
|
+
super(message);
|
|
19
|
+
this.name = 'RefreshStoreError';
|
|
20
|
+
this.cause = options === null || options === void 0 ? void 0 : options.cause;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
exports.RefreshStoreError = RefreshStoreError;
|
|
24
|
+
/** Attaches a backend `close` without repeating the five store methods. */
|
|
25
|
+
function withClose(store, close) {
|
|
26
|
+
return {
|
|
27
|
+
put: (digest, record) => store.put(digest, record),
|
|
28
|
+
consume: (digest) => store.consume(digest),
|
|
29
|
+
release: (digest) => store.release(digest),
|
|
30
|
+
revokeFamily: (familyId) => store.revokeFamily(familyId),
|
|
31
|
+
delete: (digest) => store.delete(digest),
|
|
32
|
+
close,
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
/** Wraps a backend failure. An existing `RefreshStoreError` is rethrown as-is. */
|
|
36
|
+
async function wrapStoreError(message, fn) {
|
|
37
|
+
try {
|
|
38
|
+
return await fn();
|
|
39
|
+
}
|
|
40
|
+
catch (error) {
|
|
41
|
+
if (error instanceof RefreshStoreError)
|
|
42
|
+
throw error;
|
|
43
|
+
throw new RefreshStoreError(message, { cause: error });
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
/** Store cleanup after the OAuth result is already decided. Other errors propagate. */
|
|
47
|
+
async function ignoreRefreshStoreError(work) {
|
|
48
|
+
try {
|
|
49
|
+
await work;
|
|
50
|
+
}
|
|
51
|
+
catch (error) {
|
|
52
|
+
if (error instanceof RefreshStoreError)
|
|
53
|
+
return;
|
|
54
|
+
throw error;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/** Absolute end of a refresh family, shared by the grant and every store. */
|
|
58
|
+
function sessionEndMs(firstIssuedAtMs, maxSessionSeconds) {
|
|
59
|
+
return firstIssuedAtMs + maxSessionSeconds * 1000;
|
|
60
|
+
}
|
|
61
|
+
function storeErrorCode(error) {
|
|
62
|
+
if (typeof error === 'object' && error !== null && 'code' in error) {
|
|
63
|
+
return String(error.code);
|
|
64
|
+
}
|
|
65
|
+
return 'unknown';
|
|
66
|
+
}
|
|
67
|
+
/** Process-local store used by tests and as the conformance reference. */
|
|
68
|
+
class InMemoryRefreshStore {
|
|
69
|
+
constructor() {
|
|
70
|
+
this.rows = new Map();
|
|
71
|
+
this.revoked = new Set();
|
|
72
|
+
this.chain = Promise.resolve();
|
|
73
|
+
}
|
|
74
|
+
async put(handleDigest, record) {
|
|
75
|
+
await this.exclusive(() => {
|
|
76
|
+
if (this.revoked.has(record.familyId)) {
|
|
77
|
+
throw new RefreshStoreError('Refresh family is revoked');
|
|
78
|
+
}
|
|
79
|
+
this.rows.set(handleDigest, { record: cloneRecord(record), consumed: false });
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
async consume(handleDigest) {
|
|
83
|
+
return this.exclusive(() => {
|
|
84
|
+
const row = this.rows.get(handleDigest);
|
|
85
|
+
if (!row)
|
|
86
|
+
return { status: 'missing' };
|
|
87
|
+
if (row.consumed || this.revoked.has(row.record.familyId)) {
|
|
88
|
+
return { status: 'reused', record: cloneRecord(row.record) };
|
|
89
|
+
}
|
|
90
|
+
row.consumed = true;
|
|
91
|
+
return { status: 'ok', record: cloneRecord(row.record) };
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
async release(handleDigest) {
|
|
95
|
+
await this.exclusive(() => {
|
|
96
|
+
const row = this.rows.get(handleDigest);
|
|
97
|
+
if (row)
|
|
98
|
+
row.consumed = false;
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
async revokeFamily(familyId) {
|
|
102
|
+
await this.exclusive(() => {
|
|
103
|
+
this.revoked.add(familyId);
|
|
104
|
+
for (const [digest, row] of this.rows) {
|
|
105
|
+
if (row.record.familyId === familyId)
|
|
106
|
+
this.rows.delete(digest);
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
async delete(handleDigest) {
|
|
111
|
+
await this.exclusive(() => {
|
|
112
|
+
this.rows.delete(handleDigest);
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
async exclusive(fn) {
|
|
116
|
+
const run = this.chain.then(fn, fn);
|
|
117
|
+
this.chain = run.then(() => undefined, () => undefined);
|
|
118
|
+
return run;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
exports.InMemoryRefreshStore = InMemoryRefreshStore;
|
|
122
|
+
function cloneRecord(record) {
|
|
123
|
+
return Object.assign({}, record);
|
|
124
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import type { OAuthBrokerStore } from './pending-store.js';
|
|
2
1
|
import type { McpHttpConfig } from '../../config/load-mcp-config.js';
|
|
2
|
+
import type { OAuthBrokerStore } from './pending-store.js';
|
|
3
|
+
import type { RefreshStore } from './refresh-store.js';
|
|
3
4
|
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
4
5
|
export interface OAuthBroker {
|
|
5
6
|
handle: (req: IncomingMessage, res: ServerResponse, path: string) => Promise<boolean>;
|
|
@@ -8,7 +9,8 @@ export interface OAuthBroker {
|
|
|
8
9
|
export declare function isOAuthBrokerPath(path: string): boolean;
|
|
9
10
|
/**
|
|
10
11
|
* Creates the OAuth broker request handler for co-located AS façade routes.
|
|
11
|
-
* Pending authorization
|
|
12
|
-
* sticky routing or one replica.
|
|
12
|
+
* Pending authorization and authorization-code state is process-local, so `/oauth/*`
|
|
13
|
+
* still needs sticky routing or one replica. Client registrations follow the OAuth
|
|
14
|
+
* store when it is on. Issued MCP access tokens are self-contained.
|
|
13
15
|
*/
|
|
14
|
-
export declare function createOAuthBroker(config: McpHttpConfig, store?: OAuthBrokerStore): OAuthBroker;
|
|
16
|
+
export declare function createOAuthBroker(config: McpHttpConfig, store?: OAuthBrokerStore, refreshStore?: RefreshStore): OAuthBroker;
|
|
@@ -14,12 +14,21 @@ const lightdash_token_js_1 = require("./lightdash-token.js");
|
|
|
14
14
|
const mcp_access_token_js_1 = require("./mcp-access-token.js");
|
|
15
15
|
const pending_store_js_1 = require("./pending-store.js");
|
|
16
16
|
const pkce_js_1 = require("./pkce.js");
|
|
17
|
+
const refresh_grant_js_1 = require("./refresh-grant.js");
|
|
18
|
+
const refresh_store_js_1 = require("./refresh-store.js");
|
|
17
19
|
/** Schemes that must never be used as OAuth redirect targets. */
|
|
18
20
|
const BLOCKED_REDIRECT_SCHEMES = new Set(['javascript:', 'data:', 'vbscript:', 'file:']);
|
|
19
21
|
const AS_BUSY = {
|
|
20
22
|
error: 'temporarily_unavailable',
|
|
21
23
|
error_description: 'Authorization server busy; retry later',
|
|
22
24
|
};
|
|
25
|
+
const TOKEN_RESPONSE_HEADERS = {
|
|
26
|
+
'Cache-Control': 'no-store',
|
|
27
|
+
Pragma: 'no-cache',
|
|
28
|
+
};
|
|
29
|
+
function sendTokenJson(res, status, body) {
|
|
30
|
+
(0, http_response_js_1.sendJson)(res, status, body, TOKEN_RESPONSE_HEADERS);
|
|
31
|
+
}
|
|
23
32
|
function redirect(res, location) {
|
|
24
33
|
res.writeHead(302, { Location: location }).end();
|
|
25
34
|
}
|
|
@@ -305,12 +314,11 @@ async function handleCallback(req, res, config, store) {
|
|
|
305
314
|
const tokens = await (0, lightdash_token_js_1.exchangeLightdashAuthorizationCode)(config, code);
|
|
306
315
|
// The downstream credential remains server-side. The later MCP token endpoint
|
|
307
316
|
// wraps it in an authenticated-encrypted, resource-bound broker token.
|
|
308
|
-
//
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
});
|
|
317
|
+
// A Lightdash refresh token is carried only until that exchange when refresh
|
|
318
|
+
// storage is enabled (ADR-0037). It is never placed on the client redirect.
|
|
319
|
+
const issued = await store.issueCode(pending, Object.assign({ accessToken: tokens.access_token, expiresIn: tokens.expires_in, tokenType: tokens.token_type }, (config.oauthRefresh && tokens.refresh_token
|
|
320
|
+
? { lightdashRefreshToken: tokens.refresh_token }
|
|
321
|
+
: {})));
|
|
314
322
|
if (!issued) {
|
|
315
323
|
redirectToClient(res, pending.redirectUri, Object.assign(Object.assign({}, AS_BUSY), { state: pending.clientState }));
|
|
316
324
|
return;
|
|
@@ -329,16 +337,10 @@ async function handleCallback(req, res, config, store) {
|
|
|
329
337
|
});
|
|
330
338
|
}
|
|
331
339
|
}
|
|
332
|
-
function parseTokenGrantRequest(params) {
|
|
340
|
+
function parseTokenGrantRequest(params, refreshEnabled) {
|
|
333
341
|
var _a;
|
|
334
342
|
if (params.get('grant_type') !== 'authorization_code') {
|
|
335
|
-
return
|
|
336
|
-
status: 400,
|
|
337
|
-
body: {
|
|
338
|
-
error: 'unsupported_grant_type',
|
|
339
|
-
error_description: 'Only authorization_code is supported',
|
|
340
|
-
},
|
|
341
|
-
};
|
|
343
|
+
return (0, refresh_grant_js_1.unsupportedGrantType)(refreshEnabled);
|
|
342
344
|
}
|
|
343
345
|
const code = params.get('code');
|
|
344
346
|
const redirectUri = params.get('redirect_uri');
|
|
@@ -359,23 +361,17 @@ function parseTokenGrantRequest(params) {
|
|
|
359
361
|
}
|
|
360
362
|
async function restoreInvalidGrant(store, candidate, description) {
|
|
361
363
|
await store.restoreCode(candidate);
|
|
362
|
-
return
|
|
363
|
-
status: 400,
|
|
364
|
-
body: { error: 'invalid_grant', error_description: description },
|
|
365
|
-
};
|
|
364
|
+
return (0, refresh_grant_js_1.invalidGrant)(description);
|
|
366
365
|
}
|
|
367
|
-
async function validateTokenGrant(params, store) {
|
|
368
|
-
const request = parseTokenGrantRequest(params);
|
|
366
|
+
async function validateTokenGrant(params, store, refreshEnabled) {
|
|
367
|
+
const request = parseTokenGrantRequest(params, refreshEnabled);
|
|
369
368
|
if ('status' in request)
|
|
370
369
|
return request;
|
|
371
370
|
// Atomic take first. Restore on validation failure so a bad verifier / redirect /
|
|
372
371
|
// resource does not permanently burn a one-time code.
|
|
373
372
|
const candidate = await store.takeCode(request.code);
|
|
374
373
|
if (!candidate) {
|
|
375
|
-
return
|
|
376
|
-
status: 400,
|
|
377
|
-
body: { error: 'invalid_grant', error_description: 'Invalid or expired code' },
|
|
378
|
-
};
|
|
374
|
+
return (0, refresh_grant_js_1.invalidGrant)('Invalid or expired code');
|
|
379
375
|
}
|
|
380
376
|
if (candidate.redirectUri !== request.redirectUri) {
|
|
381
377
|
return restoreInvalidGrant(store, candidate, 'redirect_uri mismatch');
|
|
@@ -391,8 +387,7 @@ async function validateTokenGrant(params, store) {
|
|
|
391
387
|
}
|
|
392
388
|
return { issued: candidate };
|
|
393
389
|
}
|
|
394
|
-
async function handleToken(req, res, config, store) {
|
|
395
|
-
var _a;
|
|
390
|
+
async function handleToken(req, res, config, store, refreshStore) {
|
|
396
391
|
if (req.method !== 'POST') {
|
|
397
392
|
res.writeHead(405, { Allow: 'POST' }).end();
|
|
398
393
|
return;
|
|
@@ -400,35 +395,99 @@ async function handleToken(req, res, config, store) {
|
|
|
400
395
|
const params = await readFormOrJson(req, res, config.maxBodyBytes);
|
|
401
396
|
if (!params)
|
|
402
397
|
return;
|
|
403
|
-
|
|
398
|
+
if (params.get('grant_type') === 'refresh_token') {
|
|
399
|
+
await handleRefreshGrant(res, config, refreshStore, params);
|
|
400
|
+
return;
|
|
401
|
+
}
|
|
402
|
+
const grant = await validateTokenGrant(params, store, Boolean(config.oauthRefresh));
|
|
404
403
|
if ('status' in grant) {
|
|
405
|
-
(
|
|
404
|
+
sendTokenJson(res, grant.status, grant.body);
|
|
406
405
|
return;
|
|
407
406
|
}
|
|
408
|
-
|
|
407
|
+
await sendAuthorizationCodeToken(res, config, store, refreshStore, grant.issued);
|
|
408
|
+
}
|
|
409
|
+
async function sendAuthorizationCodeToken(res, config, store, refreshStore, issued) {
|
|
410
|
+
var _a;
|
|
411
|
+
const nowMs = Date.now();
|
|
412
|
+
const firstIssuedAtMs = nowMs;
|
|
409
413
|
const upstreamLifetimeSeconds = (_a = issued.expiresIn) !== null && _a !== void 0 ? _a : 3600;
|
|
410
|
-
const expiresAtMs =
|
|
414
|
+
const expiresAtMs = config.oauthRefresh
|
|
415
|
+
? (0, refresh_grant_js_1.cappedExpiryMs)(nowMs, firstIssuedAtMs, config.oauthRefresh, upstreamLifetimeSeconds)
|
|
416
|
+
: issued.createdAt + upstreamLifetimeSeconds * 1000;
|
|
417
|
+
const attached = config.oauthRefresh && refreshStore
|
|
418
|
+
? await (0, refresh_grant_js_1.attachRefreshToken)({ config, refreshStore, issued, nowMs })
|
|
419
|
+
: { ok: true, refreshToken: undefined };
|
|
420
|
+
if (!attached.ok) {
|
|
421
|
+
await store.restoreCode(issued);
|
|
422
|
+
const busy = (0, refresh_grant_js_1.unavailable)('Refresh store is unavailable');
|
|
423
|
+
sendTokenJson(res, busy.status, busy.body);
|
|
424
|
+
return;
|
|
425
|
+
}
|
|
426
|
+
await sendMintedAccessToken({
|
|
427
|
+
res,
|
|
428
|
+
config,
|
|
429
|
+
refreshStore,
|
|
430
|
+
issued,
|
|
431
|
+
expiresAtMs,
|
|
432
|
+
refreshToken: attached.refreshToken,
|
|
433
|
+
});
|
|
434
|
+
}
|
|
435
|
+
async function sendMintedAccessToken(input) {
|
|
411
436
|
try {
|
|
412
|
-
const minted = (0, mcp_access_token_js_1.mintMcpAccessToken)(config, {
|
|
413
|
-
lightdashAccessToken: issued.accessToken,
|
|
414
|
-
clientId: issued.clientId,
|
|
415
|
-
resource: issued.resource,
|
|
416
|
-
scope: issued.scope,
|
|
417
|
-
expiresAtMs,
|
|
418
|
-
});
|
|
419
|
-
(0, http_response_js_1.sendJson)(res, 200, {
|
|
420
|
-
access_token: minted.accessToken,
|
|
421
|
-
token_type: 'Bearer',
|
|
422
|
-
expires_in: minted.expiresIn,
|
|
423
|
-
scope: issued.scope,
|
|
437
|
+
const minted = (0, mcp_access_token_js_1.mintMcpAccessToken)(input.config, {
|
|
438
|
+
lightdashAccessToken: input.issued.accessToken,
|
|
439
|
+
clientId: input.issued.clientId,
|
|
440
|
+
resource: input.issued.resource,
|
|
441
|
+
scope: input.issued.scope,
|
|
442
|
+
expiresAtMs: input.expiresAtMs,
|
|
424
443
|
});
|
|
444
|
+
sendTokenJson(input.res, 200, Object.assign(Object.assign({ access_token: minted.accessToken, token_type: 'Bearer', expires_in: minted.expiresIn }, (input.refreshToken ? { refresh_token: input.refreshToken } : {})), { scope: input.issued.scope }));
|
|
445
|
+
}
|
|
446
|
+
catch (error) {
|
|
447
|
+
if (!(error instanceof mcp_access_token_js_1.McpAccessTokenExpiredError))
|
|
448
|
+
throw error;
|
|
449
|
+
if (input.refreshToken && input.refreshStore) {
|
|
450
|
+
const digest = (0, refresh_grant_js_1.digestOfRefreshToken)(input.refreshToken);
|
|
451
|
+
if (digest)
|
|
452
|
+
await (0, refresh_store_js_1.ignoreRefreshStoreError)(input.refreshStore.delete(digest));
|
|
453
|
+
}
|
|
454
|
+
const expired = (0, refresh_grant_js_1.invalidGrant)('Upstream Lightdash credential expired before broker token issuance');
|
|
455
|
+
sendTokenJson(input.res, expired.status, expired.body);
|
|
425
456
|
}
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
457
|
+
}
|
|
458
|
+
async function handleRefreshGrant(res, config, refreshStore, params) {
|
|
459
|
+
var _a;
|
|
460
|
+
if (!config.oauthRefresh || !refreshStore) {
|
|
461
|
+
const unsupported = (0, refresh_grant_js_1.unsupportedGrantType)();
|
|
462
|
+
sendTokenJson(res, unsupported.status, unsupported.body);
|
|
463
|
+
return;
|
|
464
|
+
}
|
|
465
|
+
const refreshToken = params.get('refresh_token');
|
|
466
|
+
const clientId = (_a = params.get('client_id')) !== null && _a !== void 0 ? _a : undefined;
|
|
467
|
+
const resources = params.getAll('resource');
|
|
468
|
+
if (!refreshToken || resources.length > 1) {
|
|
469
|
+
sendTokenJson(res, 400, {
|
|
470
|
+
error: 'invalid_request',
|
|
471
|
+
error_description: 'refresh_token is required and at most one resource may be provided',
|
|
430
472
|
});
|
|
473
|
+
return;
|
|
431
474
|
}
|
|
475
|
+
const requestedResource = resources[0];
|
|
476
|
+
const canonical = requestedResource === undefined ? undefined : allowedMcpResource(config, requestedResource);
|
|
477
|
+
if (requestedResource !== undefined && !canonical) {
|
|
478
|
+
const mismatch = (0, refresh_grant_js_1.invalidGrant)('resource mismatch');
|
|
479
|
+
sendTokenJson(res, mismatch.status, mismatch.body);
|
|
480
|
+
return;
|
|
481
|
+
}
|
|
482
|
+
const result = await (0, refresh_grant_js_1.handleRefreshTokenGrant)({
|
|
483
|
+
config,
|
|
484
|
+
refreshStore,
|
|
485
|
+
refreshToken,
|
|
486
|
+
resource: canonical,
|
|
487
|
+
clientId,
|
|
488
|
+
isResourceAllowed: (resource) => allowedMcpResource(config, resource) !== undefined,
|
|
489
|
+
});
|
|
490
|
+
sendTokenJson(res, result.status, result.body);
|
|
432
491
|
}
|
|
433
492
|
async function handleRegister(req, res, config, store) {
|
|
434
493
|
var _a;
|
|
@@ -467,7 +526,7 @@ async function handleRegister(req, res, config, store) {
|
|
|
467
526
|
client_id: registered.clientId,
|
|
468
527
|
client_id_issued_at: Math.floor(registered.createdAt / 1000),
|
|
469
528
|
redirect_uris: [...registered.redirectUris],
|
|
470
|
-
grant_types:
|
|
529
|
+
grant_types: (0, as_metadata_js_1.brokerGrantTypes)(config),
|
|
471
530
|
response_types: ['code'],
|
|
472
531
|
token_endpoint_auth_method: 'none',
|
|
473
532
|
client_name: (_a = params.get('client_name')) !== null && _a !== void 0 ? _a : 'MCP Client',
|
|
@@ -489,10 +548,14 @@ function isOAuthBrokerPath(path) {
|
|
|
489
548
|
}
|
|
490
549
|
/**
|
|
491
550
|
* Creates the OAuth broker request handler for co-located AS façade routes.
|
|
492
|
-
* Pending authorization
|
|
493
|
-
* sticky routing or one replica.
|
|
551
|
+
* Pending authorization and authorization-code state is process-local, so `/oauth/*`
|
|
552
|
+
* still needs sticky routing or one replica. Client registrations follow the OAuth
|
|
553
|
+
* store when it is on. Issued MCP access tokens are self-contained.
|
|
494
554
|
*/
|
|
495
|
-
function createOAuthBroker(config, store = new pending_store_js_1.InMemoryOAuthBrokerStore()) {
|
|
555
|
+
function createOAuthBroker(config, store = new pending_store_js_1.InMemoryOAuthBrokerStore(), refreshStore) {
|
|
556
|
+
if (config.oauthRefresh && !refreshStore) {
|
|
557
|
+
throw new Error('OAuth refresh is configured but no refresh store was opened');
|
|
558
|
+
}
|
|
496
559
|
return {
|
|
497
560
|
async handle(req, res, path) {
|
|
498
561
|
if (!isOAuthBrokerPath(path)) {
|
|
@@ -515,7 +578,7 @@ function createOAuthBroker(config, store = new pending_store_js_1.InMemoryOAuthB
|
|
|
515
578
|
return true;
|
|
516
579
|
}
|
|
517
580
|
if (path === env_js_1.OAUTH_TOKEN_PATH) {
|
|
518
|
-
await handleToken(req, res, config, store);
|
|
581
|
+
await handleToken(req, res, config, store, refreshStore);
|
|
519
582
|
return true;
|
|
520
583
|
}
|
|
521
584
|
if (path === env_js_1.OAUTH_REGISTER_PATH) {
|
|
@@ -6,6 +6,7 @@ const client_1 = require("@lightdash-tools/client");
|
|
|
6
6
|
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
|
+
const client_directory_js_1 = require("./client-directory.js");
|
|
9
10
|
const lightdash_token_js_1 = require("./lightdash-token.js");
|
|
10
11
|
const mcp_access_token_js_1 = require("./mcp-access-token.js");
|
|
11
12
|
const pending_store_js_1 = require("./pending-store.js");
|
|
@@ -91,6 +92,7 @@ function authorizeUrl(clientId, redirectUri, challenge) {
|
|
|
91
92
|
(0, vitest_1.expect)(metadata.authorization_endpoint).toBe('https://mcp.example.com/oauth/authorize');
|
|
92
93
|
(0, vitest_1.expect)(metadata.token_endpoint).toBe('https://mcp.example.com/oauth/token');
|
|
93
94
|
(0, vitest_1.expect)(metadata.registration_endpoint).toBe('https://mcp.example.com/oauth/register');
|
|
95
|
+
(0, vitest_1.expect)(metadata.grant_types_supported).toEqual(['authorization_code']);
|
|
94
96
|
});
|
|
95
97
|
(0, vitest_1.it)('moves token and DCR onto an extra invoke origin while issuer stays public', () => {
|
|
96
98
|
const metadata = (0, as_metadata_js_1.buildBrokerAuthorizationServerMetadata)(baseConfig(), 'http://mcp.ilb.internal');
|
|
@@ -415,3 +417,49 @@ function authorizeUrl(clientId, redirectUri, challenge) {
|
|
|
415
417
|
(0, vitest_1.expect)(await store.getCode(issued.code)).toBeDefined();
|
|
416
418
|
});
|
|
417
419
|
});
|
|
420
|
+
(0, vitest_1.describe)('oauth broker client directory', () => {
|
|
421
|
+
const redirectUri = 'http://127.0.0.1:8787/callback';
|
|
422
|
+
async function register(broker) {
|
|
423
|
+
const registerRes = mockRes();
|
|
424
|
+
await broker.handle(mockReq('POST', '/oauth/register', `redirect_uris=${encodeURIComponent(redirectUri)}`), registerRes, '/oauth/register');
|
|
425
|
+
(0, vitest_1.expect)(registerRes.statusCode).toBe(201);
|
|
426
|
+
return registerRes.body.client_id;
|
|
427
|
+
}
|
|
428
|
+
function authorize(broker, clientId) {
|
|
429
|
+
const challenge = (0, node_crypto_1.createHash)('sha256').update('verifier').digest('base64url');
|
|
430
|
+
const res = mockRes();
|
|
431
|
+
const pending = broker.handle(mockReq('GET', authorizeUrl(clientId, redirectUri, challenge)), res, '/oauth/authorize');
|
|
432
|
+
return pending.then(() => res);
|
|
433
|
+
}
|
|
434
|
+
(0, vitest_1.it)('authorizes a client after process memory is gone when the directory has the row', async () => {
|
|
435
|
+
const directory = (0, client_directory_js_1.createSealedClientDirectory)(new client_directory_js_1.InMemoryClientKv(), { key: (0, node_crypto_1.randomBytes)(32) });
|
|
436
|
+
const first = (0, routes_js_1.createOAuthBroker)(baseConfig(), new pending_store_js_1.InMemoryOAuthBrokerStore({ clientDirectory: directory }));
|
|
437
|
+
const clientId = await register(first);
|
|
438
|
+
const restarted = (0, routes_js_1.createOAuthBroker)(baseConfig(), new pending_store_js_1.InMemoryOAuthBrokerStore({ clientDirectory: directory }));
|
|
439
|
+
const res = await authorize(restarted, clientId);
|
|
440
|
+
(0, vitest_1.expect)(res.statusCode).toBe(302);
|
|
441
|
+
});
|
|
442
|
+
(0, vitest_1.it)('rejects a tampered redirect row and deletes it', async () => {
|
|
443
|
+
const kv = new client_directory_js_1.InMemoryClientKv();
|
|
444
|
+
const directory = (0, client_directory_js_1.createSealedClientDirectory)(kv, { key: (0, node_crypto_1.randomBytes)(32) });
|
|
445
|
+
const first = (0, routes_js_1.createOAuthBroker)(baseConfig(), new pending_store_js_1.InMemoryOAuthBrokerStore({ clientDirectory: directory }));
|
|
446
|
+
const clientId = await register(first);
|
|
447
|
+
const [digest, raw] = [...kv.rows.entries()][0];
|
|
448
|
+
const blob = JSON.parse(raw);
|
|
449
|
+
blob.redirectUris = ['https://attacker.example/cb'];
|
|
450
|
+
kv.rows.set(digest, JSON.stringify(blob));
|
|
451
|
+
const restarted = (0, routes_js_1.createOAuthBroker)(baseConfig(), new pending_store_js_1.InMemoryOAuthBrokerStore({ clientDirectory: directory }));
|
|
452
|
+
const res = await authorize(restarted, clientId);
|
|
453
|
+
(0, vitest_1.expect)(res.statusCode).toBe(400);
|
|
454
|
+
(0, vitest_1.expect)(res.body.error).toBe('invalid_client');
|
|
455
|
+
(0, vitest_1.expect)(kv.rows.size).toBe(0);
|
|
456
|
+
});
|
|
457
|
+
(0, vitest_1.it)('rejects a saved client id when the store is unset', async () => {
|
|
458
|
+
const first = (0, routes_js_1.createOAuthBroker)(baseConfig(), new pending_store_js_1.InMemoryOAuthBrokerStore());
|
|
459
|
+
const clientId = await register(first);
|
|
460
|
+
const restarted = (0, routes_js_1.createOAuthBroker)(baseConfig(), new pending_store_js_1.InMemoryOAuthBrokerStore());
|
|
461
|
+
const res = await authorize(restarted, clientId);
|
|
462
|
+
(0, vitest_1.expect)(res.statusCode).toBe(400);
|
|
463
|
+
(0, vitest_1.expect)(res.body.error).toBe('invalid_client');
|
|
464
|
+
});
|
|
465
|
+
});
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { OAuthRefreshConfig } from '../../../config/load-mcp-config.js';
|
|
2
|
+
import type { ClientKv } from '../client-directory.js';
|
|
3
|
+
import type { SqlQueryable } from './postgres.js';
|
|
4
|
+
/** The subset of ioredis this directory uses. `set` must apply `EX`. */
|
|
5
|
+
export interface ClientRedis {
|
|
6
|
+
get(key: string): Promise<string | null>;
|
|
7
|
+
set(key: string, value: string, expirySeconds: number): Promise<unknown>;
|
|
8
|
+
del(key: string): Promise<unknown>;
|
|
9
|
+
}
|
|
10
|
+
/** Distinct from `lightdash-tools:oauth-refresh:`. */
|
|
11
|
+
export declare const OAUTH_CLIENT_REDIS_PREFIX = "lightdash-tools:oauth-client:";
|
|
12
|
+
/** Distinct from `lightdash_tools_oauth_refresh`. */
|
|
13
|
+
export declare const OAUTH_CLIENT_POSTGRES_TABLE = "lightdash_tools_oauth_client";
|
|
14
|
+
/** Sibling of the refresh collection so client documents never share it. */
|
|
15
|
+
export declare function oauthClientFirestoreCollection(refreshCollection: string): string;
|
|
16
|
+
export declare function createRedisClientKv(client: ClientRedis): ClientKv;
|
|
17
|
+
export declare function createPostgresClientKv(db: SqlQueryable): Promise<ClientKv>;
|
|
18
|
+
export interface ClientDocument {
|
|
19
|
+
body: string;
|
|
20
|
+
expiresAtMs: number;
|
|
21
|
+
}
|
|
22
|
+
/** Subset of Firestore used by the client adapter. Tests can supply the same shape. */
|
|
23
|
+
export interface ClientDocumentStore {
|
|
24
|
+
get(id: string): Promise<ClientDocument | undefined>;
|
|
25
|
+
set(id: string, doc: ClientDocument): Promise<void>;
|
|
26
|
+
delete(id: string): Promise<void>;
|
|
27
|
+
}
|
|
28
|
+
export declare function createFirestoreClientKv(store: ClientDocumentStore, now?: () => number): ClientKv;
|
|
29
|
+
export declare function openRedisClientKv(config: Extract<OAuthRefreshConfig, {
|
|
30
|
+
kind: 'redis';
|
|
31
|
+
}>): Promise<{
|
|
32
|
+
kv: ClientKv;
|
|
33
|
+
close: () => Promise<void>;
|
|
34
|
+
}>;
|
|
35
|
+
export declare function openPostgresClientKv(config: Extract<OAuthRefreshConfig, {
|
|
36
|
+
kind: 'postgres';
|
|
37
|
+
}>): Promise<{
|
|
38
|
+
kv: ClientKv;
|
|
39
|
+
close: () => Promise<void>;
|
|
40
|
+
}>;
|
|
41
|
+
export declare function openFirestoreClientKv(config: Extract<OAuthRefreshConfig, {
|
|
42
|
+
kind: 'firestore';
|
|
43
|
+
}>): Promise<{
|
|
44
|
+
kv: ClientKv;
|
|
45
|
+
close: () => Promise<void>;
|
|
46
|
+
}>;
|