@lightdash-tools/mcp 0.6.0 → 0.7.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 +133 -58
- package/dist/audit/audit.d.ts +5 -0
- package/dist/{audit.js → audit/audit.js} +1 -2
- package/dist/audit/tool-audit-context.d.ts +6 -0
- package/dist/audit/tool-audit-context.js +12 -0
- package/dist/audit/tool-audit-context.test.d.ts +1 -0
- package/dist/audit/tool-audit-context.test.js +19 -0
- package/dist/auth/auth-mode.d.ts +4 -0
- package/dist/auth/auth-mode.js +6 -0
- package/dist/auth/bearer.d.ts +3 -0
- package/dist/auth/bearer.js +13 -0
- package/dist/auth/lightdash-oauth-upstream-contract.d.ts +22 -0
- package/dist/auth/lightdash-oauth-upstream-contract.js +36 -0
- package/dist/auth/lightdash-oauth-upstream-contract.test.d.ts +1 -0
- package/dist/auth/lightdash-oauth-upstream-contract.test.js +56 -0
- package/dist/auth/oauth-broker/as-metadata.d.ts +14 -0
- package/dist/auth/oauth-broker/as-metadata.js +20 -0
- package/dist/auth/oauth-broker/lightdash-token.d.ts +20 -0
- package/dist/auth/oauth-broker/lightdash-token.js +76 -0
- package/dist/auth/oauth-broker/pending-store.d.ts +76 -0
- package/dist/auth/oauth-broker/pending-store.js +124 -0
- package/dist/auth/oauth-broker/pkce.d.ts +2 -0
- package/dist/auth/oauth-broker/pkce.js +17 -0
- package/dist/auth/oauth-broker/routes.d.ts +9 -0
- package/dist/auth/oauth-broker/routes.js +447 -0
- package/dist/auth/oauth-broker/routes.test.d.ts +1 -0
- package/dist/auth/oauth-broker/routes.test.js +237 -0
- package/dist/auth/providers/bearer-context-provider.d.ts +24 -0
- package/dist/auth/providers/bearer-context-provider.js +52 -0
- package/dist/auth/providers/env-context-provider.d.ts +13 -0
- package/dist/auth/providers/env-context-provider.js +19 -0
- package/dist/auth/resource-server/lightdash-oauth-middleware.d.ts +23 -0
- package/dist/auth/resource-server/lightdash-oauth-middleware.js +115 -0
- package/dist/auth/resource-server/lightdash-oauth-middleware.test.d.ts +1 -0
- package/dist/auth/resource-server/lightdash-oauth-middleware.test.js +202 -0
- package/dist/auth/resource-server/lightdash-token-validation.d.ts +14 -0
- package/dist/auth/resource-server/lightdash-token-validation.js +82 -0
- package/dist/auth/resource-server/lightdash-token-validation.test.d.ts +1 -0
- package/dist/auth/resource-server/lightdash-token-validation.test.js +177 -0
- package/dist/auth/resource-server/oauth-protected-resource.d.ts +25 -0
- package/dist/auth/resource-server/oauth-protected-resource.js +67 -0
- package/dist/auth/resource-server/shared-key-middleware.d.ts +13 -0
- package/dist/auth/resource-server/shared-key-middleware.js +24 -0
- package/dist/auth/resource-server/token-scopes.d.ts +15 -0
- package/dist/auth/resource-server/token-scopes.js +76 -0
- package/dist/auth/resource-server/token-scopes.test.d.ts +1 -0
- package/dist/auth/resource-server/token-scopes.test.js +51 -0
- package/dist/auth/resource-server/token-validation-cache.d.ts +15 -0
- package/dist/auth/resource-server/token-validation-cache.js +44 -0
- package/dist/auth/resource-server/token-validation-cache.test.d.ts +1 -0
- package/dist/auth/resource-server/token-validation-cache.test.js +41 -0
- package/dist/auth/resource-server/token-validation-error.d.ts +6 -0
- package/dist/auth/resource-server/token-validation-error.js +13 -0
- package/dist/auth/resource-server/www-authenticate.d.ts +8 -0
- package/dist/auth/resource-server/www-authenticate.js +24 -0
- package/dist/auth/resource-server/www-authenticate.test.d.ts +1 -0
- package/dist/auth/resource-server/www-authenticate.test.js +64 -0
- package/dist/auth/token-hash.d.ts +2 -0
- package/dist/auth/token-hash.js +8 -0
- package/dist/bin.d.ts +0 -4
- package/dist/bin.js +41 -33
- package/dist/config/env.d.ts +50 -0
- package/dist/config/env.js +53 -0
- package/dist/config/load-mcp-config.d.ts +30 -0
- package/dist/config/load-mcp-config.js +282 -0
- package/dist/config/load-mcp-config.test.d.ts +1 -0
- package/dist/config/load-mcp-config.test.js +195 -0
- package/dist/config/normalize-url.d.ts +13 -0
- package/dist/config/normalize-url.js +52 -0
- package/dist/config/normalize-url.test.d.ts +1 -0
- package/dist/config/normalize-url.test.js +41 -0
- package/dist/config/public-url.d.ts +3 -0
- package/dist/config/public-url.js +10 -0
- package/dist/config/runtime.d.ts +15 -0
- package/dist/config/runtime.js +48 -0
- package/dist/config/runtime.test.d.ts +1 -0
- package/dist/config/runtime.test.js +69 -0
- package/dist/config/test-mcp-http-config.d.ts +3 -0
- package/dist/config/test-mcp-http-config.js +7 -0
- package/dist/governance/project-pin.d.ts +14 -0
- package/dist/governance/project-pin.js +44 -0
- package/dist/governance/project-pin.test.d.ts +1 -0
- package/dist/governance/project-pin.test.js +35 -0
- package/dist/http.d.ts +1 -2
- package/dist/http.integration.test.d.ts +1 -0
- package/dist/http.integration.test.js +688 -0
- package/dist/http.js +3 -170
- package/dist/http.test.d.ts +4 -0
- package/dist/http.test.js +131 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.integration.test.js +73 -30
- package/dist/index.js +28 -27
- package/dist/index.test.js +3 -3
- package/dist/logging/structured.d.ts +27 -0
- package/dist/logging/structured.js +32 -0
- package/dist/logging/structured.test.d.ts +1 -0
- package/dist/logging/structured.test.js +33 -0
- package/dist/personas/index.d.ts +19 -0
- package/dist/personas/index.js +57 -0
- package/dist/personas/index.test.d.ts +1 -0
- package/dist/personas/index.test.js +58 -0
- package/dist/personas/organization-audit/v1/index.d.ts +8 -0
- package/dist/personas/organization-audit/v1/index.js +42 -0
- package/dist/personas/organization-audit/v1/prompts.d.ts +5 -0
- package/dist/personas/organization-audit/v1/prompts.js +111 -0
- package/dist/personas/organization-audit/v1/prompts.test.d.ts +4 -0
- package/dist/personas/organization-audit/v1/prompts.test.js +25 -0
- package/dist/personas/organization-audit/v1/resources/playbook.d.ts +11 -0
- package/dist/personas/organization-audit/v1/resources/playbook.js +40 -0
- package/dist/personas/organization-audit/v1/resources/playbook.md +52 -0
- package/dist/personas/semantic-layer/v1/index.d.ts +8 -0
- package/dist/personas/semantic-layer/v1/index.js +32 -0
- package/dist/personas/semantic-layer/v1/prompts.d.ts +5 -0
- package/dist/personas/semantic-layer/v1/prompts.js +107 -0
- package/dist/personas/semantic-layer/v1/resources/playbook.d.ts +12 -0
- package/dist/personas/semantic-layer/v1/resources/playbook.js +42 -0
- package/dist/personas/semantic-layer/v1/resources/playbook.md +153 -0
- package/dist/personas/types.d.ts +19 -0
- package/dist/personas/types.js +5 -0
- package/dist/protocol-contract.test.d.ts +1 -0
- package/dist/protocol-contract.test.js +42 -0
- package/dist/server/capabilities.d.ts +15 -0
- package/dist/server/capabilities.js +19 -0
- package/dist/server/capabilities.test.d.ts +1 -0
- package/dist/server/capabilities.test.js +39 -0
- package/dist/server/errors.test.d.ts +1 -0
- package/dist/server/errors.test.js +53 -0
- package/dist/server/request-context.d.ts +13 -0
- package/dist/server/request-context.js +2 -0
- package/dist/server/server.d.ts +4 -0
- package/dist/server/server.js +17 -0
- package/dist/server/server.test.d.ts +1 -0
- package/dist/server/server.test.js +15 -0
- package/dist/server/version.d.ts +1 -0
- package/dist/server/version.js +7 -0
- package/dist/server/version.test.d.ts +1 -0
- package/dist/server/version.test.js +9 -0
- package/dist/stdio.process.test.d.ts +1 -0
- package/dist/stdio.process.test.js +126 -0
- package/dist/tools/index.d.ts +2 -4
- package/dist/tools/index.js +4 -30
- package/dist/tools/index.test.d.ts +1 -0
- package/dist/tools/index.test.js +72 -0
- package/dist/tools/lib/api-errors.d.ts +9 -0
- package/dist/tools/lib/api-errors.js +22 -0
- package/dist/tools/lib/api-shape.d.ts +16 -0
- package/dist/tools/lib/api-shape.js +47 -0
- package/dist/tools/lib/assert-safe.d.ts +13 -0
- package/dist/tools/lib/assert-safe.js +21 -0
- package/dist/tools/lib/assert-safe.test.d.ts +4 -0
- package/dist/tools/lib/assert-safe.test.js +32 -0
- package/dist/tools/lib/concurrency.d.ts +4 -0
- package/dist/tools/lib/concurrency.js +24 -0
- package/dist/tools/lib/contracts.d.ts +34 -0
- package/dist/tools/lib/contracts.js +30 -0
- package/dist/tools/lib/redaction.d.ts +107 -0
- package/dist/tools/lib/redaction.js +299 -0
- package/dist/tools/lib/redaction.test.d.ts +4 -0
- package/dist/tools/lib/redaction.test.js +216 -0
- package/dist/tools/lib/register-org-audit.d.ts +9 -0
- package/dist/tools/lib/register-org-audit.js +20 -0
- package/dist/tools/lib/schema-fields.d.ts +9 -0
- package/dist/tools/lib/schema-fields.js +19 -0
- package/dist/tools/organization/binding.d.ts +14 -0
- package/dist/tools/organization/binding.js +31 -0
- package/dist/tools/organization/invariants.test.d.ts +4 -0
- package/dist/tools/organization/invariants.test.js +28 -0
- package/dist/tools/organization/inventory.d.ts +10 -0
- package/dist/tools/organization/inventory.js +189 -0
- package/dist/tools/organization/roles.d.ts +10 -0
- package/dist/tools/organization/roles.js +213 -0
- package/dist/tools/project/content.d.ts +9 -0
- package/dist/tools/project/content.js +192 -0
- package/dist/tools/project/projects.d.ts +7 -0
- package/dist/tools/project/projects.js +42 -0
- package/dist/tools/project/projects.test.d.ts +1 -0
- package/dist/tools/project/projects.test.js +117 -0
- package/dist/tools/project/schedulers.d.ts +7 -0
- package/dist/tools/project/schedulers.js +115 -0
- package/dist/tools/registry.d.ts +104 -0
- package/dist/tools/registry.js +59 -0
- package/dist/tools/semantic/explore-helpers.d.ts +56 -0
- package/dist/tools/semantic/explore-helpers.js +115 -0
- package/dist/tools/semantic/explore-helpers.test.d.ts +1 -0
- package/dist/tools/semantic/explore-helpers.test.js +170 -0
- package/dist/tools/semantic/explores.d.ts +9 -0
- package/dist/tools/semantic/explores.js +80 -0
- package/dist/tools/semantic/metrics.d.ts +7 -0
- package/dist/tools/semantic/metrics.js +53 -0
- package/dist/tools/semantic/query.d.ts +6 -0
- package/dist/tools/semantic/query.js +42 -0
- package/dist/tools/semantic/schema-fields.d.ts +3 -0
- package/dist/tools/semantic/schema-fields.js +7 -0
- package/dist/tools/shared.d.ts +26 -13
- package/dist/tools/shared.js +112 -157
- package/dist/tools/shared.test.js +132 -183
- package/dist/tools/space/access.d.ts +66 -0
- package/dist/tools/space/access.js +322 -0
- package/dist/tools/space/access.test.d.ts +4 -0
- package/dist/tools/space/access.test.js +90 -0
- package/dist/transports/http-body.d.ts +5 -0
- package/dist/transports/http-body.js +50 -0
- package/dist/transports/http-request-utils.d.ts +2 -0
- package/dist/transports/http-request-utils.js +13 -0
- package/dist/transports/http-response.d.ts +6 -0
- package/dist/transports/http-response.js +56 -0
- package/dist/transports/session-store.d.ts +40 -0
- package/dist/transports/session-store.js +88 -0
- package/dist/transports/session-store.test.d.ts +1 -0
- package/dist/transports/session-store.test.js +111 -0
- package/dist/transports/streamable-http.d.ts +9 -0
- package/dist/transports/streamable-http.js +454 -0
- package/dist/transports/streamable-http.test.d.ts +1 -0
- package/dist/transports/streamable-http.test.js +95 -0
- package/package.json +25 -10
- package/dist/audit.d.ts +0 -6
- package/dist/config.d.ts +0 -47
- package/dist/config.js +0 -82
- package/dist/tools/ai-agents.d.ts +0 -6
- package/dist/tools/ai-agents.js +0 -388
- package/dist/tools/charts.d.ts +0 -6
- package/dist/tools/charts.js +0 -55
- package/dist/tools/content.d.ts +0 -6
- package/dist/tools/content.js +0 -37
- package/dist/tools/dashboards.d.ts +0 -6
- package/dist/tools/dashboards.js +0 -28
- package/dist/tools/explores.d.ts +0 -6
- package/dist/tools/explores.js +0 -65
- package/dist/tools/groups.d.ts +0 -6
- package/dist/tools/groups.js +0 -121
- package/dist/tools/metrics.d.ts +0 -6
- package/dist/tools/metrics.js +0 -45
- package/dist/tools/projects.d.ts +0 -6
- package/dist/tools/projects.js +0 -58
- package/dist/tools/query.d.ts +0 -6
- package/dist/tools/query.js +0 -34
- package/dist/tools/schedulers.d.ts +0 -6
- package/dist/tools/schedulers.js +0 -45
- package/dist/tools/spaces.d.ts +0 -6
- package/dist/tools/spaces.js +0 -128
- package/dist/tools/tags.d.ts +0 -6
- package/dist/tools/tags.js +0 -28
- package/dist/tools/users.d.ts +0 -6
- package/dist/tools/users.js +0 -50
- package/src/audit.ts +0 -6
- package/src/bin.ts +0 -58
- package/src/config.ts +0 -83
- package/src/errors.ts +0 -27
- package/src/http.ts +0 -187
- package/src/index.integration.test.ts +0 -57
- package/src/index.test.ts +0 -8
- package/src/index.ts +0 -31
- package/src/tools/ai-agents.ts +0 -759
- package/src/tools/charts.ts +0 -85
- package/src/tools/content.ts +0 -44
- package/src/tools/dashboards.ts +0 -25
- package/src/tools/explores.ts +0 -98
- package/src/tools/groups.ts +0 -180
- package/src/tools/index.ts +0 -35
- package/src/tools/metrics.ts +0 -42
- package/src/tools/projects.ts +0 -74
- package/src/tools/query.ts +0 -47
- package/src/tools/schedulers.ts +0 -42
- package/src/tools/shared.test.ts +0 -365
- package/src/tools/shared.ts +0 -274
- package/src/tools/spaces.ts +0 -217
- package/src/tools/tags.ts +0 -25
- package/src/tools/users.ts +0 -64
- package/tsconfig.json +0 -13
- /package/dist/{errors.d.ts → server/errors.d.ts} +0 -0
- /package/dist/{errors.js → server/errors.js} +0 -0
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const vitest_1 = require("vitest");
|
|
4
|
+
const test_mcp_http_config_js_1 = require("../../config/test-mcp-http-config.js");
|
|
5
|
+
const index_js_1 = require("../../personas/organization-audit/v1/index.js");
|
|
6
|
+
const index_js_2 = require("../../personas/semantic-layer/v1/index.js");
|
|
7
|
+
const lightdash_oauth_middleware_js_1 = require("./lightdash-oauth-middleware.js");
|
|
8
|
+
const lightdash_token_validation_js_1 = require("./lightdash-token-validation.js");
|
|
9
|
+
const token_validation_error_js_1 = require("./token-validation-error.js");
|
|
10
|
+
vitest_1.vi.mock('./lightdash-token-validation.js', () => ({
|
|
11
|
+
validateLightdashAccessToken: vitest_1.vi.fn(),
|
|
12
|
+
}));
|
|
13
|
+
const baseConfig = (0, test_mcp_http_config_js_1.makeTestMcpHttpConfig)({
|
|
14
|
+
requiredScopes: [],
|
|
15
|
+
});
|
|
16
|
+
function jwtWithScope(scope) {
|
|
17
|
+
const header = Buffer.from(JSON.stringify({ alg: 'none', typ: 'JWT' })).toString('base64url');
|
|
18
|
+
const payload = Buffer.from(JSON.stringify({ scope })).toString('base64url');
|
|
19
|
+
return `${header}.${payload}.signature`;
|
|
20
|
+
}
|
|
21
|
+
function createRequest(authorization) {
|
|
22
|
+
return {
|
|
23
|
+
headers: authorization ? { authorization } : {},
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
(0, vitest_1.describe)('authenticateLightdashOAuth', () => {
|
|
27
|
+
(0, vitest_1.it)('returns 401 with WWW-Authenticate resource_metadata when token is missing', async () => {
|
|
28
|
+
const result = await (0, lightdash_oauth_middleware_js_1.authenticateLightdashOAuth)(createRequest(), baseConfig, index_js_2.SEMANTIC_LAYER_PERSONA_PATH);
|
|
29
|
+
(0, vitest_1.expect)(result.ok).toBe(false);
|
|
30
|
+
if (result.ok)
|
|
31
|
+
return;
|
|
32
|
+
(0, vitest_1.expect)(result.status).toBe(401);
|
|
33
|
+
(0, vitest_1.expect)(result.body).toEqual({
|
|
34
|
+
error: 'invalid_request',
|
|
35
|
+
error_description: 'Bearer access token required',
|
|
36
|
+
});
|
|
37
|
+
(0, vitest_1.expect)(result.wwwAuthenticate).toContain('resource_metadata=');
|
|
38
|
+
(0, vitest_1.expect)(result.wwwAuthenticate).toContain('https://mcp.example.com/.well-known/oauth-protected-resource/semantic-layer/v1/mcp');
|
|
39
|
+
(0, vitest_1.expect)(result.wwwAuthenticate).not.toContain('scope=');
|
|
40
|
+
});
|
|
41
|
+
(0, vitest_1.it)('points resource_metadata at the requested persona path', async () => {
|
|
42
|
+
const result = await (0, lightdash_oauth_middleware_js_1.authenticateLightdashOAuth)(createRequest(), baseConfig, index_js_1.ORGANIZATION_AUDIT_PERSONA_PATH);
|
|
43
|
+
(0, vitest_1.expect)(result.ok).toBe(false);
|
|
44
|
+
if (result.ok)
|
|
45
|
+
return;
|
|
46
|
+
(0, vitest_1.expect)(result.wwwAuthenticate).toContain('https://mcp.example.com/.well-known/oauth-protected-resource/organization-audit/v1/mcp');
|
|
47
|
+
(0, vitest_1.expect)(result.wwwAuthenticate).not.toContain('semantic-layer/v1/mcp');
|
|
48
|
+
});
|
|
49
|
+
(0, vitest_1.it)('returns 401 without echoing the bearer token when validation fails', async () => {
|
|
50
|
+
vitest_1.vi.mocked(lightdash_token_validation_js_1.validateLightdashAccessToken).mockRejectedValue(new token_validation_error_js_1.TokenValidationError('invalid_token', 'Invalid or expired Lightdash access token'));
|
|
51
|
+
const token = 'secret-oauth-token';
|
|
52
|
+
const result = await (0, lightdash_oauth_middleware_js_1.authenticateLightdashOAuth)(createRequest(`Bearer ${token}`), baseConfig, index_js_2.SEMANTIC_LAYER_PERSONA_PATH);
|
|
53
|
+
(0, vitest_1.expect)(result.ok).toBe(false);
|
|
54
|
+
if (result.ok)
|
|
55
|
+
return;
|
|
56
|
+
(0, vitest_1.expect)(result.status).toBe(401);
|
|
57
|
+
(0, vitest_1.expect)(result.body.error).toBe('invalid_token');
|
|
58
|
+
(0, vitest_1.expect)(result.body.error_description).toBe('Invalid or expired Lightdash access token');
|
|
59
|
+
(0, vitest_1.expect)(JSON.stringify(result.body)).not.toContain(token);
|
|
60
|
+
(0, vitest_1.expect)(result.wwwAuthenticate).toContain('error="invalid_token"');
|
|
61
|
+
});
|
|
62
|
+
(0, vitest_1.it)('returns 503 when Lightdash upstream is unavailable', async () => {
|
|
63
|
+
vitest_1.vi.mocked(lightdash_token_validation_js_1.validateLightdashAccessToken).mockRejectedValue(new token_validation_error_js_1.TokenValidationError('upstream_unavailable', 'Lightdash is temporarily unavailable'));
|
|
64
|
+
const result = await (0, lightdash_oauth_middleware_js_1.authenticateLightdashOAuth)(createRequest('Bearer token'), baseConfig, index_js_2.SEMANTIC_LAYER_PERSONA_PATH);
|
|
65
|
+
(0, vitest_1.expect)(result.ok).toBe(false);
|
|
66
|
+
if (result.ok)
|
|
67
|
+
return;
|
|
68
|
+
(0, vitest_1.expect)(result.status).toBe(503);
|
|
69
|
+
(0, vitest_1.expect)(result.body).toEqual({
|
|
70
|
+
error: 'temporarily_unavailable',
|
|
71
|
+
error_description: 'Lightdash is temporarily unavailable',
|
|
72
|
+
});
|
|
73
|
+
(0, vitest_1.expect)(result.wwwAuthenticate).toBeUndefined();
|
|
74
|
+
});
|
|
75
|
+
(0, vitest_1.it)('returns 503 with Retry-After when upstream is rate limited', async () => {
|
|
76
|
+
vitest_1.vi.mocked(lightdash_token_validation_js_1.validateLightdashAccessToken).mockRejectedValue(new token_validation_error_js_1.TokenValidationError('upstream_unavailable', 'Lightdash is temporarily unavailable', 60));
|
|
77
|
+
const result = await (0, lightdash_oauth_middleware_js_1.authenticateLightdashOAuth)(createRequest('Bearer token'), baseConfig, index_js_2.SEMANTIC_LAYER_PERSONA_PATH);
|
|
78
|
+
(0, vitest_1.expect)(result.ok).toBe(false);
|
|
79
|
+
if (result.ok)
|
|
80
|
+
return;
|
|
81
|
+
(0, vitest_1.expect)(result.status).toBe(503);
|
|
82
|
+
(0, vitest_1.expect)(result.headers).toEqual({ 'Retry-After': '60' });
|
|
83
|
+
});
|
|
84
|
+
(0, vitest_1.it)('returns user context when token validation succeeds', async () => {
|
|
85
|
+
vitest_1.vi.mocked(lightdash_token_validation_js_1.validateLightdashAccessToken).mockResolvedValue({
|
|
86
|
+
userUuid: 'user-uuid-1',
|
|
87
|
+
email: 'user@example.com',
|
|
88
|
+
});
|
|
89
|
+
const token = jwtWithScope('mcp:read mcp:write');
|
|
90
|
+
const result = await (0, lightdash_oauth_middleware_js_1.authenticateLightdashOAuth)(createRequest(`Bearer ${token}`), baseConfig, index_js_2.SEMANTIC_LAYER_PERSONA_PATH);
|
|
91
|
+
(0, vitest_1.expect)(result).toEqual({
|
|
92
|
+
ok: true,
|
|
93
|
+
accessToken: token,
|
|
94
|
+
user: { userUuid: 'user-uuid-1', email: 'user@example.com' },
|
|
95
|
+
scopes: ['mcp:read', 'mcp:write'],
|
|
96
|
+
});
|
|
97
|
+
(0, vitest_1.expect)(lightdash_token_validation_js_1.validateLightdashAccessToken).toHaveBeenCalledWith(baseConfig, token);
|
|
98
|
+
});
|
|
99
|
+
(0, vitest_1.it)('accepts opaque tokens when endpoint scope requirements are unset', async () => {
|
|
100
|
+
vitest_1.vi.mocked(lightdash_token_validation_js_1.validateLightdashAccessToken).mockResolvedValue({
|
|
101
|
+
userUuid: 'user-uuid-1',
|
|
102
|
+
email: 'user@example.com',
|
|
103
|
+
});
|
|
104
|
+
const result = await (0, lightdash_oauth_middleware_js_1.authenticateLightdashOAuth)(createRequest('Bearer opaque-token'), baseConfig, index_js_2.SEMANTIC_LAYER_PERSONA_PATH);
|
|
105
|
+
(0, vitest_1.expect)(result).toEqual({
|
|
106
|
+
ok: true,
|
|
107
|
+
accessToken: 'opaque-token',
|
|
108
|
+
user: { userUuid: 'user-uuid-1', email: 'user@example.com' },
|
|
109
|
+
scopes: undefined,
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
(0, vitest_1.it)('accepts JWTs without scope claims when endpoint scope requirements are unset', async () => {
|
|
113
|
+
vitest_1.vi.mocked(lightdash_token_validation_js_1.validateLightdashAccessToken).mockResolvedValue({
|
|
114
|
+
userUuid: 'user-uuid-1',
|
|
115
|
+
email: 'user@example.com',
|
|
116
|
+
});
|
|
117
|
+
const header = Buffer.from(JSON.stringify({ alg: 'none', typ: 'JWT' })).toString('base64url');
|
|
118
|
+
const payload = Buffer.from(JSON.stringify({ sub: 'user-uuid-1' })).toString('base64url');
|
|
119
|
+
const token = `${header}.${payload}.signature`;
|
|
120
|
+
const result = await (0, lightdash_oauth_middleware_js_1.authenticateLightdashOAuth)(createRequest(`Bearer ${token}`), baseConfig, index_js_2.SEMANTIC_LAYER_PERSONA_PATH);
|
|
121
|
+
(0, vitest_1.expect)(result.ok).toBe(true);
|
|
122
|
+
if (!result.ok)
|
|
123
|
+
return;
|
|
124
|
+
(0, vitest_1.expect)(result.scopes).toBeUndefined();
|
|
125
|
+
});
|
|
126
|
+
(0, vitest_1.it)('returns 403 insufficient_scope when required endpoint scopes are configured and missing', async () => {
|
|
127
|
+
vitest_1.vi.mocked(lightdash_token_validation_js_1.validateLightdashAccessToken).mockResolvedValue({
|
|
128
|
+
userUuid: 'user-uuid-1',
|
|
129
|
+
email: 'user@example.com',
|
|
130
|
+
});
|
|
131
|
+
const scopedConfig = Object.assign(Object.assign({}, baseConfig), { requiredScopes: ['mcp:read'] });
|
|
132
|
+
const result = await (0, lightdash_oauth_middleware_js_1.authenticateLightdashOAuth)(createRequest('Bearer opaque-token'), scopedConfig, index_js_2.SEMANTIC_LAYER_PERSONA_PATH);
|
|
133
|
+
(0, vitest_1.expect)(result.ok).toBe(false);
|
|
134
|
+
if (result.ok)
|
|
135
|
+
return;
|
|
136
|
+
(0, vitest_1.expect)(result.status).toBe(403);
|
|
137
|
+
(0, vitest_1.expect)(result.body.error).toBe('insufficient_scope');
|
|
138
|
+
(0, vitest_1.expect)(result.wwwAuthenticate).toContain('scope="mcp:read"');
|
|
139
|
+
});
|
|
140
|
+
(0, vitest_1.it)('returns 403 insufficient_scope when required scopes are missing from token claims', async () => {
|
|
141
|
+
vitest_1.vi.mocked(lightdash_token_validation_js_1.validateLightdashAccessToken).mockResolvedValue({
|
|
142
|
+
userUuid: 'user-uuid-1',
|
|
143
|
+
email: 'user@example.com',
|
|
144
|
+
});
|
|
145
|
+
const header = Buffer.from(JSON.stringify({ alg: 'none', typ: 'JWT' })).toString('base64url');
|
|
146
|
+
const payload = Buffer.from(JSON.stringify({ scope: 'mcp:write' })).toString('base64url');
|
|
147
|
+
const token = `${header}.${payload}.signature`;
|
|
148
|
+
const scopedConfig = Object.assign(Object.assign({}, baseConfig), { requiredScopes: ['mcp:read'] });
|
|
149
|
+
const result = await (0, lightdash_oauth_middleware_js_1.authenticateLightdashOAuth)(createRequest(`Bearer ${token}`), scopedConfig, index_js_2.SEMANTIC_LAYER_PERSONA_PATH);
|
|
150
|
+
(0, vitest_1.expect)(result.ok).toBe(false);
|
|
151
|
+
if (result.ok)
|
|
152
|
+
return;
|
|
153
|
+
(0, vitest_1.expect)(result.status).toBe(403);
|
|
154
|
+
(0, vitest_1.expect)(result.body.error).toBe('insufficient_scope');
|
|
155
|
+
(0, vitest_1.expect)(result.wwwAuthenticate).toContain('error="insufficient_scope"');
|
|
156
|
+
});
|
|
157
|
+
(0, vitest_1.it)('accepts lowercase bearer scheme prefix', async () => {
|
|
158
|
+
vitest_1.vi.mocked(lightdash_token_validation_js_1.validateLightdashAccessToken).mockResolvedValue({
|
|
159
|
+
userUuid: 'user-uuid-1',
|
|
160
|
+
email: 'user@example.com',
|
|
161
|
+
});
|
|
162
|
+
const token = jwtWithScope('mcp:read mcp:write');
|
|
163
|
+
const result = await (0, lightdash_oauth_middleware_js_1.authenticateLightdashOAuth)(createRequest(`bearer ${token}`), baseConfig, index_js_2.SEMANTIC_LAYER_PERSONA_PATH);
|
|
164
|
+
(0, vitest_1.expect)(result.ok).toBe(true);
|
|
165
|
+
if (!result.ok)
|
|
166
|
+
return;
|
|
167
|
+
(0, vitest_1.expect)(result.accessToken).toBe(token);
|
|
168
|
+
});
|
|
169
|
+
});
|
|
170
|
+
(0, vitest_1.describe)('writeOAuthAuthFailure', () => {
|
|
171
|
+
(0, vitest_1.it)('writes JSON body and WWW-Authenticate header', () => {
|
|
172
|
+
var _a;
|
|
173
|
+
const chunks = [];
|
|
174
|
+
const res = {
|
|
175
|
+
writeHead: vitest_1.vi.fn(function (_status) {
|
|
176
|
+
return this;
|
|
177
|
+
}),
|
|
178
|
+
end: vitest_1.vi.fn(function (chunk) {
|
|
179
|
+
if (chunk)
|
|
180
|
+
chunks.push(chunk);
|
|
181
|
+
return this;
|
|
182
|
+
}),
|
|
183
|
+
};
|
|
184
|
+
(0, lightdash_oauth_middleware_js_1.writeOAuthAuthFailure)(res, {
|
|
185
|
+
ok: false,
|
|
186
|
+
status: 401,
|
|
187
|
+
body: {
|
|
188
|
+
error: 'invalid_request',
|
|
189
|
+
error_description: 'Bearer access token required',
|
|
190
|
+
},
|
|
191
|
+
wwwAuthenticate: 'Bearer resource_metadata="https://mcp.example.com/.well-known/oauth-protected-resource"',
|
|
192
|
+
});
|
|
193
|
+
(0, vitest_1.expect)(res.writeHead).toHaveBeenCalledWith(401, {
|
|
194
|
+
'Content-Type': 'application/json',
|
|
195
|
+
'WWW-Authenticate': 'Bearer resource_metadata="https://mcp.example.com/.well-known/oauth-protected-resource"',
|
|
196
|
+
});
|
|
197
|
+
(0, vitest_1.expect)(JSON.parse((_a = chunks[0]) !== null && _a !== void 0 ? _a : '{}')).toEqual({
|
|
198
|
+
error: 'invalid_request',
|
|
199
|
+
error_description: 'Bearer access token required',
|
|
200
|
+
});
|
|
201
|
+
});
|
|
202
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validates a Lightdash OAuth/PAT bearer via `LIGHTDASH_OAUTH_VALIDATION_ENDPOINT`
|
|
3
|
+
* (`/api/v1/user` from `../lightdash-oauth-upstream-contract.js`).
|
|
4
|
+
*
|
|
5
|
+
* Upstream Lightdash accepts OAuth bearer tokens on this route via
|
|
6
|
+
* `allowApiKeyAuthentication` (see `../lightdash-oauth-upstream-contract.ts`).
|
|
7
|
+
*/
|
|
8
|
+
import type { McpHttpConfig } from '../../config/load-mcp-config.js';
|
|
9
|
+
export interface ValidatedLightdashUser {
|
|
10
|
+
userUuid: string;
|
|
11
|
+
email: string;
|
|
12
|
+
organizationUuid?: string;
|
|
13
|
+
}
|
|
14
|
+
export declare function validateLightdashAccessToken(config: McpHttpConfig, accessToken: string): Promise<ValidatedLightdashUser>;
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Validates a Lightdash OAuth/PAT bearer via `LIGHTDASH_OAUTH_VALIDATION_ENDPOINT`
|
|
4
|
+
* (`/api/v1/user` from `../lightdash-oauth-upstream-contract.js`).
|
|
5
|
+
*
|
|
6
|
+
* Upstream Lightdash accepts OAuth bearer tokens on this route via
|
|
7
|
+
* `allowApiKeyAuthentication` (see `../lightdash-oauth-upstream-contract.ts`).
|
|
8
|
+
*/
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.validateLightdashAccessToken = validateLightdashAccessToken;
|
|
11
|
+
const client_1 = require("@lightdash-tools/client");
|
|
12
|
+
const token_hash_js_1 = require("../token-hash.js");
|
|
13
|
+
const token_validation_cache_js_1 = require("./token-validation-cache.js");
|
|
14
|
+
const token_validation_error_js_1 = require("./token-validation-error.js");
|
|
15
|
+
const validationCaches = new WeakMap();
|
|
16
|
+
function getValidationCache(config) {
|
|
17
|
+
let cache = validationCaches.get(config);
|
|
18
|
+
if (!cache) {
|
|
19
|
+
cache = new token_validation_cache_js_1.TokenValidationCache(config.tokenValidationCacheTtlMs);
|
|
20
|
+
validationCaches.set(config, cache);
|
|
21
|
+
}
|
|
22
|
+
return cache;
|
|
23
|
+
}
|
|
24
|
+
function unvalidatedDevUser(tokenHash) {
|
|
25
|
+
return {
|
|
26
|
+
userUuid: `unvalidated:${tokenHash.slice(0, 16)}`,
|
|
27
|
+
email: 'unvalidated@localhost',
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
const UPSTREAM_UNAVAILABLE_MESSAGE = 'Lightdash is temporarily unavailable';
|
|
31
|
+
function classifyValidationError(error) {
|
|
32
|
+
if (error instanceof client_1.RateLimitError ||
|
|
33
|
+
(error instanceof client_1.LightdashApiError && error.statusCode === 429)) {
|
|
34
|
+
const retryAfter = error instanceof client_1.RateLimitError && error.retryAfter !== undefined
|
|
35
|
+
? error.retryAfter
|
|
36
|
+
: undefined;
|
|
37
|
+
return new token_validation_error_js_1.TokenValidationError('upstream_unavailable', UPSTREAM_UNAVAILABLE_MESSAGE, retryAfter);
|
|
38
|
+
}
|
|
39
|
+
if (error instanceof client_1.LightdashApiError) {
|
|
40
|
+
if (error.statusCode === 401 || error.statusCode === 403) {
|
|
41
|
+
console.warn(`Lightdash token validation rejected bearer token (upstream HTTP ${error.statusCode})`);
|
|
42
|
+
return new token_validation_error_js_1.TokenValidationError('invalid_token', 'Invalid or expired Lightdash access token');
|
|
43
|
+
}
|
|
44
|
+
if (error.statusCode >= 500) {
|
|
45
|
+
return new token_validation_error_js_1.TokenValidationError('upstream_unavailable', UPSTREAM_UNAVAILABLE_MESSAGE);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
if (error instanceof client_1.NetworkError) {
|
|
49
|
+
return new token_validation_error_js_1.TokenValidationError('upstream_unavailable', UPSTREAM_UNAVAILABLE_MESSAGE);
|
|
50
|
+
}
|
|
51
|
+
return new token_validation_error_js_1.TokenValidationError('invalid_token', 'Invalid or expired Lightdash access token');
|
|
52
|
+
}
|
|
53
|
+
async function validateLightdashAccessToken(config, accessToken) {
|
|
54
|
+
var _a;
|
|
55
|
+
const tokenHash = (0, token_hash_js_1.hashToken)(accessToken);
|
|
56
|
+
if (!config.validateToken) {
|
|
57
|
+
return unvalidatedDevUser(tokenHash);
|
|
58
|
+
}
|
|
59
|
+
const cache = getValidationCache(config);
|
|
60
|
+
const cached = cache.get(tokenHash);
|
|
61
|
+
if (cached)
|
|
62
|
+
return cached;
|
|
63
|
+
const client = new client_1.LightdashClient(Object.assign(Object.assign({}, (0, client_1.createBearerConfig)({
|
|
64
|
+
baseUrl: config.lightdashUrl,
|
|
65
|
+
accessToken,
|
|
66
|
+
proxyAuthorization: config.proxyAuthorization,
|
|
67
|
+
})), { retry: { maxRetries: 0 }, timeout: 5000 }));
|
|
68
|
+
try {
|
|
69
|
+
const user = await client.v1.users.getAuthenticatedUser();
|
|
70
|
+
const validated = {
|
|
71
|
+
userUuid: user.userUuid,
|
|
72
|
+
email: (_a = user.email) !== null && _a !== void 0 ? _a : '',
|
|
73
|
+
organizationUuid: user.organizationUuid,
|
|
74
|
+
};
|
|
75
|
+
cache.set(tokenHash, validated);
|
|
76
|
+
return validated;
|
|
77
|
+
}
|
|
78
|
+
catch (error) {
|
|
79
|
+
cache.delete(tokenHash);
|
|
80
|
+
throw classifyValidationError(error);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const node_http_1 = require("node:http");
|
|
4
|
+
const vitest_1 = require("vitest");
|
|
5
|
+
const test_mcp_http_config_js_1 = require("../../config/test-mcp-http-config.js");
|
|
6
|
+
const lightdash_token_validation_js_1 = require("./lightdash-token-validation.js");
|
|
7
|
+
function baseConfig(lightdashUrl, overrides) {
|
|
8
|
+
return (0, test_mcp_http_config_js_1.makeTestMcpHttpConfig)(Object.assign({ lightdashUrl }, overrides));
|
|
9
|
+
}
|
|
10
|
+
async function startUserApiServer(handler) {
|
|
11
|
+
const server = (0, node_http_1.createServer)(handler);
|
|
12
|
+
await new Promise((resolve, reject) => {
|
|
13
|
+
server.once('error', reject);
|
|
14
|
+
server.listen(0, '127.0.0.1', () => {
|
|
15
|
+
server.removeListener('error', reject);
|
|
16
|
+
resolve();
|
|
17
|
+
});
|
|
18
|
+
});
|
|
19
|
+
const address = server.address();
|
|
20
|
+
return {
|
|
21
|
+
baseUrl: `http://127.0.0.1:${address.port}`,
|
|
22
|
+
close: () => new Promise((resolve, reject) => {
|
|
23
|
+
server.close((err) => {
|
|
24
|
+
if (err)
|
|
25
|
+
reject(err);
|
|
26
|
+
else
|
|
27
|
+
resolve();
|
|
28
|
+
});
|
|
29
|
+
}),
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
(0, vitest_1.describe)('validateLightdashAccessToken', () => {
|
|
33
|
+
(0, vitest_1.it)('skips upstream validation when validateToken is false', async () => {
|
|
34
|
+
const user = await (0, lightdash_token_validation_js_1.validateLightdashAccessToken)(baseConfig('http://127.0.0.1:1', { validateToken: false }), 'dev-token');
|
|
35
|
+
(0, vitest_1.expect)(user.email).toBe('unvalidated@localhost');
|
|
36
|
+
(0, vitest_1.expect)(user.userUuid).toMatch(/^unvalidated:/);
|
|
37
|
+
});
|
|
38
|
+
(0, vitest_1.it)('validates Lightdash OAuth bearer via Authorization Bearer on GET /api/v1/user', async () => {
|
|
39
|
+
const seenHeaders = [];
|
|
40
|
+
const api = await startUserApiServer((req, res) => {
|
|
41
|
+
var _a, _b;
|
|
42
|
+
const auth = (_a = req.headers.authorization) !== null && _a !== void 0 ? _a : '';
|
|
43
|
+
seenHeaders.push(typeof auth === 'string' ? auth : ((_b = auth[0]) !== null && _b !== void 0 ? _b : ''));
|
|
44
|
+
if (req.method === 'GET' && req.url === '/api/v1/user') {
|
|
45
|
+
res.writeHead(200, { 'Content-Type': 'application/json' }).end(JSON.stringify({
|
|
46
|
+
status: 'ok',
|
|
47
|
+
results: { userUuid: 'u1', email: 'a@b.com', organizationUuid: 'org-1' },
|
|
48
|
+
}));
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
res.writeHead(404).end();
|
|
52
|
+
});
|
|
53
|
+
try {
|
|
54
|
+
const user = await (0, lightdash_token_validation_js_1.validateLightdashAccessToken)(baseConfig(api.baseUrl), 'oauth-access-token');
|
|
55
|
+
(0, vitest_1.expect)(user.userUuid).toBe('u1');
|
|
56
|
+
(0, vitest_1.expect)(seenHeaders).toContain('Bearer oauth-access-token');
|
|
57
|
+
}
|
|
58
|
+
finally {
|
|
59
|
+
await api.close();
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
(0, vitest_1.it)('returns validated user including organizationUuid when upstream provides it', async () => {
|
|
63
|
+
const api = await startUserApiServer((req, res) => {
|
|
64
|
+
if (req.method === 'GET' && req.url === '/api/v1/user') {
|
|
65
|
+
res.writeHead(200, { 'Content-Type': 'application/json' }).end(JSON.stringify({
|
|
66
|
+
status: 'ok',
|
|
67
|
+
results: { userUuid: 'u1', email: 'a@b.com', organizationUuid: 'org-1' },
|
|
68
|
+
}));
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
res.writeHead(404).end();
|
|
72
|
+
});
|
|
73
|
+
try {
|
|
74
|
+
const user = await (0, lightdash_token_validation_js_1.validateLightdashAccessToken)(baseConfig(api.baseUrl), 'good-token');
|
|
75
|
+
(0, vitest_1.expect)(user).toEqual({
|
|
76
|
+
userUuid: 'u1',
|
|
77
|
+
email: 'a@b.com',
|
|
78
|
+
organizationUuid: 'org-1',
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
finally {
|
|
82
|
+
await api.close();
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
(0, vitest_1.it)('returns validated user without organizationUuid when upstream omits it', async () => {
|
|
86
|
+
const api = await startUserApiServer((req, res) => {
|
|
87
|
+
if (req.method === 'GET' && req.url === '/api/v1/user') {
|
|
88
|
+
res.writeHead(200, { 'Content-Type': 'application/json' }).end(JSON.stringify({
|
|
89
|
+
status: 'ok',
|
|
90
|
+
results: { userUuid: 'u1', email: 'a@b.com' },
|
|
91
|
+
}));
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
res.writeHead(404).end();
|
|
95
|
+
});
|
|
96
|
+
try {
|
|
97
|
+
const user = await (0, lightdash_token_validation_js_1.validateLightdashAccessToken)(baseConfig(api.baseUrl), 'good-token');
|
|
98
|
+
(0, vitest_1.expect)(user.organizationUuid).toBeUndefined();
|
|
99
|
+
}
|
|
100
|
+
finally {
|
|
101
|
+
await api.close();
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
(0, vitest_1.it)('throws invalid_token for upstream 403 and logs sanitized status', async () => {
|
|
105
|
+
const warnSpy = vitest_1.vi.spyOn(console, 'warn').mockImplementation(() => undefined);
|
|
106
|
+
const api = await startUserApiServer((_req, res) => {
|
|
107
|
+
res.writeHead(403, { 'Content-Type': 'application/json' }).end(JSON.stringify({
|
|
108
|
+
status: 'error',
|
|
109
|
+
error: { statusCode: 403, name: 'Forbidden', message: 'Forbidden' },
|
|
110
|
+
}));
|
|
111
|
+
});
|
|
112
|
+
try {
|
|
113
|
+
await (0, vitest_1.expect)((0, lightdash_token_validation_js_1.validateLightdashAccessToken)(baseConfig(api.baseUrl), 'forbidden-token')).rejects.toMatchObject({
|
|
114
|
+
reason: 'invalid_token',
|
|
115
|
+
});
|
|
116
|
+
(0, vitest_1.expect)(warnSpy).toHaveBeenCalledWith('Lightdash token validation rejected bearer token (upstream HTTP 403)');
|
|
117
|
+
}
|
|
118
|
+
finally {
|
|
119
|
+
await api.close();
|
|
120
|
+
warnSpy.mockRestore();
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
(0, vitest_1.it)('throws invalid_token for upstream 401', async () => {
|
|
124
|
+
const api = await startUserApiServer((_req, res) => {
|
|
125
|
+
res.writeHead(401, { 'Content-Type': 'application/json' }).end(JSON.stringify({
|
|
126
|
+
status: 'error',
|
|
127
|
+
error: { statusCode: 401, name: 'Unauthorized', message: 'Invalid token' },
|
|
128
|
+
}));
|
|
129
|
+
});
|
|
130
|
+
try {
|
|
131
|
+
await (0, vitest_1.expect)((0, lightdash_token_validation_js_1.validateLightdashAccessToken)(baseConfig(api.baseUrl), 'bad-token')).rejects.toMatchObject({
|
|
132
|
+
reason: 'invalid_token',
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
finally {
|
|
136
|
+
await api.close();
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
(0, vitest_1.it)('throws upstream_unavailable for upstream 503', async () => {
|
|
140
|
+
const api = await startUserApiServer((_req, res) => {
|
|
141
|
+
res.writeHead(503, { 'Content-Type': 'application/json' }).end(JSON.stringify({
|
|
142
|
+
status: 'error',
|
|
143
|
+
error: { statusCode: 503, name: 'ServiceUnavailable', message: 'Down' },
|
|
144
|
+
}));
|
|
145
|
+
});
|
|
146
|
+
try {
|
|
147
|
+
await (0, vitest_1.expect)((0, lightdash_token_validation_js_1.validateLightdashAccessToken)(baseConfig(api.baseUrl), 'any-token')).rejects.toMatchObject({
|
|
148
|
+
reason: 'upstream_unavailable',
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
finally {
|
|
152
|
+
await api.close();
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
(0, vitest_1.it)('throws upstream_unavailable for upstream 429 rate limits', async () => {
|
|
156
|
+
const api = await startUserApiServer((_req, res) => {
|
|
157
|
+
res
|
|
158
|
+
.writeHead(429, {
|
|
159
|
+
'Content-Type': 'application/json',
|
|
160
|
+
'Retry-After': '30',
|
|
161
|
+
})
|
|
162
|
+
.end(JSON.stringify({
|
|
163
|
+
status: 'error',
|
|
164
|
+
error: { statusCode: 429, name: 'TooManyRequests', message: 'Slow down' },
|
|
165
|
+
}));
|
|
166
|
+
});
|
|
167
|
+
try {
|
|
168
|
+
await (0, vitest_1.expect)((0, lightdash_token_validation_js_1.validateLightdashAccessToken)(baseConfig(api.baseUrl), 'any-token')).rejects.toMatchObject({
|
|
169
|
+
reason: 'upstream_unavailable',
|
|
170
|
+
retryAfterSeconds: 30,
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
finally {
|
|
174
|
+
await api.close();
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
});
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { McpHttpConfig } from '../../config/load-mcp-config.js';
|
|
2
|
+
/** Well-known OAuth protected-resource metadata root path. */
|
|
3
|
+
export declare const OAUTH_PROTECTED_RESOURCE_ROOT = "/.well-known/oauth-protected-resource";
|
|
4
|
+
export interface OAuthProtectedResourceMetadata {
|
|
5
|
+
resource: string;
|
|
6
|
+
authorization_servers: string[];
|
|
7
|
+
bearer_methods_supported: string[];
|
|
8
|
+
scopes_supported: string[];
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Builds MCP OAuth protected-resource metadata for a persona MCP path.
|
|
12
|
+
* Callers must pass an explicit persona path (root PRM uses `config.mcpPath`).
|
|
13
|
+
*/
|
|
14
|
+
export declare function buildOAuthProtectedResourceMetadata(config: McpHttpConfig, mcpPath: string): OAuthProtectedResourceMetadata;
|
|
15
|
+
export declare function getProtectedResourceMetadataUrl(config: McpHttpConfig): string;
|
|
16
|
+
/** Path-specific PRM URL for a persona MCP endpoint. */
|
|
17
|
+
export declare function getProtectedResourceMetadataPathUrl(config: McpHttpConfig, mcpPath: string): string;
|
|
18
|
+
/**
|
|
19
|
+
* Resolves the persona MCP path for a PRM well-known request.
|
|
20
|
+
* Root PRM maps to the default persona (`config.mcpPath`); path-specific PRM
|
|
21
|
+
* is served only for shipped persona paths.
|
|
22
|
+
*/
|
|
23
|
+
export declare function resolveProtectedResourceMcpPath(path: string, config: McpHttpConfig): string | undefined;
|
|
24
|
+
/** Well-known OAuth Authorization Server Metadata URL for an AS origin. */
|
|
25
|
+
export declare function getAuthorizationServerMetadataUrl(authorizationServerOrigin: string): string;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.OAUTH_PROTECTED_RESOURCE_ROOT = void 0;
|
|
4
|
+
exports.buildOAuthProtectedResourceMetadata = buildOAuthProtectedResourceMetadata;
|
|
5
|
+
exports.getProtectedResourceMetadataUrl = getProtectedResourceMetadataUrl;
|
|
6
|
+
exports.getProtectedResourceMetadataPathUrl = getProtectedResourceMetadataPathUrl;
|
|
7
|
+
exports.resolveProtectedResourceMcpPath = resolveProtectedResourceMcpPath;
|
|
8
|
+
exports.getAuthorizationServerMetadataUrl = getAuthorizationServerMetadataUrl;
|
|
9
|
+
const env_js_1 = require("../../config/env.js");
|
|
10
|
+
const normalize_url_js_1 = require("../../config/normalize-url.js");
|
|
11
|
+
const public_url_js_1 = require("../../config/public-url.js");
|
|
12
|
+
const index_js_1 = require("../../personas/index.js");
|
|
13
|
+
const OAUTH_PROTECTED_RESOURCE_CONTEXT = 'OAuth protected resource metadata';
|
|
14
|
+
/** Well-known OAuth protected-resource metadata root path. */
|
|
15
|
+
exports.OAUTH_PROTECTED_RESOURCE_ROOT = '/.well-known/oauth-protected-resource';
|
|
16
|
+
/**
|
|
17
|
+
* Builds MCP OAuth protected-resource metadata for a persona MCP path.
|
|
18
|
+
* Callers must pass an explicit persona path (root PRM uses `config.mcpPath`).
|
|
19
|
+
*/
|
|
20
|
+
function buildOAuthProtectedResourceMetadata(config, mcpPath) {
|
|
21
|
+
const publicUrl = (0, public_url_js_1.requirePublicUrl)(config, OAUTH_PROTECTED_RESOURCE_CONTEXT);
|
|
22
|
+
const path = (0, normalize_url_js_1.normalizeMcpPath)(mcpPath);
|
|
23
|
+
// Broker mode: authorization_servers is the MCP host (PUBLIC_URL). Clients discover
|
|
24
|
+
// AS metadata at {PUBLIC_URL}/.well-known/oauth-authorization-server and never need
|
|
25
|
+
// the Lightdash client secret. Identity validation remains GET /api/v1/user until
|
|
26
|
+
// upstream tokens are resource-bound.
|
|
27
|
+
return {
|
|
28
|
+
resource: `${publicUrl}${path}`,
|
|
29
|
+
authorization_servers: [publicUrl],
|
|
30
|
+
bearer_methods_supported: ['header'],
|
|
31
|
+
scopes_supported: config.scopesSupported,
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
function getProtectedResourceMetadataUrl(config) {
|
|
35
|
+
return `${(0, public_url_js_1.requirePublicUrl)(config, OAUTH_PROTECTED_RESOURCE_CONTEXT)}${exports.OAUTH_PROTECTED_RESOURCE_ROOT}`;
|
|
36
|
+
}
|
|
37
|
+
/** Path-specific PRM URL for a persona MCP endpoint. */
|
|
38
|
+
function getProtectedResourceMetadataPathUrl(config, mcpPath) {
|
|
39
|
+
const publicUrl = (0, public_url_js_1.requirePublicUrl)(config, OAUTH_PROTECTED_RESOURCE_CONTEXT);
|
|
40
|
+
const resourcePath = (0, normalize_url_js_1.normalizeMcpPath)(mcpPath).replace(/^\//, '');
|
|
41
|
+
return `${publicUrl}${exports.OAUTH_PROTECTED_RESOURCE_ROOT}/${resourcePath}`;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Resolves the persona MCP path for a PRM well-known request.
|
|
45
|
+
* Root PRM maps to the default persona (`config.mcpPath`); path-specific PRM
|
|
46
|
+
* is served only for shipped persona paths.
|
|
47
|
+
*/
|
|
48
|
+
function resolveProtectedResourceMcpPath(path, config) {
|
|
49
|
+
var _a;
|
|
50
|
+
if (!path.startsWith(exports.OAUTH_PROTECTED_RESOURCE_ROOT)) {
|
|
51
|
+
return undefined;
|
|
52
|
+
}
|
|
53
|
+
if (path === exports.OAUTH_PROTECTED_RESOURCE_ROOT) {
|
|
54
|
+
return config.mcpPath;
|
|
55
|
+
}
|
|
56
|
+
const prefix = `${exports.OAUTH_PROTECTED_RESOURCE_ROOT}/`;
|
|
57
|
+
if (!path.startsWith(prefix)) {
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
const resourcePath = `/${path.slice(prefix.length)}`;
|
|
61
|
+
return (_a = (0, index_js_1.getPersonaByPath)(resourcePath)) === null || _a === void 0 ? void 0 : _a.path;
|
|
62
|
+
}
|
|
63
|
+
/** Well-known OAuth Authorization Server Metadata URL for an AS origin. */
|
|
64
|
+
function getAuthorizationServerMetadataUrl(authorizationServerOrigin) {
|
|
65
|
+
const base = authorizationServerOrigin.replace(/\/$/, '');
|
|
66
|
+
return `${base}${env_js_1.OAUTH_AUTHORIZATION_SERVER_METADATA_PATH}`;
|
|
67
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { McpHttpConfig } from '../../config/load-mcp-config.js';
|
|
2
|
+
import type { IncomingMessage } from 'node:http';
|
|
3
|
+
export interface SharedKeyAuthSuccess {
|
|
4
|
+
ok: true;
|
|
5
|
+
}
|
|
6
|
+
export interface SharedKeyAuthFailure {
|
|
7
|
+
ok: false;
|
|
8
|
+
status: number;
|
|
9
|
+
body: Record<string, string>;
|
|
10
|
+
}
|
|
11
|
+
export type SharedKeyAuthResult = SharedKeyAuthFailure | SharedKeyAuthSuccess;
|
|
12
|
+
/** Validates MCP endpoint shared key from Bearer or X-API-Key header. */
|
|
13
|
+
export declare function authenticateSharedKey(req: IncomingMessage, config: McpHttpConfig): SharedKeyAuthResult;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.authenticateSharedKey = authenticateSharedKey;
|
|
4
|
+
const http_response_js_1 = require("../../transports/http-response.js");
|
|
5
|
+
const bearer_js_1 = require("../bearer.js");
|
|
6
|
+
/** Validates MCP endpoint shared key from Bearer or X-API-Key header. */
|
|
7
|
+
function authenticateSharedKey(req, config) {
|
|
8
|
+
var _a, _b;
|
|
9
|
+
const expected = (_a = config.sharedKey) === null || _a === void 0 ? void 0 : _a.expose();
|
|
10
|
+
if (!expected) {
|
|
11
|
+
return {
|
|
12
|
+
ok: false,
|
|
13
|
+
status: 500,
|
|
14
|
+
body: { error: 'Server auth misconfiguration: shared key is missing' },
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
const apiKey = req.headers['x-api-key'];
|
|
18
|
+
const key = typeof apiKey === 'string' ? apiKey.trim() : undefined;
|
|
19
|
+
const provided = (_b = (0, bearer_js_1.extractBearerToken)(req)) !== null && _b !== void 0 ? _b : key;
|
|
20
|
+
if (!provided || !(0, http_response_js_1.timingSafeEqualString)(provided, expected)) {
|
|
21
|
+
return { ok: false, status: 401, body: { error: 'Unauthorized' } };
|
|
22
|
+
}
|
|
23
|
+
return { ok: true };
|
|
24
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export interface ExtractTokenScopesOptions {
|
|
2
|
+
/** Dev-only escape hatch: grant all supported scopes when claims are missing. */
|
|
3
|
+
grantAllWhenUnknown?: boolean;
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* Extracts OAuth scopes from JWT claims when present.
|
|
7
|
+
*
|
|
8
|
+
* For `lightdash-oauth`, Lightdash access tokens are typically opaque and this returns
|
|
9
|
+
* `undefined` — MCP-local scope enforcement is skipped and authorization relies on the persona
|
|
10
|
+
* tool surface plus Lightdash RBAC (ADR-0006 / ADR-0008). Do not treat decoded JWT scopes as
|
|
11
|
+
* validated Lightdash OAuth authorization unless Lightdash formally issues resource-bound JWT
|
|
12
|
+
* access tokens.
|
|
13
|
+
*/
|
|
14
|
+
export declare function extractTokenScopes(accessToken: string, scopesSupported: readonly string[], options?: ExtractTokenScopesOptions): string[] | undefined;
|
|
15
|
+
export declare function hasRequiredScopes(grantedScopes: readonly string[], requiredScopes: readonly string[]): boolean;
|