kimi-work: offer credential import in the sign-in menu

This commit is contained in:
antior
2026-11-20 10:25:21 +08:00
parent c0100a29a3
commit 0b3b89ab03
4 changed files with 47 additions and 5 deletions
@@ -8,7 +8,7 @@ Kimi Work 的对话密钥与会员令牌是两套凭据。原实现只把会员
## Decision
使用 Magpie 已有的 `auth.import` 文件导入协议,让用户选择已登录 Work 的 Kimi 桌面版 `daimon/config.json`。导入时复用本机文件的字段校验和官方 Work origin 限制,并用文件中的 Work 密钥请求 `/models` 验证后才返回成功账号。
使用 Magpie 已有的 `auth.import` 文件导入协议,让用户选择已登录 Work 的 Kimi 桌面版 `daimon/config.json`;同时在 `auth.methods` 中提供粘贴同一文件内容的第二种登录方案,使点击登录即可在本机读取和凭据导入之间选择。导入时复用本机文件的字段校验和官方 Work origin 限制,并用文件中的 Work 密钥请求 `/models` 验证后才返回成功账号。
导入账号在同一宿主账号项中显式保存 `workToken`、会员 access/refresh token、UID 和 `imported: true`。请求、模型发现和额度读取遇到这种账号时只使用已保存凭据,不调用 `readDesktop()`。原有本机及 reference-only 账号不自动迁移,仍按原路径读取桌面文件。
@@ -18,7 +18,7 @@ Kimi Work 的对话密钥与会员令牌是两套凭据。原实现只把会员
用户可以把可信机器上的 Work 凭据导入未安装 Kimi 的 Magpie,省去手工粘贴 JSON 的专用界面,并复用宿主的文件选择、代理、取消和账号保存流程。代价是 Work 密钥现在会按用户显式操作保存在宿主明文认证库中;文件仍须由某台已登录 Kimi 桌面版的机器生成,跨设备有效期和撤销策略仍由 Kimi 控制。
该入口是 Magpie 扩展,OpenCode 会忽略 `auth.import`,因此不宣称 OpenCode 也支持文件选择登录。
文件选择入口是 Magpie 扩展,OpenCode 会忽略 `auth.import`;粘贴凭据的登录方法使用标准 `auth.methods`,不依赖 Magpie 的文件选择界面。
## Alternatives considered
@@ -30,4 +30,4 @@ Kimi Work 的对话密钥与会员令牌是两套凭据。原实现只把会员
## Verification
合成 `config.json` 测试覆盖导入声明、合法文件验证、导入后无桌面读取的对话/模型/额度请求、无 refresh token 的文件、无效 JSON/字段/origin、失效 Work 密钥和重新导入提示。既有测试继续约束本机登录不复制 Work 密钥及 reference-only 行为。未使用真实账号或真实凭据测试跨设备登录。
合成 `config.json` 测试覆盖导入声明、登录菜单的粘贴方法、合法文件验证、导入后无桌面读取的对话/模型/额度请求、无 refresh token 的文件、无效 JSON/字段/origin、失效 Work 密钥和重新导入提示。既有测试继续约束本机登录不复制 Work 密钥及 reference-only 行为。未使用真实账号或真实凭据测试跨设备登录。
+1 -1
View File
@@ -9,7 +9,7 @@ magpie plugin login kimi-work
Open Kimi desktop and sign in to Work on the same computer first. Choose **使用本机 Kimi Work 登录** in the provider's sign-in menu. If the desktop provides a membership access/refresh pair, explicit sign-in imports it into the host's auth store and Magpie renews it directly with Kimi near expiry, including while the browser and desktop app are closed. Older desktops without a refresh token still sign in as reference-only accounts and read membership access from the desktop file. This package does not capture browser cookies or offer an API-key fallback.
Version **0.1.18** adds **Import accounts from a file…** in Magpie. Choose `daimon/config.json` from a trusted computer signed in to Kimi Work. Unlike local sign-in, this explicitly imports both the Work key and membership credentials, so the computer running Magpie does not need Kimi installed or the desktop data folder mounted. The file's `credentials.kimiCode`, `credentials.kimiWeb` and exact official Work origin are checked, then the Work key is verified with `/models` before the account is saved. Existing local/reference accounts are unchanged and are not migrated. This import hook is Magpie-only; OpenCode ignores it.
Version **0.1.18** adds two credential-import routes in Magpie. The provider's sign-in menu now offers **导入 Kimi Work 凭据(粘贴 config.json)** beside local sign-in; **Import accounts from a file…** in the accounts area chooses a file directly. Both take `daimon/config.json` from a trusted computer signed in to Kimi Work. Unlike local sign-in, they explicitly import both the Work key and membership credentials, so the computer running Magpie does not need Kimi installed or the desktop data folder mounted. The file's `credentials.kimiCode`, `credentials.kimiWeb` and exact official Work origin are checked, then the Work key is verified with `/models` before the account is saved. Existing local/reference accounts are unchanged and are not migrated. The file-import hook is Magpie-only; OpenCode ignores it, while the paste sign-in method uses the standard provider auth hooks.
The imported file contains sensitive Work and membership credentials, which are saved in Magpie's private plaintext `plugin-auth.json` (mode `0600`, not an encrypted vault). Import only into a trusted Magpie instance. Membership renewal continues as below, but it cannot renew the Work key: a Work 401 asks for a fresh `config.json` import. No desktop device ID or `kimi-code-key.json` is needed for an imported account.
+18
View File
@@ -25,6 +25,24 @@ test('magpie offers a guided config.json import in every window language',async(
expect(typeof p.auth.import).toBe('function');
expect(p.auth.importFrom).toContain('daimon/config.json');
for(const lang of ['zh','zh-TW','ja','de'])expect(p.auth.importFroms[lang]).toContain('daimon/config.json');
expect(p.auth.methods).toHaveLength(2);
expect(p.auth.methods[1]).toMatchObject({type:'oauth',label:'导入 Kimi Work 凭据(粘贴 config.json)'});
expect(p.auth.methods[1].prompts[0]).toMatchObject({type:'password',key:'config'});
expect(p.auth.methods[1].prompts[0].validate('')).toBeTruthy();
expect(p.auth.methods[1].prompts[0].validate(file())).toBeUndefined();
});
test('the sign-in menu also accepts pasted config.json content without reading the desktop',async()=>{
const {p,desktopReads}=fixture(async(url,init)=>{
expect(url).toBe(I.BASE+'/models');
expect(new Headers(init.headers).get('authorization')).toBe('Bearer '+workToken);
return Response.json(catalog);
});
const flow=await p.auth.methods[1].authorize({config:file()});
expect(flow).toMatchObject({url:'',method:'auto'});
const auth=await flow.callback();
expect(auth).toMatchObject({type:'success',imported:true,workToken,access:webToken});
expect(desktopReads()).toBe(0);
});
test('a selected config.json is validated and explicitly imports both Work and membership credentials',async()=>{
+25 -1
View File
@@ -94,6 +94,22 @@ const IMPORT_FROM = {
de: 'Wähle daimon/config.json eines bei Work angemeldeten Kimi-Desktops. Danach ist Kimi hier nicht nötig. Die sensiblen Zugangsdaten werden in magpies Auth-Speicher abgelegt; nach Ablauf des Work-Schlüssels erneut importieren.',
},
};
const IMPORT_METHOD = {
label:'导入 Kimi Work 凭据(粘贴 config.json)',
labels:{
en:'Import Kimi Work credentials (paste config.json)',
'zh-TW':'匯入 Kimi Work 憑證(貼上 config.json)',
ja:'Kimi Work 認証情報をインポート(config.json を貼り付け)',
de:'Kimi-Work-Zugangsdaten importieren (config.json einfügen)',
},
message:'粘贴 daimon/config.json 的完整 JSON 内容',
messages:{
en:'Paste the complete JSON contents of daimon/config.json',
'zh-TW':'貼上 daimon/config.json 的完整 JSON 內容',
ja:'daimon/config.json の完全な JSON 内容を貼り付けてください',
de:'Füge den vollständigen JSON-Inhalt von daimon/config.json ein',
},
};
// spentQuota is a 403 body saying the account's allowance is used up, not
// that the token or the model is refused (#465).
const spentQuota = text => /resource_exhausted|usage limit|billing cycle|insufficient balance|quota/i.test(String(text??''));
@@ -692,7 +708,15 @@ function createPlugin({readSession=readDesktop,fetchImpl=globalThis.fetch,platfo
// pair when available; older desktops still get a reference account.
return signedIn(s);
},
})}],
})},{
type:'oauth',label:IMPORT_METHOD.label,labels:IMPORT_METHOD.labels,
prompts:[{type:'password',key:'config',message:IMPORT_METHOD.message,messages:IMPORT_METHOD.messages,
placeholder:'{"credentials":{…}}',validate:v=>v&&v.trim()?undefined:'请粘贴 daimon/config.json 的内容'}],
authorize:async inputs=>({
url:'',method:'auto',instructions:'验证导入的 Kimi Work 凭据。',
callback:async(_,{signal}={})=>importFile(inputs?.config,signal),
}),
}],
refreshLead:REFRESH_LEAD,
async refresh(auth) {
if(!hasPair(auth))return;