@zackbart/connecta 0.8.0 → 0.9.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.
Files changed (151) hide show
  1. package/CHANGELOG.md +128 -0
  2. package/README.md +31 -55
  3. package/SECURITY.md +5 -11
  4. package/dist/activity.d.ts +0 -8
  5. package/dist/activity.d.ts.map +1 -1
  6. package/dist/activity.js +0 -1
  7. package/dist/activity.js.map +1 -1
  8. package/dist/auth/bearer.d.ts +1 -7
  9. package/dist/auth/bearer.d.ts.map +1 -1
  10. package/dist/auth/bearer.js +2 -23
  11. package/dist/auth/bearer.js.map +1 -1
  12. package/dist/auth/clerk.d.ts +2 -11
  13. package/dist/auth/clerk.d.ts.map +1 -1
  14. package/dist/auth/clerk.js +31 -46
  15. package/dist/auth/clerk.js.map +1 -1
  16. package/dist/auth/downstream-oauth.d.ts +15 -6
  17. package/dist/auth/downstream-oauth.d.ts.map +1 -1
  18. package/dist/auth/downstream-oauth.js +60 -11
  19. package/dist/auth/downstream-oauth.js.map +1 -1
  20. package/dist/call-admission.d.ts.map +1 -1
  21. package/dist/call-admission.js +0 -1
  22. package/dist/call-admission.js.map +1 -1
  23. package/dist/catalog-service.d.ts +1 -2
  24. package/dist/catalog-service.d.ts.map +1 -1
  25. package/dist/catalog-service.js +12 -18
  26. package/dist/catalog-service.js.map +1 -1
  27. package/dist/catalog.d.ts +6 -0
  28. package/dist/catalog.d.ts.map +1 -1
  29. package/dist/catalog.js +51 -0
  30. package/dist/catalog.js.map +1 -1
  31. package/dist/connectors/remote-mcp.d.ts +1 -1
  32. package/dist/connectors/remote-mcp.d.ts.map +1 -1
  33. package/dist/connectors/remote-mcp.js +73 -71
  34. package/dist/connectors/remote-mcp.js.map +1 -1
  35. package/dist/errors.d.ts +19 -1
  36. package/dist/errors.d.ts.map +1 -1
  37. package/dist/errors.js +1 -0
  38. package/dist/errors.js.map +1 -1
  39. package/dist/execute.d.ts +4 -9
  40. package/dist/execute.d.ts.map +1 -1
  41. package/dist/execute.js +41 -20
  42. package/dist/execute.js.map +1 -1
  43. package/dist/executor-result.js +1 -1
  44. package/dist/executor-result.js.map +1 -1
  45. package/dist/index.d.ts +3 -82
  46. package/dist/index.d.ts.map +1 -1
  47. package/dist/index.js +17 -90
  48. package/dist/index.js.map +1 -1
  49. package/dist/invocation.d.ts +5 -0
  50. package/dist/invocation.d.ts.map +1 -1
  51. package/dist/invocation.js +46 -10
  52. package/dist/invocation.js.map +1 -1
  53. package/dist/meta-tools.d.ts +4 -12
  54. package/dist/meta-tools.d.ts.map +1 -1
  55. package/dist/meta-tools.js +119 -169
  56. package/dist/meta-tools.js.map +1 -1
  57. package/dist/operator-ui/generated.d.ts +2 -2
  58. package/dist/operator-ui/generated.d.ts.map +1 -1
  59. package/dist/operator-ui/generated.js +2 -2
  60. package/dist/operator-ui/generated.js.map +1 -1
  61. package/dist/operator-ui/model.d.ts +1 -22
  62. package/dist/operator-ui/model.d.ts.map +1 -1
  63. package/dist/operator-ui/model.js.map +1 -1
  64. package/dist/registry.d.ts +11 -178
  65. package/dist/registry.d.ts.map +1 -1
  66. package/dist/registry.js +52 -330
  67. package/dist/registry.js.map +1 -1
  68. package/dist/retired-toolkits.d.ts +3 -0
  69. package/dist/retired-toolkits.d.ts.map +1 -0
  70. package/dist/retired-toolkits.js +9 -0
  71. package/dist/retired-toolkits.js.map +1 -0
  72. package/dist/routes/activity.d.ts.map +1 -1
  73. package/dist/routes/activity.js +2 -5
  74. package/dist/routes/activity.js.map +1 -1
  75. package/dist/routes/credentials.d.ts.map +1 -1
  76. package/dist/routes/credentials.js +1 -12
  77. package/dist/routes/credentials.js.map +1 -1
  78. package/dist/routes/mcp.d.ts.map +1 -1
  79. package/dist/routes/mcp.js +94 -188
  80. package/dist/routes/mcp.js.map +1 -1
  81. package/dist/routes/oauth.d.ts.map +1 -1
  82. package/dist/routes/oauth.js +3 -8
  83. package/dist/routes/oauth.js.map +1 -1
  84. package/dist/routes/shared.d.ts +9 -30
  85. package/dist/routes/shared.d.ts.map +1 -1
  86. package/dist/routes/shared.js +10 -69
  87. package/dist/routes/shared.js.map +1 -1
  88. package/dist/routes/ui.d.ts.map +1 -1
  89. package/dist/routes/ui.js +4 -10
  90. package/dist/routes/ui.js.map +1 -1
  91. package/dist/server.d.ts.map +1 -1
  92. package/dist/server.js +1 -31
  93. package/dist/server.js.map +1 -1
  94. package/dist/skills.d.ts +3 -6
  95. package/dist/skills.d.ts.map +1 -1
  96. package/dist/skills.js +9 -38
  97. package/dist/skills.js.map +1 -1
  98. package/dist/timeout.d.ts.map +1 -1
  99. package/dist/timeout.js +2 -4
  100. package/dist/timeout.js.map +1 -1
  101. package/dist/types.d.ts +8 -61
  102. package/dist/types.d.ts.map +1 -1
  103. package/dist/ui.d.ts +1 -2
  104. package/dist/ui.d.ts.map +1 -1
  105. package/dist/ui.js +5 -35
  106. package/dist/ui.js.map +1 -1
  107. package/dist/version.d.ts +1 -1
  108. package/dist/version.js +1 -1
  109. package/package.json +3 -2
  110. package/src/activity.ts +0 -9
  111. package/src/auth/bearer.ts +3 -32
  112. package/src/auth/clerk.ts +35 -48
  113. package/src/auth/downstream-oauth.ts +106 -24
  114. package/src/call-admission.ts +0 -1
  115. package/src/catalog-service.ts +23 -23
  116. package/src/catalog.ts +55 -0
  117. package/src/connectors/remote-mcp.ts +106 -86
  118. package/src/errors.ts +22 -0
  119. package/src/execute.ts +43 -24
  120. package/src/executor-result.ts +1 -1
  121. package/src/index.ts +24 -208
  122. package/src/invocation.ts +63 -10
  123. package/src/meta-tools.ts +139 -187
  124. package/src/operator-ui/browser.css +1 -54
  125. package/src/operator-ui/browser.ts +16 -150
  126. package/src/operator-ui/generated.ts +2 -2
  127. package/src/operator-ui/model.ts +1 -23
  128. package/src/registry.ts +59 -429
  129. package/src/retired-toolkits.ts +14 -0
  130. package/src/routes/activity.ts +1 -6
  131. package/src/routes/credentials.ts +0 -12
  132. package/src/routes/mcp.ts +110 -230
  133. package/src/routes/oauth.ts +2 -8
  134. package/src/routes/shared.ts +10 -100
  135. package/src/routes/ui.ts +2 -12
  136. package/src/server.ts +1 -31
  137. package/src/skills.ts +9 -38
  138. package/src/timeout.ts +2 -4
  139. package/src/types.ts +11 -62
  140. package/src/ui.ts +9 -46
  141. package/src/version.ts +1 -1
  142. package/dist/credential-health.d.ts +0 -205
  143. package/dist/credential-health.d.ts.map +0 -1
  144. package/dist/credential-health.js +0 -610
  145. package/dist/credential-health.js.map +0 -1
  146. package/dist/toolkits.d.ts +0 -124
  147. package/dist/toolkits.d.ts.map +0 -1
  148. package/dist/toolkits.js +0 -322
  149. package/dist/toolkits.js.map +0 -1
  150. package/src/credential-health.ts +0 -835
  151. package/src/toolkits.ts +0 -461
