@lifeaitools/clauth 1.5.27 → 1.5.28

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.
@@ -2934,21 +2934,116 @@ function createServer(initPassword, whitelist, port, tunnelHostnameInit = null,
2934
2934
  return res.end();
2935
2935
  }
2936
2936
 
2937
- // ── OAuth Discovery — 404 all endpoints ──────────────
2938
- // claude.ai probes well-known for remote MCP. If it gets 200, it insists on
2939
- // completing OAuth (which succeeds server-side but claude.ai never uses the token).
2940
- // Returning 404 makes claude.ai skip OAuth and connect directly.
2937
+ // ── OAuth Discovery — 404 well-known only ──────────────
2938
+ // claude.ai ignores metadata endpoints and constructs /register, /authorize,
2939
+ // /token from the domain root (issue #82). Keep well-known as 404 so claude.ai
2940
+ // uses the fallback paths. OAuth endpoints below are live.
2941
2941
  if (reqPath.startsWith("/.well-known/oauth-protected-resource") ||
2942
2942
  reqPath === "/.well-known/oauth-authorization-server") {
2943
2943
  res.writeHead(404, { "Content-Type": "application/json", ...CORS });
2944
2944
  return res.end(JSON.stringify({ error: "not_found" }));
2945
2945
  }
2946
2946
 
2947
- if ((method === "POST" && reqPath === "/register") ||
2948
- (method === "GET" && reqPath === "/authorize") ||
2949
- (method === "POST" && reqPath === "/token")) {
2950
- res.writeHead(404, { "Content-Type": "application/json", ...CORS });
2951
- return res.end(JSON.stringify({ error: "not_found" }));
2947
+ // ── Dynamic Client Registration (RFC 7591) ──────────────
2948
+ if (method === "POST" && reqPath === "/register") {
2949
+ let body;
2950
+ try { body = await readBody(req); } catch {
2951
+ res.writeHead(400, { "Content-Type": "application/json", ...CORS });
2952
+ return res.end(JSON.stringify({ error: "invalid_request" }));
2953
+ }
2954
+ const clientId = crypto.randomBytes(16).toString("hex");
2955
+ const clientSecret = crypto.randomBytes(32).toString("hex");
2956
+ const client = {
2957
+ client_id: clientId, client_secret: clientSecret,
2958
+ client_name: body.client_name || "unknown",
2959
+ redirect_uris: body.redirect_uris || [],
2960
+ grant_types: body.grant_types || ["authorization_code"],
2961
+ response_types: body.response_types || ["code"],
2962
+ token_endpoint_auth_method: body.token_endpoint_auth_method || "client_secret_post",
2963
+ };
2964
+ oauthClients.set(clientId, client);
2965
+ const logMsg = `[${new Date().toISOString()}] OAuth: registered client ${clientId} (${client.client_name})\n`;
2966
+ try { fs.appendFileSync(LOG_FILE, logMsg); } catch {}
2967
+ res.writeHead(201, { "Content-Type": "application/json", ...CORS });
2968
+ return res.end(JSON.stringify(client));
2969
+ }
2970
+
2971
+ // ── Authorization endpoint — auto-approve ──────────────
2972
+ if (method === "GET" && reqPath === "/authorize") {
2973
+ const clientId = url.searchParams.get("client_id");
2974
+ const redirectUri = url.searchParams.get("redirect_uri");
2975
+ const state = url.searchParams.get("state");
2976
+ const codeChallenge = url.searchParams.get("code_challenge");
2977
+
2978
+ if (!clientId || !redirectUri) {
2979
+ res.writeHead(400, { "Content-Type": "text/plain", ...CORS });
2980
+ return res.end("Missing client_id or redirect_uri");
2981
+ }
2982
+
2983
+ const code = crypto.randomBytes(32).toString("hex");
2984
+ oauthCodes.set(code, {
2985
+ client_id: clientId, redirect_uri: redirectUri,
2986
+ code_challenge: codeChallenge,
2987
+ expires: Date.now() + 300_000,
2988
+ });
2989
+
2990
+ const redirect = new URL(redirectUri);
2991
+ redirect.searchParams.set("code", code);
2992
+ if (state) redirect.searchParams.set("state", state);
2993
+
2994
+ const logMsg = `[${new Date().toISOString()}] OAuth: authorize → code for ${clientId}, redirect to ${redirect.origin}\n`;
2995
+ try { fs.appendFileSync(LOG_FILE, logMsg); } catch {}
2996
+ res.writeHead(302, { Location: redirect.toString(), ...CORS });
2997
+ return res.end();
2998
+ }
2999
+
3000
+ // ── Token endpoint ──────────────────────────────────────
3001
+ if (method === "POST" && reqPath === "/token") {
3002
+ let body;
3003
+ const ct = req.headers["content-type"] || "";
3004
+ try {
3005
+ if (ct.includes("application/json")) {
3006
+ body = await readBody(req);
3007
+ } else {
3008
+ const raw = await readRawBody(req);
3009
+ body = Object.fromEntries(new URLSearchParams(raw));
3010
+ }
3011
+ } catch {
3012
+ res.writeHead(400, { "Content-Type": "application/json", ...CORS });
3013
+ return res.end(JSON.stringify({ error: "invalid_request" }));
3014
+ }
3015
+
3016
+ if (body.grant_type !== "authorization_code") {
3017
+ res.writeHead(400, { "Content-Type": "application/json", ...CORS });
3018
+ return res.end(JSON.stringify({ error: "unsupported_grant_type" }));
3019
+ }
3020
+
3021
+ const stored = oauthCodes.get(body.code);
3022
+ if (!stored || stored.expires < Date.now()) {
3023
+ oauthCodes.delete(body.code);
3024
+ res.writeHead(400, { "Content-Type": "application/json", ...CORS });
3025
+ return res.end(JSON.stringify({ error: "invalid_grant" }));
3026
+ }
3027
+
3028
+ // PKCE verification
3029
+ if (stored.code_challenge && body.code_verifier) {
3030
+ const computed = sha256base64url(body.code_verifier);
3031
+ if (computed !== stored.code_challenge) {
3032
+ oauthCodes.delete(body.code);
3033
+ res.writeHead(400, { "Content-Type": "application/json", ...CORS });
3034
+ return res.end(JSON.stringify({ error: "invalid_grant", error_description: "PKCE failed" }));
3035
+ }
3036
+ }
3037
+
3038
+ oauthCodes.delete(body.code);
3039
+ const accessToken = crypto.randomBytes(32).toString("hex");
3040
+ oauthTokens.add(accessToken);
3041
+ saveTokens(oauthTokens);
3042
+
3043
+ const logMsg = `[${new Date().toISOString()}] OAuth: token issued for ${stored.client_id} (token=${accessToken.slice(0,8)}…)\n`;
3044
+ try { fs.appendFileSync(LOG_FILE, logMsg); } catch {}
3045
+ res.writeHead(200, { "Content-Type": "application/json", ...CORS });
3046
+ return res.end(JSON.stringify({ access_token: accessToken, token_type: "Bearer", scope: "mcp:tools", expires_in: 86400 }));
2952
3047
  }
2953
3048
 
2954
3049
  // ── MCP path helpers ──
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lifeaitools/clauth",
3
- "version": "1.5.27",
3
+ "version": "1.5.28",
4
4
  "description": "Hardware-bound credential vault for the LIFEAI infrastructure stack",
5
5
  "type": "module",
6
6
  "bin": {