{"version":3,"file":"authApi.mjs","names":[],"sources":["../../../src/web/lib/authApi.ts"],"sourcesContent":["import { fetchWithStepUp } from '@agimon-ai/po-web-security/browser';\n\nimport {\n  AUTH_LOGINS_API_ROUTE,\n  AUTH_PROVIDERS_API_ROUTE,\n  type AuthMethodType,\n  type LoginFlowSnapshot,\n  type ProviderAuthSummary,\n} from '../../types/auth';\n\nexport type ProvidersResult = { providers: ProviderAuthSummary[] } | { error: string };\nexport type LoginFlowResult = { flow: LoginFlowSnapshot } | { error: string };\nexport type LogoutResult = { ok: true } | { error: string };\n\nexport interface GatewaySettings {\n  providers: { id: string; shared: boolean }[];\n  peers: { hostId: string; machineName?: string; status: 'ok' | 'unreachable' | 'no-https' }[];\n  /** The backend's caller locality decision, never inferred from the page URL. */\n  editable?: boolean;\n}\n\nconst GATEWAY_SETTINGS_ROUTE = '/api/plugins/gateway/settings';\n\n/** A missing optional package is not a provider-auth failure. */\nexport async function readProviderSharing(): Promise<{ settings: GatewaySettings } | { error: string }> {\n  try {\n    const response = await fetchWithStepUp(GATEWAY_SETTINGS_ROUTE);\n    const body: unknown = await response.json().catch(() => undefined);\n    if (response.status === 404 || (response.ok && !isRecord(body))) return { error: 'not-installed' };\n    if (response.ok && isRecord(body) && Array.isArray(body.providers) && Array.isArray(body.peers))\n      return { settings: body as unknown as GatewaySettings };\n    return {\n      error: isRecord(body) && typeof body.error === 'string' ? body.error : `The hub answered ${response.status}.`,\n    };\n  } catch {\n    return { error: UNREACHABLE };\n  }\n}\n\nexport function writeProviderSharing(providerId: string, shared: boolean): Promise<LogoutResult> {\n  return request(\n    `/api/plugins/gateway/providers/${encodeURIComponent(providerId)}`,\n    { method: 'PUT', headers: JSON_HEADERS, body: JSON.stringify({ shared }) },\n    () => ({ ok: true as const }),\n  );\n}\n\nconst UNREACHABLE = 'The cockpit hub is unreachable.';\nconst JSON_HEADERS = { 'Content-Type': 'application/json' };\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n  return typeof value === 'object' && value !== null && !Array.isArray(value);\n}\n\n/**\n * One shape for every provider-auth call: a network failure, a non-2xx\n * answer, and a 2xx body missing the expected field all come back as\n * `{ error }`, so the settings page has one branch to render.\n */\nasync function request<T>(\n  input: string,\n  init: RequestInit | undefined,\n  pick: (body: Record<string, unknown>) => T | undefined,\n): Promise<T | { error: string }> {\n  let response: Response;\n  try {\n    // Writing or clearing a provider credential redirects the machine's model\n    // traffic, so a remote caller answers a passkey challenge for it first.\n    response = await fetchWithStepUp(input, init);\n  } catch {\n    return { error: UNREACHABLE };\n  }\n  let body: unknown;\n  try {\n    body = await response.json();\n  } catch {\n    body = undefined;\n  }\n  if (response.ok && isRecord(body)) {\n    const picked = pick(body);\n    if (picked !== undefined) return picked;\n  }\n  const error = isRecord(body) && typeof body.error === 'string' ? body.error : `The hub answered ${response.status}.`;\n  return { error };\n}\n\nconst pickFlow = (body: Record<string, unknown>): { flow: LoginFlowSnapshot } | undefined =>\n  isRecord(body.flow) ? { flow: body.flow as unknown as LoginFlowSnapshot } : undefined;\n\n/** Every provider Pi knows on the hub's machine, with its auth state. */\nexport function listProviders(): Promise<ProvidersResult> {\n  return request(AUTH_PROVIDERS_API_ROUTE, undefined, (body) =>\n    Array.isArray(body.providers) ? { providers: body.providers as ProviderAuthSummary[] } : undefined,\n  );\n}\n\n/** Starts a sign-in on the hub; the returned flow is then polled with readLogin. */\nexport function startLogin(providerId: string, type: AuthMethodType): Promise<LoginFlowResult> {\n  return request(\n    AUTH_LOGINS_API_ROUTE,\n    { method: 'POST', headers: JSON_HEADERS, body: JSON.stringify({ providerId, type }) },\n    pickFlow,\n  );\n}\n\nexport function readLogin(flowId: string): Promise<LoginFlowResult> {\n  return request(`${AUTH_LOGINS_API_ROUTE}/${encodeURIComponent(flowId)}`, undefined, pickFlow);\n}\n\nexport function answerLogin(flowId: string, promptId: string, value: string): Promise<LoginFlowResult> {\n  return request(\n    `${AUTH_LOGINS_API_ROUTE}/${encodeURIComponent(flowId)}/answer`,\n    { method: 'POST', headers: JSON_HEADERS, body: JSON.stringify({ promptId, value }) },\n    pickFlow,\n  );\n}\n\nexport function cancelLogin(flowId: string): Promise<LoginFlowResult> {\n  return request(`${AUTH_LOGINS_API_ROUTE}/${encodeURIComponent(flowId)}`, { method: 'DELETE' }, pickFlow);\n}\n\n/** Removes the credential /login stored; environment variables are untouched. */\nexport function logoutProvider(providerId: string): Promise<LogoutResult> {\n  return request(`${AUTH_PROVIDERS_API_ROUTE}/${encodeURIComponent(providerId)}`, { method: 'DELETE' }, () => ({\n    ok: true as const,\n  }));\n}\n"],"mappings":";;;AAqBA,MAAM,yBAAyB;;AAG/B,eAAsB,sBAAkF;CACtG,IAAI;EACF,MAAM,WAAW,MAAM,gBAAgB,sBAAsB;EAC7D,MAAM,OAAgB,MAAM,SAAS,KAAK,CAAC,CAAC,YAAY,KAAA,CAAS;EACjE,IAAI,SAAS,WAAW,OAAQ,SAAS,MAAM,CAAC,SAAS,IAAI,GAAI,OAAO,EAAE,OAAO,gBAAgB;EACjG,IAAI,SAAS,MAAM,SAAS,IAAI,KAAK,MAAM,QAAQ,KAAK,SAAS,KAAK,MAAM,QAAQ,KAAK,KAAK,GAC5F,OAAO,EAAE,UAAU,KAAmC;EACxD,OAAO,EACL,OAAO,SAAS,IAAI,KAAK,OAAO,KAAK,UAAU,WAAW,KAAK,QAAQ,oBAAoB,SAAS,OAAO,GAC7G;CACF,QAAQ;EACN,OAAO,EAAE,OAAO,YAAY;CAC9B;AACF;AAEA,SAAgB,qBAAqB,YAAoB,QAAwC;CAC/F,OAAO,QACL,kCAAkC,mBAAmB,UAAU,KAC/D;EAAE,QAAQ;EAAO,SAAS;EAAc,MAAM,KAAK,UAAU,EAAE,OAAO,CAAC;CAAE,UAClE,EAAE,IAAI,KAAc,EAC7B;AACF;AAEA,MAAM,cAAc;AACpB,MAAM,eAAe,EAAE,gBAAgB,mBAAmB;AAE1D,SAAS,SAAS,OAAkD;CAClE,OAAO,OAAO,UAAU,YAAY,UAAU,QAAQ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