Fix Hose web-session source persistence

This commit is contained in:
gitea committed 2026-09-22 14:15:40 +08:00
1 parent 2e941f0842
commit 1987b45336
14 files changed
+518 -316

No files matched your search

+34 -20
View File
@@ -177,7 +177,7 @@ async function diagnoseAuth(corp: CorpConfig) {
const uid = source.staffId.includes(":") ? source.staffId : `${source.corpId}:${source.staffId}`;
const stages: Array<Record<string, unknown>> = [];
if (!appKey || !appSecurity) {
if ((!appKey || !appSecurity) && !source.closeToken) {
return {
corp: {
id: corp.id,
@@ -195,26 +195,40 @@ async function diagnoseAuth(corp: CorpConfig) {
};
}
const openapi = await runDiagnoseStage("hose.openapi", async () => {
const json = await postJson(`${source.domain.replace(/\/+$/, "")}/api/openapi/v1/auth/getAccessToken`, {
appKey,
appSecurity
});
const value = objectValue(json.value);
const token = stringValue(value.accessToken) ?? stringValue(json.accessToken) ?? stringValue(json.token);
if (!token) throw new Error("Hose login did not return an access token");
return {
token,
public: {
corporationId: stringValue(value.corporationId) ?? stringValue(objectValue(value.corporation).id),
expireTime: numberValue(value.expireTime)
}
};
});
stages.push(openapi.public);
const openapi = appKey && appSecurity
? await runDiagnoseStage("hose.openapi", async () => {
const json = await postJson(`${source.domain.replace(/\/+$/, "")}/api/openapi/v1/auth/getAccessToken`, {
appKey,
appSecurity
});
const value = objectValue(json.value);
const token = stringValue(value.accessToken) ?? stringValue(json.accessToken) ?? stringValue(json.token);
if (!token) throw new Error("Hose login did not return an access token");
return {
token,
public: {
corporationId: stringValue(value.corporationId) ?? stringValue(objectValue(value.corporation).id),
expireTime: numberValue(value.expireTime)
}
};
})
: undefined;
if (openapi) stages.push(openapi.public);
const openapiToken = openapi.private?.token;
const provisional = openapiToken
const openapiToken = openapi?.private?.token;
const provisional = source.closeToken
? {
public: {
name: "hose.closeapi",
ok: true,
corpId: source.corpId,
staffId: source.staffId,
configured: true,
expiresAt: source.expiresAt
},
private: { token: source.closeToken }
}
: openapiToken
? await runDiagnoseStage("hose.provisional", async () => {
const json = await postJson(
`${source.domain.replace(/\/+$/, "")}/api/openapi/v1.1/provisional/getProvisionalAuth?accessToken=${encodeURIComponent(openapiToken)}`,
+12 -12
View File
@@ -26,7 +26,7 @@ afterEach(() => {
});
describe("corps link onboarding", () => {
test("imports a Hose assistance link and writes a verified Seal config", async () => {
test("imports a Hose assistance link and writes a verified Hose web-session config", async () => {
const enterprises = setupConfigEnv();
mockSealLinkRequests();
@@ -43,8 +43,8 @@ describe("corps link onboarding", () => {
seal: { url?: string; tenantSlug?: string };
source: {
type: string;
token: string;
expiresIn: number;
closeToken: string;
staffId: string;
provider?: string;
corpId?: string;
};
@@ -57,9 +57,9 @@ describe("corps link onboarding", () => {
tenantSlug: "acme-01"
},
source: {
type: "seal",
token: "seal-bearer-token",
expiresIn: 1800,
type: "hose",
closeToken: "hose-token",
staffId: "acme-01:admin",
provider: "hosecloud",
corpId: "acme-01"
}
@@ -171,7 +171,7 @@ describe("corps link onboarding", () => {
expect(readDirectory(enterprises)).toEqual([]);
});
test("imports a normalized Hose web session and stores a temporary Seal session", async () => {
test("imports a normalized Hose web session and stores the Hose source", async () => {
const enterprises = setupConfigEnv();
mockSealLinkRequests();
@@ -182,18 +182,18 @@ describe("corps link onboarding", () => {
JSON.stringify({
baseUrl: "https://wx2.ekuaibao.com",
corpId: "Acme-01",
accessToken: "hose-token"
accessToken: "hose-token",
staffId: "staff-1"
})
]);
expect(result.isError).toBe(false);
expect(result.exitCode).toBe(0);
const config = JSON.parse(readFileSync(join(enterprises, "acme-01.json"), "utf-8"));
expect(config.source.type).toBe("seal");
expect(config.source.type).toBe("hose");
expect(config.source.provider).toBe("hosecloud");
expect(config.source.token).toBe("seal-bearer-token");
expect(config.source.expiresIn).toBe(1800);
expect(config.source.expiresAt).toMatch(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$/);
expect(config.source.closeToken).toBe("hose-token");
expect(config.source.staffId).toBe("staff-1");
});
test("rejects --elevate-hose when passed with a Seal native delegate link", async () => {
+110 -36
View File
@@ -10,7 +10,9 @@ import {
fetchHoseRoleStaffs,
fetchHoseStaffDetails,
maskHoseSecurity,
parseHoseAssistLink,
probeHoseDomain,
resolveHoseAssistLinkDetails,
type HoseOpenApiKey
} from "../../core/auth/hose.js";
import {
@@ -78,12 +80,16 @@ function formatSafeSourceDetail(source: SealSource) {
const key = source.appKey ?? source.key;
return {
type: "hose",
provider: "hosecloud",
provider: source.provider ?? "hosecloud",
domain: source.domain,
corpId: source.corpId,
staffId: source.staffId,
appKey: maskSecret(key),
hasAppSecurity: Boolean(source.appSecurity ?? source.password),
hasCloseToken: Boolean(source.closeToken),
closeTokenExpiresIn: source.closeToken ? source.expiresIn : undefined,
closeTokenExpiresAt: source.closeToken ? source.expiresAt : undefined,
credentialMode: source.closeToken ? "web-session" : "persistent",
sealUrl: source.sealUrl
};
}
@@ -1031,6 +1037,8 @@ export async function handleCorpsCommand(action: string | undefined, maybeName:
...(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) } : {}),
@@ -1573,6 +1581,8 @@ async function addHoseWebSessionCorp(input: Record<string, unknown>) {
const accessToken = requiredStringAlias(input, ["accessToken", "accesstoken"]);
const corpId = requiredStringAlias(input, ["corpId", "ekbCorpId", "corporationId"]);
const baseUrl = optionalString(input, "baseUrl") ?? optionalString(input, "domain");
const suppliedStaffId = optionalString(input, "staffId");
const details = await resolveHoseAssistLinkDetails({ corpId, accessToken, baseUrl });
const session = await exchangeHoseAssistLink({ corpId, accessToken, baseUrl });
const client = createSealClientForUrl(session.sealUrl, session.token);
const me = await getMe(client);
@@ -1590,27 +1600,26 @@ async function addHoseWebSessionCorp(input: Record<string, unknown>) {
sealUrl: session.sealUrl,
tenant: identity.tenant,
user: identity.user,
staffId: suppliedStaffId ?? details.staffId,
expiresIn: session.expiresIn
});
return;
}
writeSealCorpConfig(
{
id: session.corpId,
corpId: session.corpId,
name: optionalString(input, "name"),
provider: "hosecloud",
overwrite: input.overwrite === true
},
{
token: session.token,
sealUrl: session.sealUrl,
expiresIn: session.expiresIn,
verify: true,
remote: identity
}
);
writeHoseWebSessionConfig({
id: session.corpId,
corpId: session.corpId,
name: optionalString(input, "name") ?? details.corpName,
provider: "hosecloud",
domain: details.baseUrl,
closeToken: accessToken,
expiresIn: optionalNumber(input, "expiresIn"),
expiresAt: optionalString(input, "expiresAt"),
staffId: suppliedStaffId ?? details.staffId,
sealUrl: session.sealUrl,
remote: identity,
overwrite: input.overwrite === true
});
}
// Validates extra headers/cookies up front so a malformed name fails here
@@ -1690,8 +1699,11 @@ async function addLinkCorp(link: string, name: string | undefined, overwrite: bo
return;
}
// Default: Hose assistance link (existing behaviour).
const session = await exchangeHoseAssistLink(link);
// Default: Hose assistance link. Keep the Hose web-session source and its
// CloseAPI token; the Seal bearer is only a runtime SSO session.
const hoseLink = parseHoseAssistLink(link);
const details = await resolveHoseAssistLinkDetails(hoseLink);
const session = await exchangeHoseAssistLink(hoseLink);
const client = createSealClientForUrl(session.sealUrl, session.token);
const me = await getMe(client);
const identity = {
@@ -1707,27 +1719,24 @@ async function addLinkCorp(link: string, name: string | undefined, overwrite: bo
corpId: session.corpId,
sealUrl: session.sealUrl,
tenant: identity.tenant,
user: identity.user
user: identity.user,
staffId: details.staffId
});
return;
}
writeSealCorpConfig(
{
id: session.corpId,
corpId: session.corpId,
name,
provider: "hosecloud",
overwrite
},
{
token: session.token,
sealUrl: session.sealUrl,
expiresIn: session.expiresIn,
verify: true,
remote: identity
}
);
writeHoseWebSessionConfig({
id: session.corpId,
corpId: session.corpId,
name: name ?? details.corpName,
provider: "hosecloud",
domain: details.baseUrl,
closeToken: hoseLink.accessToken,
staffId: details.staffId,
sealUrl: session.sealUrl,
remote: identity,
overwrite
});
}
function selectHoseOpenApiKey(
@@ -2030,6 +2039,71 @@ function writeSealCorpConfig(
});
}
function writeHoseWebSessionConfig(input: {
id: string;
corpId: string;
name?: string;
provider: string;
domain: string;
closeToken: string;
expiresIn?: number;
expiresAt?: string;
staffId: string;
sealUrl: string;
remote: RemoteSealIdentity;
overwrite: boolean;
}) {
const remoteTenant = input.remote.tenant;
const tenantSlug = stringValue(remoteTenant.tenantSlug) ?? input.corpId;
const tenantId = stringValue(remoteTenant.id);
const tenantName = stringValue(remoteTenant.name);
const config: CorpConfig = {
id: input.id,
name: input.name ?? tenantName ?? input.id,
environment: "production",
seal: {
url: input.sealUrl,
tenantSlug,
endpoints: {
approvalStylePreferences: "api/v1/agent/ai-approval/config"
}
},
source: {
type: "hose",
provider: input.provider,
domain: input.domain,
closeToken: input.closeToken,
...(input.expiresIn !== undefined ? { expiresIn: input.expiresIn } : {}),
...(input.expiresAt ? { expiresAt: input.expiresAt } : {}),
staffId: input.staffId,
corpId: input.corpId,
sealUrl: input.sealUrl
},
auth: {
refreshTtl: 300
}
};
const enterprisesDir = resolveWritableEnterprisesDir();
mkdirSync(enterprisesDir, { recursive: true, mode: 0o700 });
const file = join(enterprisesDir, `${safeFileName(input.id)}.json`);
if (existsSync(file) && !input.overwrite) {
throw new Error(`Enterprise config already exists: ${file}. Pass "overwrite": true to replace it.`);
}
writeFileSync(file, `${JSON.stringify(config, null, 2)}\n`, { mode: 0o600 });
printJson({
id: config.id,
name: config.name,
tenantId,
tenantSlug,
sourceType: "hose",
credentialMode: "web-session",
verified: true,
path: file
});
}
function formatUtcTimestampSeconds(timestampMs: number): string {
return new Date(timestampMs).toISOString().replace(/\.\d{3}Z$/, "Z");
}