111 lines
4.2 KiB
TypeScript
111 lines
4.2 KiB
TypeScript
import {
|
||
fetchHoseOpenApiKeysFromLink,
|
||
maskHoseSecurity,
|
||
type HoseOpenApiKey
|
||
} from "../../core/auth/hose.js";
|
||
import { onboardHosePersistent } from "./onboarding.js";
|
||
|
||
export class HoseKeySelectionError extends Error {
|
||
readonly nextAction: string;
|
||
|
||
constructor(message: string, nextAction: string) {
|
||
super(message);
|
||
this.name = "HoseKeySelectionError";
|
||
this.nextAction = nextAction;
|
||
}
|
||
}
|
||
|
||
export function selectHoseOpenApiKey(
|
||
keys: HoseOpenApiKey[],
|
||
corpId: string,
|
||
selection: { appKey?: string; keyId?: string },
|
||
contextCmd: string
|
||
): HoseOpenApiKey {
|
||
if (keys.length === 0) {
|
||
throw new HoseKeySelectionError(
|
||
`[合思子域提权] 未能从协助链接中提取到合思企业 [${corpId}] 的 OpenAPI 密钥凭据。\n` +
|
||
"💡 说明:此能力专属于合思 (Hose) 子域,用于将临时会话提权为标准 OpenAPI 开发者接入。需要企业管理员已在合思开放平台创建应用并生成 appKey/appSecurity。",
|
||
contextCmd
|
||
);
|
||
}
|
||
if (selection.keyId || selection.appKey) {
|
||
const matched = keys.find((key) => (selection.keyId && key.id === selection.keyId) || (selection.appKey && key.appKey === selection.appKey));
|
||
if (!matched) {
|
||
throw new HoseKeySelectionError(
|
||
`指定的合思密钥不存在,请从上述候选列表中选择正确的 appKey 或 keyId。\n` +
|
||
`当前企业存在以下 ${keys.length} 套候选密钥:\n` +
|
||
keys.map((key) => ` • "${key.name || "未命名应用"}" (appKey: ${key.appKey}, keyId: ${key.id}, 状态: ${key.active ? "🟢 启用" : "⚪ 停用"})`).join("\n"),
|
||
`${contextCmd} --app-key ${keys[0]!.appKey}`
|
||
);
|
||
}
|
||
return matched;
|
||
}
|
||
if (keys.length === 1) return keys[0]!;
|
||
throw new HoseKeySelectionError(
|
||
`
|
||
ℹ️ [合思子域提权] 合思企业 [${corpId}] 存在多套 (${keys.length} 套) OpenAPI 应用密钥凭据:
|
||
${keys.map((key, index) => ` [${index + 1}] "${key.name || "未命名应用"}" (appKey: ${key.appKey}, keyId: ${key.id}, 状态: ${key.active ? "🟢 启用" : "⚪ 停用"})`).join("\n")}
|
||
|
||
👉 为确保权限提权绑定正确的业务应用,请显式指定要使用的密钥套件:
|
||
在命令后追加 --app-key <key> 或 --key-id <id> 参数。
|
||
`,
|
||
`${contextCmd} --app-key ${keys[0]!.appKey}`
|
||
);
|
||
}
|
||
|
||
export type HoseKeyLinkOptions = {
|
||
link: string;
|
||
appKey?: string;
|
||
keyId?: string;
|
||
name?: string;
|
||
overwrite?: boolean;
|
||
force?: boolean;
|
||
dryRun?: boolean;
|
||
};
|
||
|
||
export async function elevateHoseLink(options: HoseKeyLinkOptions) {
|
||
const result = await fetchHoseOpenApiKeysFromLink(options.link);
|
||
const selectedKey = selectHoseOpenApiKey(result.keys, result.corpId, options, `seal-use corps add-link "<url>" --elevate-hose`);
|
||
if (options.dryRun) return { result, selectedKey, dryRun: true as const };
|
||
const saved = await onboardHosePersistent({
|
||
domain: result.baseUrl,
|
||
appKey: selectedKey.appKey,
|
||
appSecurity: selectedKey.appSecurity,
|
||
corpId: result.corpId,
|
||
name: options.name ?? result.corpName,
|
||
staffId: result.staffId,
|
||
overwrite: options.overwrite,
|
||
force: options.force
|
||
});
|
||
return { result, selectedKey, saved, dryRun: false as const };
|
||
}
|
||
|
||
export async function extractHoseKeysFromLink(options: HoseKeyLinkOptions & { save?: boolean; reveal?: boolean }) {
|
||
const result = await fetchHoseOpenApiKeysFromLink(options.link);
|
||
if (!options.save) return { result, reveal: options.reveal === true };
|
||
const selectedKey = selectHoseOpenApiKey(result.keys, result.corpId, options, `seal-use corps hose-extract-keys "<url>" --save`);
|
||
const saved = await onboardHosePersistent({
|
||
domain: result.baseUrl,
|
||
appKey: selectedKey.appKey,
|
||
appSecurity: selectedKey.appSecurity,
|
||
corpId: result.corpId,
|
||
name: options.name ?? result.corpName,
|
||
staffId: result.staffId,
|
||
overwrite: options.overwrite,
|
||
force: options.force
|
||
});
|
||
return { result, selectedKey, saved, reveal: options.reveal === true };
|
||
}
|
||
|
||
export function formatHoseKeyForOutput(key: HoseOpenApiKey, reveal: boolean) {
|
||
return {
|
||
id: key.id,
|
||
name: key.name || "-",
|
||
appKey: key.appKey,
|
||
appSecurity: reveal ? key.appSecurity : maskHoseSecurity(key.appSecurity),
|
||
active: key.active,
|
||
createTime: key.createTime,
|
||
updateTime: key.updateTime
|
||
};
|
||
}
|