|
| 1 | +import { maskToken, type AuthStore, type Identity, type Settings } from "bailian-cli-core"; |
| 2 | +import { runConsoleLogin, resolveConsoleOrigin } from "./login-console.ts"; |
| 3 | + |
| 4 | +/** Read-only auth snapshot the config UI account widget renders. bl stores no |
| 5 | + * user profile (name/avatar), so this exposes only which credential domains |
| 6 | + * resolve, the console region/site, and a masked token. */ |
| 7 | +export interface AuthUiStatus { |
| 8 | + authenticated: boolean; |
| 9 | + methods: { apiKey: boolean; console: boolean; openapi: boolean }; |
| 10 | + primary: "console" | "apiKey" | "openapi" | null; |
| 11 | + region?: string; |
| 12 | + site?: "domestic" | "international"; |
| 13 | + masked?: string; |
| 14 | +} |
| 15 | + |
| 16 | +/** |
| 17 | + * The auth capability surface the config UI is allowed to use. All `authStore` |
| 18 | + * access is kept inside this module (commands/auth/**), which the lint boundary |
| 19 | + * permits; commands/config/** consumes only this opaque bridge and never |
| 20 | + * touches `authStore` directly. |
| 21 | + */ |
| 22 | +export interface AuthUiBridge { |
| 23 | + status(): AuthUiStatus; |
| 24 | + /** Start browser-based console login (fire-and-forget; UI polls status). */ |
| 25 | + startConsoleLogin(): void; |
| 26 | + /** Clear all stored credentials. Returns whether anything changed. */ |
| 27 | + logout(): Promise<boolean>; |
| 28 | +} |
| 29 | + |
| 30 | +/** Build the bridge from a command context (identity/settings/authStore). */ |
| 31 | +export function makeAuthUiBridge(ctx: { |
| 32 | + identity: Identity; |
| 33 | + settings: Settings; |
| 34 | + authStore: AuthStore; |
| 35 | +}): AuthUiBridge { |
| 36 | + const { identity, settings, authStore } = ctx; |
| 37 | + return { |
| 38 | + status() { |
| 39 | + const a = authStore.describe(); |
| 40 | + const methods = { apiKey: !!a.apiKey, console: !!a.console, openapi: !!a.openapi }; |
| 41 | + let masked: string | undefined; |
| 42 | + if (a.console) masked = maskToken(a.console.token); |
| 43 | + else if (a.apiKey) masked = maskToken(a.apiKey.token); |
| 44 | + else if (a.openapi) masked = maskToken(a.openapi.accessKeyId); |
| 45 | + const primary = a.console ? "console" : a.apiKey ? "apiKey" : a.openapi ? "openapi" : null; |
| 46 | + return { |
| 47 | + authenticated: methods.apiKey || methods.console || methods.openapi, |
| 48 | + methods, |
| 49 | + primary, |
| 50 | + region: a.console?.region, |
| 51 | + site: a.console?.site, |
| 52 | + masked, |
| 53 | + }; |
| 54 | + }, |
| 55 | + startConsoleLogin() { |
| 56 | + const origin = resolveConsoleOrigin(authStore.describe().console?.site); |
| 57 | + // Mirror the CLI (`bl auth login --console`): request an api_key from the |
| 58 | + // console only when one isn't already stored, so a first console login in |
| 59 | + // the config UI also provisions the model api_key (not just access_token). |
| 60 | + const hasApiKey = !!authStore.stored().apiKey; |
| 61 | + // runConsoleLogin opens the browser and runs its own callback server |
| 62 | + // (up to 15 min). We don't await it — the config UI polls the status |
| 63 | + // endpoint to detect completion. Errors are logged, not surfaced. |
| 64 | + void runConsoleLogin( |
| 65 | + origin, |
| 66 | + { identity, settings, authStore }, |
| 67 | + { |
| 68 | + needApiKey: !hasApiKey, |
| 69 | + }, |
| 70 | + ).catch((err: unknown) => { |
| 71 | + const msg = err instanceof Error ? err.message : String(err); |
| 72 | + process.stderr.write(`console login failed: ${msg}\n`); |
| 73 | + }); |
| 74 | + }, |
| 75 | + logout() { |
| 76 | + return authStore.logout("all"); |
| 77 | + }, |
| 78 | + }; |
| 79 | +} |
0 commit comments