diff --git a/server/src/__tests__/tool-access-client-credentials.test.ts b/server/src/__tests__/tool-access-client-credentials.test.ts index c39fb3d0b42..f0f45c457ef 100644 --- a/server/src/__tests__/tool-access-client-credentials.test.ts +++ b/server/src/__tests__/tool-access-client-credentials.test.ts @@ -89,13 +89,16 @@ function fakeProvider() { metadataOnlyAtHint: false, /** Runs once when the server first receives a signed-in request (to stage races). */ onAuthorizedMcpRequest: null as null | (() => Promise), + signingAlgorithms: ["RS256", "ES256"], + /** Runs once at the start of the next token request (to stage a paused refresh). */ + onTokenRequest: null as null | (() => Promise), }; const metadata = (issuer: string) => ({ issuer, token_endpoint: `${issuer}/oauth/token`, grant_types_supported: ["client_credentials"], token_endpoint_auth_methods_supported: ["private_key_jwt", "client_secret_basic", "client_secret_post"], - token_endpoint_auth_signing_alg_values_supported: ["RS256", "ES256"], + token_endpoint_auth_signing_alg_values_supported: state.signingAlgorithms, }); const handle = async (input: string | URL | Request, init?: RequestInit): Promise => { const url = String(input instanceof Request ? input.url : input); @@ -118,6 +121,11 @@ function fakeProvider() { if (url === TOKEN_URL) { const body = new URLSearchParams(String(init?.body)); state.tokenRequests.push({ url, body, authorization: headers.get("authorization") }); + if (state.onTokenRequest) { + const hook = state.onTokenRequest; + state.onTokenRequest = null; + await hook(); + } if (state.tokenDelayMs) await new Promise((resolve) => setTimeout(resolve, state.tokenDelayMs)); if (body.get("grant_type") !== "client_credentials") return json({ error: "unsupported_grant_type" }, 400); const assertion = body.get("client_assertion"); @@ -602,6 +610,152 @@ describeEmbeddedPostgres("machine sign-in connections", () => { expect(provider.state.tokenRequests.at(-1)!.authorization).toBe(`Basic ${Buffer.from(`${CLIENT_ID}:switched-secret`).toString("base64")}`); }); + it("never leaves a refused test's secret behind with another client ID", async () => { + const company = await createCompany(); + const provider = useProvider(); + const svc = service(); + const created = await svc.createMachineSignIn(company.id, { + link: MCP_URL, method: "client_secret", clientId: CLIENT_ID, clientSecret: CLIENT_SECRET, + }, actor); + await svc.testMachineSignIn(company.id, created.connectionId, {}, actor); + provider.state.extraClients.set("other-client", "other-secret"); + // While one admin tests another client, a second admin starts a key rotation. + provider.state.onAuthorizedMcpRequest = async () => { + await svc.rotateMachineSignIn(company.id, created.connectionId, { method: "paperclip_key" }, actor); + }; + await expect(svc.testMachineSignIn(company.id, created.connectionId, { clientId: "other-client", clientSecret: "other-secret" }, actor)) + .rejects.toMatchObject({ status: 409, details: expect.objectContaining({ code: "client_credentials_changed" }) }); + expect((await svc.getMachineSignIn(company.id, created.connectionId)).clientId).toBe(CLIENT_ID); + // A normal call (no forced refresh) still runs as the original client: the + // refused test did not swap in its own token. + await svc.checkHealth(created.connectionId, actor); + const used = provider.state.mcpRequests.at(-1)!.authorization!.replace("Bearer ", ""); + expect(provider.state.tokenClients.get(used)).toBe(CLIENT_ID); + // The refused test wrote nothing: the saved ID and secret still belong together. + const grant = await organizationGrant(created.connectionId); + await svc.refreshOAuthGrantCredentials({ companyId: company.id, connectionId: created.connectionId, grantId: grant.id, forceRefresh: true, actor }); + expect(provider.state.tokenRequests.at(-1)!.authorization).toBe(`Basic ${Buffer.from(`${CLIENT_ID}:${CLIENT_SECRET}`).toString("base64")}`); + }); + + it("refuses an older retest after another retest replaced both the client ID and secret", async () => { + const company = await createCompany(); + const provider = useProvider(); + const svc = service(); + const created = await svc.createMachineSignIn(company.id, { + link: MCP_URL, method: "client_secret", clientId: CLIENT_ID, clientSecret: CLIENT_SECRET, + }, actor); + await svc.testMachineSignIn(company.id, created.connectionId, {}, actor); + provider.state.extraClients.set("other-client", "other-secret"); + // While one admin re-tests the original client, another replaces both the ID and secret. + provider.state.onAuthorizedMcpRequest = async () => { + await svc.testMachineSignIn(company.id, created.connectionId, { clientId: "other-client", clientSecret: "other-secret" }, actor); + }; + await expect(svc.testMachineSignIn(company.id, created.connectionId, { clientId: CLIENT_ID, clientSecret: CLIENT_SECRET }, actor)) + .rejects.toMatchObject({ status: 409, details: expect.objectContaining({ code: "client_credentials_changed" }) }); + expect((await svc.getMachineSignIn(company.id, created.connectionId)).clientId).toBe("other-client"); + const grant = await organizationGrant(created.connectionId); + await svc.refreshOAuthGrantCredentials({ companyId: company.id, connectionId: created.connectionId, grantId: grant.id, forceRefresh: true, actor }); + expect(provider.state.tokenRequests.at(-1)!.authorization).toBe(`Basic ${Buffer.from("other-client:other-secret").toString("base64")}`); + }); + + it("reloads accepted key types for a connection saved before they were recorded", async () => { + const company = await createCompany(); + const provider = useProvider(); + provider.state.signingAlgorithms = ["ES256"]; + const svc = service(); + const created = await svc.createMachineSignIn(company.id, { + link: MCP_URL, method: "client_secret", clientId: CLIENT_ID, clientSecret: CLIENT_SECRET, + }, actor); + await svc.testMachineSignIn(company.id, created.connectionId, {}, actor); + const [row] = await db.select().from(toolConnections).where(eq(toolConnections.id, created.connectionId)); + const { tokenEndpointAuthSigningAlgValuesSupported: _dropped, ...olderOauth } = row!.config.oauth as Record; + const olderConfig = { ...row!.config, oauth: olderOauth }; + await db.update(toolConnections).set({ config: olderConfig, transportConfig: olderConfig }).where(eq(toolConnections.id, row!.id)); + const started = await svc.rotateMachineSignIn(company.id, created.connectionId, { method: "paperclip_key" }, actor); + expect(started.machineSignIn.pendingPublicKey?.algorithm).toBe("ES256"); + }); + + it("lets a retest that replaces the secret and normal token refreshes finish together", async () => { + const company = await createCompany(); + const provider = useProvider(); + const svc = service(); + const created = await svc.createMachineSignIn(company.id, { + link: MCP_URL, method: "client_secret", clientId: CLIENT_ID, clientSecret: CLIENT_SECRET, + }, actor); + await svc.testMachineSignIn(company.id, created.connectionId, {}, actor); + const grant = await organizationGrant(created.connectionId); + const refresh = () => service().refreshOAuthGrantCredentials({ companyId: company.id, connectionId: created.connectionId, grantId: grant.id, forceRefresh: true, actor }); + const started = Date.now(); + await Promise.all([ + svc.testMachineSignIn(company.id, created.connectionId, { clientSecret: CLIENT_SECRET }, actor), + refresh(), refresh(), refresh(), + ]); + expect(Date.now() - started).toBeLessThan(15_000); + }, 60_000); + + it("recovers from a refresh abandoned by a stopped server", async () => { + const company = await createCompany(); + const provider = useProvider(); + const { svc, created } = await setupKeyConnection(company.id, provider); + const grant = await organizationGrant(created.connectionId); + const abandoned = async () => { + const current = await organizationGrant(created.connectionId); + await db.update(connectionGrants).set({ + providerTenant: { ...(current.providerTenant ?? {}), oauth: { + ...((current.providerTenant ?? {}).oauth as object ?? {}), + accessTokenExpiresAt: "2000-01-01T00:00:00.000Z", + refreshLease: { id: "stopped-server", expiresAt: "2000-01-01T00:00:00.000Z" }, + } }, + }).where(eq(connectionGrants.id, grant.id)); + }; + // The next request takes over the abandoned lease and gets a new token. + await abandoned(); + const before = provider.state.issued; + await svc.refreshOAuthGrantCredentials({ companyId: company.id, connectionId: created.connectionId, grantId: grant.id, actor }); + expect(provider.state.issued).toBe(before + 1); + // A successful retest also clears an abandoned lease. + await abandoned(); + await svc.testMachineSignIn(company.id, created.connectionId, {}, actor); + expect(((await organizationGrant(created.connectionId)).providerTenant?.oauth as Record).refreshLease).toBeUndefined(); + }); + + it("never saves the token of a refresh whose lease was taken over", async () => { + const company = await createCompany(); + const provider = useProvider(); + const { created } = await setupKeyConnection(company.id, provider); + const grant = await organizationGrant(created.connectionId); + const input = { companyId: company.id, connectionId: created.connectionId, grantId: grant.id, forceRefresh: true, actor }; + // Refresh A pauses past its lease; refresh B on another server takes over and finishes. + provider.state.onTokenRequest = async () => { + const current = await organizationGrant(created.connectionId); + const oauth = (current.providerTenant ?? {}).oauth as Record; + await db.update(connectionGrants).set({ + providerTenant: { ...(current.providerTenant ?? {}), oauth: { ...oauth, refreshLease: { ...(oauth.refreshLease as object), expiresAt: "2000-01-01T00:00:00.000Z" } } }, + }).where(eq(connectionGrants.id, grant.id)); + await service().refreshOAuthGrantCredentials(input); + }; + const issuedBefore = provider.state.issued; + await expect(service().refreshOAuthGrantCredentials(input)) + .rejects.toMatchObject({ details: expect.objectContaining({ code: "oauth_refresh_superseded" }) }); + const takeoverToken = `machine-token-${issuedBefore + 1}`; + // Normal calls use the takeover's token, not the one the paused refresh received later. + await service().checkHealth(created.connectionId, actor); + expect(provider.state.mcpRequests.at(-1)!.authorization).toBe(`Bearer ${takeoverToken}`); + }); + + it("creates the kind of key the sign-in service accepts when switching from a secret", async () => { + const company = await createCompany(); + const provider = useProvider(); + provider.state.signingAlgorithms = ["ES256"]; + const svc = service(); + const created = await svc.createMachineSignIn(company.id, { + link: MCP_URL, method: "client_secret", clientId: CLIENT_ID, clientSecret: CLIENT_SECRET, + }, actor); + await svc.testMachineSignIn(company.id, created.connectionId, {}, actor); + const started = await svc.rotateMachineSignIn(company.id, created.connectionId, { method: "paperclip_key" }, actor); + expect(started.machineSignIn.pendingPublicKey?.algorithm).toBe("ES256"); + }); + it("tries a new client secret before replacing the old one", async () => { const company = await createCompany(); const provider = useProvider(); diff --git a/server/src/services/tool-access/oauth/client-credentials-service.ts b/server/src/services/tool-access/oauth/client-credentials-service.ts index 3ef09066467..880fd550c93 100644 --- a/server/src/services/tool-access/oauth/client-credentials-service.ts +++ b/server/src/services/tool-access/oauth/client-credentials-service.ts @@ -1,4 +1,5 @@ import { randomUUID } from "node:crypto"; +import { withAccountHomeSecretMutationLock } from "@paperclipai/adapter-codex-local/server"; import { and, eq, ne, sql } from "drizzle-orm"; import type { Db } from "@paperclipai/db"; import { connectionGrants, toolApplications, toolConnections } from "@paperclipai/db"; @@ -253,6 +254,19 @@ export function createClientCredentialsService(db: Db, deps: Dependencies) { * recorded at setup. If the MCP server now advertises other services and not * that one, refuse rather than follow it. */ + /** + * The signing algorithms the pinned sign-in service accepts. Connections set + * up before this was saved reload it from that same issuer's metadata. + */ + async function acceptedSigningAlgorithms(oauth: Record, settings: ClientCredentialsSettings): Promise { + if (Array.isArray(oauth.tokenEndpointAuthSigningAlgValuesSupported)) { + return normalizeOauthScopes(oauth.tokenEndpointAuthSigningAlgValuesSupported); + } + if (!settings.issuer) return []; + const reloaded = await signInServiceMetadata(settings.issuer).catch(() => null); + return reloaded?.signingAlgorithms ?? []; + } + async function assertPinnedSignInService(connection: typeof toolConnections.$inferSelect, settings: ClientCredentialsSettings) { if (!settings.issuer) return; const serverUrl = typeof connection.config.url === "string" ? connection.config.url : null; @@ -390,24 +404,60 @@ export function createClientCredentialsService(db: Db, deps: Dependencies) { return { secretId: secret.id, versionSelector: "latest", configPath, required: false, label }; } + type ConnectionPatch = Partial; + + /** + * One locked commit: the connection must still sign in the way `previous` + * did (same method, key and credential references) and not be removed. Any + * new credential is written in the same transaction, so a client ID and its + * secret always land together or not at all. + */ async function saveConnection( previous: typeof toolConnections.$inferSelect, - patch: Partial, + patch: ConnectionPatch | ((written: Record) => ConnectionPatch), + credentials: Array<{ configPath: string; label: string; value: string; required?: boolean }> = [], + actor?: ActorInfo, ) { - // Commit only if nothing that decides how this connection signs in changed - // since it was read (a rotation or another test), and it was not removed. - const clientAuth = asRecord(oauthConfig(previous).clientAuth); - const [updated] = await db.update(toolConnections) - .set({ ...patch, ...(patch.config ? { transportConfig: patch.config } : {}), updatedAt: now() }) - .where(and( - eq(toolConnections.id, previous.id), - eq(toolConnections.companyId, previous.companyId), - ne(toolConnections.status, "archived"), - sql`coalesce(${toolConnections.config} -> 'oauth' -> 'clientAuth', '{}'::jsonb) = ${JSON.stringify(clientAuth)}::jsonb`, - sql`${toolConnections.credentialSecretRefs} = ${JSON.stringify(previous.credentialSecretRefs)}::jsonb`, - )) - .returning(); - if (!updated) throw machineSignInChangedError(); + // Everything that decides who this connection signs in as. Writes rotate + // vault entries in place (same secret ID), so a revision counter, bumped by + // every commit, catches a concurrent change the references alone would miss. + const signInState = (row: typeof toolConnections.$inferSelect) => { + const oauth = oauthConfig(row); + return JSON.stringify([asRecord(oauth.clientAuth), oauth.clientId ?? null, oauth.credentialRevision ?? 0, row.credentialSecretRefs]); + }; + // Take the company's secret-write lock before any database lock and hold it + // through commit. Each vault write takes it too (re-entrantly here); taking + // it per write inside this transaction could deadlock with a token refresh + // that holds it while waiting on a row this transaction has locked. + const updated = await withAccountHomeSecretMutationLock(undefined, previous.companyId, () => db.transaction(async (tx) => { + const [locked] = await tx.select().from(toolConnections) + .where(and(eq(toolConnections.id, previous.id), eq(toolConnections.companyId, previous.companyId))) + .for("update"); + if (!locked || locked.status === "archived" || signInState(locked) !== signInState(previous)) { + throw machineSignInChangedError(); + } + const written: Record = {}; + for (const credential of credentials) { + const existing = locked.credentialSecretRefs.find((ref) => ref.configPath === credential.configPath); + const { secret } = await writeConnectionCredential(tx, { + companyId: locked.companyId, connectionName: locked.name, configPath: credential.configPath, + label: credential.label, value: credential.value, existingRef: existing, actor: actorForSecret(actor), + }); + written[credential.configPath] = { + secretId: secret.id, versionSelector: "latest", configPath: credential.configPath, + required: credential.required ?? false, label: credential.label, + }; + } + const resolved = typeof patch === "function" ? patch(written) : patch; + const baseConfig = resolved.config ?? locked.config; + const revision = Number(oauthConfig(locked).credentialRevision ?? 0) + 1; + const config = { ...baseConfig, oauth: { ...asRecord(asRecord(baseConfig).oauth), credentialRevision: revision } }; + const [row] = await tx.update(toolConnections) + .set({ ...resolved, config, transportConfig: config, updatedAt: now() }) + .where(eq(toolConnections.id, locked.id)) + .returning(); + return row!; + })); await store.syncCredentialBindings(updated); if (updated.status === "active") await deps.ensureDefaultOrganizationGrant(updated); emitConnectionUpdated(updated, previous, "gallery"); @@ -452,6 +502,7 @@ export function createClientCredentialsService(db: Db, deps: Dependencies) { scopes: scopesFrom(input.scopes) ?? [], clientId: input.clientId ?? null, tokenEndpointAuthMethodsSupported: service.authMethods, + tokenEndpointAuthSigningAlgValuesSupported: service.signingAlgorithms, clientAuth: { method: authMethod, ...(generated ? { key: generated.publicKey } : {}), configuredAt: createdAt.toISOString() }, }; @@ -546,20 +597,6 @@ export function createClientCredentialsService(db: Db, deps: Dependencies) { // failed retest must leave a working connection's credentials untouched. await deps.probeServerWithToken(connection, `Bearer ${token.accessToken}`, actor); // The settings worked: keep them. - const credentialSecretRefs = [...connection.credentialSecretRefs]; - if (input.clientSecret) { - const existing = credentialSecretRefs.find((ref) => ref.configPath === CLIENT_SECRET_PATH); - const ref = await writeVaultCredential(connection, CLIENT_SECRET_PATH, "Machine sign-in client secret", input.clientSecret, actor, existing); - if (!existing) credentialSecretRefs.push(ref); - } - const accessRef = await store.createOrRotateOAuthSecret({ - companyId, - connection, - configPath: "oauth.access_token", - label: "OAuth access token", - value: token.accessToken, - actor, - }); const at = now(); const expiresAt = token.expiresIn ? new Date(at.getTime() + token.expiresIn * 1000).toISOString() : null; const oauth = oauthConfig(connection); @@ -576,19 +613,35 @@ export function createClientCredentialsService(db: Db, deps: Dependencies) { refreshedAt: at.toISOString(), }, }; - connection = await saveConnection(connection, { - config: nextConfig, - credentialSecretRefs: [...credentialSecretRefs.filter((ref) => ref.configPath !== "oauth.access_token"), accessRef], - credentialRefs: [ - ...connection.credentialRefs.filter((ref) => ref.name !== "oauth.access_token"), - { name: "oauth.access_token", secretId: accessRef.secretId, version: "latest", placement: "header", key: "Authorization", prefix: "Bearer " }, - ], - }); + // The new token, any replacement secret and the settings commit together, + // after the check that nothing changed: a refused test changes nothing. + connection = await saveConnection(connection, (written) => { + const accessRef = written["oauth.access_token"]!; + const secretRef = written[CLIENT_SECRET_PATH]; + return { + config: nextConfig, + credentialSecretRefs: [ + ...connection.credentialSecretRefs.filter((ref) => + ref.configPath !== "oauth.access_token" && (!secretRef || ref.configPath !== CLIENT_SECRET_PATH)), + ...(secretRef ? [secretRef] : []), + accessRef, + ], + credentialRefs: [ + ...connection.credentialRefs.filter((ref) => ref.name !== "oauth.access_token"), + { name: "oauth.access_token", secretId: accessRef.secretId, version: "latest", placement: "header", key: "Authorization", prefix: "Bearer " }, + ], + }; + }, [ + { configPath: "oauth.access_token", label: "OAuth access token", value: token.accessToken, required: true }, + ...(input.clientSecret ? [{ configPath: CLIENT_SECRET_PATH, label: "Machine sign-in client secret", value: input.clientSecret }] : []), + ], actor); // The organization grant is what tool calls run on; give it the token now, // with this token's expiry (the grant's own expiry wins over the connection's). await deps.ensureDefaultOrganizationGrant(connection); await db.update(connectionGrants).set({ - providerTenant: sql`jsonb_set(coalesce(${connectionGrants.providerTenant}, '{}'::jsonb), '{oauth}', (coalesce(${connectionGrants.providerTenant}->'oauth', '{}'::jsonb) || ${JSON.stringify({ accessTokenExpiresAt: expiresAt, tokenType: token.tokenType, refreshedAt: at.toISOString() })}::jsonb) - 'refreshLease')`, + providerTenant: sql`jsonb_set(coalesce(${connectionGrants.providerTenant}, '{}'::jsonb), '{oauth}', (coalesce(${connectionGrants.providerTenant}->'oauth', '{}'::jsonb) || ${JSON.stringify({ accessTokenExpiresAt: expiresAt, tokenType: token.tokenType, refreshedAt: at.toISOString() })}::jsonb) + -- A live refresh keeps its lease; one abandoned by a stopped server is cleared with the tested credentials. + - (case when (${connectionGrants.providerTenant} #>> '{oauth,refreshLease,expiresAt}')::timestamptz <= ${at.toISOString()}::timestamptz then 'refreshLease' else '' end))`, updatedAt: now(), }).where(and( eq(connectionGrants.companyId, companyId), @@ -647,27 +700,25 @@ export function createClientCredentialsService(db: Db, deps: Dependencies) { : settings.authMethod; await assertPinnedSignInService(connection, settings); await requestClientCredentialsToken(connection, actor, { clientSecret: input.clientSecret, authMethod }); - const existing = connection.credentialSecretRefs.find((ref) => ref.configPath === CLIENT_SECRET_PATH); - const ref = await writeVaultCredential(connection, CLIENT_SECRET_PATH, "Machine sign-in client secret", input.clientSecret, actor, existing); const retired = connection.credentialSecretRefs.filter((candidate) => candidate.configPath === CLIENT_PRIVATE_KEY_PATH || candidate.configPath === CLIENT_PRIVATE_KEY_NEXT_PATH); - const updated = await saveConnection(connection, { + const updated = await saveConnection(connection, (written) => ({ config: { ...connection.config, oauth: { ...oauth, clientAuth: { method: authMethod, configuredAt: now().toISOString() } } }, credentialSecretRefs: [ ...connection.credentialSecretRefs.filter((candidate) => candidate.configPath !== CLIENT_SECRET_PATH && !retired.includes(candidate)), - ref, + written[CLIENT_SECRET_PATH]!, ], - }); + }), [{ configPath: CLIENT_SECRET_PATH, label: "Machine sign-in client secret", value: input.clientSecret }], actor); for (const old of retired) await secrets.remove(old.secretId).catch(() => undefined); return { connectionId, connection: toConnection(updated), machineSignIn: machineSignInStatus(updated), completed: true }; } - const generated = await generateClientKeyPair(settings.key?.algorithm ?? "RS256", now()); - const existing = connection.credentialSecretRefs.find((ref) => ref.configPath === CLIENT_PRIVATE_KEY_NEXT_PATH); - const ref = await writeVaultCredential(connection, CLIENT_PRIVATE_KEY_NEXT_PATH, "Machine sign-in replacement key", generated.privateKeyPem, actor, existing); - const updated = await saveConnection(connection, { + // Keep the current key's algorithm; switching from a secret uses what the sign-in service accepts. + const algorithm = settings.key?.algorithm ?? chooseAssertionAlgorithm(await acceptedSigningAlgorithms(oauth, settings)); + const generated = await generateClientKeyPair(algorithm, now()); + const updated = await saveConnection(connection, (written) => ({ config: { ...connection.config, oauth: { ...oauth, clientAuth: { ...clientAuth, pendingKey: generated.publicKey } } }, - credentialSecretRefs: [...connection.credentialSecretRefs.filter((candidate) => candidate.configPath !== CLIENT_PRIVATE_KEY_NEXT_PATH), ref], - }); + credentialSecretRefs: [...connection.credentialSecretRefs.filter((candidate) => candidate.configPath !== CLIENT_PRIVATE_KEY_NEXT_PATH), written[CLIENT_PRIVATE_KEY_NEXT_PATH]!], + }), [{ configPath: CLIENT_PRIVATE_KEY_NEXT_PATH, label: "Machine sign-in replacement key", value: generated.privateKeyPem }], actor); return { connectionId, connection: toConnection(updated), machineSignIn: machineSignInStatus(updated), completed: false }; } @@ -695,7 +746,7 @@ export function createClientCredentialsService(db: Db, deps: Dependencies) { }); const pendingRef = connection.credentialSecretRefs.find((ref) => ref.configPath === CLIENT_PRIVATE_KEY_NEXT_PATH); // One locked step: the row still stages this key, then the vault and config change together. - const updated = await db.transaction(async (tx) => { + const updated = await withAccountHomeSecretMutationLock(undefined, companyId, () => db.transaction(async (tx) => { const [locked] = await tx.select().from(toolConnections) .where(and(eq(toolConnections.id, connection.id), eq(toolConnections.companyId, companyId))) .for("update"); @@ -712,7 +763,11 @@ export function createClientCredentialsService(db: Db, deps: Dependencies) { const lockedOauth = oauthConfig(locked); const nextConfig = { ...locked.config, - oauth: { ...lockedOauth, clientAuth: { method: "private_key_jwt", key: promoted, configuredAt: now().toISOString() } }, + oauth: { + ...lockedOauth, + clientAuth: { method: "private_key_jwt", key: promoted, configuredAt: now().toISOString() }, + credentialRevision: Number(lockedOauth.credentialRevision ?? 0) + 1, + }, }; const [row] = await tx.update(toolConnections).set({ config: nextConfig, @@ -725,7 +780,7 @@ export function createClientCredentialsService(db: Db, deps: Dependencies) { updatedAt: now(), }).where(eq(toolConnections.id, locked.id)).returning(); return row!; - }); + })); await store.syncCredentialBindings(updated); if (updated.status === "active") await deps.ensureDefaultOrganizationGrant(updated); emitConnectionUpdated(updated, connection, "gallery"); diff --git a/server/src/services/tool-access/oauth/grant-refresh.ts b/server/src/services/tool-access/oauth/grant-refresh.ts index e3ee3c79d95..1754f2c43be 100644 --- a/server/src/services/tool-access/oauth/grant-refresh.ts +++ b/server/src/services/tool-access/oauth/grant-refresh.ts @@ -1,6 +1,7 @@ import { notifyGitHubCredentialChange } from "../../github-connection-notifications.js"; import { writeToolConnectionHealthChange } from "../../tool-connection-health-notifications.js"; -import type { ActorInfo } from "../types.js"; +import type { ActorInfo, DbTransaction } from "../types.js"; +import { withAccountHomeSecretMutationLock } from "@paperclipai/adapter-codex-local/server"; import { createOAuthClientProtocol, storedOAuthTokenEndpointAuthMethod } from "./client.js"; import { oauthConfig, normalizeOauthScopes, resolveGrantedOauthScopes } from "./metadata.js"; import { googleWorkspaceEffectiveScopes, isGoogleWorkspaceScopeGrant } from "@paperclipai/shared"; @@ -478,7 +479,7 @@ export function createOAuthGrantRefresh(db: Db, { now, store, leases, currentClo : undefined, }); } - const accessRef = await createOrRotateOAuthSecret({ + const writeAccessToken = (client?: DbTransaction) => createOrRotateOAuthSecret({ companyId: connection.companyId, connection, configPath: "oauth.access_token", @@ -491,14 +492,18 @@ export function createOAuthGrantRefresh(db: Db, { now, store, leases, currentClo grant.kind === "user" ? (grant.subjectUserId ?? undefined) : undefined, - }); - const nextCredentialSecretRefs = [ + }, client ? { dbClient: client, secretClient: secretService(client) } : undefined); + // Machine sign-in may take over a lease abandoned by a paused server, so + // its token is written only inside the commit that proves this refresh + // still owns the lease (below). Other grants keep their existing order. + const accessRef = machineSignIn ? null : await writeAccessToken(); + const credentialSecretRefsWith = (access: ToolCredentialSecretRef) => [ ...grant.credentialSecretRefs.filter( (ref) => ref.configPath !== "oauth.access_token" && (!nextRefreshRef || ref.configPath !== "oauth.refresh_token"), ), - accessRef, + access, ...(nextRefreshRef ? [nextRefreshRef] : []), ]; const expiresAt = token.expiresIn @@ -540,11 +545,11 @@ export function createOAuthGrantRefresh(db: Db, { now, store, leases, currentClo tokenType: token.tokenType, refreshedAt: now().toISOString(), }; - const [updated] = await writeToolConnectionHealthChange(db, connection, tx => tx + const commitGrant = (tx: Db, access: ToolCredentialSecretRef) => tx .update(connectionGrants) .set({ providerTenant: sql`jsonb_set(coalesce(${connectionGrants.providerTenant}, '{}'::jsonb), '{oauth}', (coalesce(${connectionGrants.providerTenant}->'oauth', '{}'::jsonb) || ${JSON.stringify(oauthUpdates)}::jsonb) - 'refreshLease')`, - credentialSecretRefs: nextCredentialSecretRefs, + credentialSecretRefs: credentialSecretRefsWith(access), status: "active", updatedAt: now(), }) @@ -556,7 +561,22 @@ export function createOAuthGrantRefresh(db: Db, { now, store, leases, currentClo sql`${connectionGrants.providerTenant} -> 'oauth' -> 'refreshLease' ->> 'id' = ${lease.leaseId}`, ), ) - .returning()); + .returning(); + const [updated] = machineSignIn + ? await withAccountHomeSecretMutationLock(undefined, connection.companyId, () => + writeToolConnectionHealthChange(db, connection, async (tx) => { + const [owned] = await tx.select({ id: connectionGrants.id }).from(connectionGrants) + .where(and( + eq(connectionGrants.id, grant.id), + eq(connectionGrants.companyId, grant.companyId), + eq(connectionGrants.status, "active"), + sql`${connectionGrants.providerTenant} -> 'oauth' -> 'refreshLease' ->> 'id' = ${lease.leaseId}`, + )) + .for("update"); + if (!owned) return []; + return commitGrant(tx, await writeAccessToken(tx as unknown as DbTransaction)); + })) + : await writeToolConnectionHealthChange(db, connection, (tx) => commitGrant(tx, accessRef!)); if (!updated) { throw conflict( "OAuth credentials changed while refresh was in progress. Retry the request.", @@ -615,7 +635,7 @@ export function createOAuthGrantRefresh(db: Db, { now, store, leases, currentClo ), ); const nextKeys = new Set( - nextCredentialSecretRefs.map( + updated.credentialSecretRefs.map( (ref) => `${ref.secretId}:${ref.configPath}`, ), ); diff --git a/server/src/services/tool-access/oauth/refresh-leases.ts b/server/src/services/tool-access/oauth/refresh-leases.ts index c3040b8828d..f3f4323d52c 100644 --- a/server/src/services/tool-access/oauth/refresh-leases.ts +++ b/server/src/services/tool-access/oauth/refresh-leases.ts @@ -1,4 +1,5 @@ import { oauthConfig } from "./metadata.js"; +import { usesClientCredentials } from "./client-credentials.js"; import { randomUUID } from "node:crypto"; import { and, eq, sql } from "drizzle-orm"; import type { Db } from "@paperclipai/db"; @@ -267,6 +268,33 @@ export function createOAuthRefreshLeases(db: Db, now: () => Date, getConnectionR .returning(); if (claimed) return { grant: claimed, leaseId }; } + if (currentLeaseId && !leaseIsActive && usesClientCredentials(connection)) { + // Machine sign-in has no refresh token that an interrupted refresh + // could have consumed, so an abandoned lease is safe to take over: + // the next request simply asks for a new token. + const leaseId = randomUUID(); + const providerTenant = { + ...(latest.providerTenant ?? {}), + oauth: { + ...withoutOAuthGrantRefreshLease(oauth), + refreshLease: { id: leaseId, expiresAt: new Date(Date.now() + OAUTH_REFRESH_LEASE_MS).toISOString() }, + }, + }; + const [claimed] = await db + .update(connectionGrants) + .set({ providerTenant, updatedAt: now() }) + .where( + and( + eq(connectionGrants.id, latest.id), + eq(connectionGrants.companyId, latest.companyId), + eq(connectionGrants.status, "active"), + sql`${connectionGrants.providerTenant} #>> '{oauth,refreshLease,id}' = ${currentLeaseId}`, + ), + ) + .returning(); + if (claimed) return { grant: claimed, leaseId }; + continue; + } if (currentLeaseId && !leaseIsActive) { throw unprocessable( "The previous OAuth refresh did not finish. Reconnect this app before retrying.", diff --git a/ui/src/features/connections/ConnectionSetupFlow.tsx b/ui/src/features/connections/ConnectionSetupFlow.tsx index aa311babc4b..14f866a0215 100644 --- a/ui/src/features/connections/ConnectionSetupFlow.tsx +++ b/ui/src/features/connections/ConnectionSetupFlow.tsx @@ -2834,7 +2834,7 @@ function StandardConnectionSetupFlow({ connectApp(); }} machineSignIn={effectiveGrantKind === "organization" && selectedCompanyId && !reconnectConnectionId - ? { companyId: selectedCompanyId, onConnected: acceptMachineSignInResult } + ? { companyId: selectedCompanyId, applicationId: prefill.applicationId, onConnected: acceptMachineSignInResult } : null} /> )} diff --git a/ui/src/features/connections/GenericMcpConnectStep.tsx b/ui/src/features/connections/GenericMcpConnectStep.tsx index 34dc87c8c9d..09e2e1eddf8 100644 --- a/ui/src/features/connections/GenericMcpConnectStep.tsx +++ b/ui/src/features/connections/GenericMcpConnectStep.tsx @@ -141,7 +141,7 @@ export function GenericMcpConnectStep({ onBack: () => void; onConnect: () => void; /** Offered only for organization connections: personal connections sign in as the person. */ - machineSignIn?: { companyId: string; onConnected: (result: ConnectToolAppResult) => void } | null; + machineSignIn?: { companyId: string; applicationId?: string | null; onConnected: (result: ConnectToolAppResult) => void } | null; }) { const [clientFieldsOpen, setClientFieldsOpen] = useState(false); // Machine sign-in runs its own setup and test, so it is a choice beside the modes, not one of them. @@ -417,6 +417,7 @@ export function GenericMcpConnectStep({
{ status: vi.fn(), }; await act(async () => { - root.render( undefined} onConnected={() => undefined} />); + root.render( undefined} onConnected={() => undefined} />); }); expect(button("Test connection")?.disabled).toBe(true); await type("machine-sign-in-client-id", "id-1"); await type("machine-sign-in-client-secret", "secret-1"); await click("Test connection"); - expect(client.create).toHaveBeenCalledWith("company-1", expect.objectContaining({ method: "client_secret", clientId: "id-1", clientSecret: "secret-1" })); + expect(client.create).toHaveBeenCalledWith("company-1", expect.objectContaining({ method: "client_secret", clientId: "id-1", clientSecret: "secret-1", applicationId: "app-1" })); expect(client.test).toHaveBeenCalledWith("connection-1", { clientId: "id-1", scopes: "" }); }); @@ -115,7 +115,7 @@ describe("Machine sign-in setup", () => { }); it("resumes the key draft this tab started instead of creating another", async () => { - sessionStorage.setItem("paperclip.machine-sign-in-draft:company-1:https://mcp.example/mcp", "connection-1"); + sessionStorage.setItem("paperclip.machine-sign-in-draft.v2:company-1:new:https://mcp.example/mcp", "connection-1"); let finish!: (value: typeof draft.machineSignIn) => void; const client: MachineSignInClient = { create: vi.fn(), @@ -134,4 +134,28 @@ describe("Machine sign-in setup", () => { expect(client.create).not.toHaveBeenCalled(); sessionStorage.clear(); }); + + it("never resumes a draft started for a different app", async () => { + const client: MachineSignInClient = { + create: vi.fn(async () => draft), + test: vi.fn(), + status: vi.fn(async () => draft.machineSignIn), + }; + // App A's setup creates a key draft in this tab, then the admin leaves it. + await act(async () => { + root.render( undefined} onConnected={() => undefined} />); + }); + await click("Create key"); + await act(async () => root.unmount()); + root = createRoot(container); + // Setup for app B at the same address starts fresh instead of adopting A's draft. + await act(async () => { + root.render( undefined} onConnected={() => undefined} />); + }); + await flush(); + expect(client.status).not.toHaveBeenCalled(); + await click("Create key"); + expect(client.create).toHaveBeenLastCalledWith("company-1", expect.objectContaining({ applicationId: "app-b" })); + sessionStorage.clear(); + }); }); diff --git a/ui/src/features/connections/MachineSignInSetup.tsx b/ui/src/features/connections/MachineSignInSetup.tsx index 98f29980b62..073fdf5d3e6 100644 --- a/ui/src/features/connections/MachineSignInSetup.tsx +++ b/ui/src/features/connections/MachineSignInSetup.tsx @@ -36,8 +36,9 @@ type MachineSignInDraft = Pick(initialSetup); const [error, setError] = useState(initialError); const [busy, setBusy] = useState<"creating" | "testing" | null>(null); - const storageKey = draftStorageKey(companyId, link); + const storageKey = draftStorageKey(companyId, applicationId, link); // While a saved draft loads, creating another would race it; wait for it. const [restoring, setRestoring] = useState(() => !initialSetup && Boolean(readDraftId(storageKey))); @@ -141,6 +145,7 @@ export function MachineSignInSetup({ ...(clientId.trim() ? { clientId: clientId.trim() } : {}), ...(method === "client_secret" && clientSecret ? { clientSecret } : {}), ...(trimmedScopes ? { scopes: trimmedScopes } : {}), + ...(applicationId ? { applicationId } : {}), }); setSetup(created); writeDraftId(storageKey, created.connectionId);