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
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
import { useEffect, useRef, useState } from 'react'
|
||||
|
||||
import { cancelDingtalkSession, fetchDingtalkSession, startDingtalkSession } from './dingtalkSession'
|
||||
import type { DingtalkSessionView } from './dingtalkSession'
|
||||
|
||||
// Admin-only card control: "扫码刷新会话" opens a login session against the
|
||||
// dingtalk-login sidecar, shows its QR, and reports the capture result. The
|
||||
// token never reaches this component — only state and the QR image.
|
||||
|
||||
const finished = new Set(['success', 'expired', 'failed', 'closed'])
|
||||
|
||||
export function DingtalkSessionControl() {
|
||||
const [session, setSession] = useState<DingtalkSessionView | null>(null)
|
||||
const [failure, setFailure] = useState<string | null>(null)
|
||||
const [starting, setStarting] = useState(false)
|
||||
const idRef = useRef<string | null>(null)
|
||||
|
||||
useEffect(() => () => { if (idRef.current) void cancelDingtalkSession(idRef.current) }, [])
|
||||
|
||||
useEffect(() => {
|
||||
if (!session || finished.has(session.state)) return
|
||||
const timer = setInterval(() => {
|
||||
fetchDingtalkSession(session.id).then(setSession).catch(() => undefined)
|
||||
}, 1500)
|
||||
return () => clearInterval(timer)
|
||||
}, [session?.id, session?.state])
|
||||
|
||||
const start = async () => {
|
||||
setStarting(true); setFailure(null)
|
||||
try {
|
||||
const fresh = await startDingtalkSession()
|
||||
idRef.current = fresh.id
|
||||
setSession(fresh)
|
||||
} catch (cause) {
|
||||
setFailure(cause instanceof Error ? cause.message : '发起扫码会话失败')
|
||||
} finally {
|
||||
setStarting(false)
|
||||
}
|
||||
}
|
||||
|
||||
const close = () => {
|
||||
if (idRef.current) void cancelDingtalkSession(idRef.current)
|
||||
idRef.current = null
|
||||
setSession(null)
|
||||
}
|
||||
|
||||
const stateLabel: Record<string, string> = {
|
||||
starting: '正在打开登录页…', qr: '请用钉钉 App 扫码登录', scanned: '已扫码,正在获取会话…',
|
||||
reusing: '检测到有效浏览器会话,正在验证…', success: '✅ 钉钉扫码会话已刷新,扫描任务可继续使用',
|
||||
expired: '二维码已过期,请重新发起', failed: '会话验证失败,请重试或联系管理员', closed: '会话已关闭',
|
||||
}
|
||||
|
||||
return <div className="dingtalk-session-control">
|
||||
<button className="secondary" disabled={starting || (session ? !finished.has(session.state) : false)} onClick={() => void start()}>
|
||||
{session && !finished.has(session.state) ? '扫码进行中…' : '扫码刷新会话'}
|
||||
</button>
|
||||
{failure ? <p className="error-message" role="alert">{failure}</p> : null}
|
||||
{session ? <div className="modal-backdrop" role="dialog" aria-modal="true" aria-label="钉钉扫码登录">
|
||||
<div className="modal-card dingtalk-qr-card">
|
||||
<h3>刷新钉钉扫码会话</h3>
|
||||
<p className="dingtalk-qr-hint">{stateLabel[session.state] ?? session.state}</p>
|
||||
{session.qr_png && session.state === 'qr' ? <img className="dingtalk-qr-image" src={`data:image/png;base64,${session.qr_png}`} alt="钉钉登录二维码" /> : null}
|
||||
{session.error ? <p className="error-message" role="alert">{session.error}</p> : null}
|
||||
<div className="modal-actions">
|
||||
{finished.has(session.state)
|
||||
? <button className="secondary" onClick={close}>关闭</button>
|
||||
: <button className="danger" onClick={close}>取消扫码</button>}
|
||||
</div>
|
||||
</div>
|
||||
</div> : null}
|
||||
</div>
|
||||
}
|
||||
Reference in New Issue
Block a user