package/src/toolkits.ts DELETED
@@ -1,461 +0,0 @@
1
- // Toolkits: named, operator-defined scoped views over one deployment's
2
- // registry, selected per client connection with `?toolkit=<name>` on /mcp.
3
- //
4
- // A connecta deployment belongs to an ORG; a toolkit is the view a GROUP OF
5
- // TEAM MEMBERS inside that org gets — a "support" toolkit seeing Zendesk and
6
- // Notion, an "exec" toolkit that also sees Gmail. This module only *defines and
7
- // validates* scopes and the identity bindings that gate them. Enforcement lives
8
- // in two places, each with one job:
9
- //
10
- // - WHICH toolkit an identity may open: the connect-time binding check in
11
- // `resolveToolkitScope` (src/server.ts), run after the auth gate and before
12
- // any scoped registry exists.
13
- // - WHAT a selected toolkit may see: `ScopedRegistry` (src/registry.ts),
14
- // which every meta-tool inherits through `RegistryView`.
15
-
16
- import type { Connector, InboundAuth, ToolkitBinding } from "./types.js";
17
-
18
- /** Toolkit names share the connector-id grammar: URL-safe, no separators. */
19
- export const TOOLKIT_NAME_RE = /^[a-z0-9_-]+$/;
20
-
21
- /** One named scope, declared in `ConnectaConfig.toolkits` (config as code). */
22
- export interface ToolkitDefinition {
23
- /** Connector ids this toolkit may see. Required, and at least one. */
24
- connectors: string[];
25
- /**
26
- * Optional finer grain: full tool addresses (`"<connectorId>.<toolName>"`).
27
- * Naming ANY address of a connector narrows that connector to exactly the
28
- * addresses named; connectors with no entry here keep their whole tool list.
29
- */
30
- includeTools?: string[];
31
- /** Optional tool addresses to hide, applied after `includeTools`. */
32
- excludeTools?: string[];
33
- /** Operator note. Never sent to clients — this is documentation for config. */
34
- description?: string;
35
- }
36
-
37
- /** `ConnectaConfig.toolkits` — toolkit name → definition. */
38
- export type ToolkitConfig = Record<string, ToolkitDefinition>;
39
-
40
- /** A validated toolkit: the visibility predicate the scoped registry consults. */
41
- export interface Toolkit {
42
- readonly name: string;
43
- readonly description?: string;
44
- /**
45
- * The validated config this scope came from. Kept as immutable data so
46
- * deployment-wide operator surfaces can explain the running configuration
47
- * without inventing a second source of truth.
48
- */
49
- readonly connectors: readonly string[];
50
- readonly includeTools: readonly string[];
51
- readonly excludeTools: readonly string[];
52
- /** True when `connectorId` is inside this toolkit's scope. */
53
- hasConnector(connectorId: string): boolean;
54
- /** True when `<connectorId>.<toolName>` is inside this toolkit's scope. */
55
- hasTool(connectorId: string, toolName: string): boolean;
56
- }
57
-
58
- /**
59
- * Split `"<connectorId>.<toolName>"` on the FIRST dot — connector ids contain
60
- * no dots, so a downstream tool name may. Returns null for a malformed address.
61
- */
62
- export function splitAddress(
63
- address: string,
64
- ): { connectorId: string; toolName: string } | null {
65
- const dot = address.indexOf(".");
66
- if (dot <= 0 || dot === address.length - 1) return null;
67
- return {
68
- connectorId: address.slice(0, dot),
69
- toolName: address.slice(dot + 1),
70
- };
71
- }
72
-
73
- /** Group tool addresses by connector id, validating each against the toolkit. */
74
- function toolFilter(
75
- name: string,
76
- addresses: string[] | undefined,
77
- connectorIds: ReadonlySet<string>,
78
- staticTools: ReadonlyMap<string, ReadonlySet<string>>,
79
- field: "includeTools" | "excludeTools",
80
- ): Map<string, Set<string>> {
81
- const byConnector = new Map<string, Set<string>>();
82
- if (addresses !== undefined && !Array.isArray(addresses)) {
83
- // A bare string would otherwise iterate character by character and produce
84
- // a stream of confusing address errors; anything else would throw "not
85
- // iterable" from deep inside the loop. Name the field instead.
86
- throw new Error(
87
- `Toolkit "${name}" ${field} must be an array of "<connectorId>.<toolName>" addresses.`,
88
- );
89
- }
90
- if (
91
- addresses !== undefined &&
92
- addresses.length === 0 &&
93
- field === "includeTools"
94
- ) {
95
- // An empty allowlist reads as "only these tools" but would behave as "all
96
- // of them" — the one shape here that fails OPEN. (An empty excludeTools is
97
- // an honest no-op and is allowed.)
98
- throw new Error(
99
- `Toolkit "${name}" has an empty includeTools: remove it to expose every tool, or list the addresses this toolkit may use.`,
100
- );
101
- }
102
- for (const address of addresses ?? []) {
103
- const parts = splitAddress(address);
104
- if (!parts) {
105
- throw new Error(
106
- `Toolkit "${name}" ${field} entry "${address}" is not a tool address: expected "<connectorId>.<toolName>".`,
107
- );
108
- }
109
- if (!connectorIds.has(parts.connectorId)) {
110
- // A typo here would silently do nothing, quietly widening the scope the
111
- // operator believes they wrote. Fail at construction instead.
112
- throw new Error(
113
- `Toolkit "${name}" ${field} entry "${address}" names connector "${parts.connectorId}", which is not in this toolkit's connectors list.`,
114
- );
115
- }
116
- // Static-only, exactly like the registry's convention checks: an in-code
117
- // connector's tool list is known now, so a misspelled name — an exclude
118
- // that silently excludes nothing — is caught. Remote catalogs are fetched
119
- // lazily over the network and cannot be checked at construction.
120
- const known = staticTools.get(parts.connectorId);
121
- if (known && !known.has(parts.toolName)) {
122
- throw new Error(
123
- `Toolkit "${name}" ${field} entry "${address}" names no tool on connector "${parts.connectorId}".`,
124
- );
125
- }
126
- const tools = byConnector.get(parts.connectorId) ?? new Set<string>();
127
- tools.add(parts.toolName);
128
- byConnector.set(parts.connectorId, tools);
129
- }
130
- return byConnector;
131
- }
132
-
133
- /**
134
- * Validate one toolkit definition against the deployment's connectors.
135
- *
136
- * Structural mistakes THROW at construction rather than warn: a typo'd id in
137
- * an allowlist is a scope the operator did not write, and a scope nobody wrote
138
- * is not one an operator can reason about. (A definition scopes visibility only;
139
- * WHICH identity may select it is the separate binding below — see the module
140
- * header and documentation/toolkits.md.) Tool names are checked only for connectors that expose
141
- * `staticTools` (i.e. `api()`); a remote connector's catalog is fetched lazily
142
- * over the network and is unknown at construction time.
143
- */
144
- function resolveToolkit(
145
- name: string,
146
- definition: ToolkitDefinition,
147
- known: ReadonlySet<string>,
148
- staticTools: ReadonlyMap<string, ReadonlySet<string>>,
149
- ): Toolkit {
150
- if (!TOOLKIT_NAME_RE.test(name)) {
151
- throw new Error(
152
- `Invalid toolkit name "${name}": must match ${TOOLKIT_NAME_RE.source}`,
153
- );
154
- }
155
- if (
156
- !Array.isArray(definition.connectors) ||
157
- definition.connectors.length === 0
158
- ) {
159
- throw new Error(
160
- `Toolkit "${name}" selects no connectors: list at least one connector id in "connectors".`,
161
- );
162
- }
163
- const connectorIds = new Set<string>();
164
- for (const id of definition.connectors) {
165
- if (!known.has(id)) {
166
- throw new Error(
167
- `Toolkit "${name}" references unknown connector "${id}".`,
168
- );
169
- }
170
- connectorIds.add(id);
171
- }
172
- const includes = toolFilter(
173
- name,
174
- definition.includeTools,
175
- connectorIds,
176
- staticTools,
177
- "includeTools",
178
- );
179
- const excludes = toolFilter(
180
- name,
181
- definition.excludeTools,
182
- connectorIds,
183
- staticTools,
184
- "excludeTools",
185
- );
186
- return {
187
- name,
188
- ...(definition.description ? { description: definition.description } : {}),
189
- connectors: Object.freeze([...connectorIds]),
190
- includeTools: Object.freeze([...(definition.includeTools ?? [])]),
191
- excludeTools: Object.freeze([...(definition.excludeTools ?? [])]),
192
- hasConnector: (connectorId) => connectorIds.has(connectorId),
193
- hasTool: (connectorId, toolName) => {
194
- if (!connectorIds.has(connectorId)) return false;
195
- const include = includes.get(connectorId);
196
- if (include && !include.has(toolName)) return false;
197
- return !excludes.get(connectorId)?.has(toolName);
198
- },
199
- };
200
- }
201
-
202
- /**
203
- * Validate every declared toolkit against the connector set. Returns undefined
204
- * when no toolkits are configured, so an existing deployment keeps exactly its
205
- * current (unscoped) behavior.
206
- */
207
- export function resolveToolkits(
208
- toolkits: ToolkitConfig | undefined,
209
- connectors: readonly Connector[],
210
- ): ReadonlyMap<string, Toolkit> | undefined {
211
- if (!toolkits) return undefined;
212
- // Object.entries (not a keyed lookup) so no config key — `__proto__` and
213
- // friends included — can ever resolve through the prototype chain. Names are
214
- // then held in a Map, which has no prototype to pollute.
215
- const entries = Object.entries(toolkits);
216
- if (entries.length === 0) return undefined;
217
- const known = new Set(connectors.map((connector) => connector.id));
218
- const staticTools = new Map<string, ReadonlySet<string>>();
219
- for (const connector of connectors) {
220
- if (connector.staticTools) {
221
- staticTools.set(
222
- connector.id,
223
- new Set(connector.staticTools.map((tool) => tool.name)),
224
- );
225
- }
226
- }
227
- const resolved = new Map<string, Toolkit>();
228
- for (const [name, definition] of entries) {
229
- resolved.set(name, resolveToolkit(name, definition, known, staticTools));
230
- }
231
- return resolved;
232
- }
233
-
234
- /**
235
- * The binding half of an inbound-auth adapter's options — the shape every
236
- * shipped adapter (`bearerToken`, `clerkAuth`) mixes into its own options so an
237
- * operator writes one thing in one style, next to the credential it binds.
238
- */
239
- export interface ToolkitBindingOptions {
240
- /**
241
- * Toolkit names this credential may select with `?toolkit=<name>`. Present ⇒
242
- * the identity is BOUND: any other toolkit, and (unless `unscoped`) a
243
- * connection with no `?toolkit=`, is refused at connect time. Absent ⇒
244
- * unbound, exactly as before bindings existed.
245
- */
246
- toolkits?: readonly string[];
247
- /**
248
- * Also allow a connection with no `?toolkit=` (the full registry, and the
249
- * deployment-wide operator surfaces). Only meaningful beside `toolkits`.
250
- */
251
- unscoped?: boolean;
252
- }
253
-
254
- /**
255
- * Validate one adapter's binding options into a `ToolkitBinding`, or undefined
256
- * when the adapter declares none. Structural mistakes THROW where the operator
257
- * wrote them (adapter construction), for the same reason toolkit definitions do:
258
- * a binding that does not say what its author meant is worse than none, because
259
- * it is invisible until the day it denies — or admits — the wrong caller.
260
- *
261
- * Names are only checked against the *grammar* here; cross-checking them
262
- * against the configured toolkits happens in `validateToolkitBindings`, which
263
- * runs in `createConnecta` where both halves are finally in scope.
264
- */
265
- export function resolveToolkitBinding(
266
- source: string,
267
- options: ToolkitBindingOptions,
268
- ): ToolkitBinding | undefined {
269
- const { toolkits, unscoped } = options;
270
- if (toolkits === undefined) {
271
- if (unscoped !== undefined) {
272
- // `unscoped` alone reads like a permission but grants nothing an unbound
273
- // identity does not already have, so it is almost certainly a half-written
274
- // binding — the one shape here that would silently fail OPEN.
275
- throw new Error(
276
- `${source}: \`unscoped\` only means something beside \`toolkits\`. ` +
277
- "List the toolkits this credential may open, or drop `unscoped` to " +
278
- "leave the credential unbound.",
279
- );
280
- }
281
- return undefined;
282
- }
283
- if (!Array.isArray(toolkits)) {
284
- throw new Error(
285
- `${source}: \`toolkits\` must be an array of toolkit names.`,
286
- );
287
- }
288
- const names: string[] = [];
289
- for (const name of toolkits) {
290
- if (typeof name !== "string" || !TOOLKIT_NAME_RE.test(name)) {
291
- // A name outside the grammar can never match a declared toolkit, so this
292
- // would bind the credential to nothing selectable.
293
- throw new Error(
294
- `${source}: \`toolkits\` entry ${JSON.stringify(name)} is not a ` +
295
- `toolkit name (must match ${TOOLKIT_NAME_RE.source}).`,
296
- );
297
- }
298
- if (!names.includes(name)) names.push(name);
299
- }
300
- if (names.length === 0 && unscoped !== true) {
301
- throw new Error(
302
- `${source}: binds no toolkits and no unscoped access, so this credential ` +
303
- "could authenticate but never connect. List at least one toolkit, or " +
304
- "pass `unscoped: true` to bind it to the full registry only.",
305
- );
306
- }
307
- return Object.freeze({
308
- toolkits: Object.freeze(names) as readonly string[],
309
- ...(unscoped === true ? { unscoped: true } : {}),
310
- });
311
- }
312
-
313
- /**
314
- * Coerce an arbitrary value into a `ToolkitBinding`, or null when it is not one.
315
- *
316
- * The shipped adapters build bindings through `resolveToolkitBinding` above, but
317
- * `InboundAuth` is an open interface and `AuthResult.toolkitBinding` arrives at
318
- * REQUEST time from code connecta does not own — a custom adapter, or one
319
- * mapping an IdP claim. Every field is therefore re-checked here rather than
320
- * trusted from the type, because each way of being wrong fails OPEN if it is
321
- * merely believed:
322
- *
323
- * - `unscoped` is compared to `true` by identity, so a truthy non-boolean (the
324
- * string `"false"` out of an env var, say) cannot grant the full registry;
325
- * - `toolkits` must be a real array — a bare string would otherwise reach
326
- * `String.prototype.includes`, where `?toolkit=sup` would "match" `"support"`
327
- * by substring;
328
- * - a missing/!array `toolkits` is not treated as an empty binding, because the
329
- * caller of a null return refuses the request outright.
330
- *
331
- * Returns a frozen, deduplicated copy: nothing downstream can be mutated by the
332
- * adapter after the check, and every name is known to fit the grammar.
333
- */
334
- function normalizeToolkitBinding(value: unknown): ToolkitBinding | null {
335
- if (typeof value !== "object" || value === null || Array.isArray(value)) {
336
- return null;
337
- }
338
- const { toolkits, unscoped } = value as {
339
- toolkits?: unknown;
340
- unscoped?: unknown;
341
- };
342
- if (!Array.isArray(toolkits)) return null;
343
- if (unscoped !== undefined && typeof unscoped !== "boolean") return null;
344
- const names: string[] = [];
345
- for (const name of toolkits) {
346
- if (typeof name !== "string" || !TOOLKIT_NAME_RE.test(name)) return null;
347
- if (!names.includes(name)) names.push(name);
348
- }
349
- return Object.freeze({
350
- toolkits: Object.freeze(names) as readonly string[],
351
- ...(unscoped === true ? { unscoped: true } : {}),
352
- });
353
- }
354
-
355
- /**
356
- * Resolve the binding one admitted identity is actually held to, from the
357
- * provider's static declaration and whatever its `authorize` returned.
358
- *
359
- * - Neither ⇒ unbound (undefined), the pre-binding behavior.
360
- * - Declaration only ⇒ the declaration.
361
- * - Per-identity only ⇒ that binding, validated. This is the custom-adapter
362
- * seam: a provider that declares nothing is asserting it resolves membership
363
- * itself, so there is nothing to check it against.
364
- * - Both ⇒ the **intersection**. The declaration is a CEILING, not a default: an
365
- * adapter that maps a user-writable IdP claim to toolkits must not be able to
366
- * widen the credential's own binding, which would turn "support token" into
367
- * "any toolkit, plus the full registry" for anyone who can set that claim.
368
- * Narrowing is fine and useful (per-user subsets of the team's view).
369
- *
370
- * A malformed binding on either side is not silently ignored — it returns
371
- * `{ ok: false }` and the caller refuses the request, because the alternative
372
- * (dropping it) is the fail-open reading.
373
- */
374
- export function resolveIdentityBinding(
375
- declared: unknown,
376
- perIdentity: unknown,
377
- ):
378
- | { ok: true; binding?: ToolkitBinding }
379
- | { ok: false; reason: string } {
380
- const ceiling =
381
- declared === undefined ? undefined : normalizeToolkitBinding(declared);
382
- if (declared !== undefined && !ceiling) {
383
- return {
384
- ok: false,
385
- reason:
386
- "the toolkit binding declared on the provider is malformed " +
387
- "(`toolkits` must be an array of toolkit names, `unscoped` a boolean)",
388
- };
389
- }
390
- if (perIdentity === undefined) {
391
- return ceiling ? { ok: true, binding: ceiling } : { ok: true };
392
- }
393
- const identity = normalizeToolkitBinding(perIdentity);
394
- if (!identity) {
395
- return {
396
- ok: false,
397
- reason:
398
- "the toolkit binding its authorize() returned for this identity is " +
399
- "malformed (`toolkits` must be an array of toolkit names, `unscoped` " +
400
- "a boolean)",
401
- };
402
- }
403
- if (!ceiling) return { ok: true, binding: identity };
404
- return {
405
- ok: true,
406
- binding: Object.freeze({
407
- toolkits: Object.freeze(
408
- identity.toolkits.filter((name) => ceiling.toolkits.includes(name)),
409
- ) as readonly string[],
410
- ...(identity.unscoped === true && ceiling.unscoped === true
411
- ? { unscoped: true }
412
- : {}),
413
- }),
414
- };
415
- }
416
-
417
- /**
418
- * Cross-check every statically declared binding against the deployment's
419
- * toolkits, in `createConnecta`. A name that no toolkit declares is a typo, and
420
- * a typo here fails CLOSED — the credential would be refused every connection
421
- * with a 403 the client reads as a transport failure — so it throws at
422
- * construction rather than becoming a support ticket. A structurally malformed
423
- * declaration (only reachable from a hand-written `InboundAuth`, since the
424
- * shipped adapters validate their own options) throws here too, rather than
425
- * waiting to refuse every request at runtime.
426
- *
427
- * Bindings a provider mints per-identity (`AuthResult.toolkitBinding`) do not
428
- * exist yet and cannot be checked here; they are validated on arrival and capped
429
- * by the declaration (`resolveIdentityBinding`).
430
- */
431
- export function validateToolkitBindings(
432
- auth: readonly InboundAuth[],
433
- toolkits: ReadonlyMap<string, Toolkit> | undefined,
434
- ): void {
435
- for (const provider of auth) {
436
- if (provider.toolkitBinding === undefined) continue;
437
- const binding = normalizeToolkitBinding(provider.toolkitBinding);
438
- if (!binding) {
439
- throw new Error(
440
- `Inbound auth provider "${provider.kind}" declares a malformed ` +
441
- "toolkitBinding: `toolkits` must be an array of toolkit names " +
442
- `(matching ${TOOLKIT_NAME_RE.source}) and \`unscoped\` a boolean.`,
443
- );
444
- }
445
- if (!toolkits || toolkits.size === 0) {
446
- throw new Error(
447
- `Inbound auth provider "${provider.kind}" binds toolkits ` +
448
- `(${binding.toolkits.join(", ")}) but this deployment configures no ` +
449
- "toolkits. Declare them in `toolkits`, or drop the binding.",
450
- );
451
- }
452
- for (const name of binding.toolkits) {
453
- if (!toolkits.has(name)) {
454
- throw new Error(
455
- `Inbound auth provider "${provider.kind}" binds unknown toolkit ` +
456
- `"${name}". Configured toolkits: ${[...toolkits.keys()].join(", ")}.`,
457
- );
458
- }
459
- }
460
- }
461
- }