toolfunnel 0.7.0 → 0.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. package/README.md +228 -235
  2. package/SECURITY.md +84 -84
  3. package/bin/toolfunnel.js +451 -451
  4. package/docs/KNOWN_BUGS.md +80 -80
  5. package/docs/design.md +84 -84
  6. package/docs/packaging.md +115 -115
  7. package/hooks/hooks.manifest.json +27 -27
  8. package/hooks/scripts/example-audit.js +21 -21
  9. package/hooks/scripts/example-deny-dangerous.js +39 -39
  10. package/mcp/expose.example.json +30 -30
  11. package/mcp/servers/mock-modern/server.js +147 -147
  12. package/mcp/servers/mock-upstream/server.js +380 -380
  13. package/package.json +67 -67
  14. package/src/auth/config.js +174 -174
  15. package/src/auth/install.js +181 -181
  16. package/src/auth/resource-server.js +353 -353
  17. package/src/core/child-reaper.js +61 -61
  18. package/src/core/config-home.js +134 -134
  19. package/src/core/events.js +105 -105
  20. package/src/core/hook-engine.js +329 -329
  21. package/src/core/hook-loader.js +824 -824
  22. package/src/core/hook-runner.js +566 -566
  23. package/src/core/logger.js +297 -297
  24. package/src/core/matcher.js +100 -100
  25. package/src/core/metrics.js +75 -75
  26. package/src/core/rename-migration.js +163 -163
  27. package/src/core/requires.js +138 -138
  28. package/src/core/server-config.js +98 -98
  29. package/src/extend/add-hook.md +152 -152
  30. package/src/extend/add-mcp.md +119 -119
  31. package/src/extend/configure.md +88 -88
  32. package/src/extend/create-tool.md +168 -168
  33. package/src/extend/howto.js +102 -102
  34. package/src/extend/package.md +190 -190
  35. package/src/extend/wrap.md +99 -99
  36. package/src/mcp/aggregator.js +1719 -1674
  37. package/src/mcp/expose-store.js +577 -577
  38. package/src/mcp/gated-run.js +210 -210
  39. package/src/mcp/http-transport.js +1563 -1563
  40. package/src/mcp/mcp-client.js +1070 -1070
  41. package/src/mcp/modern.js +406 -406
  42. package/src/mcp/protocol.js +468 -468
  43. package/src/mcp/server.js +3112 -3112
  44. package/src/tools/registry.js +661 -661
  45. package/src/tools/tool-state.js +170 -170
  46. package/src/ui/public/app.js +1187 -1187
  47. package/src/ui/public/index.html +286 -286
  48. package/src/ui/public/styles.css +535 -535
  49. package/src/ui/server.js +1616 -1616
  50. package/tools/scripts/base64.js +139 -139
  51. package/tools/scripts/danger.js +61 -61
  52. package/tools/scripts/echo.js +39 -39
  53. package/tools/scripts/hash.js +101 -101
  54. package/tools/scripts/json.js +109 -109
  55. package/tools/scripts/text-stats.js +131 -131
  56. package/tools/scripts/tf-env.js +43 -43
  57. package/tools/scripts/tf-hook-add.js +151 -151
  58. package/tools/scripts/tf-hook-set.js +120 -120
  59. package/tools/scripts/tf-list.js +192 -192
  60. package/tools/scripts/tf-log.js +86 -86
  61. package/tools/scripts/tf-mcp-add.js +117 -117
  62. package/tools/scripts/tf-mcp-set.js +121 -121
  63. package/tools/scripts/tf-pack.js +237 -237
  64. package/tools/scripts/tf-tool-add.js +134 -134
  65. package/tools/scripts/tf-tool-set.js +142 -142
  66. package/tools/scripts/tf-wrap.js +103 -103
  67. package/tools/scripts/uuid.js +102 -102
