Add approval run URL command with security-guarded SSO option
This commit is contained in:
1 parent
d672ba447e
commit
bdb4a1dd9e
8 files changed
+287
-117
No files matched your search
@@ -278,14 +278,20 @@ export async function handleApprovalRunsCommand(
|
||||
}
|
||||
|
||||
if (action === "url") {
|
||||
const sso = booleanOption(args.options.sso) ?? false;
|
||||
const expiresIn = numberOption(args.options.expire) ?? numberOption(args.options.expiresIn) ?? 7200;
|
||||
const sn = stringOption(args.options.sn) ?? stringOption(args.options.sourceDocumentSN);
|
||||
|
||||
await runTool(
|
||||
"seal_approval_run_url_get",
|
||||
client,
|
||||
corp,
|
||||
{
|
||||
recordId: maybeName,
|
||||
sourceDocumentSN: stringOption(args.options.sourceDocumentSN),
|
||||
sourceDocumentId: stringOption(args.options.sourceDocumentId)
|
||||
recordId: maybeName ?? stringOption(args.options.id) ?? stringOption(args.options.recordId),
|
||||
sourceDocumentSN: sn,
|
||||
sourceDocumentId: stringOption(args.options.sourceDocumentId),
|
||||
sso,
|
||||
expiresIn
|
||||
},
|
||||
outputOptions(args.options)
|
||||
);
|
||||
|
||||
+4
-3
@@ -309,11 +309,12 @@ export const HELP_REGISTRY: AreaHelp[] = [
|
||||
},
|
||||
{
|
||||
name: "url",
|
||||
summary: "Get web console URL for approval run",
|
||||
summary: "Get web console URL for approval run (default deep link, or --sso for auto-login link)",
|
||||
usage: [
|
||||
"seal-use approval-runs url [--corp <corpId>]",
|
||||
"seal-use approval-runs url <recordId> [--corp <corpId>]",
|
||||
"seal-use approval-runs url --sourceDocumentSN B26022501 [--corp <corpId>]"
|
||||
"seal-use approval-runs url <recordId> --sso [--expire 7200] [--corp <corpId>]",
|
||||
"seal-use approval-runs url --sn <documentSN> [--sso] [--corp <corpId>]",
|
||||
"seal-use approval-runs url [--corp <corpId>]"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
||||
@@ -401,12 +401,25 @@ export async function getHoseProvisionalAuthLink(
|
||||
corpId: string;
|
||||
staffId: string;
|
||||
url: string;
|
||||
token?: string;
|
||||
expiresIn: number;
|
||||
}> {
|
||||
if (corp.source.type !== "hose") {
|
||||
throw new Error(`Unsupported Seal source: ${corp.source.type}`);
|
||||
}
|
||||
|
||||
const cache = getHoseAuthCache(corp.id);
|
||||
if (isFresh(cache.closeapi) && cache.closeapi.url && cache.closeapi.token) {
|
||||
const remainingSeconds = Math.max(60, Math.floor((cache.closeapi.expiresAt - Date.now()) / 1000));
|
||||
return {
|
||||
corpId: corp.source.corpId,
|
||||
staffId: corp.source.staffId,
|
||||
url: cache.closeapi.url,
|
||||
token: cache.closeapi.token,
|
||||
expiresIn: Math.min(expiresIn, remainingSeconds)
|
||||
};
|
||||
}
|
||||
|
||||
const openapiToken = await ensureHoseOpenapiToken(corp);
|
||||
const provisional = await fetchHoseCloseApiToken(corp.source, openapiToken, expiresIn);
|
||||
if (!provisional.url) {
|
||||
@@ -417,6 +430,7 @@ export async function getHoseProvisionalAuthLink(
|
||||
corpId: corp.source.corpId,
|
||||
staffId: corp.source.staffId,
|
||||
url: provisional.url,
|
||||
token: provisional.token,
|
||||
expiresIn
|
||||
};
|
||||
}
|
||||
|
||||
@@ -660,6 +660,9 @@ describe("sealTools", () => {
|
||||
};
|
||||
|
||||
expect(result).toMatchObject({
|
||||
url: "https://hose-corp-a.sealai.cc/approval-result/run-1",
|
||||
sealResultUrl: "https://hose-corp-a.sealai.cc/approval-result/run-1",
|
||||
type: "deep_link",
|
||||
enterpriseAssistUrl: "https://app.ekuaibao.com/web/home.html?accessToken=close-token",
|
||||
enterpriseUrl: "https://app.ekuaibao.com/web/home.html?accessToken=close-token",
|
||||
hoseDomain: "https://app.ekuaibao.com",
|
||||
@@ -705,11 +708,54 @@ describe("sealTools", () => {
|
||||
fakeApprovalRunsClient(requests),
|
||||
{ sourceDocumentSN: "B26001965" },
|
||||
fakeHoseContext()
|
||||
) as { documentUrl?: string };
|
||||
) as { documentUrl?: string; url?: string; type?: string };
|
||||
|
||||
expect(requests[0]).toContain("search=B26001965");
|
||||
expect(requests[1]).toBe("api/v1/approvals/run-1");
|
||||
expect(result.documentUrl).toBe("https://app.ekuaibao.com/web/thirdparty.html");
|
||||
expect(result.type).toBe("deep_link");
|
||||
expect(result.url).toBe("https://hose-corp-a.sealai.cc/approval-result/run-1");
|
||||
});
|
||||
|
||||
test("approval run url returns SSO link with identity, expiration, and warning when sso=true", async () => {
|
||||
const tool = sealTools.find((item) => item.name === "seal_approval_run_url_get");
|
||||
expect(tool).toBeDefined();
|
||||
primeHoseEnterpriseUrl("corp-a", "https://app.ekuaibao.com/web/home.html?accessToken=close-token");
|
||||
|
||||
const handler = tool!.handler as ToolHandler;
|
||||
const result = (await handler(
|
||||
fakeApprovalRunsClient(),
|
||||
{ recordId: "run-1", sso: true, expiresIn: 3600 },
|
||||
fakeHoseContext()
|
||||
)) as {
|
||||
url?: string;
|
||||
ssoUrl?: string;
|
||||
type?: string;
|
||||
expiresIn?: number;
|
||||
warning?: string;
|
||||
identity?: { staffId?: string };
|
||||
};
|
||||
|
||||
expect(result.type).toBe("sso_link");
|
||||
expect(result.expiresIn).toBe(3600);
|
||||
expect(result.url).toContain("/api/auth/oauth2/session/oem-hosecloud?token=close-token");
|
||||
expect(result.url).toContain("path=%2Fapproval-result%2Frun-1");
|
||||
expect(result.warning).toContain("安全警示");
|
||||
expect(result.identity?.staffId).toBe("staff-a");
|
||||
});
|
||||
|
||||
test("approval run url throws clear error when sso=true is used on non-hose enterprise", async () => {
|
||||
const tool = sealTools.find((item) => item.name === "seal_approval_run_url_get");
|
||||
expect(tool).toBeDefined();
|
||||
|
||||
const handler = tool!.handler as ToolHandler;
|
||||
await expect(
|
||||
handler(
|
||||
fakeApprovalRunsClient(),
|
||||
{ recordId: "run-1", sso: true },
|
||||
fakeContext()
|
||||
)
|
||||
).rejects.toThrow("SSO direct links currently support Hose-integrated enterprises");
|
||||
});
|
||||
|
||||
test("approval run summary filters by local date and returns compact aggregates", async () => {
|
||||
|
||||
@@ -3,7 +3,7 @@ import { KyInstance } from "ky";
|
||||
import { readFileSync } from "node:fs";
|
||||
import { randomBytes } from "node:crypto";
|
||||
import * as api from "./api.js";
|
||||
import { defaultSealUrl, getHoseEnterpriseUrl, probeHoseDomain } from "../../core/auth/hose.js";
|
||||
import { defaultSealUrl, getHoseEnterpriseUrl, getHoseProvisionalAuthLink, probeHoseDomain } from "../../core/auth/hose.js";
|
||||
import { CorpConfig } from "../../core/config/types.js";
|
||||
import { loadGlobalConfig } from "../../core/config/loader.js";
|
||||
import { createSealClientForToken } from "../../core/http/factory.js";
|
||||
@@ -1450,15 +1450,23 @@ export const sealTools = [
|
||||
},
|
||||
{
|
||||
name: "seal_approval_run_url_get",
|
||||
description: "获取合思企业协助链接,按需附带单据链接;支持只取企业协助链接,或按 Seal recordId/合思 sourceDocumentSN 查询单据链接",
|
||||
description: "获取审批单的 Web 控制台直达链接或合思企业协助链接,支持纯深度链接与带安全警告的免密 SSO 链接",
|
||||
parameters: z.object({
|
||||
recordId: z.string().optional().describe("Seal approval run record ID"),
|
||||
sourceDocumentSN: z.string().optional().describe("合思/易快报单号"),
|
||||
sourceDocumentId: z.string().optional().describe("合思/易快报源单据 ID")
|
||||
sourceDocumentId: z.string().optional().describe("合思/易快报源单据 ID"),
|
||||
sso: z.boolean().optional().describe("是否生成带临时认证凭证的免密直达链接 (SSO link)"),
|
||||
expiresIn: z.number().optional().describe("SSO 免密凭证有效秒数,默认 7200 (2小时)")
|
||||
}),
|
||||
handler: async (
|
||||
client: KyInstance,
|
||||
params: { recordId?: string; sourceDocumentSN?: string; sourceDocumentId?: string },
|
||||
params: {
|
||||
recordId?: string;
|
||||
sourceDocumentSN?: string;
|
||||
sourceDocumentId?: string;
|
||||
sso?: boolean;
|
||||
expiresIn?: number;
|
||||
},
|
||||
context: ToolContext
|
||||
) => {
|
||||
const hasDocumentLookup = Boolean(params.recordId || params.sourceDocumentSN || params.sourceDocumentId);
|
||||
@@ -1467,7 +1475,68 @@ export const sealTools = [
|
||||
const enterpriseAssistUrl = source.type === "hose" ? await getHoseEnterpriseUrl(context.corp) : undefined;
|
||||
const documentUrl = stringFromUnknown(record?.sourceExtendData?.hosecloudViewUrl);
|
||||
|
||||
const sealBaseUrl = resolveSealBaseUrl(context.corp);
|
||||
const sealResultUrl = record?.id ? `${sealBaseUrl}/approval-result/${record.id}` : undefined;
|
||||
|
||||
let ssoUrl: string | undefined;
|
||||
let identity: { name?: string; staffId?: string; isAdmin?: boolean; providerUserId?: string } | undefined;
|
||||
let warning: string | undefined;
|
||||
const expiresIn = params.expiresIn ?? 7200;
|
||||
|
||||
if (params.sso) {
|
||||
if (!record?.id) {
|
||||
throw new Error("recordId or sourceDocumentSN is required to generate an SSO approval result URL");
|
||||
}
|
||||
if (source.type !== "hose") {
|
||||
throw new Error(
|
||||
`SSO direct links currently support Hose-integrated enterprises (corp '${context.corp.id}' uses '${source.type}'). For Seal-native enterprises, use 'seal-use delegation create' for authorized access.`
|
||||
);
|
||||
}
|
||||
|
||||
const linkInfo = await getHoseProvisionalAuthLink(context.corp, expiresIn);
|
||||
const provisionalToken = new URL(linkInfo.url).searchParams.get("accessToken");
|
||||
if (!provisionalToken) {
|
||||
throw new Error("Failed to obtain Hose provisional authentication token");
|
||||
}
|
||||
|
||||
const targetPath = `/approval-result/${record.id}`;
|
||||
ssoUrl = `${sealBaseUrl}/api/auth/oauth2/session/oem-hosecloud?token=${encodeURIComponent(provisionalToken)}&path=${encodeURIComponent(targetPath)}`;
|
||||
|
||||
try {
|
||||
const me = await api.getMe(client);
|
||||
identity = {
|
||||
name: me.user.name,
|
||||
staffId: source.staffId,
|
||||
isAdmin: me.user.isAdmin,
|
||||
providerUserId: me.user.providerUserId
|
||||
};
|
||||
} catch {
|
||||
identity = {
|
||||
staffId: source.staffId
|
||||
};
|
||||
}
|
||||
|
||||
const userName = identity.name ?? "企业成员";
|
||||
const roleLabel = identity.isAdmin ? " (管理员)" : "";
|
||||
warning = `⚠️ 安全警示:该链接使用 [${userName}${roleLabel}] 身份签发,在 Seal AI 拥有全站操作权限。链接具备 ${Math.round(expiresIn / 3600 * 10) / 10} 小时时效,仅供受信任人员排查使用,请勿在外部公开分享。`;
|
||||
|
||||
console.error(
|
||||
`⚠️ 安全警示 (SSO 强化免密链接):\n` +
|
||||
`• 身份主体:${userName}${roleLabel}\n` +
|
||||
`• 权限范围:持有该链接者在 Seal AI 中拥有该用户的完整操作权限(含系统设置与规则编辑)\n` +
|
||||
`• 时效性:换票凭证有效时间 ${expiresIn} 秒(${Math.round(expiresIn / 60)} 分钟),在浏览器打开后将保留 30 天会话\n` +
|
||||
`• 安全边界:该凭证绝不包含且无法访问合思 (Hose) 主系统;仅限内部排查,严禁在外部公开分享!`
|
||||
);
|
||||
}
|
||||
|
||||
return {
|
||||
url: params.sso ? ssoUrl : sealResultUrl,
|
||||
sealResultUrl,
|
||||
ssoUrl,
|
||||
type: params.sso ? "sso_link" : "deep_link",
|
||||
expiresIn: params.sso ? expiresIn : undefined,
|
||||
identity,
|
||||
warning,
|
||||
enterpriseAssistUrl,
|
||||
enterpriseUrl: enterpriseAssistUrl,
|
||||
hoseDomain: source.type === "hose" ? normalizeUrl(source.domain) : undefined,
|
||||
@@ -3218,6 +3287,15 @@ async function resolveApprovalRunForUrl(
|
||||
return api.getApprovalRun(client, record.id);
|
||||
}
|
||||
|
||||
function resolveSealBaseUrl(corp: CorpConfig): string {
|
||||
if (corp.seal?.url) return corp.seal.url.replace(/\/+$/, "");
|
||||
if (corp.source.type === "hose") return defaultSealUrl(corp, corp.source).replace(/\/+$/, "");
|
||||
if ("sealUrl" in corp.source && typeof corp.source.sealUrl === "string" && corp.source.sealUrl) {
|
||||
return corp.source.sealUrl.replace(/\/+$/, "");
|
||||
}
|
||||
return `https://${corp.id.toLowerCase()}.sealai.cc`;
|
||||
}
|
||||
|
||||
function normalizeUrl(value: string): string {
|
||||
return value.replace(/\/+$/, "");
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user