refactor(auth,config): split 2260-line login.ts into focused modules and normalize CorpConfig fields

This commit is contained in:
gitea committed 2026-09-30 00:04:28 +08:00
1 parent 2d744d283b
commit 2b220bc5d9
7 files changed
+1786 -1661

No files matched your search

+3 -3
View File
@@ -1,7 +1,7 @@
import { chmodSync, copyFileSync, existsSync, mkdirSync, readFileSync, readdirSync, rmSync, statSync, writeFileSync } from "node:fs";
import { homedir } from "node:os";
import { dirname, join } from "node:path";
import { CorpConfig, CorpConfigSchema, GlobalConfig, GlobalConfigSchema } from "./types.js";
import { CorpConfig, CorpConfigSchema, GlobalConfig, GlobalConfigSchema, normalizeCorpConfig } from "./types.js";
const CONFIG_ENV = "HOSE_ENTERPRISES_DIR";
const USER_DIR_ENV = "HOSE_USER_ENTERPRISES_DIR";
@@ -194,7 +194,7 @@ export function loadCorpConfigs(overrideDirs?: string[]): CorpConfig[] {
try {
const content = readFileSync(join(dir, file), "utf-8");
const parsed = JSON.parse(content);
const corp = CorpConfigSchema.parse(parsed);
const corp = normalizeCorpConfig(CorpConfigSchema.parse(parsed));
// 相同企业 ID 以先出现的为准(工作区优先于全局,全局优先于历史)
if (!byId.has(corp.id)) {
byId.set(corp.id, corp);
@@ -238,7 +238,7 @@ export function saveCorpConfig(config: CorpConfig, dir?: string): string {
// ignore malformed existing file
}
}
const validated = CorpConfigSchema.parse(config);
const validated = normalizeCorpConfig(CorpConfigSchema.parse(config));
const filePath = join(targetDir, `${validated.id}.json`);
writeSecureFile(filePath, JSON.stringify(validated, null, 2));
return filePath;
+12 -10
View File
@@ -1,4 +1,10 @@
import { CorpConfig, GlobalConfig } from "./types.js";
import {
CorpConfig,
getCorpExpiresAt,
getCorpPhysicalId,
getCorpStaffId,
getCorpStaffName,
} from "./types.js";
import { saveGlobalConfig } from "./loader.js";
export interface CorpSummary {
@@ -35,11 +41,7 @@ export function resolveCorpConfig(
let corp = corps.find((item) => item.id === selectedCorpId);
// 2. 其次按企业物理 ID 匹配 (如 uzU7pcWfScck00)
if (!corp) {
corp = corps.find(
(item) =>
item.corpId === selectedCorpId ||
item.credentials.corpId === selectedCorpId
);
corp = corps.find((item) => getCorpPhysicalId(item) === selectedCorpId);
}
if (corp) return corp;
throw new Error(`未找到企业或身份标识为 [${selectedCorpId}] 的企业配置`);
@@ -160,14 +162,14 @@ export class CorpContext {
return {
id: corp.id,
name: corp.name,
corpId: corp.corpId || corp.credentials.corpId,
staffId: corp.credentials.staffId,
staffName: corp.staffName || corp.session?.staffName || undefined,
corpId: getCorpPhysicalId(corp),
staffId: getCorpStaffId(corp),
staffName: getCorpStaffName(corp),
source: corp.source || "manual",
accountId: corp.accountId,
baseUrl: corp.baseUrl,
current: corp.id === this.current?.id,
expiresAt: corp.auth?.expiresAt || (corp.session?.expTime ?? undefined),
expiresAt: getCorpExpiresAt(corp),
persistent: hasOpenApiKeys || hasAccountBinding,
};
});
+64
View File
@@ -66,6 +66,69 @@ export type CorpCredentials = z.infer<typeof CorpCredentialsSchema>;
export type CorpAuthConfig = z.infer<typeof CorpAuthConfigSchema>;
export type CorpConfig = z.infer<typeof CorpConfigSchema>;
/**
* 获取企业真实物理 corpId(统一收敛 corp.corpId / corp.credentials.corpId / corp.id)
*/
export function getCorpPhysicalId(corp: CorpConfig): string {
return corp.corpId || corp.credentials.corpId || corp.id;
}
/**
* 获取企业内当前员工工号 staffId(统一收敛 corp.credentials.staffId / corp.staffId)
*/
export function getCorpStaffId(corp: CorpConfig): string {
return corp.credentials.staffId || corp.staffId || "";
}
/**
* 获取企业内当前员工姓名(统一收敛 corp.staffName / corp.session?.staffName)
*/
export function getCorpStaffName(corp: CorpConfig): string | undefined {
return corp.staffName || corp.session?.staffName || undefined;
}
/**
* 获取凭证过期时间戳 ms(统一收敛 corp.auth?.expiresAt / corp.session?.expTime)
*/
export function getCorpExpiresAt(corp: CorpConfig): number | undefined {
return corp.auth?.expiresAt || (corp.session?.expTime ?? undefined);
}
/**
* 归一化 CorpConfig 冗余字段,确保顶层与嵌套凭证/会话字段保持一致
*/
export function normalizeCorpConfig(corp: CorpConfig): CorpConfig {
const physicalCorpId = getCorpPhysicalId(corp);
const staffId = getCorpStaffId(corp);
const staffName = getCorpStaffName(corp);
const expiresAt = getCorpExpiresAt(corp);
const appKey = corp.credentials?.appKey || corp.auth?.appKey;
const appSecurity = corp.credentials?.appSecurity || corp.auth?.appSecurity;
return {
...corp,
corpId: physicalCorpId,
...(staffId ? { staffId } : {}),
...(staffName ? { staffName } : {}),
credentials: {
...corp.credentials,
corpId: corp.credentials.corpId || physicalCorpId,
staffId,
...(appKey ? { appKey } : {}),
...(appSecurity ? { appSecurity } : {}),
},
...(corp.auth || expiresAt
? {
auth: {
refreshTtl: corp.auth?.refreshTtl ?? 300,
...corp.auth,
...(expiresAt ? { expiresAt } : {}),
},
}
: {}),
};
}
// ── 全局运行配置(有状态/无状态模式)─────────────────────────────────────────
export const GlobalConfigSchema = z.object({
@@ -79,3 +142,4 @@ export const GlobalConfigSchema = z.object({
export type GlobalConfig = z.infer<typeof GlobalConfigSchema>;