@jini-ai/integrations 0.6.19 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +41 -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/package.json +34 -11
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
import { randomBytes, randomUUID } from 'node:crypto';
|
|
2
|
+
import { ApiConnectionError, assertSharedOAuthCompatibility, createSharedOAuthConsumer, deriveApiConnectionKey } from '../index.js';
|
|
3
|
+
const MCP_PLUGIN_ID = 'external-mcp';
|
|
4
|
+
const consumerId = (serverId) => `mcp:${serverId}`;
|
|
5
|
+
/** Site-specific bindings over the existing owner vault. Every call checks live grants; no token is copied to the site.
|
|
6
|
+
* @complexity Selected-row reads per call; one cached refresher per selected owner credential, no timers or polling.
|
|
7
|
+
*/
|
|
8
|
+
export function createSharedExternalMcpOAuth(required, _optional = {}) {
|
|
9
|
+
const { deps, shared, configure, resolveProvider, ports } = required;
|
|
10
|
+
const { owner, bindings, attachments, siteId } = shared;
|
|
11
|
+
const unavailable = () => new ApiConnectionError({ code: 'connection-not-found', message: ports.copy.sharedUnavailable });
|
|
12
|
+
const invalidState = () => ports.errors.oauth({ code: 'OAUTH_INVALID_STATE', message: 'This shared sign-in was stopped or replaced.', operatorAction: ports.copy.actions.restartShared });
|
|
13
|
+
const refreshers = new Map();
|
|
14
|
+
const deviceKeyFor = (serverId, credentialId, generation) => JSON.stringify(['shared', siteId, serverId, credentialId, generation]);
|
|
15
|
+
const bindingFor = ({ serverId }) => bindings.read({ consumerId: consumerId(serverId) });
|
|
16
|
+
async function contextFor(credentialId) {
|
|
17
|
+
const context = await owner.readContext({ credentialId });
|
|
18
|
+
if (!context?.mcp || context.identity.pluginId !== MCP_PLUGIN_ID || context.identity.connectionId !== 'oauth')
|
|
19
|
+
throw unavailable();
|
|
20
|
+
return context;
|
|
21
|
+
}
|
|
22
|
+
function optionalAuthorization(authorizationEndpoint) {
|
|
23
|
+
return authorizationEndpoint === undefined ? {} : { authorizationEndpoint };
|
|
24
|
+
}
|
|
25
|
+
function audienceIdentity(endpoints, identity) {
|
|
26
|
+
const resource = endpoints.resource ?? identity.resource;
|
|
27
|
+
const issuer = endpoints.issuer ?? identity.issuer;
|
|
28
|
+
return { ...(resource === undefined ? {} : { resource }), ...(issuer === undefined ? {} : { issuer }) };
|
|
29
|
+
}
|
|
30
|
+
function endpointIdentity(record, context) {
|
|
31
|
+
const endpoints = JSON.parse(record.oauthEndpointsJson ?? '{}');
|
|
32
|
+
const expectedProvider = record.oauthProviderId === null ? undefined : resolveProvider(record);
|
|
33
|
+
return { tokenEndpoint: endpoints.tokenEndpoint ?? expectedProvider?.tokenEndpoint ?? context.identity.tokenEndpoint,
|
|
34
|
+
...optionalAuthorization(endpoints.authorizationEndpoint ?? expectedProvider?.authorizationEndpoint ?? context.identity.authorizationEndpoint),
|
|
35
|
+
...audienceIdentity(endpoints, context.identity) };
|
|
36
|
+
}
|
|
37
|
+
function target(record, context) {
|
|
38
|
+
if (record.authMode !== 'oauth' || record.transport === 'stdio' || record.url !== context.mcp.url)
|
|
39
|
+
throw unavailable();
|
|
40
|
+
const scopes = JSON.parse(record.oauthScopesJson ?? '[]');
|
|
41
|
+
return { siteId, consumerId: consumerId(record.serverId), identity: { pluginId: MCP_PLUGIN_ID, connectionId: 'oauth',
|
|
42
|
+
...endpointIdentity(record, context) }, requiredScopes: scopes };
|
|
43
|
+
}
|
|
44
|
+
async function refreshFor(credentialId) {
|
|
45
|
+
const cached = refreshers.get(credentialId);
|
|
46
|
+
if (cached)
|
|
47
|
+
return cached;
|
|
48
|
+
const context = await contextFor(credentialId);
|
|
49
|
+
const tokens = owner.tokensFor({ context });
|
|
50
|
+
const refresher = ports.flow.createTokenRefresher({ clock: deps.clock, sleep: ({ ms }) => new Promise(resolve => setTimeout(resolve, ms)), port: {
|
|
51
|
+
...tokens,
|
|
52
|
+
refresh: ({ refreshToken }) => ports.flow.refreshAccessToken({ ...deps.httpPorts, clock: deps.clock, tokenEndpoint: context.identity.tokenEndpoint,
|
|
53
|
+
client: context.client, refreshToken }, { scopes: context.mcp.scopes, ...(context.identity.resource === undefined ? {} : { resource: context.identity.resource }), timeoutMs: 15_000 }),
|
|
54
|
+
} });
|
|
55
|
+
refreshers.set(credentialId, refresher);
|
|
56
|
+
return refresher;
|
|
57
|
+
}
|
|
58
|
+
const consumer = createSharedOAuthConsumer({
|
|
59
|
+
bindings: { read: ({ consumerId }) => bindings.read({ consumerId }) },
|
|
60
|
+
grants: { isGranted: ({ siteId, credentialId }) => attachments.isAttached({ siteId, keyId: credentialId }) },
|
|
61
|
+
connections: { read: input => owner.readConnection(input), getAccessToken: async ({ credentialId, expectedGeneration }) => {
|
|
62
|
+
const record = await owner.records.read({ key: credentialId });
|
|
63
|
+
if (record?.generation !== expectedGeneration || record.status !== 'connected')
|
|
64
|
+
throw unavailable();
|
|
65
|
+
try {
|
|
66
|
+
return await (await refreshFor(credentialId)).getAccessToken({ key: credentialId });
|
|
67
|
+
}
|
|
68
|
+
catch (error) {
|
|
69
|
+
if (ports.errors.isOAuthError(error) && error.code === 'OAUTH_INVALID_GRANT') {
|
|
70
|
+
await owner.records.compareAndSet({ key: credentialId, expectedRevision: record.revision,
|
|
71
|
+
record: { ...record, status: 'reconnect-required', revision: record.revision + 1 } });
|
|
72
|
+
}
|
|
73
|
+
throw error;
|
|
74
|
+
}
|
|
75
|
+
} },
|
|
76
|
+
});
|
|
77
|
+
async function selected(serverId) {
|
|
78
|
+
const binding = await bindingFor({ serverId });
|
|
79
|
+
if (!binding)
|
|
80
|
+
return null;
|
|
81
|
+
const [context, record] = await Promise.all([contextFor(binding.credentialId), deps.repo.findByServerId({ workspaceId: deps.workspaceId, serverId })]);
|
|
82
|
+
if (!record)
|
|
83
|
+
throw unavailable();
|
|
84
|
+
return { binding, context, record, target: target(record, context) };
|
|
85
|
+
}
|
|
86
|
+
async function assertAttempt(serverId, credentialId, generation) {
|
|
87
|
+
const [binding, record, granted] = await Promise.all([bindingFor({ serverId }), owner.records.read({ key: credentialId }), attachments.isAttached({ siteId, keyId: credentialId })]);
|
|
88
|
+
if (binding?.credentialId !== credentialId || !granted || !record)
|
|
89
|
+
throw invalidState();
|
|
90
|
+
if (record.generation !== generation || record.status === 'revocation-pending')
|
|
91
|
+
throw invalidState();
|
|
92
|
+
return record;
|
|
93
|
+
}
|
|
94
|
+
function stateFor(input) {
|
|
95
|
+
const aad = ports.aad.format({ kind: 'oauth-redirect-state', version: 'v1', parts: [JSON.stringify(['shared', siteId, input.serverId, input.credentialId, input.generation, input.browserBinding])] });
|
|
96
|
+
return ports.flow.createSealedAuthorizationState({ clock: deps.clock, workspaceId: siteId, serverId: input.serverId, codec: {
|
|
97
|
+
seal: async ({ plaintext }) => Buffer.from(JSON.stringify(await deps.sealer.seal({ plaintext, key: await deps.keyring.activeKey({}), aad }))).toString('base64url'),
|
|
98
|
+
open: async ({ state }) => deps.sealer.open({ sealed: JSON.parse(Buffer.from(state, 'base64url').toString()) }, { aad }),
|
|
99
|
+
} });
|
|
100
|
+
}
|
|
101
|
+
async function assertOwner(actorId) {
|
|
102
|
+
if (actorId !== await shared.ownerPrincipalId)
|
|
103
|
+
throw new ApiConnectionError({ code: 'access-denied', message: ports.copy.sharedOwnerOnly });
|
|
104
|
+
}
|
|
105
|
+
async function createSelection(input) {
|
|
106
|
+
const server = await deps.repo.findByServerId({ workspaceId: deps.workspaceId, serverId: input.serverId });
|
|
107
|
+
if (!server || server.transport === 'stdio')
|
|
108
|
+
throw unavailable();
|
|
109
|
+
const config = await configure(input);
|
|
110
|
+
const credentialId = deriveApiConnectionKey({ workspaceId: shared.ownerWorkspaceId, pluginId: MCP_PLUGIN_ID, connectionId: randomUUID() });
|
|
111
|
+
const context = { credentialId, identity: { pluginId: MCP_PLUGIN_ID, connectionId: 'oauth', clientId: config.client.clientId,
|
|
112
|
+
tokenEndpoint: config.provider.tokenEndpoint, ...(config.provider.authorizationEndpoint === undefined ? {} : { authorizationEndpoint: config.provider.authorizationEndpoint }), ...(config.resource === undefined ? {} : { resource: config.resource }), ...(config.issuer === undefined ? {} : { issuer: config.issuer }) },
|
|
113
|
+
client: config.client, clientKind: config.client.authMethod === 'none' ? 'public' : 'confidential', descriptor: { pluginId: MCP_PLUGIN_ID, connectionId: 'oauth', label: config.record.label ?? config.record.serverId,
|
|
114
|
+
authorizationEndpoint: config.provider.authorizationEndpoint ?? config.provider.tokenEndpoint, tokenEndpoint: config.provider.tokenEndpoint,
|
|
115
|
+
authorizationParams: { public: {}, confidential: {} }, forceConsentParams: {}, startParams: [], callbackFields: [] },
|
|
116
|
+
mcp: { provider: config.provider, scopes: config.scopes, url: config.record.url, grant: config.record.oauthGrant } };
|
|
117
|
+
const record = { key: credentialId, workspaceId: shared.ownerWorkspaceId, pluginId: MCP_PLUGIN_ID, connectionId: 'oauth', status: 'disconnected',
|
|
118
|
+
revision: 1, generation: 0, account: null, grantedScopes: [], staged: null, revocationFailure: null, updatedAt: new Date(deps.clock.nowMs()).toISOString() };
|
|
119
|
+
await owner.saveContext({ credentialId, context });
|
|
120
|
+
if (!await owner.records.compareAndSet({ key: credentialId, expectedRevision: 0, record }))
|
|
121
|
+
throw unavailable();
|
|
122
|
+
// Reference first. A partial write confers no authority; a later Stop using here removes the grant before completion.
|
|
123
|
+
await shared.transaction({ run: async () => {
|
|
124
|
+
await bindings.set({ consumerId: consumerId(input.serverId), credentialId });
|
|
125
|
+
await attachments.attach({ siteId, keyId: credentialId, siteName: shared.siteName });
|
|
126
|
+
} });
|
|
127
|
+
return { binding: { credentialId }, context };
|
|
128
|
+
}
|
|
129
|
+
async function install(serverId, context, generation, tokens) {
|
|
130
|
+
const grantedScopes = tokens.scopes.length ? tokens.scopes : context.mcp.scopes;
|
|
131
|
+
if (context.mcp.scopes.some(scope => !grantedScopes.includes(scope)))
|
|
132
|
+
throw unavailable();
|
|
133
|
+
const record = await assertAttempt(serverId, context.credentialId, generation);
|
|
134
|
+
const next = { ...record, revision: record.revision + 1, generation: generation + 1, status: 'connected',
|
|
135
|
+
grantedScopes: [...grantedScopes], updatedAt: new Date(deps.clock.nowMs()).toISOString() };
|
|
136
|
+
if (!await owner.records.compareAndSet({ key: record.key, expectedRevision: record.revision, record: next }))
|
|
137
|
+
throw invalidState();
|
|
138
|
+
await owner.tokensFor({ context }).install({ key: record.key, tokens: { ...tokens, scopes: [...grantedScopes] }, generation: next.generation });
|
|
139
|
+
await assertAttempt(serverId, context.credentialId, next.generation);
|
|
140
|
+
}
|
|
141
|
+
async function connectionOption(credentialId, record) {
|
|
142
|
+
const context = await owner.readContext({ credentialId });
|
|
143
|
+
if (!context?.mcp || context.mcp.url !== record?.url)
|
|
144
|
+
return null;
|
|
145
|
+
const connection = await owner.readConnection({ credentialId });
|
|
146
|
+
return connection?.status === 'connected' ? { credentialId, label: context.descriptor.label } : null;
|
|
147
|
+
}
|
|
148
|
+
async function currentSelection(input) {
|
|
149
|
+
const current = await bindingFor(input);
|
|
150
|
+
if (!current)
|
|
151
|
+
return null;
|
|
152
|
+
const connection = await owner.readConnection(current);
|
|
153
|
+
return { ...current, status: connection?.status ?? 'disconnected', granted: await attachments.isAttached({ siteId, keyId: current.credentialId }) };
|
|
154
|
+
}
|
|
155
|
+
return {
|
|
156
|
+
async list(input) {
|
|
157
|
+
await assertOwner(input.actorId);
|
|
158
|
+
const rows = await shared.inventory?.() ?? [];
|
|
159
|
+
const candidates = rows.filter(row => row.meta.credentialFormat === 'oauth-token-set').slice(0, 50);
|
|
160
|
+
const record = await deps.repo.findByServerId({ workspaceId: deps.workspaceId, serverId: input.serverId });
|
|
161
|
+
const connections = [];
|
|
162
|
+
for (const row of candidates) {
|
|
163
|
+
const option = await connectionOption(row.id, record);
|
|
164
|
+
if (option)
|
|
165
|
+
connections.push(option);
|
|
166
|
+
}
|
|
167
|
+
return { available: true, connections, current: await currentSelection(input) };
|
|
168
|
+
},
|
|
169
|
+
hasBinding: async (input) => (await bindingFor(input)) !== null,
|
|
170
|
+
async getAccessToken(input) {
|
|
171
|
+
const found = await selected(input.serverId);
|
|
172
|
+
return found ? consumer.getAccessToken(found.target) : undefined;
|
|
173
|
+
},
|
|
174
|
+
async assertAuthorized(input) {
|
|
175
|
+
const found = await selected(input.serverId);
|
|
176
|
+
if (!found)
|
|
177
|
+
return false;
|
|
178
|
+
await consumer.assertAuthorized(found.target);
|
|
179
|
+
return true;
|
|
180
|
+
},
|
|
181
|
+
async useSaved(input) {
|
|
182
|
+
await assertOwner(input.actorId);
|
|
183
|
+
const context = await contextFor(input.credentialId);
|
|
184
|
+
const record = await deps.repo.findByServerId({ workspaceId: deps.workspaceId, serverId: input.serverId });
|
|
185
|
+
if (!record)
|
|
186
|
+
throw unavailable();
|
|
187
|
+
const wanted = target(record, context);
|
|
188
|
+
await shared.transaction({ run: async () => {
|
|
189
|
+
const connection = await owner.readConnection({ credentialId: input.credentialId });
|
|
190
|
+
if (connection?.status !== 'connected')
|
|
191
|
+
throw unavailable();
|
|
192
|
+
assertSharedOAuthCompatibility({ connection, target: wanted });
|
|
193
|
+
await bindings.set({ consumerId: consumerId(input.serverId), credentialId: input.credentialId });
|
|
194
|
+
await attachments.attach({ siteId, keyId: input.credentialId, siteName: shared.siteName });
|
|
195
|
+
} });
|
|
196
|
+
await consumer.assertAuthorized(wanted);
|
|
197
|
+
},
|
|
198
|
+
async useLocal(input) {
|
|
199
|
+
await assertOwner(input.actorId);
|
|
200
|
+
await shared.transaction({ run: async () => {
|
|
201
|
+
const binding = await bindingFor(input);
|
|
202
|
+
if (binding)
|
|
203
|
+
await attachments.detach({ siteId, keyId: binding.credentialId });
|
|
204
|
+
await bindings.remove({ consumerId: consumerId(input.serverId) });
|
|
205
|
+
} });
|
|
206
|
+
},
|
|
207
|
+
async beginConnect(input) {
|
|
208
|
+
await assertOwner(input.actorId);
|
|
209
|
+
const found = input.destination === 'shared' ? await createSelection(input) : await selected(input.serverId) ?? await createSelection(input);
|
|
210
|
+
const { context, binding } = found;
|
|
211
|
+
const record = await owner.records.read({ key: binding.credentialId });
|
|
212
|
+
if (!record)
|
|
213
|
+
throw unavailable();
|
|
214
|
+
await assertAttempt(input.serverId, binding.credentialId, record.generation);
|
|
215
|
+
const provider = context.mcp.provider;
|
|
216
|
+
if (context.mcp.grant === 'device_code') {
|
|
217
|
+
const authorization = await ports.flow.beginDeviceAuthorization({ ...deps.httpPorts, provider, clock: deps.clock, client: context.client }, { scopes: context.mcp.scopes, ...(context.identity.resource === undefined ? {} : { resource: context.identity.resource }), timeoutMs: 15_000 });
|
|
218
|
+
await assertAttempt(input.serverId, binding.credentialId, record.generation);
|
|
219
|
+
await deps.devices.put(deviceKeyFor(input.serverId, binding.credentialId, record.generation), authorization);
|
|
220
|
+
return { kind: 'device_code', userCode: authorization.userCode, verificationUri: authorization.verificationUri,
|
|
221
|
+
verificationUriComplete: authorization.verificationUriComplete, expiresAt: authorization.expiresAt, intervalSeconds: authorization.intervalSeconds };
|
|
222
|
+
}
|
|
223
|
+
if (!input.redirectUri)
|
|
224
|
+
throw unavailable();
|
|
225
|
+
if (input.delivery === 'hosted')
|
|
226
|
+
return { kind: 'redirect_required', authorizationUrl: ports.signInLink({ callbackUrl: input.redirectUri,
|
|
227
|
+
workspaceId: deps.workspaceId, serverId: input.serverId }), expiresAt: new Date(deps.clock.nowMs() + 600_000).toISOString() };
|
|
228
|
+
const started = await ports.flow.beginAuthorizationCode({ provider, guard: deps.httpPorts.guard, randomBytesFn: ({ byteLength }) => randomBytes(byteLength),
|
|
229
|
+
stateless: stateFor({ ...input, credentialId: binding.credentialId, generation: record.generation }), ownerKey: JSON.stringify([siteId, input.serverId, binding.credentialId]),
|
|
230
|
+
client: context.client, redirectUri: input.redirectUri, options: { clientDisplayName: ports.registrationIdentity.sharedClientName, softwareId: ports.registrationIdentity.softwareId, redirectUris: [input.redirectUri] } }, { scopes: context.mcp.scopes, ...(context.identity.resource === undefined ? {} : { resource: context.identity.resource }) });
|
|
231
|
+
return { kind: 'redirect_required', authorizationUrl: started.authorizationUrl, expiresAt: started.expiresAt };
|
|
232
|
+
},
|
|
233
|
+
async completeAuthorizationCallback(input) {
|
|
234
|
+
const found = await selected(input.serverId);
|
|
235
|
+
if (!found)
|
|
236
|
+
throw invalidState();
|
|
237
|
+
const { context, binding } = found;
|
|
238
|
+
const record = await owner.records.read({ key: binding.credentialId });
|
|
239
|
+
if (!record)
|
|
240
|
+
throw invalidState();
|
|
241
|
+
await assertAttempt(input.serverId, binding.credentialId, record.generation);
|
|
242
|
+
const tokens = await ports.flow.completeAuthorizationCode({ ...deps.httpPorts, provider: context.mcp.provider, clock: deps.clock,
|
|
243
|
+
stateless: stateFor({ ...input, credentialId: binding.credentialId, generation: record.generation }), redirectUri: input.redirectUri,
|
|
244
|
+
ownerKey: JSON.stringify([siteId, input.serverId, binding.credentialId]), client: context.client, params: input.params }, { timeoutMs: 15_000 });
|
|
245
|
+
await install(input.serverId, context, record.generation, tokens);
|
|
246
|
+
},
|
|
247
|
+
async pollDeviceAuthorization(input) {
|
|
248
|
+
const found = await selected(input.serverId);
|
|
249
|
+
if (!found)
|
|
250
|
+
throw invalidState();
|
|
251
|
+
const { context, binding } = found;
|
|
252
|
+
const record = await owner.records.read({ key: binding.credentialId });
|
|
253
|
+
if (!record)
|
|
254
|
+
throw invalidState();
|
|
255
|
+
await assertAttempt(input.serverId, binding.credentialId, record.generation);
|
|
256
|
+
const deviceKey = deviceKeyFor(input.serverId, binding.credentialId, record.generation);
|
|
257
|
+
const authorization = await deps.devices.get(deviceKey);
|
|
258
|
+
if (!authorization)
|
|
259
|
+
throw invalidState();
|
|
260
|
+
try {
|
|
261
|
+
const tokens = await ports.flow.pollDeviceAuthorizationOnce({ ...deps.httpPorts, provider: context.mcp.provider, clock: deps.clock,
|
|
262
|
+
client: context.client, deviceCode: authorization.deviceCode, expiresAt: authorization.expiresAt }, { ...(context.identity.resource === undefined ? {} : { resource: context.identity.resource }), timeoutMs: 15_000 });
|
|
263
|
+
await deps.devices.delete(deviceKey);
|
|
264
|
+
await install(input.serverId, context, record.generation, tokens);
|
|
265
|
+
return { status: 'connected' };
|
|
266
|
+
}
|
|
267
|
+
catch (error) {
|
|
268
|
+
if (ports.errors.isOAuthError(error) && error.retryable)
|
|
269
|
+
return { status: 'pending', retryAfterSeconds: error.retryAfterSeconds ?? authorization.intervalSeconds };
|
|
270
|
+
await deps.devices.delete(deviceKey);
|
|
271
|
+
throw error;
|
|
272
|
+
}
|
|
273
|
+
},
|
|
274
|
+
async markNeedsReauth(input) {
|
|
275
|
+
const found = await selected(input.serverId);
|
|
276
|
+
if (!found || !await attachments.isAttached({ siteId, keyId: found.binding.credentialId }))
|
|
277
|
+
return;
|
|
278
|
+
const record = await owner.records.read({ key: found.binding.credentialId });
|
|
279
|
+
if (record?.status !== 'connected')
|
|
280
|
+
return;
|
|
281
|
+
await owner.records.compareAndSet({ key: record.key, expectedRevision: record.revision,
|
|
282
|
+
record: { ...record, status: 'reconnect-required', revision: record.revision + 1 } });
|
|
283
|
+
},
|
|
284
|
+
async disconnect(input) {
|
|
285
|
+
await shared.transaction({ run: async () => {
|
|
286
|
+
const binding = await bindingFor(input);
|
|
287
|
+
if (binding)
|
|
288
|
+
await attachments.detach({ siteId, keyId: binding.credentialId });
|
|
289
|
+
} });
|
|
290
|
+
},
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
//# sourceMappingURL=shared-sign-in.js.map
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { DeviceAuthorizationStore, ExternalMcpOAuthDeps as HostedMcpOAuthDeps, ExternalMcpOAuthService, HostedMcpSignInPorts } from "./contracts.js";
|
|
2
|
+
import { type SharedExternalMcpOwner } from "./shared-sign-in.js";
|
|
3
|
+
/** Bounded by the per-workspace server cap, so no explicit eviction policy is needed beyond
|
|
4
|
+
* overwriting a connection's own previous attempt. */
|
|
5
|
+
export declare function createDeviceAuthorizationStore(): DeviceAuthorizationStore;
|
|
6
|
+
/** {@link ExternalMcpOAuthDeps} as the service holds them: `providers` defaulted once at construction,
|
|
7
|
+
* so nothing downstream builds a second registry. */
|
|
8
|
+
/** The sign-in's whole input: the canonical deps plus the host's {@link HostedMcpSignInPorts}. */
|
|
9
|
+
type SignInDeps = HostedMcpOAuthDeps<SharedExternalMcpOwner> & {
|
|
10
|
+
readonly ports: HostedMcpSignInPorts;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Builds the OAuth service for one workspace.
|
|
14
|
+
*
|
|
15
|
+
* @returns The service plus its `tokenResolver`, which `readEnabledExternalMcpConfigs` takes.
|
|
16
|
+
* @complexity Construction is O(1) and performs no I/O.
|
|
17
|
+
* @tradeoffs The assistant daemon can start a redirect that the web server completes: encrypted
|
|
18
|
+
* state carries the verifier and connection binding across that split using the same site key.
|
|
19
|
+
* No pending row or shared process memory is needed. The provider's single-use code and the
|
|
20
|
+
* state's expiry prevent replay. Device authorization still uses the injected durable store;
|
|
21
|
+
* its in-memory adapter is only suitable when begin and poll run in the same process.
|
|
22
|
+
*/
|
|
23
|
+
export declare function createHostedMcpSignInService(required: SignInDeps, _optional?: Record<string, never>): ExternalMcpOAuthService;
|
|
24
|
+
export {};
|
|
25
|
+
//# sourceMappingURL=sign-in.d.ts.map
|