feat(auth): 实现三种登录覆盖矩阵与企业配置自动生成 (账密/短信/浏览器同步)

This commit is contained in:
gitea committed 2026-08-27 10:43:27 +08:00
1 parent 2085a97e34
commit 255c7aee8d
7 files changed
+643 -6

No files matched your search

+113
View File
@@ -0,0 +1,113 @@
import {
fullPasswordLogin,
fullSmsLogin,
fullBrowserSyncLogin,
sendSmsVerificationCode,
listUserCorporations,
} from "../src/core/auth/login.js";
import readline from "readline";
function prompt(question: string): Promise<string> {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
return new Promise((resolve) => {
rl.question(question, (answer) => {
rl.close();
resolve(answer.trim());
});
});
}
async function main() {
const args = process.argv.slice(2);
const isSync = args.includes("--sync");
const isSms = args.includes("--sms");
const isPwd = args.includes("--pwd") || (!isSync && !isSms);
console.log("==========================================");
console.log(" 合思 (ekb-mcp) 登录授权助手 ");
console.log("==========================================\n");
try {
if (isSync) {
console.log("正在尝试从当前运行的浏览器会话中同步登录态...");
const result = await fullBrowserSyncLogin({ saveAll: true });
console.log("\n[SUCCESS] 成功从浏览器同步企业凭证!");
console.log(`默认企业: ${result.selectedCorp.name} (${result.selectedCorp.id})`);
console.log(`共保存企业配置: ${result.allCorps.length} 家`);
return;
}
if (isSms) {
const phone = await prompt("请输入手机号: ");
if (!phone) {
console.error("手机号不能为空");
process.exit(1);
}
console.log(`\n正在向 ${phone} 请求发送短信验证码...`);
let codeId: string | undefined;
try {
const sendRes = await sendSmsVerificationCode({ phone });
codeId = sendRes.codeId;
console.log("短信验证码已发送!");
} catch (e) {
console.warn("直接发送提示:", e instanceof Error ? e.message : String(e));
console.log("如果网页端触发了人机验证,请在浏览器中获取验证码后在下方输入。");
}
const code = await prompt("请输入收到的 6 位短信验证码: ");
if (!code) {
console.error("验证码不能为空");
process.exit(1);
}
console.log("\n正在验证并换取企业访问令牌...");
const result = await fullSmsLogin({
phone,
code,
codeId,
saveAll: true,
});
console.log("\n[SUCCESS] 短信验证码登录成功!");
console.log(`默认企业: ${result.selectedCorp.name} (${result.selectedCorp.id})`);
console.log(`共保存企业配置: ${result.allCorps.length} 家`);
return;
}
if (isPwd) {
const phone = args.find((a) => a.startsWith("--phone="))?.split("=")[1] ||
(await prompt("请输入手机号: "));
const password = args.find((a) => a.startsWith("--password="))?.split("=")[1] ||
(await prompt("请输入密码: "));
if (!phone || !password) {
console.error("手机号和密码不能为空");
process.exit(1);
}
console.log("\n正在进行全自动 RSA 账密登录...");
const result = await fullPasswordLogin({
phone,
password,
saveAll: true,
});
console.log("\n[SUCCESS] 账密登录成功!");
console.log(`默认企业: ${result.selectedCorp.name} (${result.selectedCorp.id})`);
console.log(`共保存企业配置: ${result.allCorps.length} 家`);
result.allCorps.forEach((c, idx) => {
console.log(` ${idx + 1}. [${c.id}] ${c.name}`);
});
return;
}
} catch (error) {
console.error("\n[ERROR] 登录失败:", error instanceof Error ? error.message : String(error));
process.exit(1);
}
}
main();