Support Seal native delegate link in corps add-link and refine hose keys
This commit is contained in:
1 parent
3f95932377
commit
d42755f9bc
13 files changed
+1065
-388
No files matched your search
+88
-14
@@ -13,6 +13,12 @@ import {
|
||||
probeHoseDomain,
|
||||
type HoseOpenApiKey
|
||||
} from "../../core/auth/hose.js";
|
||||
import {
|
||||
exchangeSealDelegateLink,
|
||||
parseSealDelegateLink,
|
||||
previewSealDelegateLink,
|
||||
scoreAssistLink
|
||||
} from "../../core/auth/delegate.js";
|
||||
import { clearCorpTokenCache } from "../../core/auth/token-store.js";
|
||||
import { createSealClient, createSealClientForUrl } from "../../core/http/factory.js";
|
||||
import { resolveSealCustomHeaders } from "../../core/http/request-headers.js";
|
||||
@@ -41,14 +47,12 @@ import { resolveLiveSealEnterpriseConfig } from "../../domains/seal/source.js";
|
||||
import { selectCorp } from "../helpers.js";
|
||||
import { CliError, confirmationRequired, usageError } from "../errors.js";
|
||||
import {
|
||||
inferCorpIdFromStaffId,
|
||||
booleanOption,
|
||||
jsonObject,
|
||||
numberOption,
|
||||
optionalNumber,
|
||||
optionalString,
|
||||
requiredString,
|
||||
requiredStringAlias,
|
||||
safeFileName,
|
||||
stringOption
|
||||
} from "../parser.js";
|
||||
@@ -1027,11 +1031,19 @@ export async function handleCorpsCommand(action: string | undefined, maybeName:
|
||||
const link = maybeName ?? stringOption(args.options.link) ?? stringOption(args.options.url);
|
||||
if (!link) {
|
||||
throw new Error(
|
||||
"Usage: seal-use corps add-link <https://.../web/thirdparty.html?...> [--name <customName>] [--as-hose] [--overwrite]"
|
||||
"Usage: seal-use corps add-link <url> [--name <customName>] [--as-hose] [--overwrite]"
|
||||
);
|
||||
}
|
||||
const dryRun = booleanOption(args.options["dry-run"]) === true || booleanOption(args.options.dryRun) === true;
|
||||
const asHose = booleanOption(args.options["as-hose"]) === true || booleanOption(args.options.hose) === true;
|
||||
if (asHose) {
|
||||
const scores = scoreAssistLink(link);
|
||||
const winner = scores[0];
|
||||
if (winner?.kind === "seal-delegate") {
|
||||
throw new Error(
|
||||
"Option --as-hose cannot be used with a Seal native delegate link. Seal delegate links directly authenticate to Seal."
|
||||
);
|
||||
}
|
||||
await addLinkAsHoseCorp(
|
||||
link,
|
||||
stringOption(args.options.name),
|
||||
@@ -1044,15 +1056,15 @@ export async function handleCorpsCommand(action: string | undefined, maybeName:
|
||||
);
|
||||
return;
|
||||
}
|
||||
await addLinkCorp(link, stringOption(args.options.name), booleanOption(args.options.overwrite) === true);
|
||||
await addLinkCorp(link, stringOption(args.options.name), booleanOption(args.options.overwrite) === true, dryRun);
|
||||
return;
|
||||
}
|
||||
|
||||
if (action === "extract-keys" || action === "extract-key") {
|
||||
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 extract-keys <https://.../web/thirdparty.html?...> [--reveal] [--save] [--app-key <key>] [--key-id <id>] [--name <customName>] [--overwrite]"
|
||||
"Usage: seal-use corps extract-keys <url> [--reveal] [--save] [--app-key <key>] [--key-id <id>] [--name <customName>] [--overwrite]"
|
||||
);
|
||||
}
|
||||
const reveal =
|
||||
@@ -1072,7 +1084,7 @@ export async function handleCorpsCommand(action: string | undefined, maybeName:
|
||||
return;
|
||||
}
|
||||
|
||||
if (action === "keys") {
|
||||
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 ||
|
||||
@@ -1510,7 +1522,75 @@ function sealCustomHeadersFromInput(input: Record<string, unknown>) {
|
||||
};
|
||||
}
|
||||
|
||||
async function addLinkCorp(link: string, name: string | undefined, overwrite: boolean) {
|
||||
async function addLinkCorp(link: string, name: string | undefined, overwrite: boolean, dryRun = false) {
|
||||
// Classify the incoming link by score. The highest-scoring scorer decides
|
||||
// which exchange path to take. This lets one command consume both Seal
|
||||
// native delegate links (*/delegate?token=...) and Hose assistance links
|
||||
// (*/web/thirdparty.html) without the caller having to know which they got.
|
||||
const scores = scoreAssistLink(link);
|
||||
const winner = scores[0];
|
||||
|
||||
if (!winner || winner.score < 50) {
|
||||
throw new Error(
|
||||
`Unrecognized assistance/delegate link. Expected either a Seal native delegate link ` +
|
||||
`(https://<tenant>.sealai.cc/delegate?token=...) ` +
|
||||
`or a Hose assistance link ` +
|
||||
`(https://<corp>.ekuaibao.com/web/thirdparty.html?accessToken=...&ekbCorpId=...). ` +
|
||||
`Top score: ${winner ? winner.kind + "=" + winner.score : "none"}.`
|
||||
);
|
||||
}
|
||||
|
||||
if (winner.kind === "seal-delegate") {
|
||||
if (dryRun) {
|
||||
const preview = await previewSealDelegateLink(link);
|
||||
const parsed = parseSealDelegateLink(link);
|
||||
printJson({
|
||||
dryRun: true,
|
||||
kind: "seal-delegate",
|
||||
sealUrl: parsed.sealUrl,
|
||||
tenant: {
|
||||
id: preview.tenantId,
|
||||
name: preview.tenantName
|
||||
},
|
||||
user: {
|
||||
id: preview.userId,
|
||||
name: preview.userName
|
||||
},
|
||||
delegationId: preview.delegationId
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// Seal native delegate link: exchange directly against the Seal tenant.
|
||||
console.error(`ℹ️ [Seal 委托链接] 识别为 Seal 原生委托链接 (${winner.reason}),正在交换临时会话...`);
|
||||
const session = await exchangeSealDelegateLink(link);
|
||||
const identity = { user: session.user, tenant: session.tenant };
|
||||
const tenantSlug =
|
||||
(typeof session.tenant.tenantSlug === "string" && session.tenant.tenantSlug) || undefined;
|
||||
const tenantId =
|
||||
(typeof session.tenant.id === "string" && session.tenant.id) || undefined;
|
||||
const corpId = tenantSlug ?? tenantId ?? "seal-delegate";
|
||||
|
||||
writeSealCorpConfig(
|
||||
{
|
||||
id: corpId,
|
||||
corpId,
|
||||
name,
|
||||
provider: "seal",
|
||||
overwrite
|
||||
},
|
||||
{
|
||||
token: session.token,
|
||||
sealUrl: session.sealUrl,
|
||||
expiresIn: session.expiresIn,
|
||||
verify: true,
|
||||
remote: identity
|
||||
}
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
// Default: Hose assistance link (existing behaviour).
|
||||
const session = await exchangeHoseAssistLink(link);
|
||||
const client = createSealClientForUrl(session.sealUrl, session.token);
|
||||
const me = await getMe(client);
|
||||
@@ -1857,12 +1937,6 @@ async function resolveSealWhoami(sealUrl: string, token: string, customHeaders:
|
||||
};
|
||||
}
|
||||
|
||||
function objectValue(value: unknown): Record<string, unknown> {
|
||||
return value && typeof value === "object" && !Array.isArray(value)
|
||||
? (value as Record<string, unknown>)
|
||||
: {};
|
||||
}
|
||||
|
||||
function stringValue(value: unknown): string | undefined {
|
||||
return typeof value === "string" && value ? value : undefined;
|
||||
}
|
||||
Reference in new issue
Block a user