WorkBuddy、CodeBuddy 桌面端增强助手:签到猫猫旅行成长计划查询、多账号独立备份、点切即用;免打扰模式让 AI 无人值守跑长任务;跨账号会话迁移、异常中断自动续接;暂存/快捷提示词;毛玻璃主题与更多实用功能。
fix(login): harden seamless OAuth flow
[email protected]c5f2a8d7ad30
diff --git a/scripts/daemon.js b/scripts/daemon.jsindex 7000bfc..10ccf41 100644--- a/scripts/daemon.js+++ b/scripts/daemon.js@@ -365,6 +365,7 @@ function sha256File(file) {const WB_API_ENDPOINT = 'https://www.codebuddy.cn';const WB_API_PREFIX = '/v2/plugin';const OAUTH_TIMEOUT_SECONDS = 600;+const OAUTH_RESULT_RETENTION_SECONDS = 300;const oauthStates = new Map(); // loginId -> { state, expiresAt, done, result, error }// 时间戳归一化:秒/毫秒/字符串 → 毫秒;无效返回 null@@ -418,12 +419,23 @@ function httpJson(url, method, body, headers) {// ({account, auth, accounts, allAccounts},与 lib.js switchTo 写回的格式一致)function buildSeamlessAuthFile(tokenData, accData) {const now = Date.now();- const domain = String(tokenData.domain || '');- let expiresAt = normTs(tokenData.expiresAt);- if (expiresAt == null && tokenData.expiresIn) expiresAt = now + Number(tokenData.expiresIn) * 1000;- let refreshExpiresAt = normTs(tokenData.refreshExpiresAt);- if (refreshExpiresAt == null && tokenData.refreshExpiresIn) {- refreshExpiresAt = now + Number(tokenData.refreshExpiresIn) * 1000;+ const rawToken = tokenData && typeof tokenData === 'object' ? tokenData : {};+ const domain = String(rawToken.domain || '');+ let expiresAt = normTs(rawToken.expiresAt != null ? rawToken.expiresAt : rawToken.expires_at);+ if (expiresAt == null) {+ const expiresIn = Number(rawToken.expiresIn != null ? rawToken.expiresIn : rawToken.expires_in);+ if (Number.isFinite(expiresIn) && expiresIn > 0) expiresAt = now + expiresIn * 1000;+ }+ let refreshExpiresAt = normTs(+ rawToken.refreshExpiresAt != null ? rawToken.refreshExpiresAt : rawToken.refresh_expires_at+ );+ if (refreshExpiresAt == null) {+ const refreshExpiresIn = Number(+ rawToken.refreshExpiresIn != null ? rawToken.refreshExpiresIn : rawToken.refresh_expires_in+ );+ if (Number.isFinite(refreshExpiresIn) && refreshExpiresIn > 0) {+ refreshExpiresAt = now + refreshExpiresIn * 1000;+ }}const accountObj = Object.assign({}, accData && typeof accData === 'object' ? accData : {}, {@@ -436,16 +448,18 @@ function buildSeamlessAuthFile(tokenData, accData) {pluginEnabled: true,});- const authObj = {- accessToken: String(tokenData.accessToken || ''),- refreshToken: String(tokenData.refreshToken || ''),- tokenType: String(tokenData.tokenType || 'Bearer'),+ // 保留官方响应中的额外字段(例如 idToken/sessionState),只覆盖标准字段。+ // WorkBuddy 后续可能依赖这些字段,不能把 OAuth 响应压缩成固定白名单。+ const authObj = Object.assign({}, rawToken, {+ accessToken: String(rawToken.accessToken || rawToken.access_token || ''),+ refreshToken: String(rawToken.refreshToken || rawToken.refresh_token || ''),+ tokenType: String(rawToken.tokenType || rawToken.token_type || 'Bearer'),domain,lastRefreshTime: now,- scope: tokenData.scope || 'openid profile offline_access email',- notBeforePolicy: 0,- sessionState: '',- };+ scope: rawToken.scope || 'openid profile offline_access email',+ notBeforePolicy: rawToken.notBeforePolicy != null ? rawToken.notBeforePolicy : 0,+ sessionState: rawToken.sessionState || '',+ });if (expiresAt != null) {authObj.expiresAt = expiresAt;authObj.expiresIn = Math.max(0, Math.round((expiresAt - now) / 1000));@@ -469,6 +483,11 @@ function buildSeamlessAuthFile(tokenData, accData) {return { account: accountObj, auth: authObj, accounts: all, allAccounts: all };}+function scheduleOAuthStateCleanup(loginId) {+ const timer = setTimeout(() => oauthStates.delete(loginId), OAUTH_RESULT_RETENTION_SECONDS * 1000);+ if (timer.unref) timer.unref();+}+// 把无感登录采集到的账号写入 accounts/<uid>.info 备份(不触碰当前登录文件)function saveSeamlessAccount(tokenData, accData) {const uid = String(accData.uid || '');@@ -498,6 +517,7 @@ async function oauthPollOnce(loginId) {if (Date.now() > info.expiresAt) {info.done = true;info.error = '登录超时,请重新发起';+ scheduleOAuthStateCleanup(loginId);return { done: true, error: info.error };}const tokenResp = await httpJson(@@ -526,6 +546,7 @@ async function oauthPollOnce(loginId) {} catch (e) {info.error = e.message;}+ scheduleOAuthStateCleanup(loginId);return { done: true, result: info.result, error: info.error };}@@ -3075,6 +3096,11 @@ function handleApi(req, res) {result: null,error: null,});+ const cleanupTimer = setTimeout(+ () => oauthStates.delete(loginId),+ (OAUTH_TIMEOUT_SECONDS + OAUTH_RESULT_RETENTION_SECONDS) * 1000+ );+ if (cleanupTimer.unref) cleanupTimer.unref();log(`[oauth] 发起无感登录 loginId=${loginId}`);return json(res, 200, { ok: true, loginId, verificationUri: authUrl, expiresIn: OAUTH_TIMEOUT_SECONDS });} catch (e) {diff --git a/scripts/inject.js b/scripts/inject.jsindex 80887de..17ea1c8 100644--- a/scripts/inject.js+++ b/scripts/inject.js@@ -2491,6 +2491,13 @@ if (typeof window !== 'undefined' && typeof document !== 'undefined') {// 方法一:假退出(保留两击确认,防误触)var logoutOpt = mask.querySelector('[data-way="logout"]');var logoutArmed = false;+ var logoutArmedTimer = null;+ var resetLogoutArm = function () {+ logoutArmed = false;+ logoutOpt.classList.remove('armed');+ logoutOpt.querySelector('.wbs-login-opt-title').textContent = '方法一:假退出当前账号';+ logoutOpt.querySelector('.wbs-login-opt-desc').textContent = '退出 WorkBuddy 回到登录页扫码,当前账号已备份,随时可切回';+ };logoutOpt.addEventListener('click', function () {if (logoutOpt.disabled) return;if (!logoutArmed) {@@ -2498,8 +2505,10 @@ if (typeof window !== 'undefined' && typeof document !== 'undefined') {logoutOpt.classList.add('armed');logoutOpt.querySelector('.wbs-login-opt-title').textContent = '再点一次,确认假退出';logoutOpt.querySelector('.wbs-login-opt-desc').textContent = 'WorkBuddy 将退出并重新打开到登录页';+ logoutArmedTimer = setBuildTimeout(resetLogoutArm, 4000);return;}+ if (logoutArmedTimer) clearTimeout(logoutArmedTimer);logoutOpt.disabled = true;api('/api/logout', { method: 'POST' }).then(function () {@@ -2508,11 +2517,8 @@ if (typeof window !== 'undefined' && typeof document !== 'undefined') {}).catch(function (e) {toast('退出失败: ' + (e.message || e), true, root);- logoutArmed = false;+ resetLogoutArm();logoutOpt.disabled = false;- logoutOpt.classList.remove('armed');- logoutOpt.querySelector('.wbs-login-opt-title').textContent = '方法一:假退出当前账号';- logoutOpt.querySelector('.wbs-login-opt-desc').textContent = '退出 WorkBuddy 回到登录页扫码,当前账号已备份,随时可切回';});});@@ -2568,7 +2574,8 @@ if (typeof window !== 'undefined' && typeof document !== 'undefined') {.catch(function (e) {if (cancelled) return;var errEl = statusEl();- if (errEl) errEl.textContent = '登录失败: ' + (e.message || e);+ if (errEl) errEl.textContent = '网络暂时失败,正在重试…';+ pollTimer = setTimeout(poll, 1500);});};pollTimer = setTimeout(poll, 1500);