@jini-ai/integrations 0.6.19 → 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.
- package/CHANGELOG.md +48 -0
- package/dist/api-connections/access-token.js +4 -14
- package/dist/api-connections/account-actions.d.ts +6 -1
- package/dist/api-connections/attempt-state.d.ts +2 -2
- package/dist/api-connections/attempt-state.js +10 -11
- package/dist/api-connections/authorization.js +13 -20
- package/dist/api-connections/budgets.d.ts +91 -0
- package/dist/api-connections/budgets.js +87 -0
- package/dist/api-connections/byo-client.d.ts +38 -0
- package/dist/api-connections/byo-client.js +28 -0
- package/dist/api-connections/connect.d.ts +87 -0
- package/dist/api-connections/connect.js +143 -0
- package/dist/api-connections/declarations.d.ts +68 -0
- package/dist/api-connections/declarations.js +178 -0
- package/dist/api-connections/deps.d.ts +4 -1
- package/dist/api-connections/descriptor.d.ts +15 -0
- package/dist/api-connections/descriptor.js +1 -5
- package/dist/api-connections/finish-authorization.d.ts +1 -1
- package/dist/api-connections/hook-http.d.ts +20 -0
- package/dist/api-connections/hook-http.js +27 -0
- package/dist/api-connections/host-ports.d.ts +244 -0
- package/dist/api-connections/host-ports.js +2 -0
- package/dist/api-connections/host.d.ts +81 -0
- package/dist/api-connections/host.js +320 -0
- package/dist/api-connections/hosted-mcp/contracts.d.ts +291 -0
- package/dist/api-connections/hosted-mcp/contracts.js +2 -0
- package/dist/api-connections/hosted-mcp/index.d.ts +5 -0
- package/dist/api-connections/hosted-mcp/index.js +3 -0
- package/dist/api-connections/hosted-mcp/shared-sign-in.d.ts +167 -0
- package/dist/api-connections/hosted-mcp/shared-sign-in.js +293 -0
- package/dist/api-connections/hosted-mcp/sign-in.d.ts +25 -0
- package/dist/api-connections/hosted-mcp/sign-in.js +889 -0
- package/dist/api-connections/hosted-sign-in.d.ts +48 -0
- package/dist/api-connections/hosted-sign-in.js +51 -0
- package/dist/api-connections/index.d.ts +23 -1
- package/dist/api-connections/index.js +15 -0
- package/dist/api-connections/install-credential.d.ts +1 -1
- package/dist/api-connections/oauth-capabilities.d.ts +63 -0
- package/dist/api-connections/oauth-capabilities.js +2 -0
- package/dist/api-connections/ports.d.ts +1 -1
- package/dist/api-connections/request-checks.d.ts +11 -1
- package/dist/api-connections/request-checks.js +2 -3
- package/dist/api-connections/shared.d.ts +79 -0
- package/dist/api-connections/shared.js +106 -0
- package/dist/api-connections/token-store/record.d.ts +2 -2
- package/dist/api-connections/token-store/vault-token-store.d.ts +30 -4
- package/dist/api-connections/token-store/vault-token-store.js +1 -4
- package/dist/credentialed-http/sets/host.d.ts +146 -0
- package/dist/credentialed-http/sets/host.js +2 -0
- package/dist/credentialed-http/sets/index.d.ts +24 -0
- package/dist/credentialed-http/sets/index.js +24 -0
- package/dist/credentialed-http/sets/store.d.ts +193 -0
- package/dist/credentialed-http/sets/store.js +489 -0
- package/dist/credentialed-http/sets/vault-repo.d.ts +81 -0
- package/dist/credentialed-http/sets/vault-repo.js +147 -0
- package/dist/credentialed-http/tools/credential-copy-tool.d.ts +34 -0
- package/dist/credentialed-http/tools/credential-copy-tool.js +132 -0
- package/dist/credentialed-http/tools/credential-tool-host.d.ts +152 -0
- package/dist/credentialed-http/tools/credential-tool-host.js +2 -0
- package/dist/credentialed-http/tools/credential-tool-operations.d.ts +370 -0
- package/dist/credentialed-http/tools/credential-tool-operations.js +2 -0
- package/dist/credentialed-http/tools/index.d.ts +7 -0
- package/dist/credentialed-http/tools/index.js +4 -0
- package/dist/credentialed-http/tools/tool-registrations.d.ts +55 -0
- package/dist/credentialed-http/tools/tool-registrations.js +729 -0
- package/dist/webhooks/signing.js +2 -1
- package/package.json +34 -11
|
@@ -0,0 +1,489 @@
|
|
|
1
|
+
import { credentialSetVaultRecord, credentialSetVaultScope } from "./vault-repo.js";
|
|
2
|
+
/**
|
|
3
|
+
* @file Credential-set validation and CRUD over a host-supplied vault. Public summaries contain
|
|
4
|
+
* saved origins, usernames and safe hints only. Tokens live in sealed JSON; boot/recovery owns legacy
|
|
5
|
+
* decoding. Label selection and origin checks never open unrelated credentials. Every product value
|
|
6
|
+
* (owner, metadata policy, categories, scope policy, token policy, wording) arrives as the required
|
|
7
|
+
* `host` input (`./host.ts`); there is no default for any of them.
|
|
8
|
+
* Provenance: moved unchanged (EXTRACT-S8 C1 move) from the first host's credential-set seam file
|
|
9
|
+
* `sets/store.ts` at its commit 43a0e5d4; earlier history lives in that host's repository.
|
|
10
|
+
*/
|
|
11
|
+
const MAX_LABEL_LENGTH = 200;
|
|
12
|
+
export class CustomCredentialValidationError extends Error {
|
|
13
|
+
}
|
|
14
|
+
/** A `(workspaceId, label)` collision — the route maps this to `409 DUPLICATE_LABEL`. */
|
|
15
|
+
export class CustomCredentialDuplicateLabelError extends Error {
|
|
16
|
+
}
|
|
17
|
+
/** A share copied an older revision; the concurrently changed site row remains intact. */
|
|
18
|
+
export class CustomCredentialConcurrentModificationError extends Error {
|
|
19
|
+
}
|
|
20
|
+
/** Thrown when `sealer.seal()`/`keyring.activeKey()` fails while writing a connection — the
|
|
21
|
+
* realistic cause is a missing store key (a host environment setting), same fail-closed
|
|
22
|
+
* contract every sibling credential table's store documents. */
|
|
23
|
+
export class CustomCredentialSecretStoreUnconfiguredError extends Error {
|
|
24
|
+
}
|
|
25
|
+
export class CustomCredentialNotFoundError extends Error {
|
|
26
|
+
}
|
|
27
|
+
/** Safe hints are stored with the envelope; no per-row decrypt is needed for readback. `scope` is
|
|
28
|
+
* reported only where a project store is attached, so a site-only server's summaries are unchanged. */
|
|
29
|
+
async function toSummary(record, optional = {}) {
|
|
30
|
+
const row = record.vaultRecord;
|
|
31
|
+
return { credentialType: "custom", worksFor: [...(record.worksFor ?? [])], id: record.id, label: record.label, category: record.category, baseUrl: record.baseUrl,
|
|
32
|
+
additionalHosts: record.additionalHosts, ...(record.username !== undefined ? { username: record.username } : {}),
|
|
33
|
+
configured: true, tokenHint: row?.tokenLength === null || row?.tokenLength === undefined ? null :
|
|
34
|
+
{ length: row.tokenLength, last4: row.tokenLength >= 12 ? row.last4 : null },
|
|
35
|
+
...(optional.scope ? { scope: optional.scope } : {}),
|
|
36
|
+
createdAt: record.createdAt, updatedAt: record.updatedAt };
|
|
37
|
+
}
|
|
38
|
+
/** The summary's `scope`, only when a project store is attached. */
|
|
39
|
+
const reportedScope = (deps, scoped) => deps.repo.project ? { scope: scoped.scope } : {};
|
|
40
|
+
/** The row with this id, from the first store holding it (ids are unique across both). @complexity O(scopes). */
|
|
41
|
+
async function locateById(host, deps, input) {
|
|
42
|
+
for (const scoped of host.scopes(deps, input.workspaceId)) {
|
|
43
|
+
const record = await scoped.deps.repo.findById({ workspaceId: scoped.workspaceId, id: input.id });
|
|
44
|
+
if (record)
|
|
45
|
+
return { scoped, record };
|
|
46
|
+
}
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Every row this site uses, in label order: its own rows, then project rows whose label no site row
|
|
51
|
+
* already claims (a site row of the same label is the override).
|
|
52
|
+
* @complexity O(n log n) in the two stores' (small) credential counts; no row is opened.
|
|
53
|
+
*/
|
|
54
|
+
async function listEffective(host, deps, workspaceId) {
|
|
55
|
+
const found = [];
|
|
56
|
+
const labels = new Set();
|
|
57
|
+
for (const scoped of host.scopes(deps, workspaceId)) {
|
|
58
|
+
const records = await scoped.deps.repo.listByWorkspace({ workspaceId: scoped.workspaceId });
|
|
59
|
+
for (const record of records.filter((row) => !labels.has(row.label)))
|
|
60
|
+
found.push({ scoped, record });
|
|
61
|
+
for (const record of records)
|
|
62
|
+
labels.add(record.label);
|
|
63
|
+
}
|
|
64
|
+
return found.sort((a, b) => host.vault.compareLabels(a.record, b.record));
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* The read model for ONE credential set. Pure DB read — no sealer, no keyring, cannot fail on a
|
|
68
|
+
* misconfigured site key. Returns `null` if no row exists for `(workspaceId, id)` (not an
|
|
69
|
+
* error — the caller decides whether that is a 404).
|
|
70
|
+
*
|
|
71
|
+
* @agent-note Metadata only: never opens an envelope.
|
|
72
|
+
* @complexity O(1) — one `findById` lookup.
|
|
73
|
+
*/
|
|
74
|
+
export async function describeCredentialSet({ host, deps, input }, _optional = {}) {
|
|
75
|
+
const located = await locateById(host, deps, input);
|
|
76
|
+
return located ? toSummary(located.record, reportedScope(deps, located.scoped)) : null;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* The non-decrypting sibling of {@link resolveCredentialSetByLabel}: finds a credential by its
|
|
80
|
+
* exact label and returns its read model — `baseUrl`/`additionalHosts` included, since both are
|
|
81
|
+
* plaintext metadata — WITHOUT ever touching `sealer`/`keyring`. Exists so a caller that only needs
|
|
82
|
+
* to validate a request's target origin or render a confirmation dialog never has to decrypt just to
|
|
83
|
+
* read two plaintext fields — the same "never touches the sealer" contract
|
|
84
|
+
* {@link describeCredentialSet} already documents, applied to a label lookup instead of an id lookup.
|
|
85
|
+
*
|
|
86
|
+
* @agent-note Metadata only: never opens an envelope.
|
|
87
|
+
* @complexity O(n) in the workspace's own (small) credential-set count, or one indexed read where
|
|
88
|
+
* the repo supplies `findByLabel`.
|
|
89
|
+
*/
|
|
90
|
+
export async function describeCredentialSetByLabel({ host, deps, input }, _optional = {}) {
|
|
91
|
+
const located = await locateByLabel({ host, deps, input });
|
|
92
|
+
return located ? toSummary(located.record, reportedScope(deps, located.scoped)) : null;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* The effective credential sets this workspace uses. The admin inventory has its own unfiltered
|
|
96
|
+
* read model below. Same "never decrypts" contract as {@link describeCredentialSet}.
|
|
97
|
+
*
|
|
98
|
+
* @complexity O(n) in the workspace's own (small) credential-set count. One repo read, one array
|
|
99
|
+
* map, no per-row I/O.
|
|
100
|
+
*/
|
|
101
|
+
export async function listCredentialSets({ host, deps, input }, _optional = {}) {
|
|
102
|
+
const located = await listEffective(host, deps, input.workspaceId);
|
|
103
|
+
return Promise.all(located.map(({ scoped, record }) => toSummary(record, reportedScope(deps, scoped))));
|
|
104
|
+
}
|
|
105
|
+
/** Admin inventory includes unattached project keys and same-label site overrides. The existing
|
|
106
|
+
* guarded metadata repo exposes those rows without changing runtime credential resolution.
|
|
107
|
+
* @param required Host policy, metadata repositories and the current site's workspace id.
|
|
108
|
+
* @returns Safe summaries only; never opens an envelope.
|
|
109
|
+
* @throws Repository or metadata-validation errors propagate to the admin adapter.
|
|
110
|
+
* @complexity O(n log n) time and O(n) space for n saved keys; at most two store reads, no per-key I/O.
|
|
111
|
+
* @example await listCredentialSetInventory({ host, deps, workspaceId });
|
|
112
|
+
*/
|
|
113
|
+
export async function listCredentialSetInventory({ host, deps, workspaceId }, _optional = {}) {
|
|
114
|
+
const stores = await Promise.all(host.scopes(deps, workspaceId).map(async (scoped) => {
|
|
115
|
+
const source = scoped.scope === "project" ? deps.repo.project?.inventoryRepo ?? scoped.deps.repo : scoped.deps.repo;
|
|
116
|
+
const records = await source.listByWorkspace({ workspaceId: scoped.workspaceId });
|
|
117
|
+
return Promise.all(records.map(record => toSummary(record, reportedScope(deps, scoped))));
|
|
118
|
+
}));
|
|
119
|
+
return stores.flat().sort((a, b) => a.label.localeCompare(b.label));
|
|
120
|
+
}
|
|
121
|
+
/** Narrows and validates a caller-supplied `label`. Never throws a raw `TypeError` — every
|
|
122
|
+
* rejection is a {@link CustomCredentialValidationError} the route layer can map to its own `400`. */
|
|
123
|
+
function validateLabel(host, raw) {
|
|
124
|
+
if (typeof raw !== "string" || raw.trim() === "") {
|
|
125
|
+
throw new CustomCredentialValidationError("label must be a non-empty string");
|
|
126
|
+
}
|
|
127
|
+
if (raw.length > MAX_LABEL_LENGTH) {
|
|
128
|
+
throw new CustomCredentialValidationError(`label must be ${MAX_LABEL_LENGTH} characters or fewer`);
|
|
129
|
+
}
|
|
130
|
+
assertPlainField(host, { value: raw, field: "label" });
|
|
131
|
+
return raw;
|
|
132
|
+
}
|
|
133
|
+
/** Narrows and validates a caller-supplied `category` against the host's own category list (stored
|
|
134
|
+
* as plaintext metadata so it is readable/filterable without ever decrypting a row). */
|
|
135
|
+
function validateCategory(host, raw) {
|
|
136
|
+
if (typeof raw === "string")
|
|
137
|
+
assertPlainField(host, { value: raw, field: "category" });
|
|
138
|
+
if (typeof raw !== "string" || !host.categories.includes(raw)) {
|
|
139
|
+
throw new CustomCredentialValidationError(`category must be one of: ${host.categories.join(", ")}`);
|
|
140
|
+
}
|
|
141
|
+
return raw;
|
|
142
|
+
}
|
|
143
|
+
/** Narrows and validates a caller-supplied `baseUrl` — must parse as an absolute `http`/`https` URL
|
|
144
|
+
* (`new URL()` accepts far more schemes than this form should — `javascript:`, `file:`, a bare
|
|
145
|
+
* `mailto:` — so the scheme is checked explicitly rather than trusting "did not throw"). */
|
|
146
|
+
function validateBaseUrl(host, raw) {
|
|
147
|
+
if (typeof raw !== "string" || raw.trim() === "") {
|
|
148
|
+
throw new CustomCredentialValidationError("baseUrl must be a non-empty string");
|
|
149
|
+
}
|
|
150
|
+
let parsed;
|
|
151
|
+
try {
|
|
152
|
+
parsed = new URL(raw);
|
|
153
|
+
}
|
|
154
|
+
catch {
|
|
155
|
+
throw new CustomCredentialValidationError("baseUrl must be a valid absolute URL");
|
|
156
|
+
}
|
|
157
|
+
if (parsed.protocol !== "http:" && parsed.protocol !== "https:") {
|
|
158
|
+
throw new CustomCredentialValidationError("baseUrl must use http or https");
|
|
159
|
+
}
|
|
160
|
+
assertPlainField(host, { value: raw, field: "baseUrl" });
|
|
161
|
+
return raw;
|
|
162
|
+
}
|
|
163
|
+
/** One `additionalHosts` entry's own validation — split out of {@link validateAdditionalHosts} so
|
|
164
|
+
* that function's `.map()` body stays a single call, and so each rejection names its own index. */
|
|
165
|
+
function validateAdditionalHostEntry(host, raw, index) {
|
|
166
|
+
if (typeof raw !== "string" || raw.trim() === "") {
|
|
167
|
+
throw new CustomCredentialValidationError(`additionalHosts[${index}] must be a non-empty string`);
|
|
168
|
+
}
|
|
169
|
+
let parsed;
|
|
170
|
+
try {
|
|
171
|
+
parsed = new URL(raw);
|
|
172
|
+
}
|
|
173
|
+
catch {
|
|
174
|
+
throw new CustomCredentialValidationError(`additionalHosts[${index}] must be a valid absolute URL`);
|
|
175
|
+
}
|
|
176
|
+
if (parsed.protocol !== "http:" && parsed.protocol !== "https:") {
|
|
177
|
+
throw new CustomCredentialValidationError(`additionalHosts[${index}] must use http or https`);
|
|
178
|
+
}
|
|
179
|
+
assertPlainField(host, { value: raw, field: "additionalHosts" });
|
|
180
|
+
return parsed.origin;
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Validates and normalizes the optional `additionalHosts` field (owner-driven multi-host support).
|
|
184
|
+
* `undefined` (the field was omitted) degrades to no extra hosts, matching every other optional
|
|
185
|
+
* field's "omitted is not an error" contract on this store. Each entry is normalized to its own
|
|
186
|
+
* ORIGIN (scheme+host+port, no path) and the result is deduped — this is what lets a credentialed
|
|
187
|
+
* request compare a resolved URL origin against this list with plain `===`/`.includes()`, never
|
|
188
|
+
* re-parsing at request time.
|
|
189
|
+
*
|
|
190
|
+
* @throws {CustomCredentialValidationError} `raw` is not an array, or any entry fails
|
|
191
|
+
* {@link validateAdditionalHostEntry}.
|
|
192
|
+
* @complexity O(n) in the number of supplied hosts.
|
|
193
|
+
*/
|
|
194
|
+
function validateAdditionalHosts(host, raw) {
|
|
195
|
+
if (raw === undefined)
|
|
196
|
+
return [];
|
|
197
|
+
if (!Array.isArray(raw)) {
|
|
198
|
+
throw new CustomCredentialValidationError("additionalHosts must be an array of absolute http/https URLs");
|
|
199
|
+
}
|
|
200
|
+
return [...new Set(raw.map((entry, index) => validateAdditionalHostEntry(host, entry, index)))];
|
|
201
|
+
}
|
|
202
|
+
function optionalString(raw, field) {
|
|
203
|
+
if (raw === undefined)
|
|
204
|
+
return undefined;
|
|
205
|
+
if (typeof raw !== "string" || raw.trim() === "") {
|
|
206
|
+
throw new CustomCredentialValidationError(`'${field}' must be a non-empty string when provided`);
|
|
207
|
+
}
|
|
208
|
+
return raw;
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Validates {@link UpdateCredentialSetInput.username} — a THREE-way field, unlike
|
|
212
|
+
* {@link optionalString}'s two-way "omitted or a non-empty string": this function is only ever
|
|
213
|
+
* called once the caller has already checked `raw !== undefined` (omitted = leave the column alone,
|
|
214
|
+
* a case this function never sees), so the two values left to distinguish are "clear it" and "set
|
|
215
|
+
* it". `null` is the deliberate clear sentinel — chosen specifically because a blank string could not
|
|
216
|
+
* serve double duty as both "leave alone" (the `optionalString` convention every other field on this
|
|
217
|
+
* store already trained a caller to expect from a falsy value) and "clear" without one of those two
|
|
218
|
+
* meanings silently winning over the other; `updateCredentialSet`'s own doc has the full
|
|
219
|
+
* precedence writeup. A blank string is therefore still rejected here, same as everywhere else on
|
|
220
|
+
* this store — it is neither of this field's two valid non-omitted values, not a quiet no-op.
|
|
221
|
+
*
|
|
222
|
+
* @throws {CustomCredentialValidationError} `raw` is present, not `null`, and not a non-empty string.
|
|
223
|
+
* @complexity O(1).
|
|
224
|
+
*/
|
|
225
|
+
function validateUsernamePatch(host, raw) {
|
|
226
|
+
if (raw === null)
|
|
227
|
+
return undefined;
|
|
228
|
+
if (typeof raw !== "string" || raw.trim() === "") {
|
|
229
|
+
throw new CustomCredentialValidationError("'username' must be a non-empty string, or null to clear it");
|
|
230
|
+
}
|
|
231
|
+
assertPlainField(host, { value: raw, field: "username" });
|
|
232
|
+
return raw;
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Validates a caller-supplied `connection` — just `{token, username?}`, no provider dispatch (see
|
|
236
|
+
* this file's own header). Never throws a raw shape error — every rejection is a
|
|
237
|
+
* {@link CustomCredentialValidationError}.
|
|
238
|
+
*
|
|
239
|
+
* @complexity O(1) — fixed-shape field reads, no iteration.
|
|
240
|
+
*/
|
|
241
|
+
function isEmptyTokenPatch(raw) {
|
|
242
|
+
if (!raw || typeof raw !== 'object' || Array.isArray(raw))
|
|
243
|
+
return false;
|
|
244
|
+
const token = raw.token;
|
|
245
|
+
return token === '' || token === undefined;
|
|
246
|
+
}
|
|
247
|
+
function normalizeStoredToken(host, { value }) {
|
|
248
|
+
try {
|
|
249
|
+
return host.token.normalize({ value });
|
|
250
|
+
}
|
|
251
|
+
catch (err) {
|
|
252
|
+
if (host.token.isInputError(err)) {
|
|
253
|
+
throw new CustomCredentialValidationError(err.message);
|
|
254
|
+
}
|
|
255
|
+
throw err;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
function assertPlainField(host, required) {
|
|
259
|
+
try {
|
|
260
|
+
host.token.assertFreeField(required);
|
|
261
|
+
}
|
|
262
|
+
catch (err) {
|
|
263
|
+
if (host.token.isInputError(err)) {
|
|
264
|
+
throw new CustomCredentialValidationError(err.message);
|
|
265
|
+
}
|
|
266
|
+
throw err;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
function validateConnection(host, raw) {
|
|
270
|
+
if (typeof raw !== "object" || raw === null || Array.isArray(raw)) {
|
|
271
|
+
throw new CustomCredentialValidationError("connection must be an object");
|
|
272
|
+
}
|
|
273
|
+
const value = raw;
|
|
274
|
+
const token = normalizeStoredToken(host, { value: value.token });
|
|
275
|
+
const username = optionalString(value.username, "username");
|
|
276
|
+
if (username !== undefined)
|
|
277
|
+
assertPlainField(host, { value: username, field: "username" });
|
|
278
|
+
return { token, ...(username !== undefined ? { username } : {}) };
|
|
279
|
+
}
|
|
280
|
+
/** Where a new row is written (host scope policy): the project store unless "site".
|
|
281
|
+
* @throws {CustomCredentialValidationError} `scope` is neither omitted, `"site"` nor `"project"`. */
|
|
282
|
+
const createTarget = (host, deps, input) => host.createTarget(deps, input, message => new CustomCredentialValidationError(message));
|
|
283
|
+
const duplicateLabel = (host, label) => new CustomCredentialDuplicateLabelError(host.messages.duplicateLabel({ label }));
|
|
284
|
+
/**
|
|
285
|
+
* A project row whose label this site already uses would be hidden here by the site row (site rows
|
|
286
|
+
* override), so the save would look lost: refused as the same duplicate as within one store.
|
|
287
|
+
*/
|
|
288
|
+
async function assertLabelFreeOnSite(host, deps, input) {
|
|
289
|
+
if (input.target.scope !== "project")
|
|
290
|
+
return;
|
|
291
|
+
const site = await deps.repo.listByWorkspace({ workspaceId: input.workspaceId });
|
|
292
|
+
if (site.some((row) => row.label === input.label))
|
|
293
|
+
throw duplicateLabel(host, input.label);
|
|
294
|
+
}
|
|
295
|
+
/**
|
|
296
|
+
* Translates a failed insert/update of row `id` into the domain duplicate-label error, else rethrows.
|
|
297
|
+
* @throws {CustomCredentialDuplicateLabelError} The store refused `label` as taken.
|
|
298
|
+
* @throws The original error otherwise.
|
|
299
|
+
*/
|
|
300
|
+
async function throwSaveError(host, deps, input) {
|
|
301
|
+
const { err, target, id, label } = input;
|
|
302
|
+
if (host.isCreateConflict(err))
|
|
303
|
+
throw new CustomCredentialDuplicateLabelError(err.message);
|
|
304
|
+
// Unattached project rows are hidden from the normal reader. Check metadata only to distinguish
|
|
305
|
+
// a duplicate label from a CAS conflict; never attach or open the existing credential.
|
|
306
|
+
const inventory = deps.repo.project;
|
|
307
|
+
const hiddenDuplicate = target.scope === "project" && host.vault.vaultErrors.codeOf(err) === "conflict"
|
|
308
|
+
&& (await inventory.repo.listByWorkspace({ workspaceId: target.workspaceId })).some(row => row.label === label && row.id !== id);
|
|
309
|
+
// The host's storage kernel recognizes the label's UNIQUE (workspace_id, …, label) index on any database.
|
|
310
|
+
if (host.isUniqueViolation(err) || hiddenDuplicate)
|
|
311
|
+
throw target.scope === "project" ? new CustomCredentialDuplicateLabelError(host.messages.projectDuplicateLabel({ label })) : duplicateLabel(host, label);
|
|
312
|
+
throw err;
|
|
313
|
+
}
|
|
314
|
+
/**
|
|
315
|
+
* Validates, seals, and inserts a new credential set. `id` is minted here (`deps.idGen`), not
|
|
316
|
+
* caller-supplied.
|
|
317
|
+
*
|
|
318
|
+
* @agent-note Validation order is label, category, baseUrl, additionalHosts, connection, then scope;
|
|
319
|
+
* one seal (via `host.openVault(...).prepare`) and one insert follow. Ownership comes from the
|
|
320
|
+
* trusted host policy, never from input.
|
|
321
|
+
* @throws {CustomCredentialValidationError} `label`/`category`/`baseUrl`/`connection` fails shape
|
|
322
|
+
* validation.
|
|
323
|
+
* @throws {CustomCredentialDuplicateLabelError} `(workspaceId, label)` already exists.
|
|
324
|
+
* @throws {CustomCredentialSecretStoreUnconfiguredError} The store key is unavailable.
|
|
325
|
+
* @complexity O(1) plus one keyring derivation, one seal, and one insert (which may itself throw on
|
|
326
|
+
* the UNIQUE index, translated here rather than propagated raw).
|
|
327
|
+
*/
|
|
328
|
+
export async function createCredentialSet({ host, deps, input }, _optional = {}) {
|
|
329
|
+
const label = validateLabel(host, input.label);
|
|
330
|
+
const category = validateCategory(host, input.category);
|
|
331
|
+
const baseUrl = validateBaseUrl(host, input.baseUrl);
|
|
332
|
+
const additionalHosts = validateAdditionalHosts(host, input.additionalHosts);
|
|
333
|
+
const connection = validateConnection(host, input.connection);
|
|
334
|
+
const target = createTarget(host, deps, input);
|
|
335
|
+
await assertLabelFreeOnSite(host, deps, { target, workspaceId: input.workspaceId, label });
|
|
336
|
+
const id = deps.idGen.newId();
|
|
337
|
+
const prepared = await host.openVault({ deps: target.deps, kind: "vendor" }).prepare({ ...credentialSetVaultScope({ workspaceId: target.workspaceId, id, ownerKey: host.vault.ownerKey }),
|
|
338
|
+
id, expectedRevision: null, provider: null, label,
|
|
339
|
+
meta: { credentialType: "custom", worksFor: validatedWorksFor(host, { value: input.worksFor === undefined ? [] : input.worksFor }), category, baseUrl, additionalHosts: [...additionalHosts], username: connection.username ?? null },
|
|
340
|
+
payload: { token: connection.token }, hintToken: connection.token });
|
|
341
|
+
const record = credentialSetVaultRecord({ row: prepared, expectedRevision: null, policy: host.vault });
|
|
342
|
+
try {
|
|
343
|
+
await target.deps.repo.insert(record);
|
|
344
|
+
}
|
|
345
|
+
catch (err) {
|
|
346
|
+
return throwSaveError(host, deps, { err, target, id, label });
|
|
347
|
+
}
|
|
348
|
+
return toSummary(record, reportedScope(deps, target));
|
|
349
|
+
}
|
|
350
|
+
/** Validate metadata in its original order before inspecting a replacement connection. */
|
|
351
|
+
function updatedMetadata({ host, input, existing }, _options = {}) {
|
|
352
|
+
const label = input.label !== undefined ? validateLabel(host, input.label) : existing.label;
|
|
353
|
+
const category = input.category !== undefined ? validateCategory(host, input.category) : existing.category;
|
|
354
|
+
const baseUrl = input.baseUrl !== undefined ? validateBaseUrl(host, input.baseUrl) : existing.baseUrl;
|
|
355
|
+
const additionalHosts = input.additionalHosts !== undefined ? validateAdditionalHosts(host, input.additionalHosts) : existing.additionalHosts;
|
|
356
|
+
return { label, category, baseUrl, additionalHosts };
|
|
357
|
+
}
|
|
358
|
+
/** Blank connection patches preserve both the envelope and its saved username. */
|
|
359
|
+
function updatedConnection({ host, input, existing }, _options = {}) {
|
|
360
|
+
const connection = input.connection !== undefined && !isEmptyTokenPatch(input.connection) ? validateConnection(host, input.connection) : undefined;
|
|
361
|
+
let username = connection ? connection.username : existing.username;
|
|
362
|
+
if (input.username !== undefined)
|
|
363
|
+
username = validateUsernamePatch(host, input.username);
|
|
364
|
+
return { connection, username };
|
|
365
|
+
}
|
|
366
|
+
/** Only the trusted owner card may replace a granted shared entry. Local rows use their own repo.
|
|
367
|
+
* The replacement adapter rechecks the grant at commit, so revocation while a card is open wins.
|
|
368
|
+
* @agent-note `allowSharedKeyReplacement` is trusted host state, never model input.
|
|
369
|
+
* @complexity O(1); no inventory reads or key material are exposed.
|
|
370
|
+
*/
|
|
371
|
+
function sharedReplacementScope({ host, deps, scoped }) {
|
|
372
|
+
const project = deps.repo.project;
|
|
373
|
+
if (scoped.scope !== "project" || !project?.createRepo)
|
|
374
|
+
return scoped;
|
|
375
|
+
if (!deps.allowSharedKeyReplacement || !project.replaceRepo)
|
|
376
|
+
throw new CustomCredentialValidationError(host.messages.sharedReplacementReadOnly);
|
|
377
|
+
return { ...scoped, deps: { ...scoped.deps, repo: project.replaceRepo } };
|
|
378
|
+
}
|
|
379
|
+
/** Partial update: blank/omitted connection preserves the envelope, replacing a connection resets
|
|
380
|
+
* its username, and a supplied top-level username wins (null clears). Username is metadata only;
|
|
381
|
+
* removing the old ciphertext fallback makes clear impossible to resurrect, without opening it.
|
|
382
|
+
* @agent-note The row keeps its stored kind (legacy `custom` stays `custom`) so its AAD still
|
|
383
|
+
* authenticates; the write is CAS on the revision read here, through the store that holds the row.
|
|
384
|
+
* @complexity Constant queries/seals plus metadata size; CAS rejects a concurrent replacement.
|
|
385
|
+
*/
|
|
386
|
+
export async function updateCredentialSet({ host, deps, input }, _optional = {}) {
|
|
387
|
+
// Changed where it lives: this site's own row, else the project's attached row.
|
|
388
|
+
const located = await locateById(host, deps, input);
|
|
389
|
+
if (!located)
|
|
390
|
+
throw new CustomCredentialNotFoundError(host.messages.notFound({ id: input.id }));
|
|
391
|
+
const { record: existing } = located;
|
|
392
|
+
const scoped = sharedReplacementScope({ host, deps, scoped: located.scoped });
|
|
393
|
+
const { label, category, baseUrl, additionalHosts } = updatedMetadata({ host, input, existing });
|
|
394
|
+
const { connection, username } = updatedConnection({ host, input, existing });
|
|
395
|
+
// A rename gets the same site-label check as a create (`assertLabelFreeOnSite`).
|
|
396
|
+
if (label !== existing.label)
|
|
397
|
+
await assertLabelFreeOnSite(host, deps, { target: scoped, workspaceId: input.workspaceId, label });
|
|
398
|
+
const expectedRevision = existing.vaultRecord.revision;
|
|
399
|
+
const kind = existing.vaultRecord.kind === "custom" ? "custom" : "vendor";
|
|
400
|
+
const prepared = await host.openVault({ deps: scoped.deps, kind }).prepare({ ...credentialSetVaultScope({ workspaceId: scoped.workspaceId, kind, ownerKey: host.vault.ownerKey }), id: input.id,
|
|
401
|
+
expectedRevision, provider: null, label,
|
|
402
|
+
meta: { ...existing.vaultRecord.meta, worksFor: input.worksFor === undefined ? [...(existing.worksFor ?? [])] : validatedWorksFor(host, { value: input.worksFor }), category, baseUrl, additionalHosts: [...additionalHosts], username: username ?? null },
|
|
403
|
+
...(connection ? { payload: { token: connection.token }, hintToken: connection.token } : {}) });
|
|
404
|
+
const record = credentialSetVaultRecord({ row: prepared, expectedRevision, policy: host.vault });
|
|
405
|
+
try {
|
|
406
|
+
await scoped.deps.repo.update(record);
|
|
407
|
+
}
|
|
408
|
+
catch (err) {
|
|
409
|
+
return throwSaveError(host, deps, { err, target: scoped, id: input.id, label });
|
|
410
|
+
}
|
|
411
|
+
return toSummary(record, reportedScope(deps, scoped));
|
|
412
|
+
}
|
|
413
|
+
/**
|
|
414
|
+
* Deletes a credential set. No-op (not an error) if no row exists for `(workspaceId, id)` — matches
|
|
415
|
+
* `CredentialSetRepoPort.delete`'s own idempotent contract.
|
|
416
|
+
*
|
|
417
|
+
* @complexity O(1).
|
|
418
|
+
*/
|
|
419
|
+
export async function deleteCredentialSet({ host, deps, input }, _optional = {}) {
|
|
420
|
+
// A project row is removed only when no other site uses it, with its attachments.
|
|
421
|
+
const located = await locateById(host, { ...deps, repo: deps.repo }, input);
|
|
422
|
+
if (located?.scoped.scope === "project" && deps.repo.project?.createRepo)
|
|
423
|
+
throw new CustomCredentialValidationError(host.messages.sharedDeleteReadOnly);
|
|
424
|
+
if (located)
|
|
425
|
+
await located.scoped.deps.repo.delete({ workspaceId: located.scoped.workspaceId, id: input.id });
|
|
426
|
+
}
|
|
427
|
+
/** Select by saved label without opening, then open only that row under its expected ownership.
|
|
428
|
+
* Saved metadata is the authoritative source of allowed origins and username, including clears.
|
|
429
|
+
* @agent-note Exactly one envelope is opened: the selected, authorized row, with the store that
|
|
430
|
+
* holds it. Origins and username come from the SAME read as the token.
|
|
431
|
+
* @throws CustomCredentialSecretStoreUnconfiguredError on crypto failure.
|
|
432
|
+
* @complexity O(n) metadata selection, n workspace rows, plus one selected envelope open.
|
|
433
|
+
*/
|
|
434
|
+
export async function resolveCredentialSetByLabel({ host, deps, input }, _optional = {}) {
|
|
435
|
+
const located = await locateByLabel({ host, deps, input });
|
|
436
|
+
if (!located)
|
|
437
|
+
return null;
|
|
438
|
+
const opened = await openLocated(host, located);
|
|
439
|
+
if (!opened)
|
|
440
|
+
return null;
|
|
441
|
+
// Use metadata from the same read as the payload, so a concurrent origin/username update cannot
|
|
442
|
+
// pair a new token with the old allowlist. Never fall back to an embedded legacy username.
|
|
443
|
+
const { token, meta: current } = opened;
|
|
444
|
+
return { id: located.record.id, category: current.category, baseUrl: current.baseUrl,
|
|
445
|
+
additionalHosts: current.additionalHosts, connection: { token,
|
|
446
|
+
...(typeof current.username === "string" ? { username: current.username } : {}) } };
|
|
447
|
+
}
|
|
448
|
+
/** Production project readers resolve one granted label through an indexed join, never inventory.
|
|
449
|
+
* Compatibility fixture ports without that query retain their existing metadata scan.
|
|
450
|
+
* @complexity O(1) selected row reads on the composed owner store.
|
|
451
|
+
*/
|
|
452
|
+
async function locateByLabel({ host, deps, input }) {
|
|
453
|
+
for (const scoped of host.scopes(deps, input.workspaceId)) {
|
|
454
|
+
const repo = scoped.deps.repo;
|
|
455
|
+
const record = repo.findByLabel ? await repo.findByLabel({ workspaceId: scoped.workspaceId, label: input.label })
|
|
456
|
+
: (await repo.listByWorkspace({ workspaceId: scoped.workspaceId })).find(row => row.label === input.label);
|
|
457
|
+
if (record)
|
|
458
|
+
return { scoped, record };
|
|
459
|
+
}
|
|
460
|
+
return undefined;
|
|
461
|
+
}
|
|
462
|
+
/** Opens one located row with the store that sealed it: its token and the metadata of that same read.
|
|
463
|
+
* @throws CustomCredentialSecretStoreUnconfiguredError on crypto failure or a payload without a token. */
|
|
464
|
+
async function openLocated(host, { scoped, record }) {
|
|
465
|
+
const kind = record.vaultRecord.kind === "custom" ? "custom" : "vendor";
|
|
466
|
+
const resolved = await host.openVault({ deps: scoped.deps, kind }).get({ ...credentialSetVaultScope({ ...record, kind, ownerKey: host.vault.ownerKey }), id: record.id });
|
|
467
|
+
if (!resolved)
|
|
468
|
+
return null;
|
|
469
|
+
if (typeof resolved.payload?.token !== "string")
|
|
470
|
+
throw new CustomCredentialSecretStoreUnconfiguredError(host.messages.storage);
|
|
471
|
+
return { token: resolved.payload.token, meta: resolved.summary.meta };
|
|
472
|
+
}
|
|
473
|
+
/** Keep the store's validation error contract while reusing the host's canonical ID parser. */
|
|
474
|
+
function validatedWorksFor(host, { value }, _options = {}) {
|
|
475
|
+
try {
|
|
476
|
+
const ids = host.normalizeWorksFor({ value });
|
|
477
|
+
for (const id of ids) {
|
|
478
|
+
assertPlainField(host, { value: id, field: "worksFor" });
|
|
479
|
+
}
|
|
480
|
+
return ids;
|
|
481
|
+
}
|
|
482
|
+
catch (error) {
|
|
483
|
+
if (host.vault.vaultErrors.codeOf(error) !== undefined) {
|
|
484
|
+
throw new CustomCredentialValidationError(host.messages.worksForInvalid);
|
|
485
|
+
}
|
|
486
|
+
throw error;
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
//# sourceMappingURL=store.js.map
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import type { CredentialSetProjectScope, CredentialSetRecord, CredentialSetRepoPort } from "@jini-ai/core/contracts/credentials";
|
|
2
|
+
import type { VaultPutRecord, VaultRepoPort } from "@jini-ai/core/contracts/vault";
|
|
3
|
+
import type { CredentialSetRowByLabel, CredentialSetVaultPolicy } from "./host.js";
|
|
4
|
+
/**
|
|
5
|
+
* @file Credential-set repository over a host-supplied vault. Owner, metadata parser, label order
|
|
6
|
+
* and canonical errors are required host inputs (`CredentialSetVaultPolicy`); nothing here names a
|
|
7
|
+
* product owner. Rows keep their stored kind, so legacy envelopes keep authenticating.
|
|
8
|
+
* Provenance: moved unchanged (EXTRACT-S8 C1 move) from the first host's credential-set seam file
|
|
9
|
+
* `sets/vault-repo.ts` at its commit 43a0e5d4; earlier history lives in that host's repository.
|
|
10
|
+
*/
|
|
11
|
+
/** @agent-note Exact expected vault context for a credential-set row: the trusted host owner, null
|
|
12
|
+
* principal and binding. `kind` defaults to the unified `vendor` kind; pass `custom` only for a
|
|
13
|
+
* legacy row being read or rewritten under its own stored kind. */
|
|
14
|
+
export declare function credentialSetVaultScope({ workspaceId, id, kind, ownerKey }: {
|
|
15
|
+
workspaceId: string;
|
|
16
|
+
id?: string;
|
|
17
|
+
kind?: "vendor" | "custom";
|
|
18
|
+
ownerKey: string;
|
|
19
|
+
}, _options?: {}): {
|
|
20
|
+
id?: string;
|
|
21
|
+
workspaceId: string;
|
|
22
|
+
kind: "vendor" | "custom";
|
|
23
|
+
ownerKey: string;
|
|
24
|
+
principalId: null;
|
|
25
|
+
binding: null;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* @agent-note Converts one vault row into a credential-set record, rejecting any row outside the
|
|
29
|
+
* trusted ownership with the canonical `context` error and a row without an envelope with
|
|
30
|
+
* `invalid-record`. Username belongs exclusively to metadata after normalization; no sealed fallback
|
|
31
|
+
* can resurrect an explicit clear. The private envelope/revision are attached non-enumerable and
|
|
32
|
+
* never become a public summary field.
|
|
33
|
+
*/
|
|
34
|
+
export declare function credentialSetVaultRecord<C extends string>({ row, expectedRevision, policy }: {
|
|
35
|
+
row: VaultPutRecord;
|
|
36
|
+
expectedRevision?: number | null;
|
|
37
|
+
policy: CredentialSetVaultPolicy;
|
|
38
|
+
}, _options?: {}): CredentialSetRecord<C>;
|
|
39
|
+
/**
|
|
40
|
+
* @agent-note Fixed owner/provider scope keeps labels unique across the workspace in both adapters.
|
|
41
|
+
* Every write is one CAS `put`/`delete` on the SAME `vault` passed in (`repo.vault === vault`); no
|
|
42
|
+
* write path opens a sealed envelope. `findRowByLabel`, when supplied, must be the vault's own
|
|
43
|
+
* indexed, authorization-preserving lookup.
|
|
44
|
+
*/
|
|
45
|
+
export declare class CredentialSetVaultRepo<C extends string = string> implements CredentialSetRepoPort<C> {
|
|
46
|
+
readonly vault: VaultRepoPort;
|
|
47
|
+
readonly project?: CredentialSetProjectScope<C>;
|
|
48
|
+
readonly findByLabel?: (input: {
|
|
49
|
+
workspaceId: string;
|
|
50
|
+
label: string;
|
|
51
|
+
}) => Promise<CredentialSetRecord<C> | null>;
|
|
52
|
+
/** Host policy, stored non-enumerable (not a `#private` field) so prototype-derived wrappers
|
|
53
|
+
* (`Object.create(repo)`) keep working exactly as with the historical class. */
|
|
54
|
+
private readonly policy;
|
|
55
|
+
/** @param optional.project The project store this site's rows fall back to. */
|
|
56
|
+
constructor({ vault, policy }: {
|
|
57
|
+
vault: VaultRepoPort;
|
|
58
|
+
policy: CredentialSetVaultPolicy;
|
|
59
|
+
}, optional?: {
|
|
60
|
+
project?: CredentialSetProjectScope<C>;
|
|
61
|
+
findRowByLabel?: CredentialSetRowByLabel;
|
|
62
|
+
});
|
|
63
|
+
findById(input: {
|
|
64
|
+
workspaceId: string;
|
|
65
|
+
id: string;
|
|
66
|
+
}): Promise<CredentialSetRecord<C> | null>;
|
|
67
|
+
listByWorkspace(input: {
|
|
68
|
+
workspaceId: string;
|
|
69
|
+
}): Promise<CredentialSetRecord<C>[]>;
|
|
70
|
+
insert(record: CredentialSetRecord<C>): Promise<void>;
|
|
71
|
+
update(record: CredentialSetRecord<C>): Promise<void>;
|
|
72
|
+
/** Prepared records carry the caller's CAS; sealed DTO compatibility preserves hints only when
|
|
73
|
+
* the envelope is unchanged. Label conflicts keep the host's historical raw UNIQUE error contract.
|
|
74
|
+
*/
|
|
75
|
+
private write;
|
|
76
|
+
delete(input: {
|
|
77
|
+
workspaceId: string;
|
|
78
|
+
id: string;
|
|
79
|
+
}): Promise<void>;
|
|
80
|
+
}
|
|
81
|
+
//# sourceMappingURL=vault-repo.d.ts.map
|