package/package.json CHANGED
@@ -1,67 +1,67 @@
1
- {
2
- "name": "toolfunnel",
3
- "version": "0.7.0",
4
- "description": "A zero-dependency MCP gateway: host your own tools, forward and curate tools from other MCP servers, expose them leanly to cut agent token cost, and gate every call through your own policy hooks before it runs.",
5
- "keywords": [
6
- "mcp",
7
- "model-context-protocol",
8
- "mcp-gateway",
9
- "mcp-proxy",
10
- "mcp-server",
11
- "ai",
12
- "agents",
13
- "llm",
14
- "tools",
15
- "claude",
16
- "anthropic",
17
- "aggregator",
18
- "token-efficiency"
19
- ],
20
- "license": "MIT",
21
- "type": "commonjs",
22
- "main": "src/mcp/server.js",
23
- "bin": {
24
- "toolfunnel": "bin/toolfunnel.js"
25
- },
26
- "engines": {
27
- "node": ">=18"
28
- },
29
- "scripts": {
30
- "start": "node bin/toolfunnel.js",
31
- "build": "echo toolfunnel has no build step - zero-dependency, runs from source",
32
- "http": "node bin/toolfunnel.js --http",
33
- "install-oauth": "node bin/toolfunnel.js install-oauth",
34
- "test": "node test/run-all.js",
35
- "prepublishOnly": "node test/run-all.js",
36
- "release": "node scripts/release.js",
37
- "test:integration": "node test/integration-real-mcp.test.js && node test/integration-http-client.test.js"
38
- },
39
- "dependencies": {},
40
- "author": "rendeverance",
41
- "repository": {
42
- "type": "git",
43
- "url": "git+https://github.com/Rendeverance/toolfunnel.git"
44
- },
45
- "homepage": "https://github.com/Rendeverance/toolfunnel#readme",
46
- "bugs": {
47
- "url": "https://github.com/Rendeverance/toolfunnel/issues"
48
- },
49
- "files": [
50
- "bin/",
51
- "src/",
52
- "tools/tools.register.json",
53
- "tools/scripts/",
54
- "mcp/expose.example.json",
55
- "mcp/servers/",
56
- "hooks/hooks.manifest.json",
57
- "hooks/scripts/",
58
- "docs/",
59
- "README.md",
60
- "SECURITY.md",
61
- "LICENSE"
62
- ],
63
- "devDependencies": {
64
- "@modelcontextprotocol/sdk": "^1.0.0",
65
- "jose": "^5.10.0"
66
- }
67
- }
1
+ {
2
+ "name": "toolfunnel",
3
+ "version": "0.7.1",
4
+ "description": "A zero-dependency MCP gateway: host your own tools, forward and curate tools from other MCP servers, expose them leanly to cut agent token cost, and gate every call through your own policy hooks before it runs.",
5
+ "keywords": [
6
+ "mcp",
7
+ "model-context-protocol",
8
+ "mcp-gateway",
9
+ "mcp-proxy",
10
+ "mcp-server",
11
+ "ai",
12
+ "agents",
13
+ "llm",
14
+ "tools",
15
+ "claude",
16
+ "anthropic",
17
+ "aggregator",
18
+ "token-efficiency"
19
+ ],
20
+ "license": "MIT",
21
+ "type": "commonjs",
22
+ "main": "src/mcp/server.js",
23
+ "bin": {
24
+ "toolfunnel": "bin/toolfunnel.js"
25
+ },
26
+ "engines": {
27
+ "node": ">=18"
28
+ },
29
+ "scripts": {
30
+ "start": "node bin/toolfunnel.js",
31
+ "build": "echo toolfunnel has no build step - zero-dependency, runs from source",
32
+ "http": "node bin/toolfunnel.js --http",
33
+ "install-oauth": "node bin/toolfunnel.js install-oauth",
34
+ "test": "node test/run-all.js",
35
+ "prepublishOnly": "node test/run-all.js",
36
+ "release": "node scripts/release.js",
37
+ "test:integration": "node test/integration-real-mcp.test.js && node test/integration-http-client.test.js"
38
+ },
39
+ "dependencies": {},
40
+ "author": "rendeverance",
41
+ "repository": {
42
+ "type": "git",
43
+ "url": "git+https://github.com/Rendeverance/toolfunnel.git"
44
+ },
45
+ "homepage": "https://github.com/Rendeverance/toolfunnel#readme",
46
+ "bugs": {
47
+ "url": "https://github.com/Rendeverance/toolfunnel/issues"
48
+ },
49
+ "files": [
50
+ "bin/",
51
+ "src/",
52
+ "tools/tools.register.json",
53
+ "tools/scripts/",
54
+ "mcp/expose.example.json",
55
+ "mcp/servers/",
56
+ "hooks/hooks.manifest.json",
57
+ "hooks/scripts/",
58
+ "docs/",
59
+ "README.md",
60
+ "SECURITY.md",
61
+ "LICENSE"
62
+ ],
63
+ "devDependencies": {
64
+ "@modelcontextprotocol/sdk": "^1.0.0",
65
+ "jose": "^5.10.0"
66
+ }
67
+ }
@@ -1,174 +1,174 @@
1
- 'use strict';
2
-
3
- /**
4
- * auth/config.js - the toggle + settings for OPTIONAL OAuth 2.1 resource-server validation.
5
- *
6
- * DEFAULT OFF (the gateway is loopback-only and unauthenticated until you turn this on). Nothing
7
- * is read or required unless auth has been explicitly enabled via setConfig(). A MISSING config
8
- * file means disabled - the safe default - so a fresh checkout authenticates nothing and pulls in
9
- * no dependency.
10
- *
11
- * Design rules (mirrors src/core/logger.js - the proven config pattern):
12
- * - Zero RUNTIME dependencies. Node built-ins only (fs, path). The jose library is pulled in ONLY
13
- * by the validator (src/auth/resource-server.js), ONLY when auth is enabled, and is installed
14
- * on demand (CLI `install-oauth` / the UI button). This module never touches jose.
15
- * - Config is read FRESH on every call (no caching), so a toggle/edit takes effect for the very
16
- * next request without a reconnect or restart - exactly like the logger + tool-state overlays.
17
- * - Nothing here throws. A bad/unreadable/malformed config degrades to the safe default (disabled)
18
- * so a misconfiguration can never crash the transport.
19
- *
20
- * Config file: <root>/auth/auth.config.json (NOT created until setConfig() writes it)
21
- * {
22
- * "enabled": false,
23
- * "issuer": "https://auth.example.com", // the OAuth 2.1 / OIDC authorization server
24
- * "audience": "https://gateway.example.com", // THIS gateway's resource URI - RFC 8707 audience
25
- * // binding; the confused-deputy defence
26
- * "jwksUri": "https://auth.example.com/.well-known/jwks.json", // explicit JWKS; else derived
27
- * // from the issuer via OIDC discovery
28
- * "algorithms": ["RS256", "ES256"], // the PINNED signature-algorithm allowlist
29
- * "requiredScopes": [], // optional - every listed scope must be present
30
- * "clockToleranceSec": 30 // exp/nbf skew tolerance (seconds)
31
- * }
32
- *
33
- * CommonJS only.
34
- */
35
-
36
- const fs = require('node:fs');
37
- const path = require('node:path');
38
-
39
- /** The CONFIG HOME (TOOLFUNNEL_HOME / --config-dir; defaults to the package root - see
40
- * src/core/config-home.js). Auth config is user-state, so it lives with the home. */
41
- const { resolveConfigHome } = require('../core/config-home');
42
- const ROOT = resolveConfigHome();
43
-
44
- /** The toggle/config file. NOT created until setConfig() writes it. */
45
- const CONFIG_PATH = path.join(ROOT, 'auth', 'auth.config.json');
46
-
47
- /** Safe defaults - used whenever the config file is absent or unreadable. */
48
- const DEFAULTS = Object.freeze({
49
- enabled: false,
50
- issuer: '',
51
- audience: '',
52
- jwksUri: '',
53
- algorithms: ['RS256', 'ES256'],
54
- requiredScopes: [],
55
- clockToleranceSec: 30,
56
- });
57
-
58
- /** A string field, defaulting to '' when not a non-empty string. */
59
- function str(v, dflt) {
60
- return typeof v === 'string' && v.length > 0 ? v : dflt;
61
- }
62
-
63
- /** A string[] field - keep only non-empty strings; fall back to `dflt` when not a usable array. */
64
- function strArray(v, dflt) {
65
- if (!Array.isArray(v)) return dflt.slice();
66
- const out = v.filter((x) => typeof x === 'string' && x.length > 0);
67
- return out.length > 0 ? out : dflt.slice();
68
- }
69
-
70
- /**
71
- * getConfig - the resolved auth config. Reads the file fresh; a missing/unreadable/malformed file
72
- * resolves to the safe defaults (disabled). NEVER throws.
73
- *
74
- * @returns {{ enabled:boolean, issuer:string, audience:string, jwksUri:string,
75
- * algorithms:string[], requiredScopes:string[], clockToleranceSec:number }}
76
- */
77
- function getConfig() {
78
- try {
79
- const raw = fs.readFileSync(CONFIG_PATH, 'utf8');
80
- const parsed = JSON.parse(raw);
81
- if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
82
- return cloneDefaults();
83
- }
84
- const clock = Number(parsed.clockToleranceSec);
85
- return {
86
- enabled: parsed.enabled === true,
87
- issuer: str(parsed.issuer, DEFAULTS.issuer),
88
- audience: str(parsed.audience, DEFAULTS.audience),
89
- jwksUri: str(parsed.jwksUri, DEFAULTS.jwksUri),
90
- algorithms: strArray(parsed.algorithms, DEFAULTS.algorithms),
91
- requiredScopes: strArray(parsed.requiredScopes, DEFAULTS.requiredScopes),
92
- // Clamp to a sane, non-negative tolerance; a >5min skew signals a clock problem, not validity.
93
- clockToleranceSec:
94
- Number.isFinite(clock) && clock >= 0 ? Math.min(Math.floor(clock), 300) : DEFAULTS.clockToleranceSec,
95
- };
96
- } catch (_err) {
97
- // Missing file = disabled (the safe default); also covers unreadable/malformed.
98
- return cloneDefaults();
99
- }
100
- }
101
-
102
- /** A fresh, mutation-safe copy of the defaults (arrays cloned so a caller can't corrupt the frozen one). */
103
- function cloneDefaults() {
104
- return {
105
- enabled: DEFAULTS.enabled,
106
- issuer: DEFAULTS.issuer,
107
- audience: DEFAULTS.audience,
108
- jwksUri: DEFAULTS.jwksUri,
109
- algorithms: DEFAULTS.algorithms.slice(),
110
- requiredScopes: DEFAULTS.requiredScopes.slice(),
111
- clockToleranceSec: DEFAULTS.clockToleranceSec,
112
- };
113
- }
114
-
115
- /**
116
- * configError - a human-readable reason the CURRENT config could not gate requests, or null if it
117
- * is coherent enough to enforce. Used by the transport + UI to fail CLOSED with a clear message
118
- * rather than silently mis-validating. Only meaningful when `enabled` is true.
119
- *
120
- * Enforcement requires: an issuer, an audience (the RFC 8707 confused-deputy defence - a gateway
121
- * that does not bind tokens to its own resource URI is unsafe), at least one pinned algorithm,
122
- * and a key source (explicit jwksUri OR an issuer to derive one from via OIDC discovery).
123
- *
124
- * @param {object} [cfg] defaults to getConfig()
125
- * @returns {string|null}
126
- */
127
- function configError(cfg) {
128
- const c = cfg || getConfig();
129
- if (!c.enabled) return null; // disabled -> nothing to enforce, no error
130
- if (!c.issuer) return 'auth enabled but "issuer" is not set';
131
- if (!c.audience) return 'auth enabled but "audience" (this gateway\'s resource URI) is not set - required to bind tokens (RFC 8707)';
132
- if (!Array.isArray(c.algorithms) || c.algorithms.length === 0) return 'auth enabled but no signature "algorithms" are pinned';
133
- // (A key source is always available: issuer is mandatory above, and jwksUri is derived from it
134
- // via OIDC discovery when not given - so no separate jwksUri-or-issuer check is needed here.)
135
- return null;
136
- }
137
-
138
- /**
139
- * setConfig - atomically merge a patch into auth/auth.config.json (temp + rename). Merges with the
140
- * current resolved config so a partial patch (e.g. { enabled: true }) preserves the other fields.
141
- * Creates the auth/ dir and the file if absent - this is the ONLY function that creates it.
142
- *
143
- * @param {object} patch any subset of the config fields
144
- * @returns {object} the merged, written config
145
- */
146
- function setConfig(patch) {
147
- const current = getConfig();
148
- const p = patch && typeof patch === 'object' && !Array.isArray(patch) ? patch : {};
149
-
150
- const next = {
151
- enabled: typeof p.enabled === 'boolean' ? p.enabled : current.enabled,
152
- issuer: typeof p.issuer === 'string' ? p.issuer : current.issuer,
153
- audience: typeof p.audience === 'string' ? p.audience : current.audience,
154
- jwksUri: typeof p.jwksUri === 'string' ? p.jwksUri : current.jwksUri,
155
- algorithms: Array.isArray(p.algorithms) ? strArray(p.algorithms, current.algorithms) : current.algorithms,
156
- requiredScopes: Array.isArray(p.requiredScopes)
157
- ? p.requiredScopes.filter((x) => typeof x === 'string' && x.length > 0)
158
- : current.requiredScopes,
159
- clockToleranceSec:
160
- Number.isFinite(Number(p.clockToleranceSec)) && Number(p.clockToleranceSec) >= 0
161
- ? Math.min(Math.floor(Number(p.clockToleranceSec)), 300)
162
- : current.clockToleranceSec,
163
- };
164
-
165
- fs.mkdirSync(path.dirname(CONFIG_PATH), { recursive: true });
166
- // Atomic write: unique temp file in the same dir, then rename over the target.
167
- const tmp = CONFIG_PATH + '.' + process.pid + '.' + Date.now() + '.tmp';
168
- fs.writeFileSync(tmp, JSON.stringify(next, null, 2) + '\n');
169
- fs.renameSync(tmp, CONFIG_PATH);
170
-
171
- return next;
172
- }
173
-
174
- module.exports = { getConfig, setConfig, configError, CONFIG_PATH, DEFAULTS };
1
+ 'use strict';
2
+
3
+ /**
4
+ * auth/config.js - the toggle + settings for OPTIONAL OAuth 2.1 resource-server validation.
5
+ *
6
+ * DEFAULT OFF (the gateway is loopback-only and unauthenticated until you turn this on). Nothing
7
+ * is read or required unless auth has been explicitly enabled via setConfig(). A MISSING config
8
+ * file means disabled - the safe default - so a fresh checkout authenticates nothing and pulls in
9
+ * no dependency.
10
+ *
11
+ * Design rules (mirrors src/core/logger.js - the proven config pattern):
12
+ * - Zero RUNTIME dependencies. Node built-ins only (fs, path). The jose library is pulled in ONLY
13
+ * by the validator (src/auth/resource-server.js), ONLY when auth is enabled, and is installed
14
+ * on demand (CLI `install-oauth` / the UI button). This module never touches jose.
15
+ * - Config is read FRESH on every call (no caching), so a toggle/edit takes effect for the very
16
+ * next request without a reconnect or restart - exactly like the logger + tool-state overlays.
17
+ * - Nothing here throws. A bad/unreadable/malformed config degrades to the safe default (disabled)
18
+ * so a misconfiguration can never crash the transport.
19
+ *
20
+ * Config file: <root>/auth/auth.config.json (NOT created until setConfig() writes it)
21
+ * {
22
+ * "enabled": false,
23
+ * "issuer": "https://auth.example.com", // the OAuth 2.1 / OIDC authorization server
24
+ * "audience": "https://gateway.example.com", // THIS gateway's resource URI - RFC 8707 audience
25
+ * // binding; the confused-deputy defence
26
+ * "jwksUri": "https://auth.example.com/.well-known/jwks.json", // explicit JWKS; else derived
27
+ * // from the issuer via OIDC discovery
28
+ * "algorithms": ["RS256", "ES256"], // the PINNED signature-algorithm allowlist
29
+ * "requiredScopes": [], // optional - every listed scope must be present
30
+ * "clockToleranceSec": 30 // exp/nbf skew tolerance (seconds)
31
+ * }
32
+ *
33
+ * CommonJS only.
34
+ */
35
+
36
+ const fs = require('node:fs');
37
+ const path = require('node:path');
38
+
39
+ /** The CONFIG HOME (TOOLFUNNEL_HOME / --config-dir; defaults to the package root - see
40
+ * src/core/config-home.js). Auth config is user-state, so it lives with the home. */
41
+ const { resolveConfigHome } = require('../core/config-home');
42
+ const ROOT = resolveConfigHome();
43
+
44
+ /** The toggle/config file. NOT created until setConfig() writes it. */
45
+ const CONFIG_PATH = path.join(ROOT, 'auth', 'auth.config.json');
46
+
47
+ /** Safe defaults - used whenever the config file is absent or unreadable. */
48
+ const DEFAULTS = Object.freeze({
49
+ enabled: false,
50
+ issuer: '',
51
+ audience: '',
52
+ jwksUri: '',
53
+ algorithms: ['RS256', 'ES256'],
54
+ requiredScopes: [],
55
+ clockToleranceSec: 30,
56
+ });
57
+
58
+ /** A string field, defaulting to '' when not a non-empty string. */
59
+ function str(v, dflt) {
60
+ return typeof v === 'string' && v.length > 0 ? v : dflt;
61
+ }
62
+
63
+ /** A string[] field - keep only non-empty strings; fall back to `dflt` when not a usable array. */
64
+ function strArray(v, dflt) {
65
+ if (!Array.isArray(v)) return dflt.slice();
66
+ const out = v.filter((x) => typeof x === 'string' && x.length > 0);
67
+ return out.length > 0 ? out : dflt.slice();
68
+ }
69
+
70
+ /**
71
+ * getConfig - the resolved auth config. Reads the file fresh; a missing/unreadable/malformed file
72
+ * resolves to the safe defaults (disabled). NEVER throws.
73
+ *
74
+ * @returns {{ enabled:boolean, issuer:string, audience:string, jwksUri:string,
75
+ * algorithms:string[], requiredScopes:string[], clockToleranceSec:number }}
76
+ */
77
+ function getConfig() {
78
+ try {
79
+ const raw = fs.readFileSync(CONFIG_PATH, 'utf8');
80
+ const parsed = JSON.parse(raw);
81
+ if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
82
+ return cloneDefaults();
83
+ }
84
+ const clock = Number(parsed.clockToleranceSec);
85
+ return {
86
+ enabled: parsed.enabled === true,
87
+ issuer: str(parsed.issuer, DEFAULTS.issuer),
88
+ audience: str(parsed.audience, DEFAULTS.audience),
89
+ jwksUri: str(parsed.jwksUri, DEFAULTS.jwksUri),
90
+ algorithms: strArray(parsed.algorithms, DEFAULTS.algorithms),
91
+ requiredScopes: strArray(parsed.requiredScopes, DEFAULTS.requiredScopes),
92
+ // Clamp to a sane, non-negative tolerance; a >5min skew signals a clock problem, not validity.
93
+ clockToleranceSec:
94
+ Number.isFinite(clock) && clock >= 0 ? Math.min(Math.floor(clock), 300) : DEFAULTS.clockToleranceSec,
95
+ };
96
+ } catch (_err) {
97
+ // Missing file = disabled (the safe default); also covers unreadable/malformed.
98
+ return cloneDefaults();
99
+ }
100
+ }
101
+
102
+ /** A fresh, mutation-safe copy of the defaults (arrays cloned so a caller can't corrupt the frozen one). */
103
+ function cloneDefaults() {
104
+ return {
105
+ enabled: DEFAULTS.enabled,
106
+ issuer: DEFAULTS.issuer,
107
+ audience: DEFAULTS.audience,
108
+ jwksUri: DEFAULTS.jwksUri,
109
+ algorithms: DEFAULTS.algorithms.slice(),
110
+ requiredScopes: DEFAULTS.requiredScopes.slice(),
111
+ clockToleranceSec: DEFAULTS.clockToleranceSec,
112
+ };
113
+ }
114
+
115
+ /**
116
+ * configError - a human-readable reason the CURRENT config could not gate requests, or null if it
117
+ * is coherent enough to enforce. Used by the transport + UI to fail CLOSED with a clear message
118
+ * rather than silently mis-validating. Only meaningful when `enabled` is true.
119
+ *
120
+ * Enforcement requires: an issuer, an audience (the RFC 8707 confused-deputy defence - a gateway
121
+ * that does not bind tokens to its own resource URI is unsafe), at least one pinned algorithm,
122
+ * and a key source (explicit jwksUri OR an issuer to derive one from via OIDC discovery).
123
+ *
124
+ * @param {object} [cfg] defaults to getConfig()
125
+ * @returns {string|null}
126
+ */
127
+ function configError(cfg) {
128
+ const c = cfg || getConfig();
129
+ if (!c.enabled) return null; // disabled -> nothing to enforce, no error
130
+ if (!c.issuer) return 'auth enabled but "issuer" is not set';
131
+ if (!c.audience) return 'auth enabled but "audience" (this gateway\'s resource URI) is not set - required to bind tokens (RFC 8707)';
132
+ if (!Array.isArray(c.algorithms) || c.algorithms.length === 0) return 'auth enabled but no signature "algorithms" are pinned';
133
+ // (A key source is always available: issuer is mandatory above, and jwksUri is derived from it
134
+ // via OIDC discovery when not given - so no separate jwksUri-or-issuer check is needed here.)
135
+ return null;
136
+ }
137
+
138
+ /**
139
+ * setConfig - atomically merge a patch into auth/auth.config.json (temp + rename). Merges with the
140
+ * current resolved config so a partial patch (e.g. { enabled: true }) preserves the other fields.
141
+ * Creates the auth/ dir and the file if absent - this is the ONLY function that creates it.
142
+ *
143
+ * @param {object} patch any subset of the config fields
144
+ * @returns {object} the merged, written config
145
+ */
146
+ function setConfig(patch) {
147
+ const current = getConfig();
148
+ const p = patch && typeof patch === 'object' && !Array.isArray(patch) ? patch : {};
149
+
150
+ const next = {
151
+ enabled: typeof p.enabled === 'boolean' ? p.enabled : current.enabled,
152
+ issuer: typeof p.issuer === 'string' ? p.issuer : current.issuer,
153
+ audience: typeof p.audience === 'string' ? p.audience : current.audience,
154
+ jwksUri: typeof p.jwksUri === 'string' ? p.jwksUri : current.jwksUri,
155
+ algorithms: Array.isArray(p.algorithms) ? strArray(p.algorithms, current.algorithms) : current.algorithms,
156
+ requiredScopes: Array.isArray(p.requiredScopes)
157
+ ? p.requiredScopes.filter((x) => typeof x === 'string' && x.length > 0)
158
+ : current.requiredScopes,
159
+ clockToleranceSec:
160
+ Number.isFinite(Number(p.clockToleranceSec)) && Number(p.clockToleranceSec) >= 0
161
+ ? Math.min(Math.floor(Number(p.clockToleranceSec)), 300)
162
+ : current.clockToleranceSec,
163
+ };
164
+
165
+ fs.mkdirSync(path.dirname(CONFIG_PATH), { recursive: true });
166
+ // Atomic write: unique temp file in the same dir, then rename over the target.
167
+ const tmp = CONFIG_PATH + '.' + process.pid + '.' + Date.now() + '.tmp';
168
+ fs.writeFileSync(tmp, JSON.stringify(next, null, 2) + '\n');
169
+ fs.renameSync(tmp, CONFIG_PATH);
170
+
171
+ return next;
172
+ }
173
+
174
+ module.exports = { getConfig, setConfig, configError, CONFIG_PATH, DEFAULTS };