import type { AuthenticatedAppUser } from '#app/authenticated-user.ts' import { operationResult } from '#app/operation-result.ts' import { readNonEmptyTrimmedString } from '#app/request-body.ts' import { McpCallerError } from '#mcp/caller-error.ts' import { AuthorizationError, authorize, } from '#worker/authorization/authorize.ts' import { credentialFillPlacementSchema, credentialInjectSchema, defaultCredentialInject, readCredentialFields, updateCredentialFields, } from '#worker/credentials/credential-fields.ts' import { credentialPermissionFamily, credentialResource, getOrgCredential, missingCredentialResource, } from '#worker/credentials/credential-ids.ts' import { recordOwnerFillPlacementOptIn } from '#worker/credentials/fill-placement-opt-ins.ts' import { orgAuditWriterFromRequest } from '#worker/orgs/org-audit.ts' import { ownerIdFromCaller } from '#worker/request-context/owner-id.ts' import type { CredentialFillPlacementView } from '#universal/credential-fill-placement.ts' export type SetCredentialPlacementResponse = { ok: true placement: CredentialFillPlacementView | null changed: boolean } /** Approves a typed request profile on a Key, or restores its default inject. */ export async function setCredentialRequestProfile(input: { env: Env user: AuthenticatedAppUser credentialId: string profile: string | null }) { const { request } = input.user if (request.membership?.role !== 'owner') return operationResult( { ok: false, error: 'Only the owner can approve a request profile.' }, 403, ) let inject = defaultCredentialInject if (input.profile !== null) { let raw: unknown try { raw = JSON.parse(input.profile) } catch { return operationResult( { ok: false, error: 'Enter a valid JSON request profile.' }, 400, ) } const parsed = credentialInjectSchema.safeParse(raw) if ( !parsed.success || !['body', 'path', 'composition'].includes(parsed.data.kind) ) return operationResult( { ok: false, error: 'Enter a valid body, path, or composition profile.', }, 400, ) inject = parsed.data } const orgId = ownerIdFromCaller({ request, user: input.user.mcpUser }) try { const credential = await getOrgCredential( input.env.APP_DB, orgId, input.credentialId, ) if (!credential?.live || credential.kind !== 'secret') return operationResult( { ok: false, error: 'Select a Key in this organization.' }, 404, ) await authorize( { env: input.env, request }, 'secret:write', credentialResource(request.org.id, credential), ) if (input.profile === null) { const before = await readCredentialFields({ db: input.env.APP_DB, credential, }) if (!['body', 'path', 'composition'].includes(before.inject.kind)) return operationResult( { ok: false, error: 'This Key has no approved request profile.' }, 400, ) } await updateCredentialFields({ db: input.env.APP_DB, orgId, credentialId: input.credentialId, update: { inject }, audit: orgAuditWriterFromRequest(input.env, request), }) return operationResult({ ok: true }) } catch (error) { if (error instanceof AuthorizationError) return operationResult({ ok: false, error: error.message }, 403) if (error instanceof McpCallerError) return operationResult({ ok: false, error: error.message }, 400) throw error } } export async function setCredentialPlacement(input: { env: Env user: AuthenticatedAppUser body: object }) { const credentialId = readNonEmptyTrimmedString(input.body, 'credentialId') if (!credentialId) { return operationResult( { ok: false, error: 'Credential id is required.' }, 400, ) } const raw = (input.body as Record)['placement'] const parsed = raw === null ? { success: true as const, data: null } : credentialFillPlacementSchema.safeParse(raw) if (!parsed.success) { return operationResult( { ok: false, error: parsed.error.issues[0]?.message ?? 'Placement must allow at least one request part.', }, 400, ) } const request = input.user.request const orgId = ownerIdFromCaller({ request, user: input.user.mcpUser }) try { const credential = await getOrgCredential( input.env.APP_DB, orgId, credentialId, ) if (!credential?.live) { await authorize( { env: input.env, request }, 'secret:write', missingCredentialResource(request.org.id, credentialId), ) return operationResult( { ok: false, error: 'No credential with that id in this org.' }, 404, ) } if (credential.kind === 'mcp-server') { return operationResult( { ok: false, error: 'MCP servers have no placeholder fill placement.', }, 400, ) } await authorize( { env: input.env, request }, `${credentialPermissionFamily(credential.kind)}:write`, credentialResource(request.org.id, credential), ) const before = await readCredentialFields({ db: input.env.APP_DB, credential, }) const unchanged = JSON.stringify(before.placement) === JSON.stringify(parsed.data) if (unchanged) { // Saving on the website is the owner's opt-in, even when a package // or code already set the same placement. await recordOwnerFillPlacementOptIn({ db: input.env.APP_DB, credentialId: credential.id, placement: before.placement, }) return operationResult({ ok: true, placement: before.placement, changed: false, } satisfies SetCredentialPlacementResponse) } const updated = await updateCredentialFields({ db: input.env.APP_DB, orgId, credentialId: credential.id, update: { placement: parsed.data }, audit: orgAuditWriterFromRequest(input.env, request), ownerOptIn: true, }) return operationResult({ ok: true, placement: updated.placement, changed: true, } satisfies SetCredentialPlacementResponse) } catch (error) { if (error instanceof AuthorizationError) { return operationResult({ ok: false, error: error.message }, 403) } if (error instanceof McpCallerError) { return operationResult({ ok: false, error: error.message }, 400) } throw error } }