Add Hose assistance link onboarding (#13)
This commit is contained in:
1 parent
9f3975a6a5
commit
4ce206a2ce
13 files changed
+527
-17
No files matched your search
@@ -0,0 +1,197 @@
|
||||
import { afterEach, describe, expect, test } from "bun:test";
|
||||
import { mkdtempSync, readFileSync, rmSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { runCli } from "../runner.js";
|
||||
|
||||
const ORIGINAL_ENV = {
|
||||
enterprises: process.env.SEAL_HOME_ENTERPRISES_DIR,
|
||||
config: process.env.SEAL_HOME_CONFIG_FILE,
|
||||
tokenStore: process.env.SEAL_HOME_TOKEN_STORE
|
||||
};
|
||||
const ORIGINAL_FETCH = globalThis.fetch;
|
||||
const tempDirs: string[] = [];
|
||||
|
||||
afterEach(() => {
|
||||
restoreEnv("SEAL_HOME_ENTERPRISES_DIR", ORIGINAL_ENV.enterprises);
|
||||
restoreEnv("SEAL_HOME_CONFIG_FILE", ORIGINAL_ENV.config);
|
||||
restoreEnv("SEAL_HOME_TOKEN_STORE", ORIGINAL_ENV.tokenStore);
|
||||
globalThis.fetch = ORIGINAL_FETCH;
|
||||
|
||||
for (const dir of tempDirs.splice(0)) {
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
describe("corps link onboarding", () => {
|
||||
test("imports a Hose assistance link and writes a verified Seal config", async () => {
|
||||
const enterprises = setupConfigEnv();
|
||||
mockSealLinkRequests();
|
||||
|
||||
const link = hoseLink();
|
||||
const result = await runCli(["corps", "add-link", link, "--name", "Acme Corporation"]);
|
||||
|
||||
expect(result.isError).toBe(false);
|
||||
expect(result.stdout).not.toContain("hose-token");
|
||||
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;
|
||||
expiresIn: number;
|
||||
provider?: string;
|
||||
corpId?: string;
|
||||
};
|
||||
};
|
||||
expect(config).toMatchObject({
|
||||
id: "acme-01",
|
||||
name: "Acme Corporation",
|
||||
seal: {
|
||||
url: "https://acme-01.sealai.cc",
|
||||
tenantSlug: "acme-01"
|
||||
},
|
||||
source: {
|
||||
type: "seal",
|
||||
token: "seal-bearer-token",
|
||||
expiresIn: 1800,
|
||||
provider: "hosecloud",
|
||||
corpId: "acme-01"
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
test("creates a delegation directly from a Hose assistance link", async () => {
|
||||
mockSealLinkRequests({ delegation: true });
|
||||
|
||||
const result = await runCli([
|
||||
"delegation",
|
||||
"create",
|
||||
"--from-link",
|
||||
hoseLink(),
|
||||
"--note",
|
||||
"Customer support"
|
||||
]);
|
||||
|
||||
expect(result.isError).toBe(false);
|
||||
expect(JSON.parse(result.stdout)).toMatchObject({
|
||||
tenant: {
|
||||
corpId: "acme-01",
|
||||
tenantSlug: "acme-01"
|
||||
},
|
||||
user: {
|
||||
id: "user-1",
|
||||
providerUserId: "staff-1"
|
||||
},
|
||||
delegation: {
|
||||
id: "delegation-1",
|
||||
token: "delegation-token",
|
||||
link: "https://acme-01.sealai.cc/delegation?token=delegation-token"
|
||||
},
|
||||
expiresIn: 1800
|
||||
});
|
||||
});
|
||||
|
||||
test("rejects a link when Seal identity does not match ekbCorpId", async () => {
|
||||
mockSealLinkRequests({ tenantSlug: "other-tenant", corporationId: "other-corp" });
|
||||
const enterprises = setupConfigEnv();
|
||||
|
||||
const result = await runCli(["corps", "add-link", hoseLink()]);
|
||||
|
||||
expect(result.isError).toBe(true);
|
||||
expect(result.stderr).toContain("tenant mismatch");
|
||||
expect(result.stderr).not.toContain("hose-token");
|
||||
expect(readDirectory(enterprises)).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
function setupConfigEnv() {
|
||||
const root = mkdtempSync(join(tmpdir(), "seal-home-link-cli-"));
|
||||
const enterprises = join(root, "enterprises");
|
||||
tempDirs.push(root);
|
||||
process.env.SEAL_HOME_ENTERPRISES_DIR = enterprises;
|
||||
process.env.SEAL_HOME_CONFIG_FILE = join(root, "config.json");
|
||||
process.env.SEAL_HOME_TOKEN_STORE = join(root, "tokens.json");
|
||||
return enterprises;
|
||||
}
|
||||
|
||||
function hoseLink() {
|
||||
return "https://wx2.ekuaibao.com/web/thirdparty.html?accessToken=hose-token&ekbCorpId=Acme-01&pageType=home";
|
||||
}
|
||||
|
||||
function mockSealLinkRequests(options: {
|
||||
delegation?: boolean;
|
||||
tenantSlug?: string;
|
||||
corporationId?: string;
|
||||
} = {}) {
|
||||
globalThis.fetch = (async (input: RequestInfo | URL) => {
|
||||
const requestUrl = typeof input === "string"
|
||||
? input
|
||||
: input instanceof URL
|
||||
? input.toString()
|
||||
: input.url;
|
||||
const url = new URL(requestUrl);
|
||||
|
||||
if (url.pathname === "/api/auth/oauth2/session/oem-hosecloud") {
|
||||
return jsonResponse({ accessToken: "seal-bearer-token", expiresIn: 1800 });
|
||||
}
|
||||
|
||||
if (url.pathname === "/api/v1/auth/me") {
|
||||
return jsonResponse({
|
||||
data: {
|
||||
user: {
|
||||
id: "user-1",
|
||||
tenantId: "tenant-1",
|
||||
name: "Support User",
|
||||
providerUserId: "staff-1",
|
||||
isAdmin: true
|
||||
},
|
||||
tenant: {
|
||||
id: "tenant-1",
|
||||
name: "Acme Tenant",
|
||||
tenantSlug: options.tenantSlug ?? "acme-01",
|
||||
hoseCorpConfig: {
|
||||
corporationId: options.corporationId ?? "acme-01",
|
||||
corporationName: "Acme Corporation"
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (options.delegation && url.pathname === "/api/v1/delegation/create") {
|
||||
return jsonResponse({
|
||||
data: {
|
||||
id: "delegation-1",
|
||||
token: "delegation-token",
|
||||
link: "https://acme-01.sealai.cc/delegation?token=delegation-token"
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return jsonResponse({ error: `Unexpected request: ${url.pathname}` }, 404);
|
||||
}) as typeof fetch;
|
||||
}
|
||||
|
||||
function jsonResponse(value: unknown, status = 200) {
|
||||
return new Response(JSON.stringify(value), {
|
||||
status,
|
||||
headers: { "content-type": "application/json" }
|
||||
});
|
||||
}
|
||||
|
||||
function readDirectory(path: string) {
|
||||
try {
|
||||
return Array.from(new Bun.Glob("*.json").scanSync({ cwd: path })).sort();
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
function restoreEnv(name: string, value: string | undefined) {
|
||||
if (value === undefined) delete process.env[name];
|
||||
else process.env[name] = value;
|
||||
}
|
||||
@@ -1,12 +1,15 @@
|
||||
import { existsSync, mkdirSync, writeFileSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import {
|
||||
assertHoseAssistTenantMatches,
|
||||
exchangeHoseAssistLink,
|
||||
fetchHoseRoleDefGroups,
|
||||
fetchHoseRoleStaffs,
|
||||
fetchHoseStaffDetails,
|
||||
probeHoseDomain
|
||||
} from "../../core/auth/hose.js";
|
||||
import { clearCorpTokenCache } from "../../core/auth/token-store.js";
|
||||
import { createSealClientForUrl } from "../../core/http/factory.js";
|
||||
import {
|
||||
loadCorpConfigs,
|
||||
loadGlobalConfig,
|
||||
@@ -14,10 +17,12 @@ import {
|
||||
saveGlobalConfig
|
||||
} from "../../core/config/loader.js";
|
||||
import type { CorpConfig } from "../../core/config/types.js";
|
||||
import { getMe } from "../../domains/seal/api.js";
|
||||
import { resolveLiveSealEnterpriseConfig } from "../../domains/seal/source.js";
|
||||
import { selectCorp } from "../helpers.js";
|
||||
import {
|
||||
inferCorpIdFromStaffId,
|
||||
booleanOption,
|
||||
jsonObject,
|
||||
optionalNumber,
|
||||
optionalString,
|
||||
@@ -103,6 +108,17 @@ export async function handleCorpsCommand(action: string | undefined, maybeName:
|
||||
return;
|
||||
}
|
||||
|
||||
if (action === "add-link") {
|
||||
const link = maybeName ?? stringOption(args.options.link) ?? stringOption(args.options.url);
|
||||
if (!link) {
|
||||
throw new Error(
|
||||
"Usage: seal-home corps add-link <https://.../web/thirdparty.html?...> [--name <customName>] [--overwrite]"
|
||||
);
|
||||
}
|
||||
await addLinkCorp(link, stringOption(args.options.name), booleanOption(args.options.overwrite) === true);
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Error(`Unknown corps command: ${action ?? ""}. Run 'seal-home help' for available commands.`);
|
||||
}
|
||||
|
||||
@@ -359,13 +375,58 @@ async function addSealCorp(input: Record<string, unknown>) {
|
||||
const expiresIn = optionalNumber(input, "expiresIn") ?? 30 * 24 * 60 * 60;
|
||||
const verify = input.force === true || input.verify === false ? false : true;
|
||||
const remote = verify ? await resolveSealWhoami(sealUrl, token) : undefined;
|
||||
writeSealCorpConfig(input, { token, sealUrl, expiresIn, verify, remote });
|
||||
}
|
||||
|
||||
async function addLinkCorp(link: string, name: string | undefined, overwrite: boolean) {
|
||||
const session = await exchangeHoseAssistLink(link);
|
||||
const client = createSealClientForUrl(session.sealUrl, session.token);
|
||||
const me = await getMe(client);
|
||||
const identity = {
|
||||
user: me.user as unknown as Record<string, unknown>,
|
||||
tenant: me.tenant as unknown as Record<string, unknown>
|
||||
};
|
||||
|
||||
assertHoseAssistTenantMatches(session.corpId, identity.tenant);
|
||||
writeSealCorpConfig(
|
||||
{
|
||||
id: session.corpId,
|
||||
corpId: session.corpId,
|
||||
name,
|
||||
provider: "hosecloud",
|
||||
overwrite
|
||||
},
|
||||
{
|
||||
token: session.token,
|
||||
sealUrl: session.sealUrl,
|
||||
expiresIn: session.expiresIn,
|
||||
verify: true,
|
||||
remote: identity
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
type RemoteSealIdentity = {
|
||||
user: Record<string, unknown>;
|
||||
tenant: Record<string, unknown>;
|
||||
};
|
||||
|
||||
function writeSealCorpConfig(
|
||||
input: Record<string, unknown>,
|
||||
auth: {
|
||||
token: string;
|
||||
sealUrl: string;
|
||||
expiresIn: number;
|
||||
verify: boolean;
|
||||
remote?: RemoteSealIdentity;
|
||||
}
|
||||
) {
|
||||
const { token, sealUrl, expiresIn, verify, remote } = auth;
|
||||
const remoteUser = remote?.user;
|
||||
const remoteTenant = remote?.tenant;
|
||||
const tenantSlug = optionalString(input, "tenantSlug") ?? stringValue(remoteTenant?.tenantSlug);
|
||||
const tenantId = optionalString(input, "tenantId") ?? stringValue(remoteTenant?.id);
|
||||
const tenantName = optionalString(input, "tenantName") ??
|
||||
optionalString(input, "name") ??
|
||||
stringValue(remoteTenant?.name);
|
||||
const tenantName = optionalString(input, "tenantName") ?? stringValue(remoteTenant?.name);
|
||||
const id = optionalString(input, "id") ?? tenantSlug ?? tenantId;
|
||||
if (!id) {
|
||||
throw new Error("Missing required JSON field: id. Seal whoami did not return tenantSlug or tenantId.");
|
||||
@@ -378,10 +439,11 @@ async function addSealCorp(input: Record<string, unknown>) {
|
||||
const provider = optionalString(input, "provider") ??
|
||||
stringValue(remoteUser?.provider) ??
|
||||
stringValue((remoteTenant as any)?.providerConfig?.provider);
|
||||
const corpId = optionalString(input, "corpId") ?? tenantSlug ?? tenantId ?? id;
|
||||
|
||||
const config: CorpConfig = {
|
||||
id,
|
||||
name: tenantName ?? id,
|
||||
name: optionalString(input, "name") ?? tenantName ?? id,
|
||||
seal: {
|
||||
url: sealUrl,
|
||||
tenantSlug,
|
||||
@@ -395,7 +457,7 @@ async function addSealCorp(input: Record<string, unknown>) {
|
||||
sealUrl,
|
||||
expiresIn,
|
||||
staffId,
|
||||
corpId: tenantSlug ?? tenantId ?? id,
|
||||
corpId,
|
||||
tenantId,
|
||||
tenantSlug,
|
||||
tenantName,
|
||||
|
||||
@@ -1,9 +1,37 @@
|
||||
import type { CorpConfig } from "../../core/config/types.js";
|
||||
import type { createSealClient } from "../../core/http/factory.js";
|
||||
import { assertHoseAssistTenantMatches, exchangeHoseAssistLink } from "../../core/auth/hose.js";
|
||||
import { createSealClientForUrl } from "../../core/http/factory.js";
|
||||
import { createDelegation, getMe } from "../../domains/seal/api.js";
|
||||
import { runTool } from "../helpers.js";
|
||||
import { stringOption } from "../parser.js";
|
||||
import { outputOptions } from "../printer.js";
|
||||
import { outputOptions, printJson } from "../printer.js";
|
||||
import type { ParsedArgs } from "../types.js";
|
||||
import type { createSealClient } from "../../core/http/factory.js";
|
||||
|
||||
export async function handleDelegationFromHoseLink(link: string, note?: string) {
|
||||
const session = await exchangeHoseAssistLink(link);
|
||||
const client = createSealClientForUrl(session.sealUrl, session.token);
|
||||
const me = await getMe(client);
|
||||
assertHoseAssistTenantMatches(session.corpId, me.tenant as unknown as Record<string, unknown>);
|
||||
const delegation = await createDelegation(client, { note });
|
||||
|
||||
printJson({
|
||||
tenant: {
|
||||
id: me.tenant.id,
|
||||
name: me.tenant.name,
|
||||
tenantSlug: me.tenant.tenantSlug,
|
||||
corpId: session.corpId
|
||||
},
|
||||
user: {
|
||||
id: me.user.id,
|
||||
name: me.user.name,
|
||||
providerUserId: me.user.providerUserId,
|
||||
isAdmin: me.user.isAdmin
|
||||
},
|
||||
delegation,
|
||||
expiresIn: session.expiresIn
|
||||
});
|
||||
}
|
||||
|
||||
export async function handleDelegationCommand(
|
||||
action: string | undefined,
|
||||
|
||||
Reference in new issue
Block a user