{"version":3,"file":"piece-auth-BS5CtQOG.mjs","names":[],"sources":["../../editors/connection-editor/piece-auth.ts"],"sourcesContent":["// Turns a piece's PieceAuth descriptor into a concrete field plan that maps\n// onto powerhouse/connection state: config fields vs secret refs.\nimport type { ConnectionAuthType } from \"document-models/connection\";\n\nexport interface AuthField {\n  name: string;\n  displayName: string;\n  required: boolean;\n  description?: string;\n  // Non-secret fields only: SHORT_TEXT | NUMBER | CHECKBOX | STATIC_DROPDOWN.\n  inputType?: string;\n  options?: { label: string; value: unknown }[];\n}\n\n// A piece auth type this runtime doesn't know: shown, never saved.\nexport const UNKNOWN_AUTH = \"UNKNOWN\";\n\nexport interface AuthPlan {\n  authType: ConnectionAuthType | typeof UNKNOWN_AUTH;\n  displayName?: string;\n  description?: string;\n  configFields: AuthField[];\n  secretFields: AuthField[];\n  // OIDC is declared but not executable by the runtime yet.\n  supported: boolean;\n  // Signed in through the provider; the token is stored by the switchboard.\n  oauth2?: boolean;\n  // The piece's own name for an UNKNOWN auth type.\n  declaredType?: string;\n}\n\n// Missing means unset, explicitly null, or emptied to \"\" - not `false`/`0`.\nexport function isConfigValueMissing(value: unknown): boolean {\n  return value === undefined || value === null || value === \"\";\n}\n\n// Whether every required config/secret field of the plan is filled; pure so\n// it can drive a live re-check as the user edits, not just a one-time mark.\nexport function isAuthComplete(\n  plan: AuthPlan,\n  config: Record<string, unknown>,\n  refs: Map<string, string>,\n): boolean {\n  const configOk = plan.configFields.every(\n    (field) => !field.required || !isConfigValueMissing(config[field.name]),\n  );\n  const secretsOk = plan.secretFields.every(\n    (field) => !field.required || Boolean(refs.get(field.name)),\n  );\n  return configOk && secretsOk;\n}\n\ninterface AuthPropDescriptor {\n  displayName?: string;\n  description?: string;\n  required?: boolean;\n  type?: string;\n  options?: { options?: { label?: string; value?: unknown }[] };\n}\n\ninterface PieceAuthDescriptor {\n  type?: string;\n  unsupported?: string;\n  displayName?: string;\n  description?: string;\n  required?: boolean;\n  // A record in a published listing; a named list from a loaded piece.\n  props?:\n    | Partial<Record<string, AuthPropDescriptor>>\n    | (AuthPropDescriptor & { name?: string })[];\n}\n\n// Names the connection's config and secretRefs use for an OAuth2 app.\nexport const OAUTH_CLIENT_ID = \"client_id\";\nexport const OAUTH_CLIENT_SECRET = \"client_secret\";\nexport const OAUTH_TOKEN = \"token\";\n\nfunction propEntries(\n  props: PieceAuthDescriptor[\"props\"],\n): [string, AuthPropDescriptor][] {\n  if (Array.isArray(props)) {\n    return props.flatMap((prop) => (prop.name ? [[prop.name, prop]] : []));\n  }\n  return Object.entries(props ?? {}).filter(\n    (entry): entry is [string, AuthPropDescriptor] => entry[1] !== undefined,\n  );\n}\n\nfunction toField(name: string, prop: AuthPropDescriptor): AuthField {\n  return {\n    name,\n    displayName: prop.displayName ?? name,\n    required: prop.required ?? false,\n    description: prop.description,\n    inputType: prop.type,\n    options: prop.options?.options\n      ?.filter((option) => option.value !== undefined)\n      .map((option) => ({\n        label: option.label ?? String(option.value as string),\n        value: option.value,\n      })),\n  };\n}\n\n// Every sign-in method a piece offers, in its order, one per auth type.\nexport function plansFromAuth(auth: unknown): AuthPlan[] {\n  const plans = (Array.isArray(auth) ? auth : [auth]).map(planFromAuth);\n  return plans.filter(\n    (plan, index) =>\n      plans.findIndex((other) => other.authType === plan.authType) === index,\n  );\n}\n\n// The method a connection signs in with: the one of its auth type when the\n// runtime can run it, otherwise the piece's default.\nexport function planForConnection(\n  auth: unknown,\n  authType: ConnectionAuthType,\n): AuthPlan {\n  return (\n    plansFromAuth(auth).find(\n      (plan) => plan.authType === authType && plan.supported,\n    ) ?? planFromAuth(auth)\n  );\n}\n\nexport function planFromAuth(auth: unknown): AuthPlan {\n  // Some pieces declare multiple auth methods; prefer one the runtime supports.\n  if (Array.isArray(auth)) {\n    const plans = auth.map(planFromAuth);\n    return (\n      plans.find((plan) => plan.supported && plan.authType !== \"NONE\") ??\n      plans.at(0) ??\n      planFromAuth(null)\n    );\n  }\n  const descriptor = (auth ?? null) as PieceAuthDescriptor | null;\n  const plan = planForDescriptor(descriptor);\n  // The runtime's own verdict, e.g. a CUSTOM_AUTH that needs token refresh.\n  return descriptor?.unsupported ? { ...plan, supported: false } : plan;\n}\n\nfunction planForDescriptor(descriptor: PieceAuthDescriptor | null): AuthPlan {\n  const base = {\n    displayName: descriptor?.displayName,\n    description: descriptor?.description,\n  };\n  switch (descriptor?.type) {\n    case \"SECRET_TEXT\":\n      return {\n        ...base,\n        authType: \"SECRET_TEXT\",\n        configFields: [],\n        secretFields: [\n          {\n            name: \"value\",\n            displayName: descriptor.displayName ?? \"Secret\",\n            required: descriptor.required ?? true,\n            description: descriptor.description,\n          },\n        ],\n        supported: true,\n      };\n    case \"BASIC_AUTH\": {\n      const props: Partial<Record<string, AuthPropDescriptor>> =\n        Object.fromEntries(propEntries(descriptor.props));\n      return {\n        ...base,\n        authType: \"BASIC_AUTH\",\n        configFields: [\n          toField(\"username\", props.username ?? { displayName: \"Username\" }),\n        ],\n        secretFields: [\n          toField(\"password\", props.password ?? { displayName: \"Password\" }),\n        ],\n        supported: true,\n      };\n    }\n    case \"CUSTOM_AUTH\": {\n      const entries = propEntries(descriptor.props);\n      return {\n        ...base,\n        authType: \"CUSTOM_AUTH\",\n        configFields: entries\n          .filter(([, prop]) => prop.type !== \"SECRET_TEXT\")\n          .map(([name, prop]) => toField(name, prop)),\n        secretFields: entries\n          .filter(([, prop]) => prop.type === \"SECRET_TEXT\")\n          .map(([name, prop]) => toField(name, prop)),\n        supported: true,\n      };\n    }\n    case \"OAUTH2\": {\n      const entries = propEntries(descriptor.props);\n      return {\n        ...base,\n        authType: \"OAUTH2\",\n        configFields: [\n          {\n            name: OAUTH_CLIENT_ID,\n            displayName: \"Client ID\",\n            required: true,\n            description: \"From the OAuth app you registered with the service.\",\n          },\n          ...entries\n            .filter(([, prop]) => prop.type !== \"SECRET_TEXT\")\n            .map(([name, prop]) => toField(name, prop)),\n        ],\n        secretFields: [\n          {\n            name: OAUTH_CLIENT_SECRET,\n            displayName: \"Client secret\",\n            required: true,\n          },\n          ...entries\n            .filter(([, prop]) => prop.type === \"SECRET_TEXT\")\n            .map(([name, prop]) => toField(name, prop)),\n        ],\n        supported: true,\n        oauth2: true,\n      };\n    }\n    case \"OIDC\":\n      return {\n        ...base,\n        authType: \"OIDC\",\n        configFields: [],\n        secretFields: [],\n        supported: false,\n      };\n    case undefined:\n    case \"NONE\":\n      return {\n        ...base,\n        authType: \"NONE\",\n        configFields: [],\n        secretFields: [],\n        supported: true,\n      };\n    default:\n      return {\n        ...base,\n        authType: UNKNOWN_AUTH,\n        declaredType: descriptor?.type,\n        configFields: [],\n        secretFields: [],\n        supported: false,\n      };\n  }\n}\n\n// \"@activepieces/piece-gotify\" -> \"@activepieces/piece-gotify#gotify\"\nexport function connectorIdForPiece(packageName: string): string {\n  const short =\n    packageName\n      .split(\"/\")\n      .pop()\n      ?.replace(/^piece-/, \"\") ?? packageName;\n  return `${packageName}#${short}`;\n}\n\n// \"<piece package>#<short name>\" -> the package; a bare name is its own.\nexport function packageFromConnectorId(connectorId: string): string {\n  const separator = connectorId.lastIndexOf(\"#\");\n  if (separator <= 0) return connectorId;\n  const spec = connectorId.slice(0, separator);\n  const at = spec.indexOf(\"@\", 1);\n  return at > 0 ? spec.slice(0, at) : spec;\n}\n"],"mappings":";AAeA,MAAa,eAAe;AAiB5B,SAAgB,qBAAqB,OAAyB;CAC5D,OAAO,UAAU,KAAA,KAAa,UAAU,QAAQ,UAAU;AAC5D;AAIA,SAAgB,eACd,MACA,QACA,MACS;CACT,MAAM,WAAW,KAAK,aAAa,OAChC,UAAU,CAAC,MAAM,YAAY,CAAC,qBAAqB,OAAO,MAAM,KAAK,CACxE;CACA,MAAM,YAAY,KAAK,aAAa,OACjC,UAAU,CAAC,MAAM,YAAY,QAAQ,KAAK,IAAI,MAAM,IAAI,CAAC,CAC5D;CACA,OAAO,YAAY;AACrB;AAuBA,MAAa,kBAAkB;AAC/B,MAAa,sBAAsB;AACnC,MAAa,cAAc;AAE3B,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