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
@@ -135,7 +135,7 @@ export async function handleAuthCommand(action: string | undefined, corp: CorpCo
|
||||
return;
|
||||
}
|
||||
|
||||
if (action === "keys") {
|
||||
if (action === "keys" || action === "hose-keys") {
|
||||
const reveal =
|
||||
booleanOption(args.options.reveal) === true ||
|
||||
booleanOption(args.options.r) === true ||
|
||||
|
||||
@@ -97,6 +97,74 @@ describe("corps link onboarding", () => {
|
||||
});
|
||||
});
|
||||
|
||||
test("imports a Seal native delegate link and writes a verified Seal config", async () => {
|
||||
const enterprises = setupConfigEnv();
|
||||
mockSealLinkRequests();
|
||||
|
||||
const link = "https://acme-01.sealai.cc/delegate?token=del-tok";
|
||||
const result = await runCli(["corps", "add-link", link, "--name", "Acme Seal Corp"]);
|
||||
|
||||
expect(result.isError).toBe(false);
|
||||
expect(result.stdout).not.toContain("del-tok");
|
||||
expect(result.stdout).not.toContain(link);
|
||||
|
||||
const config = JSON.parse(readFileSync(join(enterprises, "acme-01.json"), "utf-8")) as {
|
||||
id: string;
|
||||
name: string;
|
||||
seal: { url?: string; tenantSlug?: string };
|
||||
source: {
|
||||
type: string;
|
||||
token: string;
|
||||
provider?: string;
|
||||
};
|
||||
};
|
||||
expect(config).toMatchObject({
|
||||
id: "acme-01",
|
||||
name: "Acme Seal Corp",
|
||||
seal: {
|
||||
url: "https://acme-01.sealai.cc",
|
||||
tenantSlug: "acme-01"
|
||||
},
|
||||
source: {
|
||||
type: "seal",
|
||||
token: "mock-delegate-token",
|
||||
provider: "seal"
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
test("previews a Seal native delegate link with --dry-run", async () => {
|
||||
mockSealLinkRequests();
|
||||
|
||||
const link = "https://acme-01.sealai.cc/delegate?token=del-tok";
|
||||
const result = await runCli(["corps", "add-link", link, "--dry-run"]);
|
||||
|
||||
expect(result.isError).toBe(false);
|
||||
const parsed = JSON.parse(result.stdout);
|
||||
expect(parsed).toMatchObject({
|
||||
dryRun: true,
|
||||
kind: "seal-delegate",
|
||||
sealUrl: "https://acme-01.sealai.cc",
|
||||
tenant: {
|
||||
name: "Delegate Tenant"
|
||||
},
|
||||
user: {
|
||||
name: "Delegate User"
|
||||
},
|
||||
delegationId: "del-1"
|
||||
});
|
||||
});
|
||||
|
||||
test("rejects --as-hose when passed with a Seal native delegate link", async () => {
|
||||
mockSealLinkRequests();
|
||||
|
||||
const link = "https://acme-01.sealai.cc/delegate?token=del-tok";
|
||||
const result = await runCli(["corps", "add-link", link, "--as-hose"]);
|
||||
|
||||
expect(result.isError).toBe(true);
|
||||
expect(result.stderr).toContain("Option --as-hose cannot be used with a Seal native delegate link");
|
||||
});
|
||||
|
||||
test("rejects a link when Seal identity does not match ekbCorpId", async () => {
|
||||
mockSealLinkRequests({ tenantSlug: "other-tenant", corporationId: "other-corp" });
|
||||
const enterprises = setupConfigEnv();
|
||||
@@ -394,6 +462,45 @@ function mockSealLinkRequests(options: {
|
||||
});
|
||||
}
|
||||
|
||||
if (url.pathname === "/api/v1/delegation/consume") {
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
data: {
|
||||
userId: "user-delegate",
|
||||
userName: "Delegate User",
|
||||
tenantId: "tenant-delegate",
|
||||
tenantName: "Delegate Tenant",
|
||||
delegationId: "del-1"
|
||||
}
|
||||
}),
|
||||
{
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
"set-cookie": "__Secure-x-seal.session_token=mock-delegate-token.sig; Path=/; HttpOnly; Secure"
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
if (url.pathname === "/api/v1/delegation/preview") {
|
||||
return jsonResponse({
|
||||
data: {
|
||||
userId: "user-delegate",
|
||||
userName: "Delegate User",
|
||||
tenantId: "tenant-delegate",
|
||||
tenantName: "Delegate Tenant",
|
||||
delegationId: "del-1"
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (url.pathname === "/api/auth/get-session") {
|
||||
return jsonResponse({
|
||||
user: { name: "Delegate User", id: "user-delegate" },
|
||||
session: { token: "mock-delegate-token", expiresAt: "2026-10-22T01:00:00.000Z" }
|
||||
});
|
||||
}
|
||||
|
||||
return jsonResponse({ error: `Unexpected request: ${url.pathname}` }, 404);
|
||||
}) as typeof fetch;
|
||||
}
|
||||
@@ -654,6 +761,34 @@ describe("Hose OpenAPI key extraction from assistance link", () => {
|
||||
expect(parsed.keys[0].appKey).toBe("ak_acme_production_123");
|
||||
});
|
||||
|
||||
test("corps hose-keys works as alias for keys", async () => {
|
||||
const enterprises = setupConfigEnv();
|
||||
mkdirSync(enterprises, { recursive: true });
|
||||
writeFileSync(
|
||||
join(enterprises, "demo-hose.json"),
|
||||
JSON.stringify({
|
||||
id: "demo-hose",
|
||||
name: "Demo Hose Corp",
|
||||
environment: "production",
|
||||
seal: { url: "https://demo.sealai.cc", endpoints: { approvalStylePreferences: "" } },
|
||||
source: {
|
||||
type: "hose",
|
||||
domain: "https://app.ekuaibao.com",
|
||||
appKey: "my-app-key-111",
|
||||
appSecurity: "my-super-secret-password-222",
|
||||
corpId: "demo-hose",
|
||||
staffId: "demo:admin"
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
const result = await runCli(["corps", "hose-keys", "demo-hose"]);
|
||||
expect(result.isError).toBe(false);
|
||||
expect(result.exitCode).toBe(0);
|
||||
const parsed = JSON.parse(result.stdout);
|
||||
expect(parsed.corpId).toBe("demo-hose");
|
||||
});
|
||||
|
||||
test("auth keys displays configured Hose keys and supports --reveal", async () => {
|
||||
const enterprises = setupConfigEnv();
|
||||
mkdirSync(enterprises, { recursive: true });
|
||||
|
||||
+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