431 lines
18 KiB
TypeScript
431 lines
18 KiB
TypeScript
import { maskHoseSecurity } from "../../../core/auth/hose.js";
|
|
import { scoreAssistLink } from "../../../core/auth/delegate.js";
|
|
import { onboardSealBearer } from "../../../domains/seal/onboarding.js";
|
|
import { onboardAssistanceLink } from "../../../domains/enterprise/onboarding.js";
|
|
import {
|
|
listHoseStaffs,
|
|
onboardHosePersistent,
|
|
onboardHoseWebSession,
|
|
probeHoseEnterprise
|
|
} from "../../../domains/hose/onboarding.js";
|
|
import {
|
|
elevateHoseLink,
|
|
extractHoseKeysFromLink,
|
|
formatHoseKeyForOutput,
|
|
HoseKeySelectionError
|
|
} from "../../../domains/hose/keys.js";
|
|
import { CliError } from "../../errors.js";
|
|
import { selectCorp } from "../../helpers.js";
|
|
import {
|
|
booleanOption,
|
|
jsonObject,
|
|
optionalNumber,
|
|
optionalString,
|
|
requiredString,
|
|
stringOption
|
|
} from "../../parser.js";
|
|
import { printJson } from "../../printer.js";
|
|
import type { ParsedArgs } from "../../types.js";
|
|
import { maskCellphone } from "./shared.js";
|
|
|
|
export async function handleCorpOnboarding(
|
|
action: string | undefined,
|
|
maybeName: string | undefined,
|
|
args: ParsedArgs
|
|
): Promise<boolean> {
|
|
if (action === "probe-hose") {
|
|
const input = jsonObject(args.json);
|
|
const result = await probeHoseEnterprise({
|
|
appKey: requiredString(input, "appKey"),
|
|
appSecurity: requiredString(input, "appSecurity"),
|
|
domain: optionalString(input, "domain")
|
|
});
|
|
printJson({
|
|
domain: result.domain,
|
|
corpId: result.corpId,
|
|
name: result.corpName ?? result.corpId,
|
|
roleCount: result.roleCount,
|
|
roleGroups: result.roleGroups
|
|
});
|
|
return true;
|
|
}
|
|
|
|
if (action === "hose-staffs") {
|
|
const input = jsonObject(args.json);
|
|
const result = await listHoseStaffs({
|
|
appKey: requiredString(input, "appKey"),
|
|
appSecurity: requiredString(input, "appSecurity"),
|
|
domain: optionalString(input, "domain"),
|
|
role: optionalString(input, "role"),
|
|
roleDefId: optionalString(input, "roleDefId")
|
|
});
|
|
printJson({
|
|
domain: result.domain,
|
|
corpId: result.corpId,
|
|
corpName: result.corpName,
|
|
roleId: result.roleId,
|
|
roleName: result.roleName,
|
|
count: result.staffs.length,
|
|
staffs: result.staffs.map((staff) => ({
|
|
id: staff.id,
|
|
name: staff.name,
|
|
cellphone: maskCellphone(staff.cellphone),
|
|
active: staff.active,
|
|
authState: staff.authState,
|
|
userId: staff.userId,
|
|
email: staff.email
|
|
}))
|
|
});
|
|
return true;
|
|
}
|
|
|
|
if (action === "add-hose-persistent" || action === "add-hose") {
|
|
const jsonPayload = jsonObject(args.json);
|
|
const merged = {
|
|
...jsonPayload,
|
|
...(args.options.staffName ? { staffName: stringOption(args.options.staffName) } : {}),
|
|
...(args.options["staff-name"] ? { staffName: stringOption(args.options["staff-name"]) } : {}),
|
|
...(args.options.staffPhone ? { staffPhone: stringOption(args.options.staffPhone) } : {}),
|
|
...(args.options["staff-phone"] ? { staffPhone: stringOption(args.options["staff-phone"]) } : {}),
|
|
...(args.options.name ? { name: stringOption(args.options.name) } : {}),
|
|
...(args.options.domain ? { domain: stringOption(args.options.domain) } : {}),
|
|
...(args.options.corpId ? { corpId: stringOption(args.options.corpId) } : {}),
|
|
...(args.options["corp-id"] ? { corpId: stringOption(args.options["corp-id"]) } : {}),
|
|
...(args.options.appKey ? { appKey: stringOption(args.options.appKey) } : {}),
|
|
...(args.options["app-key"] ? { appKey: stringOption(args.options["app-key"]) } : {}),
|
|
...(args.options.appSecurity ? { appSecurity: stringOption(args.options.appSecurity) } : {}),
|
|
...(args.options["app-security"] ? { appSecurity: stringOption(args.options["app-security"]) } : {}),
|
|
...(args.options.force !== undefined ? { force: booleanOption(args.options.force) } : {}),
|
|
...(args.options.overwrite !== undefined ? { overwrite: booleanOption(args.options.overwrite) } : {})
|
|
};
|
|
const result = await onboardHosePersistent({
|
|
...merged,
|
|
appKey: requiredString(merged, "appKey"),
|
|
appSecurity: requiredString(merged, "appSecurity")
|
|
});
|
|
printJson({
|
|
id: result.config.id,
|
|
name: result.config.name,
|
|
corpId: result.config.source.type === "hose" ? result.config.source.corpId : undefined,
|
|
domain: result.domain,
|
|
staffId: result.staffId,
|
|
verified: result.verified,
|
|
path: result.path
|
|
});
|
|
return true;
|
|
}
|
|
|
|
if (action === "add-hose-web-session") {
|
|
const jsonPayload = jsonObject(args.json);
|
|
const merged = {
|
|
...jsonPayload,
|
|
...(args.options.name ? { name: stringOption(args.options.name) } : {}),
|
|
...(args.options.domain ? { baseUrl: stringOption(args.options.domain) } : {}),
|
|
...(args.options["base-url"] ? { baseUrl: stringOption(args.options["base-url"]) } : {}),
|
|
...(args.options.corpId ? { corpId: stringOption(args.options.corpId) } : {}),
|
|
...(args.options["corp-id"] ? { corpId: stringOption(args.options["corp-id"]) } : {}),
|
|
...(args.options.staffId ? { staffId: stringOption(args.options.staffId) } : {}),
|
|
...(args.options["staff-id"] ? { staffId: stringOption(args.options["staff-id"]) } : {}),
|
|
...(args.options.accessToken ? { accessToken: stringOption(args.options.accessToken) } : {}),
|
|
...(args.options["access-token"] ? { accessToken: stringOption(args.options["access-token"]) } : {}),
|
|
...(args.options.overwrite !== undefined ? { overwrite: booleanOption(args.options.overwrite) } : {}),
|
|
...(args.options["dry-run"] !== undefined ? { dryRun: booleanOption(args.options["dry-run"]) } : {})
|
|
};
|
|
const dryRun = merged.dryRun === true;
|
|
const input = merged as Parameters<typeof onboardHoseWebSession>[0] & { dryRun?: boolean };
|
|
const result = await onboardHoseWebSession({
|
|
...input,
|
|
persist: !dryRun
|
|
});
|
|
if (dryRun) {
|
|
printJson({
|
|
dryRun: true,
|
|
kind: "hose-web-session",
|
|
corpId: result.session.corpId,
|
|
sealUrl: result.session.sealUrl,
|
|
tenant: result.identity.tenant,
|
|
user: result.identity.user,
|
|
staffId: result.staffId,
|
|
expiresIn: result.session.expiresIn
|
|
});
|
|
} else {
|
|
printJson({
|
|
id: result.config.id,
|
|
name: result.config.name,
|
|
tenantId: result.identity.tenant.id,
|
|
tenantSlug: result.config.seal.tenantSlug,
|
|
sourceType: "hose",
|
|
credentialMode: "web-session",
|
|
verified: true,
|
|
path: result.path
|
|
});
|
|
}
|
|
return true;
|
|
}
|
|
|
|
if (action === "add-seal-bearer" || action === "add-seal") {
|
|
const jsonPayload = jsonObject(args.json);
|
|
const merged = {
|
|
...jsonPayload,
|
|
...(args.options.name ? { name: stringOption(args.options.name) } : {}),
|
|
...(args.options.token ? { token: stringOption(args.options.token) } : {}),
|
|
...(args.options.sealUrl ? { sealUrl: stringOption(args.options.sealUrl) } : {}),
|
|
...(args.options.url ? { sealUrl: stringOption(args.options.url) } : {}),
|
|
...(args.options.force !== undefined ? { force: booleanOption(args.options.force) } : {}),
|
|
...(args.options.overwrite !== undefined ? { overwrite: booleanOption(args.options.overwrite) } : {})
|
|
};
|
|
await addSealCorp(merged);
|
|
return true;
|
|
}
|
|
|
|
if (action === "add-link") {
|
|
const link = maybeName ?? stringOption(args.options.link) ?? stringOption(args.options.url);
|
|
if (!link) {
|
|
throw new Error("Usage: seal-use corps add-link <url> [--name <customName>] [--elevate-hose] [--overwrite]");
|
|
}
|
|
const dryRun = booleanOption(args.options["dry-run"]) === true || booleanOption(args.options.dryRun) === true;
|
|
const legacyAsHose = booleanOption(args.options["as-hose"]) === true || booleanOption(args.options.hose) === true;
|
|
const elevateHose = booleanOption(args.options["elevate-hose"]) === true || legacyAsHose;
|
|
if (elevateHose) {
|
|
const winner = scoreAssistLink(link)[0];
|
|
if (winner?.kind === "seal-delegate") {
|
|
throw new Error("Option --elevate-hose cannot be used with a Seal native delegate link. Seal delegate links directly authenticate to Seal.");
|
|
}
|
|
await addLinkAsHoseCorp(
|
|
link,
|
|
stringOption(args.options.name),
|
|
booleanOption(args.options.overwrite) === true,
|
|
booleanOption(args.options.force) === true,
|
|
dryRun,
|
|
{
|
|
appKey: stringOption(args.options["app-key"]) ?? stringOption(args.options.appKey),
|
|
keyId: stringOption(args.options["key-id"]) ?? stringOption(args.options.keyId)
|
|
}
|
|
);
|
|
return true;
|
|
}
|
|
await addLinkCorp(link, stringOption(args.options.name), booleanOption(args.options.overwrite) === true, dryRun);
|
|
return true;
|
|
}
|
|
|
|
if (action === "extract-keys" || action === "extract-key" || action === "extract-hose-keys" || action === "hose-extract-keys") {
|
|
const link = maybeName ?? stringOption(args.options.link) ?? stringOption(args.options.url);
|
|
if (!link) {
|
|
throw new Error("Usage: seal-use corps hose-extract-keys <url> [--reveal] [--save] [--app-key <key>] [--key-id <id>] [--name <customName>] [--overwrite]");
|
|
}
|
|
await extractKeysFromLinkAction(link, {
|
|
reveal: booleanOption(args.options.reveal) === true || booleanOption(args.options.r) === true || booleanOption(args.options.plain) === true,
|
|
save: booleanOption(args.options.save) === true || booleanOption(args.options.add) === true,
|
|
name: stringOption(args.options.name),
|
|
overwrite: booleanOption(args.options.overwrite) === true,
|
|
force: booleanOption(args.options.force) === true,
|
|
appKey: stringOption(args.options["app-key"]) ?? stringOption(args.options.appKey),
|
|
keyId: stringOption(args.options["key-id"]) ?? stringOption(args.options.keyId)
|
|
});
|
|
return true;
|
|
}
|
|
|
|
if (action === "keys" || action === "hose-keys") {
|
|
const target = maybeName ?? stringOption(args.options.link) ?? stringOption(args.options.url) ?? stringOption(args.options.corp);
|
|
const reveal = booleanOption(args.options.reveal) === true || booleanOption(args.options.r) === true || booleanOption(args.options.plain) === true;
|
|
if (target && (target.startsWith("http://") || target.startsWith("https://"))) {
|
|
await extractKeysFromLinkAction(target, {
|
|
reveal,
|
|
save: booleanOption(args.options.save) === true || booleanOption(args.options.add) === true,
|
|
name: stringOption(args.options.name),
|
|
overwrite: booleanOption(args.options.overwrite) === true,
|
|
force: booleanOption(args.options.force) === true,
|
|
appKey: stringOption(args.options["app-key"]) ?? stringOption(args.options.appKey),
|
|
keyId: stringOption(args.options["key-id"]) ?? stringOption(args.options.keyId)
|
|
});
|
|
return true;
|
|
}
|
|
await showCorpKeysAction(target, { reveal });
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
async function addSealCorp(input: Record<string, unknown>) {
|
|
const token = requiredString(input, "token");
|
|
const sealUrl = (optionalString(input, "sealUrl") ?? optionalString(input, "url") ?? "").replace(/\/+$/, "");
|
|
if (!sealUrl) throw new Error("Missing required JSON field: sealUrl");
|
|
const result = await onboardSealBearer({
|
|
...input,
|
|
token,
|
|
sealUrl,
|
|
expiresIn: optionalNumber(input, "expiresIn"),
|
|
expiresAt: optionalString(input, "expiresAt"),
|
|
id: optionalString(input, "id"),
|
|
name: optionalString(input, "name"),
|
|
tenantSlug: optionalString(input, "tenantSlug"),
|
|
tenantId: optionalString(input, "tenantId"),
|
|
tenantName: optionalString(input, "tenantName"),
|
|
corpId: optionalString(input, "corpId"),
|
|
staffId: optionalString(input, "staffId"),
|
|
provider: optionalString(input, "provider"),
|
|
force: input.force === true,
|
|
verify: input.verify === false,
|
|
overwrite: input.overwrite === true,
|
|
headers: input.headers,
|
|
cookies: input.cookies
|
|
});
|
|
printJson({
|
|
id: result.config.id,
|
|
name: result.config.name,
|
|
tenantId: result.tenantId,
|
|
tenantSlug: result.tenantSlug,
|
|
verified: result.verified,
|
|
path: result.path
|
|
});
|
|
}
|
|
|
|
async function addLinkCorp(link: string, name: string | undefined, overwrite: boolean, dryRun = false) {
|
|
const result = await onboardAssistanceLink(link, { name, overwrite, dryRun });
|
|
if (result.kind === "seal-delegate") {
|
|
if (result.dryRun) {
|
|
printJson(result);
|
|
return;
|
|
}
|
|
printJson({
|
|
id: result.saved.config.id,
|
|
name: result.saved.config.name,
|
|
tenantId: result.saved.tenantId,
|
|
tenantSlug: result.saved.tenantSlug,
|
|
verified: true,
|
|
path: result.saved.path
|
|
});
|
|
return;
|
|
}
|
|
if (result.dryRun) {
|
|
printJson({
|
|
dryRun: true,
|
|
kind: "hose",
|
|
corpId: result.result.session.corpId,
|
|
sealUrl: result.result.session.sealUrl,
|
|
tenant: result.result.identity.tenant,
|
|
user: result.result.identity.user,
|
|
staffId: result.result.staffId
|
|
});
|
|
return;
|
|
}
|
|
printJson({
|
|
id: result.result.config.id,
|
|
name: result.result.config.name,
|
|
tenantId: result.result.identity.tenant.id,
|
|
tenantSlug: result.result.config.seal.tenantSlug,
|
|
sourceType: "hose",
|
|
credentialMode: "web-session",
|
|
verified: true,
|
|
path: result.result.path
|
|
});
|
|
}
|
|
|
|
async function addLinkAsHoseCorp(link: string, name: string | undefined, overwrite: boolean, force = false, dryRun = false, selection?: { appKey?: string; keyId?: string }) {
|
|
console.error("ℹ️ [合思子域提权] 正在通过合思协助链接提取 OpenAPI 开发者密钥并提升企业权限等级...");
|
|
let elevated;
|
|
try {
|
|
elevated = await elevateHoseLink({ link, name, overwrite, force, dryRun, ...(selection ?? {}) });
|
|
} catch (error) {
|
|
if (error instanceof HoseKeySelectionError) throw new CliError(error.message, 2, error.nextAction);
|
|
throw error;
|
|
}
|
|
const { result, selectedKey } = elevated;
|
|
console.error(dryRun
|
|
? `ℹ️ [合思子域提权] 已选定合思 OpenAPI 凭据 (appKey: ${selectedKey.appKey}, 应用: "${selectedKey.name || "未命名应用"}"),正在生成预览...`
|
|
: `ℹ️ [合思子域提权] 已选定合思 OpenAPI 凭据 (appKey: ${selectedKey.appKey}, 应用: "${selectedKey.name || "未命名应用"}"),正在初始化为长期合思标准配置...`);
|
|
if (dryRun) {
|
|
printJson({
|
|
dryRun: true,
|
|
kind: "hose",
|
|
provider: "hose",
|
|
corpId: result.corpId,
|
|
corpName: result.corpName,
|
|
baseUrl: result.baseUrl,
|
|
staffId: result.staffId,
|
|
selectedKey: {
|
|
id: selectedKey.id,
|
|
name: selectedKey.name || "-",
|
|
appKey: selectedKey.appKey,
|
|
appSecurity: maskHoseSecurity(selectedKey.appSecurity),
|
|
active: selectedKey.active
|
|
}
|
|
});
|
|
return;
|
|
}
|
|
const saved = elevated.saved;
|
|
if (!saved) throw new Error("Hose elevation completed without a saved enterprise configuration");
|
|
printJson({
|
|
id: saved.config.id,
|
|
name: saved.config.name,
|
|
corpId: result.corpId,
|
|
domain: saved.domain,
|
|
staffId: saved.staffId,
|
|
verified: saved.verified,
|
|
path: saved.path
|
|
});
|
|
}
|
|
|
|
export async function extractKeysFromLinkAction(link: string, options: { reveal?: boolean; save?: boolean; name?: string; overwrite?: boolean; force?: boolean; appKey?: string; keyId?: string }) {
|
|
let extracted;
|
|
try {
|
|
extracted = await extractHoseKeysFromLink({ link, ...options });
|
|
} catch (error) {
|
|
if (error instanceof HoseKeySelectionError) throw new CliError(error.message, 2, error.nextAction);
|
|
throw error;
|
|
}
|
|
const { result } = extracted;
|
|
const reveal = extracted.reveal;
|
|
if (options.save) {
|
|
const selectedKey = extracted.selectedKey!;
|
|
console.error(`ℹ️ [合思子域提权] 正在将选定的合思 OpenAPI 密钥 (appKey: ${selectedKey.appKey}, 应用: "${selectedKey.name || "未命名应用"}") 保存为标准合思企业配置...`);
|
|
printJson({
|
|
id: extracted.saved!.config.id,
|
|
name: extracted.saved!.config.name,
|
|
corpId: result.corpId,
|
|
domain: extracted.saved!.domain,
|
|
staffId: extracted.saved!.staffId,
|
|
verified: extracted.saved!.verified,
|
|
path: extracted.saved!.path
|
|
});
|
|
return;
|
|
}
|
|
if (!reveal) console.error("ℹ️ [合思子域提权] 成功提取合思 (Hose) OpenAPI 密钥。接入密码默认脱敏,附加 --reveal (或 -r) 可查看完整 appSecurity 明文;附加 --save 可将临时会话提权并持久化为标准合思企业。\n");
|
|
else console.error("⚠️ [合思子域提权] 正在以明文展示合思 (Hose) appSecurity 接入密码,请妥善保管敏感凭据。\n");
|
|
printJson({
|
|
provider: "hose",
|
|
capability: "privilege-elevation",
|
|
corpId: result.corpId,
|
|
corpName: result.corpName ?? result.corpId,
|
|
baseUrl: result.baseUrl,
|
|
staffId: result.staffId,
|
|
keyCount: result.keys.length,
|
|
keys: result.keys.map((key) => formatHoseKeyForOutput(key, reveal))
|
|
});
|
|
}
|
|
|
|
export async function showCorpKeysAction(targetCorpId: string | undefined, options: { reveal?: boolean }) {
|
|
const corp = selectCorp(targetCorpId);
|
|
const reveal = options.reveal === true;
|
|
if (corp.source.type !== "hose") {
|
|
throw new CliError(`【合思子域专享能力】当前企业「${corp.name}」(id: ${corp.id}) 为原生 Seal (${corp.source.type}) 租户,非合思 (Hose/合思云/易快报) 渠道企业。\n` +
|
|
"OpenAPI 密钥查询与提权能力专属于合思生态租户(用于从合思协助链接或合思凭据中提取开发者密钥提升权限等级),非合思渠道企业不支持此能力。", 2);
|
|
}
|
|
const appKey = corp.source.appKey ?? corp.source.key;
|
|
const appSecurity = corp.source.appSecurity ?? corp.source.password;
|
|
if (!reveal) console.error("💡 [合思子域] 接入密码默认脱敏,附加 --reveal (或 -r) 可查看完整 appSecurity 明文。\n");
|
|
else console.error("⚠️ [合思子域] 正在以明文展示合思 appSecurity 接入密码,请妥善保管敏感凭据。\n");
|
|
printJson({
|
|
provider: "hose",
|
|
capability: "privilege-elevation",
|
|
corpId: corp.id,
|
|
corpName: corp.name,
|
|
domain: corp.source.domain,
|
|
staffId: corp.source.staffId,
|
|
appKey: appKey ?? "-",
|
|
appSecurity: appSecurity ? (reveal ? appSecurity : maskHoseSecurity(appSecurity)) : "-",
|
|
active: true
|
|
});
|
|
}
|