dsh-qwenwork-connect 0.3.0 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -0
- package/bin/login.mjs +26 -6
- package/lib/client.js +81 -4
- package/lib/index.js +7 -1
- package/lib/login-device-flow.js +20 -2
- package/lib/login-route.js +25 -3
- package/lib/logout-route.js +90 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -59,8 +59,20 @@ npx dsh-qwenwork-connect login
|
|
|
59
59
|
```
|
|
60
60
|
POST /plugins/dsh-qwenwork-connect/login/start
|
|
61
61
|
GET /plugins/dsh-qwenwork-connect/login/status
|
|
62
|
+
POST /plugins/dsh-qwenwork-connect/logout
|
|
62
63
|
```
|
|
63
64
|
|
|
65
|
+
**退出登录**在同一张卡片的账号行右侧点「退出登录」,确认后删除
|
|
66
|
+
`~/.dsh/qwenwork-desktop.json`,状态立即回到未登录,可随时重新登录。命令行
|
|
67
|
+
等价操作是:
|
|
68
|
+
|
|
69
|
+
```bash
|
|
70
|
+
npx dsh-qwenwork-connect logout
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
退出只删凭据文件,保留 `~/.dsh/qwenwork-machine-id`——设备标识不变,重新登录
|
|
74
|
+
不会换设备。三条路由都只接受本机环回请求。
|
|
75
|
+
|
|
64
76
|
## 三个坑
|
|
65
77
|
|
|
66
78
|
**machine_id 必须一致。** 签名头里的 `Cosy-MachineId` 要等于登录时用的那个,
|
package/bin/login.mjs
CHANGED
|
@@ -1,28 +1,48 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
/**
|
|
3
|
-
* `dsh-qwenwork-connect login`
|
|
3
|
+
* `dsh-qwenwork-connect login` —— 在终端里跑一次 device flow。
|
|
4
|
+
* `dsh-qwenwork-connect logout` —— 删除本机凭据,退出登录。
|
|
4
5
|
*
|
|
5
|
-
*
|
|
6
|
+
* 登录会打印授权链接,等你在浏览器里选完账号和团队,把 token 写到
|
|
6
7
|
* `~/.dsh/qwenwork-desktop.json`,插件的下一次请求就会用它。
|
|
8
|
+
* 退出只删除凭据文件,保留 `~/.dsh/qwenwork-machine-id`。
|
|
7
9
|
*/
|
|
8
10
|
|
|
9
|
-
import
|
|
11
|
+
import fs from 'node:fs';
|
|
12
|
+
import { startDeviceLogin, resolveCredentialPath } from '../lib/login-device-flow.js';
|
|
10
13
|
|
|
11
14
|
const args = process.argv.slice(2);
|
|
12
15
|
const command = args[0] ?? 'login';
|
|
13
16
|
|
|
14
17
|
if (command === '--help' || command === 'help') {
|
|
15
|
-
console.log('用法: dsh-qwenwork-connect login [--credential <path>]');
|
|
18
|
+
console.log('用法: dsh-qwenwork-connect <login|logout> [--credential <path>]');
|
|
16
19
|
process.exit(0);
|
|
17
20
|
}
|
|
18
21
|
|
|
19
|
-
if (command !== 'login') {
|
|
22
|
+
if (command !== 'login' && command !== 'logout') {
|
|
20
23
|
console.error('未知命令: ' + command);
|
|
21
24
|
process.exit(1);
|
|
22
25
|
}
|
|
23
26
|
|
|
24
27
|
const flagIndex = args.indexOf('--credential');
|
|
25
|
-
const credentialPath =
|
|
28
|
+
const credentialPath = resolveCredentialPath(
|
|
29
|
+
flagIndex >= 0 ? args[flagIndex + 1] : undefined,
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
if (command === 'logout') {
|
|
33
|
+
try {
|
|
34
|
+
fs.unlinkSync(credentialPath);
|
|
35
|
+
console.log('已退出登录,删除 ' + credentialPath);
|
|
36
|
+
} catch (error) {
|
|
37
|
+
if (/** @type {any} */ (error)?.code === 'ENOENT') {
|
|
38
|
+
console.log('当前未登录(凭据文件不存在)');
|
|
39
|
+
} else {
|
|
40
|
+
console.error('退出登录失败: ' + error.message);
|
|
41
|
+
process.exit(1);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
process.exit(0);
|
|
45
|
+
}
|
|
26
46
|
|
|
27
47
|
console.log('打开下面的链接,用已登录千问办公的浏览器继续:\n');
|
|
28
48
|
const result = await startDeviceLogin({
|
package/lib/client.js
CHANGED
|
@@ -32,6 +32,7 @@ window.__ModuleLoader__.load({
|
|
|
32
32
|
/** 设置卡片上的设备码登录(路由在 lib/login-route.js)。 */
|
|
33
33
|
const LOGIN_START_PATH = '/plugins/dsh-qwenwork-connect/login/start';
|
|
34
34
|
const LOGIN_STATUS_PATH = '/plugins/dsh-qwenwork-connect/login/status';
|
|
35
|
+
const LOGOUT_PATH = '/plugins/dsh-qwenwork-connect/logout';
|
|
35
36
|
const LOGIN_POLL_MS = 2000;
|
|
36
37
|
const LOGIN_WAIT_MS = 180000;
|
|
37
38
|
//#endregion
|
|
@@ -187,6 +188,14 @@ window.__ModuleLoader__.load({
|
|
|
187
188
|
fontSize: 13,
|
|
188
189
|
cursor: 'pointer',
|
|
189
190
|
};
|
|
191
|
+
/** 账号行右侧的退出按钮:用错误色标出「这是破坏性操作」,但仍需确认。 */
|
|
192
|
+
const logoutButtonStyle = {
|
|
193
|
+
...refreshButtonStyle,
|
|
194
|
+
marginLeft: 'auto',
|
|
195
|
+
color: 'var(--dsw-alias-state-error-primary, #d92d20)',
|
|
196
|
+
fontSize: 12,
|
|
197
|
+
padding: '3px 10px',
|
|
198
|
+
};
|
|
190
199
|
const actionRowStyle = {
|
|
191
200
|
display: 'flex',
|
|
192
201
|
alignItems: 'center',
|
|
@@ -423,6 +432,11 @@ window.__ModuleLoader__.load({
|
|
|
423
432
|
if (state === null) {
|
|
424
433
|
return react_jsx_runtime.jsx('div', { style: errorStyle, children: t('loading') });
|
|
425
434
|
}
|
|
435
|
+
// 退出登录后 getAccountContext 会抛 CREDENTIALS_MISSING——这是预期状态,
|
|
436
|
+
// 不是故障:顶部已有「尚未登录 + 登录按钮」,不该再甩一段红色报错。
|
|
437
|
+
if (state.status === 'signed-out' && state.error?.code === 'CREDENTIALS_MISSING') {
|
|
438
|
+
return null;
|
|
439
|
+
}
|
|
426
440
|
if (state.error !== undefined && state.status !== 'signed-in') {
|
|
427
441
|
const err = state.error;
|
|
428
442
|
const text = [err.message, err.recovery].filter(Boolean).join('\n');
|
|
@@ -443,6 +457,18 @@ window.__ModuleLoader__.load({
|
|
|
443
457
|
react_jsx_runtime.jsx('span', {
|
|
444
458
|
children: t('signedInAs', { name: state.nickname ?? state.account ?? '—' }),
|
|
445
459
|
}),
|
|
460
|
+
typeof props.onLogout === 'function'
|
|
461
|
+
? react_jsx_runtime.jsx('button', {
|
|
462
|
+
type: 'button',
|
|
463
|
+
disabled: props.logoutBusy === true,
|
|
464
|
+
style:
|
|
465
|
+
props.logoutBusy === true
|
|
466
|
+
? { ...logoutButtonStyle, opacity: 0.6, cursor: 'default' }
|
|
467
|
+
: logoutButtonStyle,
|
|
468
|
+
onClick: props.onLogout,
|
|
469
|
+
children: props.logoutBusy === true ? t('loggingOut') : t('logout'),
|
|
470
|
+
})
|
|
471
|
+
: null,
|
|
446
472
|
],
|
|
447
473
|
},
|
|
448
474
|
'who',
|
|
@@ -635,6 +661,8 @@ window.__ModuleLoader__.load({
|
|
|
635
661
|
/** 设备码登录的本地阶段:null | { phase, url?, error? }。 */
|
|
636
662
|
const [login, setLogin] = react.useState(null);
|
|
637
663
|
const [loginBusy, setLoginBusy] = react.useState(false);
|
|
664
|
+
const [logoutBusy, setLogoutBusy] = react.useState(false);
|
|
665
|
+
const [logoutError, setLogoutError] = react.useState(null);
|
|
638
666
|
|
|
639
667
|
react.useEffect(() => {
|
|
640
668
|
let cancelled = false;
|
|
@@ -742,6 +770,34 @@ window.__ModuleLoader__.load({
|
|
|
742
770
|
}
|
|
743
771
|
};
|
|
744
772
|
|
|
773
|
+
/** 退出登录:确认 → 删除本机凭据 → 立即重拉状态(回到未登录)。 */
|
|
774
|
+
const logout = async () => {
|
|
775
|
+
if (typeof window !== 'undefined' && typeof window.confirm === 'function') {
|
|
776
|
+
if (!window.confirm(t('logoutConfirm'))) return;
|
|
777
|
+
}
|
|
778
|
+
setLogoutBusy(true);
|
|
779
|
+
setLogoutError(null);
|
|
780
|
+
try {
|
|
781
|
+
const response = await fetch(LOGOUT_PATH, {
|
|
782
|
+
method: 'POST',
|
|
783
|
+
headers: { accept: 'application/json' },
|
|
784
|
+
credentials: 'same-origin',
|
|
785
|
+
});
|
|
786
|
+
const body = await response.json().catch(() => null);
|
|
787
|
+
if (!response.ok) {
|
|
788
|
+
throw new Error(body?.error ?? `${t('requestFailed')} (HTTP ${response.status})`);
|
|
789
|
+
}
|
|
790
|
+
setLogin(null);
|
|
791
|
+
setNonce((value) => value + 1);
|
|
792
|
+
} catch (error) {
|
|
793
|
+
setLogoutError(
|
|
794
|
+
`${t('logoutFailed')}: ${error instanceof Error ? error.message : String(error)}`,
|
|
795
|
+
);
|
|
796
|
+
} finally {
|
|
797
|
+
setLogoutBusy(false);
|
|
798
|
+
}
|
|
799
|
+
};
|
|
800
|
+
|
|
745
801
|
const signedIn = state !== null && state.status === 'signed-in';
|
|
746
802
|
const subtitle = signedIn
|
|
747
803
|
? state.remaining === undefined
|
|
@@ -785,7 +841,7 @@ window.__ModuleLoader__.load({
|
|
|
785
841
|
children: [
|
|
786
842
|
react_jsx_runtime.jsx('div', {
|
|
787
843
|
style: { fontSize: 13, lineHeight: '20px', color: 'var(--dsw-alias-label-secondary)' },
|
|
788
|
-
children: signedIn ? t('signedIn') : t('
|
|
844
|
+
children: signedIn ? t('signedIn') : t('notSignedIn'),
|
|
789
845
|
}),
|
|
790
846
|
react_jsx_runtime.jsxs('div', {
|
|
791
847
|
style: actionRowStyle,
|
|
@@ -850,7 +906,18 @@ window.__ModuleLoader__.load({
|
|
|
850
906
|
: null,
|
|
851
907
|
],
|
|
852
908
|
}),
|
|
853
|
-
|
|
909
|
+
logoutError === null
|
|
910
|
+
? null
|
|
911
|
+
: react_jsx_runtime.jsx('div', {
|
|
912
|
+
style: loginPanelStyle,
|
|
913
|
+
children: logoutError,
|
|
914
|
+
}),
|
|
915
|
+
react_jsx_runtime.jsx(StatusBody, {
|
|
916
|
+
state,
|
|
917
|
+
t,
|
|
918
|
+
onLogout: logout,
|
|
919
|
+
logoutBusy,
|
|
920
|
+
}),
|
|
854
921
|
],
|
|
855
922
|
})
|
|
856
923
|
: null,
|
|
@@ -1001,7 +1068,8 @@ window.__ModuleLoader__.load({
|
|
|
1001
1068
|
//#region 文案
|
|
1002
1069
|
const zh = {
|
|
1003
1070
|
title: '千问办公',
|
|
1004
|
-
subtitle: '
|
|
1071
|
+
subtitle: '设备码登录,直连千问办公网关',
|
|
1072
|
+
notSignedIn: '未登录',
|
|
1005
1073
|
signedIn: '已登录',
|
|
1006
1074
|
signedInAs: '已登录:{name}',
|
|
1007
1075
|
loading: '正在读取状态…',
|
|
@@ -1013,6 +1081,10 @@ window.__ModuleLoader__.load({
|
|
|
1013
1081
|
loginWaiting: '已打开授权页,等待你在浏览器里完成授权…',
|
|
1014
1082
|
loginTimeout: '登录超时,请重试',
|
|
1015
1083
|
loginFailed: '登录失败',
|
|
1084
|
+
logout: '退出登录',
|
|
1085
|
+
loggingOut: '正在退出…',
|
|
1086
|
+
logoutConfirm: '确定要退出登录吗?本机凭据会被删除,可随时重新登录。',
|
|
1087
|
+
logoutFailed: '退出登录失败',
|
|
1016
1088
|
accountHeading: '账号',
|
|
1017
1089
|
quotaHeading: '剩余积分',
|
|
1018
1090
|
nickname: '账号',
|
|
@@ -1059,7 +1131,8 @@ window.__ModuleLoader__.load({
|
|
|
1059
1131
|
};
|
|
1060
1132
|
const en = {
|
|
1061
1133
|
title: 'QwenWork',
|
|
1062
|
-
subtitle: "
|
|
1134
|
+
subtitle: "Device-code sign-in, direct to the QwenWork gateway",
|
|
1135
|
+
notSignedIn: 'Not signed in',
|
|
1063
1136
|
signedIn: 'Signed in',
|
|
1064
1137
|
signedInAs: 'Signed in: {name}',
|
|
1065
1138
|
loading: 'Reading status…',
|
|
@@ -1071,6 +1144,10 @@ window.__ModuleLoader__.load({
|
|
|
1071
1144
|
loginWaiting: 'Authorization page opened; waiting for you to finish in the browser…',
|
|
1072
1145
|
loginTimeout: 'Sign-in timed out. Please try again.',
|
|
1073
1146
|
loginFailed: 'Sign-in failed',
|
|
1147
|
+
logout: 'Sign out',
|
|
1148
|
+
loggingOut: 'Signing out…',
|
|
1149
|
+
logoutConfirm: 'Sign out of this device? The local credentials will be removed; you can sign in again anytime.',
|
|
1150
|
+
logoutFailed: 'Sign-out failed',
|
|
1074
1151
|
accountHeading: 'Account',
|
|
1075
1152
|
quotaHeading: 'Remaining credits',
|
|
1076
1153
|
nickname: 'Account',
|
package/lib/index.js
CHANGED
|
@@ -34,6 +34,7 @@ import { CATALOG_TTL_MS, createCatalogStore } from './catalog.js';
|
|
|
34
34
|
import { relaxTlsForVpc, resolveGatewayEndpoint } from './endpoint.js';
|
|
35
35
|
import { registerQwenWorkStatusRoute } from './web-status.js';
|
|
36
36
|
import { registerDeviceLoginRoute } from './login-route.js';
|
|
37
|
+
import { registerLogoutRoute } from './logout-route.js';
|
|
37
38
|
import { perfSummary } from './perf.js';
|
|
38
39
|
import { installStallGuard } from './stall-guard.js';
|
|
39
40
|
|
|
@@ -433,12 +434,17 @@ export function apply(ctx, config) {
|
|
|
433
434
|
// ---- 设置卡片数据源 ------------------------------------------------
|
|
434
435
|
// 无论 provider 是否注册成功,卡片都要能显示——它是阶段 B 的主要可见成果。
|
|
435
436
|
ctx.inject(['webServer'], (webCtx) => {
|
|
436
|
-
registerDeviceLoginRoute(webCtx, {
|
|
437
|
+
const loginRoutes = registerDeviceLoginRoute(webCtx, {
|
|
437
438
|
logger: ctx.logger,
|
|
438
439
|
onLogin: () => {
|
|
439
440
|
void refreshCatalog(true);
|
|
440
441
|
},
|
|
441
442
|
});
|
|
443
|
+
registerLogoutRoute(webCtx, {
|
|
444
|
+
logger: ctx.logger,
|
|
445
|
+
// 清掉登录页缓存,避免退出后 /login/status 仍回上一次的账号摘要。
|
|
446
|
+
onLogout: () => loginRoutes?.reset?.(),
|
|
447
|
+
});
|
|
442
448
|
registerQwenWorkStatusRoute(webCtx, {
|
|
443
449
|
getAccountContext,
|
|
444
450
|
tokenAvailable: isWired,
|
package/lib/login-device-flow.js
CHANGED
|
@@ -27,6 +27,23 @@ export const DEVICE_BASE = 'https://gateway.qwenwork.cn';
|
|
|
27
27
|
export const DEFAULT_CREDENTIAL_PATH = path.join(os.homedir(), '.dsh', 'qwenwork-desktop.json');
|
|
28
28
|
export const DEFAULT_MACHINE_ID_PATH = path.join(os.homedir(), '.dsh', 'qwenwork-machine-id');
|
|
29
29
|
|
|
30
|
+
/**
|
|
31
|
+
* 凭据文件路径的唯一解析口:显式参数 > `QWENWORK_CREDENTIAL_FILE` > 默认路径。
|
|
32
|
+
*
|
|
33
|
+
* `credentials.js` 的读取侧早就支持这个环境变量;写入侧(登录 / CLI)与
|
|
34
|
+
* 删除侧(退出登录)必须走同一套优先级,否则会出现「登录写 A、读取读 B」
|
|
35
|
+
* 或「退出删 A、实际用的是 B」这种自相矛盾的状态。
|
|
36
|
+
*
|
|
37
|
+
* @param {string | undefined} explicit
|
|
38
|
+
* @returns {string}
|
|
39
|
+
*/
|
|
40
|
+
export function resolveCredentialPath(explicit) {
|
|
41
|
+
if (typeof explicit === 'string' && explicit !== '') return explicit;
|
|
42
|
+
const fromEnv = process.env.QWENWORK_CREDENTIAL_FILE;
|
|
43
|
+
if (typeof fromEnv === 'string' && fromEnv.trim() !== '') return fromEnv.trim();
|
|
44
|
+
return DEFAULT_CREDENTIAL_PATH;
|
|
45
|
+
}
|
|
46
|
+
|
|
30
47
|
export function createPkce() {
|
|
31
48
|
const verifier = crypto.randomBytes(48).toString('base64url');
|
|
32
49
|
const challenge = crypto.createHash('sha256').update(verifier).digest('base64url');
|
|
@@ -134,7 +151,8 @@ function sleep(ms) {
|
|
|
134
151
|
* 字段名沿用上游 `normalizeCredentials` 的契约,换实现时不用改契约。
|
|
135
152
|
*/
|
|
136
153
|
export function writeCredential(payload, options = {}) {
|
|
137
|
-
const { machineId
|
|
154
|
+
const { machineId } = options;
|
|
155
|
+
const credentialPath = resolveCredentialPath(options.credentialPath);
|
|
138
156
|
const claims = decodeJwt(payload.token);
|
|
139
157
|
const user = {
|
|
140
158
|
id: payload.user_id ?? claims.user_id ?? claims.sub ?? null,
|
|
@@ -162,7 +180,7 @@ export async function startDeviceLogin(options = {}) {
|
|
|
162
180
|
const {
|
|
163
181
|
clientId = DEVICE_CLIENT_ID,
|
|
164
182
|
base = DEVICE_BASE,
|
|
165
|
-
credentialPath
|
|
183
|
+
credentialPath,
|
|
166
184
|
machineIdPath = DEFAULT_MACHINE_ID_PATH,
|
|
167
185
|
intervalMs = 2000,
|
|
168
186
|
timeoutMs = 5 * 60 * 1000,
|
package/lib/login-route.js
CHANGED
|
@@ -13,9 +13,9 @@ import {
|
|
|
13
13
|
buildAuthUrl,
|
|
14
14
|
createPkce,
|
|
15
15
|
pollDeviceToken,
|
|
16
|
+
resolveCredentialPath,
|
|
16
17
|
resolveMachineId,
|
|
17
18
|
writeCredential,
|
|
18
|
-
DEFAULT_CREDENTIAL_PATH,
|
|
19
19
|
} from './login-device-flow.js';
|
|
20
20
|
|
|
21
21
|
export const LOGIN_START_PATH = '/plugins/dsh-qwenwork-connect/login/start';
|
|
@@ -36,8 +36,13 @@ function json(res, status, body) {
|
|
|
36
36
|
* @param {{ logger?: any, credentialPath?: string, onLogin?: () => void }} [deps]
|
|
37
37
|
*/
|
|
38
38
|
export function registerDeviceLoginRoute(ctx, deps = {}) {
|
|
39
|
-
const credentialPath = deps.credentialPath
|
|
39
|
+
const credentialPath = resolveCredentialPath(deps.credentialPath);
|
|
40
40
|
const state = { phase: 'idle', url: null, error: null, account: null, startedAt: null };
|
|
41
|
+
/**
|
|
42
|
+
* 登录轮询的代际号:退出登录时 +1,让尚在途中的 device flow 结果作废。
|
|
43
|
+
* 只改 `state` 不够——旧轮询拿到 token 后仍会写盘,把刚退出的账号又登回去。
|
|
44
|
+
*/
|
|
45
|
+
let epoch = 0;
|
|
41
46
|
|
|
42
47
|
const handle = (req, res) => {
|
|
43
48
|
if (!loopbackRequest(req)) {
|
|
@@ -70,8 +75,13 @@ export function registerDeviceLoginRoute(ctx, deps = {}) {
|
|
|
70
75
|
state.account = null;
|
|
71
76
|
state.startedAt = Date.now();
|
|
72
77
|
|
|
78
|
+
const loginEpoch = epoch;
|
|
73
79
|
void pollDeviceToken({ nonce, verifier })
|
|
74
80
|
.then((payload) => {
|
|
81
|
+
if (loginEpoch !== epoch) {
|
|
82
|
+
// 等待期间用户已退出登录:丢弃这次授权结果,不写回凭据。
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
75
85
|
const written = writeCredential(payload, { machineId, credentialPath });
|
|
76
86
|
state.phase = 'ready';
|
|
77
87
|
state.account = {
|
|
@@ -88,6 +98,7 @@ export function registerDeviceLoginRoute(ctx, deps = {}) {
|
|
|
88
98
|
}
|
|
89
99
|
})
|
|
90
100
|
.catch((error) => {
|
|
101
|
+
if (loginEpoch !== epoch) return;
|
|
91
102
|
state.phase = 'failed';
|
|
92
103
|
state.error = String(error?.message ?? error).slice(0, 300);
|
|
93
104
|
deps.logger?.warn?.('dsh-qwenwork-connect: device login failed', state.error);
|
|
@@ -116,13 +127,24 @@ export function registerDeviceLoginRoute(ctx, deps = {}) {
|
|
|
116
127
|
};
|
|
117
128
|
};
|
|
118
129
|
|
|
130
|
+
/** 退出登录时清空登录页状态,并让在途轮询作废。 */
|
|
131
|
+
const reset = () => {
|
|
132
|
+
epoch += 1;
|
|
133
|
+
state.phase = 'idle';
|
|
134
|
+
state.url = null;
|
|
135
|
+
state.error = null;
|
|
136
|
+
state.account = null;
|
|
137
|
+
state.startedAt = null;
|
|
138
|
+
};
|
|
139
|
+
|
|
119
140
|
if (typeof ctx.effect === 'function') {
|
|
120
141
|
ctx.effect(() => mount(), 'dsh-qwenwork-connect: device login routes');
|
|
121
|
-
return;
|
|
142
|
+
return { reset };
|
|
122
143
|
}
|
|
123
144
|
try {
|
|
124
145
|
mount();
|
|
125
146
|
} catch (error) {
|
|
126
147
|
void error;
|
|
127
148
|
}
|
|
149
|
+
return { reset };
|
|
128
150
|
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 退出登录:设置卡片上点「退出登录」走这里。
|
|
3
|
+
*
|
|
4
|
+
* 只做一件事——删掉本机凭据文件,让下一次请求重新回到「未登录」。
|
|
5
|
+
* 不吊销上游 token(千问办公没有公开的吊销接口),但 token 离开本机后
|
|
6
|
+
* 已经无法再被本插件使用;设备标识 `~/.dsh/qwenwork-machine-id` 刻意保留,
|
|
7
|
+
* 这样重新登录不会换设备。
|
|
8
|
+
*
|
|
9
|
+
* 安全边界与登录 / 状态路由一致:只接受环回请求;错误文本不回显凭据内容。
|
|
10
|
+
*
|
|
11
|
+
* @module dsh-qwenwork-connect/logout-route
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import fs from 'node:fs';
|
|
15
|
+
import { loopbackRequest } from './loopback.js';
|
|
16
|
+
import { resolveCredentialPath } from './login-device-flow.js';
|
|
17
|
+
|
|
18
|
+
export const LOGOUT_PATH = '/plugins/dsh-qwenwork-connect/logout';
|
|
19
|
+
|
|
20
|
+
function json(res, status, body) {
|
|
21
|
+
const payload = JSON.stringify(body);
|
|
22
|
+
res.writeHead(status, {
|
|
23
|
+
'Content-Type': 'application/json; charset=utf-8',
|
|
24
|
+
'Content-Length': Buffer.byteLength(payload),
|
|
25
|
+
'Cache-Control': 'no-store',
|
|
26
|
+
});
|
|
27
|
+
res.end(payload);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* @param {any} ctx 已注入 webServer 的上下文
|
|
32
|
+
* @param {{ logger?: any, credentialPath?: string, onLogout?: () => void }} [deps]
|
|
33
|
+
*/
|
|
34
|
+
export function registerLogoutRoute(ctx, deps = {}) {
|
|
35
|
+
const credentialPath = resolveCredentialPath(deps.credentialPath);
|
|
36
|
+
|
|
37
|
+
const handle = (req, res) => {
|
|
38
|
+
if (!loopbackRequest(req)) {
|
|
39
|
+
json(res, 403, { error: 'loopback only' });
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
if (req.method !== 'POST') {
|
|
43
|
+
json(res, 405, { error: 'POST only' });
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
try {
|
|
48
|
+
let removed = false;
|
|
49
|
+
try {
|
|
50
|
+
fs.unlinkSync(credentialPath);
|
|
51
|
+
removed = true;
|
|
52
|
+
} catch (error) {
|
|
53
|
+
// 已经退出过 / 从来没登录过:幂等成功,不把「文件不存在」当失败。
|
|
54
|
+
if (/** @type {any} */ (error)?.code !== 'ENOENT') throw error;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
try {
|
|
58
|
+
deps.onLogout?.();
|
|
59
|
+
} catch {
|
|
60
|
+
// 内存态清理失败不应把「凭据已删除」判成退出失败。
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
deps.logger?.info?.(`dsh-qwenwork-connect: 已退出登录${removed ? '' : '(凭据文件原本不存在)'}`);
|
|
64
|
+
json(res, 200, { ok: true, removed });
|
|
65
|
+
} catch (error) {
|
|
66
|
+
const message = String(/** @type {any} */ (error)?.message ?? error).slice(0, 300);
|
|
67
|
+
deps.logger?.warn?.('dsh-qwenwork-connect: 退出登录失败', message);
|
|
68
|
+
json(res, 500, { error: message });
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
const mount = () => {
|
|
73
|
+
const dispose = ctx.webServer.register({
|
|
74
|
+
kind: 'exact',
|
|
75
|
+
path: LOGOUT_PATH,
|
|
76
|
+
handler: handle,
|
|
77
|
+
});
|
|
78
|
+
return dispose;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
if (typeof ctx.effect === 'function') {
|
|
82
|
+
ctx.effect(() => mount(), 'dsh-qwenwork-connect: device logout route');
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
try {
|
|
86
|
+
mount();
|
|
87
|
+
} catch (error) {
|
|
88
|
+
void error;
|
|
89
|
+
}
|
|
90
|
+
}
|