lumoauth 1.2.0 → 1.3.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 +67 -1
- package/dist/cli.js +330 -52
- package/dist/cli.js.map +1 -1
- package/dist/index.d.mts +51 -2
- package/dist/index.d.ts +51 -2
- package/dist/index.js +315 -33
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +320 -31
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/cli.js
CHANGED
|
@@ -3,12 +3,10 @@
|
|
|
3
3
|
|
|
4
4
|
// src/cli.ts
|
|
5
5
|
var import_node_util = require("util");
|
|
6
|
-
var
|
|
6
|
+
var import_mcp3 = require("@lumoauth/mcp");
|
|
7
7
|
|
|
8
|
-
// src/
|
|
8
|
+
// src/ext-authz.ts
|
|
9
9
|
var import_node_http = require("http");
|
|
10
|
-
var import_node_stream = require("stream");
|
|
11
|
-
var import_promises = require("stream/promises");
|
|
12
10
|
var import_mcp = require("@lumoauth/mcp");
|
|
13
11
|
|
|
14
12
|
// src/jsonrpc.ts
|
|
@@ -26,9 +24,13 @@ function inspect(body) {
|
|
|
26
24
|
result.isBatch = Array.isArray(parsed);
|
|
27
25
|
const messages = Array.isArray(parsed) ? parsed : [parsed];
|
|
28
26
|
for (const message of messages) {
|
|
29
|
-
if (typeof message !== "object" || message === null
|
|
27
|
+
if (typeof message !== "object" || message === null) continue;
|
|
28
|
+
const params = message.params;
|
|
29
|
+
if (!result.traceparent && typeof params?._meta?.traceparent === "string") {
|
|
30
|
+
result.traceparent = params._meta.traceparent;
|
|
31
|
+
}
|
|
32
|
+
if (message.id === void 0) continue;
|
|
30
33
|
if (message.method === "tools/call") {
|
|
31
|
-
const params = message.params;
|
|
32
34
|
result.toolCalls.push({ id: asId(message.id), name: typeof params?.name === "string" ? params.name : "" });
|
|
33
35
|
} else if (message.method === "tools/list") {
|
|
34
36
|
result.toolListIds.add(JSON.stringify(message.id));
|
|
@@ -39,6 +41,28 @@ function inspect(body) {
|
|
|
39
41
|
function refusedToolResult(id, message) {
|
|
40
42
|
return { jsonrpc: "2.0", id, result: { content: [{ type: "text", text: message }], isError: true } };
|
|
41
43
|
}
|
|
44
|
+
function refusedJitToolResult(id, tool, requiredDetails) {
|
|
45
|
+
const hintJson = JSON.stringify(requiredDetails);
|
|
46
|
+
const headerValue = Buffer.from(hintJson, "utf8").toString("base64");
|
|
47
|
+
const types = requiredDetails.map((d) => d.type).join(", ");
|
|
48
|
+
const message = `Tool '${tool}' requires JIT authorization_details (${types}); authorization_details_hint=${hintJson}`;
|
|
49
|
+
return {
|
|
50
|
+
headerValue,
|
|
51
|
+
body: {
|
|
52
|
+
jsonrpc: "2.0",
|
|
53
|
+
id,
|
|
54
|
+
result: {
|
|
55
|
+
content: [{ type: "text", text: message }],
|
|
56
|
+
isError: true,
|
|
57
|
+
_meta: {
|
|
58
|
+
error: "insufficient_authorization_details",
|
|
59
|
+
authorization_details_hint: requiredDetails,
|
|
60
|
+
"Insufficient-Authorization-Details": headerValue
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
}
|
|
42
66
|
async function filterMessage(message, ids, decide) {
|
|
43
67
|
if (typeof message !== "object" || message === null) return message;
|
|
44
68
|
const response = message;
|
|
@@ -74,7 +98,141 @@ async function filterSseEvent(event, ids, decide) {
|
|
|
74
98
|
return [...others, `data: ${JSON.stringify(filtered)}`].join("\n");
|
|
75
99
|
}
|
|
76
100
|
|
|
101
|
+
// src/ext-authz.ts
|
|
102
|
+
function headerOf(req, name) {
|
|
103
|
+
const value = req.headers[name];
|
|
104
|
+
return Array.isArray(value) ? value[0] : value;
|
|
105
|
+
}
|
|
106
|
+
async function readBody(req, limit) {
|
|
107
|
+
const chunks = [];
|
|
108
|
+
let size = 0;
|
|
109
|
+
for await (const chunk of req) {
|
|
110
|
+
size += chunk.length;
|
|
111
|
+
if (size > limit) break;
|
|
112
|
+
chunks.push(chunk);
|
|
113
|
+
}
|
|
114
|
+
return Buffer.concat(chunks);
|
|
115
|
+
}
|
|
116
|
+
function writeJson(res, status, body, headers = {}) {
|
|
117
|
+
const text = JSON.stringify(body);
|
|
118
|
+
res.statusCode = status;
|
|
119
|
+
for (const [k, v] of Object.entries(headers)) res.setHeader(k, v);
|
|
120
|
+
res.setHeader("Content-Type", "application/json");
|
|
121
|
+
res.setHeader("Content-Length", Buffer.byteLength(text));
|
|
122
|
+
res.end(text);
|
|
123
|
+
}
|
|
124
|
+
function createExtAuthzServer(options) {
|
|
125
|
+
const { auth } = options;
|
|
126
|
+
const log2 = options.log ?? ((line) => console.error(line));
|
|
127
|
+
const identityHeaders = options.identityHeaders ?? true;
|
|
128
|
+
const publicPaths = options.publicPaths ?? ["/healthz", "/readyz"];
|
|
129
|
+
const maxBody = options.maxBodyBytes ?? 1024 * 1024;
|
|
130
|
+
async function handle(req, res) {
|
|
131
|
+
const method = headerOf(req, "x-envoy-original-method") ?? req.method ?? "GET";
|
|
132
|
+
const rawPath = headerOf(req, "x-envoy-original-path") ?? headerOf(req, "x-forwarded-uri") ?? req.url ?? "/";
|
|
133
|
+
const url = new URL(rawPath, "http://ext-authz.invalid");
|
|
134
|
+
if (publicPaths.includes(url.pathname)) {
|
|
135
|
+
res.statusCode = 200;
|
|
136
|
+
res.end();
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
const discovery = await auth.discoveryResponse(method, url.pathname);
|
|
140
|
+
if (discovery) {
|
|
141
|
+
res.statusCode = discovery.status;
|
|
142
|
+
for (const [k, v] of Object.entries(discovery.headers)) res.setHeader(k, v);
|
|
143
|
+
res.end(discovery.body);
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
let info;
|
|
147
|
+
try {
|
|
148
|
+
info = await auth.authenticate({ method, url: rawPath, headers: req.headers });
|
|
149
|
+
} catch (error) {
|
|
150
|
+
if (error instanceof import_mcp.LumoAuthChallenge) {
|
|
151
|
+
(0, import_mcp.writeChallenge)(res, error);
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
throw error;
|
|
155
|
+
}
|
|
156
|
+
const caller = info.extra.caller;
|
|
157
|
+
const body = method === "POST" ? await readBody(req, maxBody) : void 0;
|
|
158
|
+
if (body && body.length > 0 && (options.toolPolicy || options.jit)) {
|
|
159
|
+
const inspection = inspect(body.toString("utf8"));
|
|
160
|
+
if (!caller.traceparent && inspection.traceparent) {
|
|
161
|
+
const parsedTp = (0, import_mcp.parseTraceparent)(inspection.traceparent);
|
|
162
|
+
if (parsedTp) {
|
|
163
|
+
caller.traceparent = parsedTp.traceparent;
|
|
164
|
+
caller.traceId ?? (caller.traceId = parsedTp.traceId);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
if (options.toolPolicy) {
|
|
168
|
+
for (const call of inspection.toolCalls) {
|
|
169
|
+
if (!await auth.toolPolicy().isAllowed(caller, call.name)) {
|
|
170
|
+
log2(`lumoauth ext-authz: refused tool '${call.name}' for ${caller.sub}`);
|
|
171
|
+
writeJson(
|
|
172
|
+
res,
|
|
173
|
+
403,
|
|
174
|
+
refusedToolResult(call.id, `LumoAuth policy does not allow this caller to run '${call.name}'.`),
|
|
175
|
+
{ "X-LumoAuth-Denied-Tool": call.name }
|
|
176
|
+
);
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
if (options.jit) {
|
|
182
|
+
for (const call of inspection.toolCalls) {
|
|
183
|
+
const jitSpec = options.jit[call.name];
|
|
184
|
+
if (!jitSpec) continue;
|
|
185
|
+
const required = (0, import_mcp.normalizeAuthorizationDetails)(jitSpec);
|
|
186
|
+
const missing = (0, import_mcp.missingAuthorizationDetails)(required, caller.authorizationDetails);
|
|
187
|
+
if (missing.length > 0) {
|
|
188
|
+
const jitRefusal = refusedJitToolResult(call.id, call.name, missing);
|
|
189
|
+
writeJson(res, 403, jitRefusal.body, {
|
|
190
|
+
"Insufficient-Authorization-Details": jitRefusal.headerValue,
|
|
191
|
+
"WWW-Authenticate": `Bearer error="insufficient_authorization_details", error_description="JIT step-up required for tool '${call.name}'"`
|
|
192
|
+
});
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
res.statusCode = 200;
|
|
199
|
+
if (identityHeaders) {
|
|
200
|
+
res.setHeader("x-lumoauth-subject", caller.sub);
|
|
201
|
+
res.setHeader("x-lumoauth-subject-type", caller.isAgent ? "agent" : "user");
|
|
202
|
+
if (caller.agentId) res.setHeader("x-lumoauth-agent-id", caller.agentId);
|
|
203
|
+
if (caller.actor) res.setHeader("x-lumoauth-actor", caller.actor);
|
|
204
|
+
if (caller.clientId) res.setHeader("x-lumoauth-client-id", caller.clientId);
|
|
205
|
+
res.setHeader("x-lumoauth-scopes", caller.scopes.join(" "));
|
|
206
|
+
if (caller.traceId) res.setHeader("x-lumoauth-trace-id", caller.traceId);
|
|
207
|
+
if (caller.isJit) res.setHeader("x-lumoauth-is-jit", "true");
|
|
208
|
+
}
|
|
209
|
+
if (caller.traceparent) {
|
|
210
|
+
res.setHeader("traceparent", caller.traceparent);
|
|
211
|
+
}
|
|
212
|
+
if (options.vaultConnections) {
|
|
213
|
+
for (const [slug, connectionId] of Object.entries(options.vaultConnections)) {
|
|
214
|
+
const cleanSlug = slug.trim().toLowerCase().replace(/[^a-z0-9-]/g, "-");
|
|
215
|
+
if (!cleanSlug || !connectionId) continue;
|
|
216
|
+
const vaultToken = await auth.vaultToken(connectionId, caller);
|
|
217
|
+
res.setHeader(`x-lumoauth-vault-${cleanSlug}-token`, vaultToken.accessToken);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
res.end();
|
|
221
|
+
}
|
|
222
|
+
return (0, import_node_http.createServer)((req, res) => {
|
|
223
|
+
handle(req, res).catch((error) => {
|
|
224
|
+
log2(`lumoauth ext-authz: ${error.stack ?? error}`);
|
|
225
|
+
if (!res.headersSent) writeJson(res, 500, { error: "server_error" });
|
|
226
|
+
else res.destroy();
|
|
227
|
+
});
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
|
|
77
231
|
// src/proxy.ts
|
|
232
|
+
var import_node_http2 = require("http");
|
|
233
|
+
var import_node_stream = require("stream");
|
|
234
|
+
var import_promises = require("stream/promises");
|
|
235
|
+
var import_mcp2 = require("@lumoauth/mcp");
|
|
78
236
|
var HOP_BY_HOP = ["connection", "keep-alive", "proxy-authenticate", "proxy-authorization", "te", "trailer", "transfer-encoding", "upgrade"];
|
|
79
237
|
var REQUEST_DROP = /* @__PURE__ */ new Set([...HOP_BY_HOP, "host", "content-length", "authorization", "dpop", "accept-encoding"]);
|
|
80
238
|
var RESPONSE_DROP = /* @__PURE__ */ new Set([...HOP_BY_HOP, "content-length", "content-encoding", "set-cookie"]);
|
|
@@ -106,7 +264,7 @@ var SessionOwners = class {
|
|
|
106
264
|
function principal(caller) {
|
|
107
265
|
return JSON.stringify([caller.issuer, caller.sub, caller.clientId ?? null]);
|
|
108
266
|
}
|
|
109
|
-
function
|
|
267
|
+
function headerOf2(req, name) {
|
|
110
268
|
const value = req.headers[name];
|
|
111
269
|
return Array.isArray(value) ? value[0] : value;
|
|
112
270
|
}
|
|
@@ -115,7 +273,7 @@ function writeSimple(res, response) {
|
|
|
115
273
|
for (const [name, value] of Object.entries(response.headers)) res.setHeader(name, value);
|
|
116
274
|
res.end(response.body);
|
|
117
275
|
}
|
|
118
|
-
function
|
|
276
|
+
function writeJson2(res, status, body) {
|
|
119
277
|
const text = JSON.stringify(body);
|
|
120
278
|
res.statusCode = status;
|
|
121
279
|
res.setHeader("Content-Type", "application/json");
|
|
@@ -124,7 +282,7 @@ function writeJson(res, status, body) {
|
|
|
124
282
|
}
|
|
125
283
|
var BodyTooLargeError = class extends Error {
|
|
126
284
|
};
|
|
127
|
-
async function
|
|
285
|
+
async function readBody2(req, limit) {
|
|
128
286
|
const chunks = [];
|
|
129
287
|
let size = 0;
|
|
130
288
|
for await (const chunk of req) {
|
|
@@ -184,7 +342,7 @@ function createSecureProxy(options) {
|
|
|
184
342
|
headers.set("accept-encoding", "identity");
|
|
185
343
|
if (req.headers.host) headers.set("x-forwarded-host", req.headers.host);
|
|
186
344
|
const remote = req.socket.remoteAddress;
|
|
187
|
-
if (remote) headers.set("x-forwarded-for", [
|
|
345
|
+
if (remote) headers.set("x-forwarded-for", [headerOf2(req, "x-forwarded-for"), remote].filter(Boolean).join(", "));
|
|
188
346
|
if (caller && options.identityHeaders) {
|
|
189
347
|
headers.set("x-lumoauth-subject", caller.sub);
|
|
190
348
|
headers.set("x-lumoauth-subject-type", caller.isAgent ? "agent" : "user");
|
|
@@ -192,6 +350,19 @@ function createSecureProxy(options) {
|
|
|
192
350
|
if (caller.actor) headers.set("x-lumoauth-actor", caller.actor);
|
|
193
351
|
if (caller.clientId) headers.set("x-lumoauth-client-id", caller.clientId);
|
|
194
352
|
headers.set("x-lumoauth-scopes", caller.scopes.join(" "));
|
|
353
|
+
if (caller.traceId) headers.set("x-lumoauth-trace-id", caller.traceId);
|
|
354
|
+
if (caller.isJit) headers.set("x-lumoauth-is-jit", "true");
|
|
355
|
+
}
|
|
356
|
+
if (caller?.traceparent && !headers.has("traceparent")) {
|
|
357
|
+
headers.set("traceparent", caller.traceparent);
|
|
358
|
+
}
|
|
359
|
+
if (caller && options.vaultConnections) {
|
|
360
|
+
for (const [slug, connectionId] of Object.entries(options.vaultConnections)) {
|
|
361
|
+
const cleanSlug = slug.trim().toLowerCase().replace(/[^a-z0-9-]/g, "-");
|
|
362
|
+
if (!cleanSlug || !connectionId) continue;
|
|
363
|
+
const vaultToken = await auth.vaultToken(connectionId, caller);
|
|
364
|
+
headers.set(`x-lumoauth-vault-${cleanSlug}-token`, vaultToken.accessToken);
|
|
365
|
+
}
|
|
195
366
|
}
|
|
196
367
|
const controller = new AbortController();
|
|
197
368
|
res.on("close", () => controller.abort());
|
|
@@ -208,7 +379,7 @@ function createSecureProxy(options) {
|
|
|
208
379
|
} catch (error) {
|
|
209
380
|
if (controller.signal.aborted) return;
|
|
210
381
|
log2(`lumoauth: upstream ${upstream.origin} is not reachable: ${error.message}`);
|
|
211
|
-
|
|
382
|
+
writeJson2(res, 502, { error: "bad_gateway", error_description: "The MCP server is not reachable" });
|
|
212
383
|
return;
|
|
213
384
|
}
|
|
214
385
|
const sessionId = answer.headers.get("mcp-session-id");
|
|
@@ -256,46 +427,73 @@ function createSecureProxy(options) {
|
|
|
256
427
|
return;
|
|
257
428
|
}
|
|
258
429
|
if (options.bridge && url.pathname !== options.path) {
|
|
259
|
-
|
|
430
|
+
writeJson2(res, 404, { error: "not_found" });
|
|
260
431
|
return;
|
|
261
432
|
}
|
|
262
433
|
if (!options.bridge && options.publicPaths.includes(url.pathname)) {
|
|
263
|
-
await forward(req, res, url, await
|
|
434
|
+
await forward(req, res, url, await readBody2(req, maxBody), void 0, void 0);
|
|
264
435
|
return;
|
|
265
436
|
}
|
|
266
437
|
let info;
|
|
267
438
|
try {
|
|
268
439
|
info = await auth.authenticate({ method, url: req.url ?? "/", headers: req.headers });
|
|
269
440
|
} catch (error) {
|
|
270
|
-
if (error instanceof
|
|
271
|
-
(0,
|
|
441
|
+
if (error instanceof import_mcp2.LumoAuthChallenge) {
|
|
442
|
+
(0, import_mcp2.writeChallenge)(res, error);
|
|
272
443
|
return;
|
|
273
444
|
}
|
|
274
445
|
throw error;
|
|
275
446
|
}
|
|
276
447
|
const caller = info.extra.caller;
|
|
277
|
-
const sessionId =
|
|
448
|
+
const sessionId = headerOf2(req, "mcp-session-id");
|
|
278
449
|
if (!options.bridge && sessionId && !sessions.claim(sessionId, principal(caller))) {
|
|
279
|
-
|
|
450
|
+
writeJson2(res, 404, { jsonrpc: "2.0", id: null, error: { code: -32001, message: "Session not found" } });
|
|
280
451
|
return;
|
|
281
452
|
}
|
|
282
|
-
const body = method === "POST" ? await
|
|
453
|
+
const body = method === "POST" ? await readBody2(req, maxBody) : void 0;
|
|
283
454
|
let inspection;
|
|
284
|
-
if (body && options.toolPolicy) {
|
|
455
|
+
if (body && (options.toolPolicy || options.jit)) {
|
|
285
456
|
inspection = inspect(body.toString("utf8"));
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
if (
|
|
457
|
+
if (!caller.traceparent && inspection.traceparent) {
|
|
458
|
+
const parsedTp = (0, import_mcp2.parseTraceparent)(inspection.traceparent);
|
|
459
|
+
if (parsedTp) {
|
|
460
|
+
caller.traceparent = parsedTp.traceparent;
|
|
461
|
+
caller.traceId ?? (caller.traceId = parsedTp.traceId);
|
|
462
|
+
}
|
|
289
463
|
}
|
|
290
|
-
if (
|
|
291
|
-
const
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
464
|
+
if (options.toolPolicy) {
|
|
465
|
+
const refused = [];
|
|
466
|
+
for (const call of inspection.toolCalls) {
|
|
467
|
+
if (!await auth.toolPolicy().isAllowed(caller, call.name)) refused.push(call);
|
|
468
|
+
}
|
|
469
|
+
if (refused.length > 0) {
|
|
470
|
+
const reason = (name) => `LumoAuth policy does not allow this caller to run '${name}'.`;
|
|
471
|
+
log2(`lumoauth: refused ${refused.map((c) => c.name).join(", ")} for ${caller.sub}`);
|
|
472
|
+
if (inspection.isBatch) {
|
|
473
|
+
writeJson2(res, 200, inspection.toolCalls.map((c) => refusedToolResult(c.id, reason(refused[0].name))));
|
|
474
|
+
} else {
|
|
475
|
+
writeJson2(res, 200, refusedToolResult(refused[0].id, reason(refused[0].name)));
|
|
476
|
+
}
|
|
477
|
+
return;
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
if (options.jit) {
|
|
481
|
+
for (const call of inspection.toolCalls) {
|
|
482
|
+
const jitSpec = options.jit[call.name];
|
|
483
|
+
if (!jitSpec) continue;
|
|
484
|
+
const required = (0, import_mcp2.normalizeAuthorizationDetails)(jitSpec);
|
|
485
|
+
const missing = (0, import_mcp2.missingAuthorizationDetails)(required, caller.authorizationDetails);
|
|
486
|
+
if (missing.length > 0) {
|
|
487
|
+
const jitRefusal = refusedJitToolResult(call.id, call.name, missing);
|
|
488
|
+
res.setHeader("Insufficient-Authorization-Details", jitRefusal.headerValue);
|
|
489
|
+
writeJson2(
|
|
490
|
+
res,
|
|
491
|
+
200,
|
|
492
|
+
inspection.isBatch ? inspection.toolCalls.map((c) => refusedJitToolResult(c.id, call.name, missing).body) : jitRefusal.body
|
|
493
|
+
);
|
|
494
|
+
return;
|
|
495
|
+
}
|
|
297
496
|
}
|
|
298
|
-
return;
|
|
299
497
|
}
|
|
300
498
|
}
|
|
301
499
|
if (options.bridge) {
|
|
@@ -304,7 +502,7 @@ function createSecureProxy(options) {
|
|
|
304
502
|
try {
|
|
305
503
|
parsed = JSON.parse(body.toString("utf8"));
|
|
306
504
|
} catch {
|
|
307
|
-
|
|
505
|
+
writeJson2(res, 400, { jsonrpc: "2.0", id: null, error: { code: -32700, message: "Parse error" } });
|
|
308
506
|
return;
|
|
309
507
|
}
|
|
310
508
|
}
|
|
@@ -313,14 +511,14 @@ function createSecureProxy(options) {
|
|
|
313
511
|
}
|
|
314
512
|
await forward(req, res, url, body, caller, inspection);
|
|
315
513
|
}
|
|
316
|
-
return (0,
|
|
514
|
+
return (0, import_node_http2.createServer)((req, res) => {
|
|
317
515
|
handle(req, res).catch((error) => {
|
|
318
516
|
if (error instanceof BodyTooLargeError) {
|
|
319
|
-
|
|
517
|
+
writeJson2(res, 413, { error: "request_too_large" });
|
|
320
518
|
return;
|
|
321
519
|
}
|
|
322
520
|
log2(`lumoauth: ${error.stack ?? error}`);
|
|
323
|
-
if (!res.headersSent)
|
|
521
|
+
if (!res.headersSent) writeJson2(res, 500, { error: "server_error" });
|
|
324
522
|
else res.destroy();
|
|
325
523
|
});
|
|
326
524
|
});
|
|
@@ -482,20 +680,24 @@ async function waitForUpstream(url, child, timeoutMs = 3e4) {
|
|
|
482
680
|
}
|
|
483
681
|
|
|
484
682
|
// src/cli.ts
|
|
485
|
-
var VERSION = "1.
|
|
683
|
+
var VERSION = "1.3.0";
|
|
486
684
|
var HELP = `lumoauth: put LumoAuth in front of an MCP server
|
|
487
685
|
|
|
488
686
|
Usage:
|
|
489
687
|
npx lumoauth secure --upstream <url> [options] [-- <command that starts it>]
|
|
490
688
|
npx lumoauth secure --stdio [options] -- <stdio server command>
|
|
689
|
+
npx lumoauth ext-authz [options]
|
|
491
690
|
npx lumoauth check [options]
|
|
492
691
|
|
|
493
692
|
Commands:
|
|
494
|
-
secure
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
693
|
+
secure Serve the MCP server over HTTP behind LumoAuth: OAuth discovery,
|
|
694
|
+
token checks (Bearer and DPoP) and LumoAuth's per-tool policy.
|
|
695
|
+
The server's own code does not change.
|
|
696
|
+
ext-authz Run as an Envoy HTTP ext_authz check server for Kubernetes / service
|
|
697
|
+
mesh deployments, returning 200 OK with X-LumoAuth-* headers or
|
|
698
|
+
401/403 RFC 9728 challenges.
|
|
699
|
+
check Show what LumoAuth's registration says, the discovery document
|
|
700
|
+
and the 401 challenge, then exit.
|
|
499
701
|
|
|
500
702
|
Options:
|
|
501
703
|
--org <slug> Organization (LUMOAUTH_ORG_ID)
|
|
@@ -505,11 +707,14 @@ Options:
|
|
|
505
707
|
--issuer <url> Organization issuer, for a custom domain (LUMOAUTH_ISSUER)
|
|
506
708
|
--upstream <url> The MCP endpoint to protect, e.g. http://127.0.0.1:8000/mcp
|
|
507
709
|
--stdio The command after -- is a stdio MCP server
|
|
710
|
+
--ext-authz Run in Envoy HTTP ext_authz check mode
|
|
508
711
|
--port <n> Port to listen on (PORT, default 8080)
|
|
509
712
|
--host <address> Address to listen on (default 0.0.0.0)
|
|
510
713
|
--path <path> Public path of the MCP endpoint (default: the Resource URI's path, or /mcp)
|
|
511
714
|
--scope <scope> A scope every request needs; repeat for more
|
|
512
715
|
--public <path> Forward this path without a token (health checks); repeatable
|
|
716
|
+
--jit <tool=type[:a]> Require a JIT/RAR grant for a tool (e.g. refund=payment:refund); repeatable
|
|
717
|
+
--vault <slug=conn> Inject X-LumoAuth-Vault-<Slug>-Token from Token Vault; repeatable
|
|
513
718
|
--tool-policy Require LumoAuth's per-tool policy (LUMOAUTH_MCP_CLIENT_ID and _SECRET)
|
|
514
719
|
--no-tool-policy Do not ask LumoAuth about tools
|
|
515
720
|
--no-identity-headers Do not send X-LumoAuth-* identity headers upstream
|
|
@@ -527,7 +732,7 @@ var LUMOAUTH_OPTIONS = {
|
|
|
527
732
|
var UsageError = class extends Error {
|
|
528
733
|
};
|
|
529
734
|
function makeAuth(values) {
|
|
530
|
-
return (0,
|
|
735
|
+
return (0, import_mcp3.lumoAuth)({
|
|
531
736
|
orgId: values.org,
|
|
532
737
|
serverId: values.server,
|
|
533
738
|
resource: values.resource,
|
|
@@ -537,7 +742,39 @@ function makeAuth(values) {
|
|
|
537
742
|
});
|
|
538
743
|
}
|
|
539
744
|
var log = (line) => console.error(line);
|
|
540
|
-
|
|
745
|
+
function parseJitSpecs(specs) {
|
|
746
|
+
if (!specs || specs.length === 0) return void 0;
|
|
747
|
+
const out = {};
|
|
748
|
+
for (const raw of specs) {
|
|
749
|
+
const eq = raw.indexOf("=");
|
|
750
|
+
if (eq <= 0 || eq === raw.length - 1) {
|
|
751
|
+
throw new UsageError(`--jit must be <tool>=<type>[:action1,action2], got '${raw}'`);
|
|
752
|
+
}
|
|
753
|
+
const tool = raw.slice(0, eq).trim();
|
|
754
|
+
const rest = raw.slice(eq + 1).trim();
|
|
755
|
+
const [type, actionsRaw] = rest.split(":", 2);
|
|
756
|
+
if (!tool || !type) throw new UsageError(`--jit must be <tool>=<type>[:action1,action2], got '${raw}'`);
|
|
757
|
+
const actions = actionsRaw ? actionsRaw.split(",").map((a) => a.trim()).filter(Boolean) : void 0;
|
|
758
|
+
out[tool] = actions && actions.length > 0 ? { type, actions } : { type };
|
|
759
|
+
}
|
|
760
|
+
return out;
|
|
761
|
+
}
|
|
762
|
+
function parseVaultSpecs(specs) {
|
|
763
|
+
if (!specs || specs.length === 0) return void 0;
|
|
764
|
+
const out = {};
|
|
765
|
+
for (const raw of specs) {
|
|
766
|
+
const eq = raw.indexOf("=");
|
|
767
|
+
if (eq <= 0 || eq === raw.length - 1) {
|
|
768
|
+
throw new UsageError(`--vault must be <slug>=<connection_id>, got '${raw}'`);
|
|
769
|
+
}
|
|
770
|
+
const slug = raw.slice(0, eq).trim();
|
|
771
|
+
const conn = raw.slice(eq + 1).trim();
|
|
772
|
+
if (!slug || !conn) throw new UsageError(`--vault must be <slug>=<connection_id>, got '${raw}'`);
|
|
773
|
+
out[slug] = conn;
|
|
774
|
+
}
|
|
775
|
+
return out;
|
|
776
|
+
}
|
|
777
|
+
async function secure(args, command, forceExtAuthz = false) {
|
|
541
778
|
const { values } = (0, import_node_util.parseArgs)({
|
|
542
779
|
args,
|
|
543
780
|
strict: true,
|
|
@@ -545,25 +782,33 @@ async function secure(args, command) {
|
|
|
545
782
|
...LUMOAUTH_OPTIONS,
|
|
546
783
|
upstream: { type: "string" },
|
|
547
784
|
stdio: { type: "boolean" },
|
|
785
|
+
"ext-authz": { type: "boolean" },
|
|
548
786
|
port: { type: "string" },
|
|
549
787
|
host: { type: "string" },
|
|
550
788
|
path: { type: "string" },
|
|
551
789
|
public: { type: "string", multiple: true },
|
|
790
|
+
jit: { type: "string", multiple: true },
|
|
791
|
+
vault: { type: "string", multiple: true },
|
|
552
792
|
"tool-policy": { type: "boolean" },
|
|
553
793
|
"no-tool-policy": { type: "boolean" },
|
|
554
794
|
"no-identity-headers": { type: "boolean" }
|
|
555
795
|
}
|
|
556
796
|
});
|
|
557
|
-
|
|
558
|
-
if (
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
797
|
+
const isExtAuthz = forceExtAuthz || Boolean(values["ext-authz"]);
|
|
798
|
+
if (!isExtAuthz) {
|
|
799
|
+
if (values.stdio && values.upstream) throw new UsageError("use --upstream or --stdio, not both");
|
|
800
|
+
if (values.stdio && command.length === 0) {
|
|
801
|
+
throw new UsageError("--stdio needs the server command after --, for example: lumoauth secure --stdio -- node server.js");
|
|
802
|
+
}
|
|
803
|
+
if (!values.stdio && !values.upstream) {
|
|
804
|
+
throw new UsageError("say which server to protect: --upstream <url> for an HTTP server, or --stdio -- <command>");
|
|
805
|
+
}
|
|
806
|
+
if (values.upstream && !/^https?:\/\//.test(values.upstream)) {
|
|
807
|
+
throw new UsageError(`--upstream must be an http(s) URL, got '${values.upstream}'`);
|
|
808
|
+
}
|
|
566
809
|
}
|
|
810
|
+
const jit = parseJitSpecs(values.jit);
|
|
811
|
+
const vaultConnections = parseVaultSpecs(values.vault);
|
|
567
812
|
const auth = makeAuth(values);
|
|
568
813
|
const config = await auth.ready();
|
|
569
814
|
let toolPolicy = auth.hasToolPolicy && !values["no-tool-policy"];
|
|
@@ -580,6 +825,35 @@ async function secure(args, command) {
|
|
|
580
825
|
const port = Number(values.port ?? process.env.PORT ?? 8080);
|
|
581
826
|
const host = values.host ?? "0.0.0.0";
|
|
582
827
|
if (!Number.isInteger(port) || port < 0 || port > 65535) throw new UsageError(`--port must be a port number, got '${values.port}'`);
|
|
828
|
+
if (isExtAuthz) {
|
|
829
|
+
const extServer = createExtAuthzServer({
|
|
830
|
+
auth,
|
|
831
|
+
toolPolicy,
|
|
832
|
+
identityHeaders: !values["no-identity-headers"],
|
|
833
|
+
publicPaths: values.public ?? ["/healthz", "/readyz"],
|
|
834
|
+
jit,
|
|
835
|
+
vaultConnections,
|
|
836
|
+
log
|
|
837
|
+
});
|
|
838
|
+
await new Promise((resolve, reject) => {
|
|
839
|
+
extServer.once("error", reject);
|
|
840
|
+
extServer.listen(port, host, resolve);
|
|
841
|
+
});
|
|
842
|
+
const addr = extServer.address();
|
|
843
|
+
const listening2 = typeof addr === "object" && addr ? `${host}:${addr.port}` : `${host}:${port}`;
|
|
844
|
+
log(`lumoauth: Envoy ext_authz server listening on http://${listening2}`);
|
|
845
|
+
log(` resource ${config.resource}${config.requireDpop ? " (DPoP required)" : ""}`);
|
|
846
|
+
log(` issuer ${config.issuer}`);
|
|
847
|
+
log(` tool policy ${toolPolicy ? `on (server ${config.serverId})` : "off"}`);
|
|
848
|
+
const stopExt = () => {
|
|
849
|
+
log("lumoauth: stopping ext-authz");
|
|
850
|
+
extServer.close();
|
|
851
|
+
setTimeout(() => process.exit(0), 200).unref();
|
|
852
|
+
};
|
|
853
|
+
process.once("SIGINT", stopExt);
|
|
854
|
+
process.once("SIGTERM", stopExt);
|
|
855
|
+
return;
|
|
856
|
+
}
|
|
583
857
|
let child;
|
|
584
858
|
if (values.upstream && command.length > 0) {
|
|
585
859
|
child = startUpstream(command);
|
|
@@ -602,6 +876,8 @@ async function secure(args, command) {
|
|
|
602
876
|
toolPolicy,
|
|
603
877
|
identityHeaders: !values["no-identity-headers"],
|
|
604
878
|
publicPaths: values.public ?? [],
|
|
879
|
+
jit,
|
|
880
|
+
vaultConnections,
|
|
605
881
|
log
|
|
606
882
|
});
|
|
607
883
|
await new Promise((resolve, reject) => {
|
|
@@ -680,7 +956,9 @@ async function main(argv) {
|
|
|
680
956
|
const [name, ...args] = own;
|
|
681
957
|
switch (name) {
|
|
682
958
|
case "secure":
|
|
683
|
-
return secure(args, command);
|
|
959
|
+
return secure(args, command, false);
|
|
960
|
+
case "ext-authz":
|
|
961
|
+
return secure(args, command, true);
|
|
684
962
|
case "check":
|
|
685
963
|
return check(args);
|
|
686
964
|
case "-v":
|