import ky, { type KyInstance } from "ky"; import { AuthenticationError } from "../errors.js"; export const DEFAULT_SSO_BASE_URL = "https://sso.hosecloud.com"; export interface SsoClientOptions { baseUrl?: string; timeout?: number; } export function createSsoClient(options: SsoClientOptions = {}): KyInstance { const baseUrl = options.baseUrl || DEFAULT_SSO_BASE_URL; return ky.create({ prefix: baseUrl, timeout: options.timeout ?? 15000, headers: { accept: "application/json, text/plain, */*", "user-agent": "hose-use/assist-platform-client", referer: `${baseUrl}/`, }, }); } export interface QrCodeStatusResponse { code: number; message: string; data?: { token?: string; ticket?: string; redirectUrl?: string; [key: string]: unknown; } | string; } /** * 轮询 HoseCloudSSO 钉钉扫码登录确认状态 */ export async function pollSsoQrCodeStatus( client: KyInstance, options: { sessionId?: string; cookie?: string; signal?: AbortSignal; } = {} ): Promise { const headers: Record = {}; if (options.cookie) { headers.cookie = options.cookie; } else if (options.sessionId) { headers.cookie = `SSO_SESSION_ID=${options.sessionId}`; } try { return await client .get("api/v1/user/login/qrcode/status", { searchParams: { _t: Date.now() }, headers, signal: options.signal, }) .json(); } catch (error) { if (error instanceof Error && error.name === "AbortError") { throw error; } throw new AuthenticationError( `SSO 状态轮询异常: ${error instanceof Error ? error.message : String(error)}` ); } }