/** * GitHub user OAuth binding for Plugin Runtime (PAT + Device Flow). * Persists into DatabaseHost table owned by `@zhin.js/adapter-github`. */ import { GhClient, GITHUB_OAUTH_USERS_TABLE, type GithubEndpoint, } from '@zhin.js/adapter-github'; import { type DatabaseHostModel, } from 'zhin.js'; export { GITHUB_OAUTH_USERS_TABLE }; export interface GithubOauthRow { readonly id: number; readonly platform: string; readonly platform_uid: string; readonly github_login: string; readonly access_token: string; readonly created_at: number; } export function requireOauthModel(endpoint: GithubEndpoint): DatabaseHostModel | string { const host = endpoint.database; if (!host) return '数据库未就绪(缺少 DatabaseHost)'; if (!host.started) return '数据库尚未启动'; const model = host.models.get(GITHUB_OAUTH_USERS_TABLE); if (!model) return `表 ${GITHUB_OAUTH_USERS_TABLE} 未定义`; return model; } export function platformIdentity(input: unknown): { platform: string; uid: string } | string { if (!input || typeof input !== 'object') return '无法获取用户信息(缺少消息上下文)'; const value = input as Record; const meta = value.metadata && typeof value.metadata === 'object' ? value.metadata as Record : {}; const platform = typeof value.$adapter === 'string' ? value.$adapter : typeof value.adapter === 'string' ? value.adapter : typeof meta.adapter === 'string' ? meta.adapter : ''; let uid = ''; const sender = value.$sender; if (sender && typeof sender === 'object' && 'id' in sender) { uid = String((sender as { id?: unknown }).id ?? ''); } else if (typeof value.sender === 'string') { uid = value.sender; } else if (typeof meta.senderId === 'string') { uid = meta.senderId; } if (!platform || !uid) return '无法获取用户信息(需要 platform + sender)'; return { platform, uid }; } export async function findOauthBinding( model: DatabaseHostModel, platform: string, uid: string, ): Promise { const rows = await model.select().where({ platform, platform_uid: uid }); const row = rows[0]; if (!row) return null; return { id: Number(row.id), platform: String(row.platform), platform_uid: String(row.platform_uid), github_login: String(row.github_login), access_token: String(row.access_token), created_at: Number(row.created_at), }; } export async function bindWithPat( endpoint: GithubEndpoint, model: DatabaseHostModel, platform: string, uid: string, token: string, ): Promise { const existing = await findOauthBinding(model, platform, uid); if (existing) { return `已绑定 GitHub 账号: ${existing.github_login}\n请先执行 gh unbind`; } const userGh = new GhClient({ host: endpoint.host, token }); const auth = await userGh.verifyAuth(); if (!auth.ok) return `Token 验证失败: ${auth.message}`; await model.insert({ id: Date.now(), platform, platform_uid: uid, github_login: auth.user, access_token: token, created_at: Date.now(), }); return `GitHub 绑定成功\n用户: ${auth.user}`; } export async function startDeviceFlowBind( endpoint: GithubEndpoint, model: DatabaseHostModel, platform: string, uid: string, reply?: (text: string) => Promise, ): Promise { const existing = await findOauthBinding(model, platform, uid); if (existing) { return `已绑定 GitHub 账号: ${existing.github_login}\n请先执行 gh unbind`; } const clientId = endpoint.clientId; if (!clientId) { return [ 'Device Flow 不可用(App 未配置 client_id)', '', '改用 PAT:', '1. https://github.com/settings/tokens/new (repo, read:org)', '2. gh bind ', ].join('\n'); } const host = endpoint.host; let codeResp: Awaited>; try { codeResp = await GhClient.deviceFlowRequestCode(clientId, host); } catch (error) { return [ `Device Flow 失败: ${error instanceof Error ? error.message : String(error)}`, '', '也可: gh bind ', ].join('\n'); } void GhClient.deviceFlowPollToken({ clientId, deviceCode: codeResp.device_code, interval: codeResp.interval, expiresIn: codeResp.expires_in, host, }).then(async (tokenData) => { if (tokenData.status !== 'authorized') return; const userGh = new GhClient({ host, token: tokenData.access_token }); const auth = await userGh.verifyAuth(); // Do not store a token that GitHub would not vouch for. if (!auth.ok || !auth.user) return; const login = auth.user; const still = await findOauthBinding(model, platform, uid); if (still) return; await model.insert({ id: Date.now(), platform, platform_uid: uid, github_login: login, access_token: tokenData.access_token, created_at: Date.now(), }); if (reply) await reply(`GitHub 绑定成功\n用户: ${login}`); }).catch(() => { /* poll errors are non-fatal for the initial reply */ }); return [ '请在浏览器打开:', ` ${codeResp.verification_uri}`, '', `输入验证码: ${codeResp.user_code}`, '', `${Math.floor(codeResp.expires_in / 60)} 分钟内有效`, ].join('\n'); } export async function unbindOauth( model: DatabaseHostModel, platform: string, uid: string, ): Promise { const existing = await findOauthBinding(model, platform, uid); if (!existing) return '尚未绑定 GitHub 账号'; await model.delete().where({ id: existing.id }); return `已解绑: ${existing.github_login}`; } export async function whoamiOauth( endpoint: GithubEndpoint, model: DatabaseHostModel, platform: string, uid: string, ): Promise { const existing = await findOauthBinding(model, platform, uid); const appAuth = await endpoint.verifyAuth(); const appLine = appAuth.ok ? `Bot / App: ${appAuth.user}` : `Bot / App: ${appAuth.message}`; if (!existing) { return `${appLine}\n用户绑定: 无(gh bind 或 Device Flow)`; } const userGh = new GhClient({ host: endpoint.host, token: existing.access_token }); const auth = await userGh.verifyAuth(); if (auth.ok) { return [ appLine, `用户绑定: ${auth.user}`, `绑定时间: ${new Date(existing.created_at).toLocaleString('zh-CN')}`, ].join('\n'); } return [ appLine, `用户绑定: ${existing.github_login}(Token 已失效,请 gh unbind 后重绑)`, ].join('\n'); }