mirror of
https://github.com/jackwener/OpenCLI.git
synced 2026-09-14 18:25:42 +08:00
9139baaef8
Adds the no-navigation `quickCheck` to each adapter's registerSiteAuthCommands config so `opencli auth status` (PR #1879) resolves login state in quick mode (CDP getCookies, no per-site goto) instead of reporting `unknown`. - quickCheck reuses each adapter's existing poll cookie gate (has<Site>Cookie), which is a logged-in-only, no-nav check returning boolean. - Deliberately NOT wired (stay `unknown` in quick mode, available via --full): - gitee/hf/deepseek/quark/reuters/zsxq: no reliable logged-in cookie; they detect via no-nav fetch / localStorage / Bearer which need the site origin. - doubao/ke/coupang/manus: session cookie is present for anonymous users, so a cookie quickCheck would false-positive — `unknown` is more honest. Live: auth status --site resolves logged_in/not_logged_in for cookie-gate sites (v2ex/github/zhihu/claude/taobao/twitter/bilibili) in quick mode; excluded sites report unknown. Audits new=0/new=0; suite 5054 passed.
54 lines
2.2 KiB
JavaScript
54 lines
2.2 KiB
JavaScript
import { AuthRequiredError, CommandExecutionError } from '@jackwener/opencli/errors';
|
|
import { registerSiteAuthCommands } from '../_shared/site-auth.js';
|
|
|
|
async function hasUpworkSessionCookie(page) {
|
|
const cookies = await page.getCookies({ url: 'https://www.upwork.com' });
|
|
const names = new Set(cookies.map(c => c.name));
|
|
return names.has('master_access_token') || names.has('XSRF-TOKEN') && names.has('user_uid');
|
|
}
|
|
|
|
async function verifyUpworkIdentity(page) {
|
|
if (!await hasUpworkSessionCookie(page)) {
|
|
throw new AuthRequiredError('upwork.com', 'Upwork session cookies missing');
|
|
}
|
|
await page.goto('https://www.upwork.com/nx/find-work/');
|
|
await page.wait(3);
|
|
const probe = await page.evaluate(`
|
|
(() => {
|
|
if (/\\/(ab|account-security\\/login|signup)\\//.test(location.pathname)) {
|
|
return { kind: 'auth', detail: 'Upwork redirected to login flow' };
|
|
}
|
|
const nuxt = (typeof window !== 'undefined' && window.__NUXT__) ? window.__NUXT__ : null;
|
|
const state = nuxt && (nuxt.state || (nuxt.data && nuxt.data[0]));
|
|
const user = state && (state.user || (state.auth && state.auth.user));
|
|
const profile = user && (user.profile || user);
|
|
if (!profile || !profile.id) {
|
|
return { kind: 'auth', detail: 'Upwork __NUXT__ has no profile id — anonymous' };
|
|
}
|
|
return {
|
|
ok: true,
|
|
user_id: String(profile.id || profile.uid || ''),
|
|
ciphertext: String(profile.ciphertext || ''),
|
|
};
|
|
})()
|
|
`);
|
|
if (probe?.kind === 'auth') throw new AuthRequiredError('upwork.com', probe.detail);
|
|
if (!probe?.ok) throw new CommandExecutionError(`Unexpected Upwork probe: ${JSON.stringify(probe)}`);
|
|
return { user_id: probe.user_id, ciphertext: probe.ciphertext };
|
|
}
|
|
|
|
registerSiteAuthCommands({
|
|
site: 'upwork',
|
|
domain: 'upwork.com',
|
|
loginUrl: 'https://www.upwork.com/ab/account-security/login',
|
|
columns: ['user_id', 'ciphertext'],
|
|
quickCheck: hasUpworkSessionCookie,
|
|
verify: verifyUpworkIdentity,
|
|
poll: async (page) => {
|
|
if (!await hasUpworkSessionCookie(page)) {
|
|
throw new AuthRequiredError('upwork.com', 'Waiting for Upwork session cookies');
|
|
}
|
|
return verifyUpworkIdentity(page);
|
|
},
|
|
});
|