Files
knowledge-governance/web/src/dingtalkSession.ts
T
姚章浩 173b44abca feat(settings): refresh DingTalk scan sessions from the data-source page
DingTalk source scanning depends on a shared browser session whose capture
file had to be replaced by hand whenever it expired (every scan 401s).

- services/dingtalk-login: admin-triggered Playwright sidecar that opens the
  space page in a persistent headless Chromium, publishes the QR through
  session polling, verifies the first authenticated XHR against the box API,
  then writes the capture in place (same inode keeps the worker's ro bind
  mount current); tokens never leave the sidecar
- api: POST/GET/DELETE /api/v1/dingtalk-session authenticated relay; QR is a
  login artifact, so the whole prefix is admin-only in classifyRole; OpenAPI
  contract and proxy/503/hostile-ID tests included
- web: 系统设置 → 数据源配置 钉钉卡片新增扫码刷新会话(仅管理员可见),
  QR 弹窗 + 1.5s 轮询 + 成功/过期/失败状态
- compose: sidecar service owns the ONLY rw mount of secrets/; worker keeps ro
2026-09-30 10:54:07 +08:00

57 lines
2.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { newOperationID } from './operationKey'
// Client for the admin-only DingTalk scan-code session (系统设置 → 数据源配置).
// The API proxies to the dingtalk-login sidecar; this module never sees the
// token itself — only the base64 QR image and a terminal state.
export type DingtalkSessionState = 'starting' | 'qr' | 'scanned' | 'reusing' | 'success' | 'expired' | 'failed' | 'closed'
export interface DingtalkSessionView {
id: string
state: DingtalkSessionState
created_at: number
expires_at: number
qr_png?: string
error?: string
}
const sessionStates = new Set<string>(['starting', 'qr', 'scanned', 'reusing', 'success', 'expired', 'failed', 'closed'])
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === 'object' && value !== null && !Array.isArray(value)
}
export async function startDingtalkSession(space?: string): Promise<DingtalkSessionView> {
const response = await fetch('/api/v1/dingtalk-session', {
method: 'POST',
headers: { Accept: 'application/json', 'Content-Type': 'application/json', 'X-Governance-Action': 'dingtalk-session', 'Idempotency-Key': newOperationID() },
body: JSON.stringify(space ? { space } : {}),
})
if (!response.ok) throw new Error(`发起钉钉扫码会话失败(HTTP ${response.status})`)
return parseSession(await response.json())
}
export async function fetchDingtalkSession(id: string, signal?: AbortSignal): Promise<DingtalkSessionView> {
const response = await fetch(`/api/v1/dingtalk-session/${encodeURIComponent(id)}`, { headers: { Accept: 'application/json' }, signal })
if (!response.ok) throw new Error(`读取钉钉扫码会话失败(HTTP ${response.status})`)
return parseSession(await response.json())
}
export async function cancelDingtalkSession(id: string): Promise<void> {
await fetch(`/api/v1/dingtalk-session/${encodeURIComponent(id)}`, { method: 'DELETE', headers: { Accept: 'application/json' } }).catch(() => undefined)
}
function parseSession(value: unknown): DingtalkSessionView {
if (!isRecord(value) || typeof value.id !== 'string' || typeof value.state !== 'string' || !sessionStates.has(value.state)) {
throw new Error('invalid dingtalk session response')
}
return {
id: value.id,
state: value.state as DingtalkSessionState,
created_at: typeof value.created_at === 'number' ? value.created_at : 0,
expires_at: typeof value.expires_at === 'number' ? value.expires_at : 0,
qr_png: typeof value.qr_png === 'string' ? value.qr_png : undefined,
error: typeof value.error === 'string' ? value.error : undefined,
}
}