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
@@ -6,12 +6,15 @@ import { CorpConfig, HoseSealSource } from "../config/types.js";
|
||||
import {
|
||||
HOSE_STANDARD_DOMAINS,
|
||||
defaultSealUrl,
|
||||
exchangeHoseAssistLink,
|
||||
fetchHoseStaffDetails,
|
||||
getHoseEnterpriseUrl,
|
||||
normalizeBaseUrl
|
||||
normalizeBaseUrl,
|
||||
parseHoseAssistLink
|
||||
} from "./hose.js";
|
||||
|
||||
const ORIGINAL_ENV = process.env.SEAL_HOME_TOKEN_STORE;
|
||||
const ORIGINAL_FETCH = globalThis.fetch;
|
||||
const tempDirs: string[] = [];
|
||||
|
||||
function setupTokenStore(data: Record<string, unknown>): string {
|
||||
@@ -29,6 +32,7 @@ afterEach(() => {
|
||||
} else {
|
||||
process.env.SEAL_HOME_TOKEN_STORE = ORIGINAL_ENV;
|
||||
}
|
||||
globalThis.fetch = ORIGINAL_FETCH;
|
||||
|
||||
for (const dir of tempDirs.splice(0)) {
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
@@ -116,5 +120,66 @@ describe("hose auth helpers", () => {
|
||||
const details = await fetchHoseStaffDetails("https://app.ekuaibao.com", "fake-token", []);
|
||||
expect(details).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
test("parses a Hose third-party assistance link without exposing its token", () => {
|
||||
const result = parseHoseAssistLink(
|
||||
"https://wx2.ekuaibao.com/web/thirdparty.html?accessToken=temporary-token&ekbCorpId=Acme-01&pageType=home"
|
||||
);
|
||||
|
||||
expect(result).toEqual({
|
||||
corpId: "acme-01",
|
||||
accessToken: "temporary-token"
|
||||
});
|
||||
});
|
||||
|
||||
test("rejects unsupported Hose assistance link shapes", () => {
|
||||
expect(() => parseHoseAssistLink("http://wx2.ekuaibao.com/web/thirdparty.html?accessToken=t&ekbCorpId=acme")).toThrow(
|
||||
"expected an HTTPS"
|
||||
);
|
||||
expect(() => parseHoseAssistLink("https://example.com/web/thirdparty.html?accessToken=t&ekbCorpId=acme")).toThrow(
|
||||
"expected an HTTPS"
|
||||
);
|
||||
expect(() => parseHoseAssistLink("https://wx2.ekuaibao.com/web/home.html?accessToken=t&ekbCorpId=acme")).toThrow(
|
||||
"expected an HTTPS"
|
||||
);
|
||||
expect(() => parseHoseAssistLink("https://wx2.ekuaibao.com/web/thirdparty.html?ekbCorpId=acme")).toThrow(
|
||||
"required exactly once"
|
||||
);
|
||||
});
|
||||
|
||||
test("exchanges a Hose assistance link for a Seal session", async () => {
|
||||
let requestedUrl = "";
|
||||
globalThis.fetch = (async (input: RequestInfo | URL) => {
|
||||
requestedUrl = typeof input === "string"
|
||||
? input
|
||||
: input instanceof URL
|
||||
? input.toString()
|
||||
: input.url;
|
||||
return new Response(JSON.stringify({
|
||||
data: {
|
||||
accessToken: "seal-bearer-token",
|
||||
expiresIn: 1800
|
||||
}
|
||||
}), {
|
||||
status: 200,
|
||||
headers: { "content-type": "application/json" }
|
||||
});
|
||||
}) as typeof fetch;
|
||||
|
||||
const result = await exchangeHoseAssistLink(
|
||||
"https://wx2.ekuaibao.com/web/thirdparty.html?accessToken=hose-token&ekbCorpId=Acme-01"
|
||||
);
|
||||
|
||||
const url = new URL(requestedUrl);
|
||||
expect(url.origin).toBe("https://acme-01.sealai.cc");
|
||||
expect(url.pathname).toBe("/api/auth/oauth2/session/oem-hosecloud");
|
||||
expect(url.searchParams.get("token")).toBe("hose-token");
|
||||
expect(url.searchParams.get("returnToken")).toBe("1");
|
||||
expect(result).toEqual({
|
||||
corpId: "acme-01",
|
||||
sealUrl: "https://acme-01.sealai.cc",
|
||||
token: "seal-bearer-token",
|
||||
expiresIn: 1800
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -40,6 +40,18 @@ type SealTokenResponse = {
|
||||
};
|
||||
};
|
||||
|
||||
export type HoseAssistLink = {
|
||||
corpId: string;
|
||||
accessToken: string;
|
||||
};
|
||||
|
||||
export type HoseAssistSealSession = {
|
||||
corpId: string;
|
||||
sealUrl: string;
|
||||
token: string;
|
||||
expiresIn: number;
|
||||
};
|
||||
|
||||
export const HOSE_STANDARD_DOMAINS = [
|
||||
"https://app.ekuaibao.com",
|
||||
"https://dd2.hosecloud.com",
|
||||
@@ -49,6 +61,10 @@ export const HOSE_STANDARD_DOMAINS = [
|
||||
"https://feishu.ekuaibao.com"
|
||||
] as const;
|
||||
|
||||
const HOSE_ASSIST_HOST_SUFFIXES = [".ekuaibao.com", ".hosecloud.com"] as const;
|
||||
const HOSE_ASSIST_PATH = "/web/thirdparty.html";
|
||||
const CORP_ID_PATTERN = /^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$/;
|
||||
|
||||
export type HoseRoleDef = {
|
||||
id: string;
|
||||
name: string;
|
||||
@@ -101,6 +117,109 @@ export function normalizeBaseUrl(url: string): string {
|
||||
return url.replace(/\/+$/, "");
|
||||
}
|
||||
|
||||
export function parseHoseAssistLink(value: string): HoseAssistLink {
|
||||
let url: URL;
|
||||
try {
|
||||
url = new URL(value.trim());
|
||||
} catch {
|
||||
throw new Error("Invalid Hose assistance link: expected a valid HTTPS URL");
|
||||
}
|
||||
|
||||
if (
|
||||
url.protocol !== "https:" ||
|
||||
url.port ||
|
||||
!isHoseAssistHost(url.hostname) ||
|
||||
url.pathname.replace(/\/+$/, "") !== HOSE_ASSIST_PATH
|
||||
) {
|
||||
throw new Error(
|
||||
"Invalid Hose assistance link: expected an HTTPS */web/thirdparty.html URL from ekuaibao.com or hosecloud.com"
|
||||
);
|
||||
}
|
||||
|
||||
const accessTokens = url.searchParams.getAll("accessToken").filter(Boolean);
|
||||
const corpIds = url.searchParams.getAll("ekbCorpId").filter(Boolean);
|
||||
if (accessTokens.length !== 1 || corpIds.length !== 1) {
|
||||
throw new Error("Invalid Hose assistance link: accessToken and ekbCorpId are required exactly once");
|
||||
}
|
||||
|
||||
const corpId = corpIds[0]!.trim().toLowerCase();
|
||||
if (!CORP_ID_PATTERN.test(corpId)) {
|
||||
throw new Error("Invalid Hose assistance link: ekbCorpId is not a valid tenant slug");
|
||||
}
|
||||
|
||||
return {
|
||||
corpId,
|
||||
accessToken: accessTokens[0]!
|
||||
};
|
||||
}
|
||||
|
||||
export async function exchangeHoseAssistLink(
|
||||
value: string | HoseAssistLink
|
||||
): Promise<HoseAssistSealSession> {
|
||||
const link = typeof value === "string" ? parseHoseAssistLink(value) : normalizeHoseAssistLink(value);
|
||||
const sealUrl = `https://${link.corpId}.sealai.cc`;
|
||||
const sealResponse = await ky
|
||||
.get(`${sealUrl}/api/auth/oauth2/session/oem-hosecloud`, {
|
||||
searchParams: {
|
||||
token: link.accessToken,
|
||||
returnToken: "1"
|
||||
},
|
||||
timeout: 30000
|
||||
})
|
||||
.json<SealTokenResponse>();
|
||||
|
||||
const token =
|
||||
sealResponse.token ??
|
||||
sealResponse.accessToken ??
|
||||
sealResponse.data?.token ??
|
||||
sealResponse.data?.accessToken;
|
||||
if (!token) {
|
||||
throw new Error("Seal SSO did not return a bearer token");
|
||||
}
|
||||
|
||||
const expiresIn =
|
||||
sealResponse.expiresIn ??
|
||||
sealResponse.expires_in ??
|
||||
sealResponse.data?.expiresIn ??
|
||||
sealResponse.data?.expires_in ??
|
||||
3600;
|
||||
|
||||
return {
|
||||
corpId: link.corpId,
|
||||
sealUrl,
|
||||
token,
|
||||
expiresIn: Number.isFinite(expiresIn) && expiresIn > 0 ? expiresIn : 3600
|
||||
};
|
||||
}
|
||||
|
||||
export function assertHoseAssistTenantMatches(corpId: string, tenant: Record<string, unknown>): void {
|
||||
const hoseCorpConfig = tenant.hoseCorpConfig && typeof tenant.hoseCorpConfig === "object"
|
||||
? tenant.hoseCorpConfig as Record<string, unknown>
|
||||
: undefined;
|
||||
const candidates = [tenant.tenantSlug, hoseCorpConfig?.corporationId]
|
||||
.filter((value): value is string => typeof value === "string" && Boolean(value));
|
||||
if (candidates.length > 0 && !candidates.some((value) => value.toLowerCase() === corpId)) {
|
||||
throw new Error("Hose assistance link tenant mismatch: the Seal tenant does not match ekbCorpId");
|
||||
}
|
||||
}
|
||||
|
||||
function isHoseAssistHost(hostname: string): boolean {
|
||||
const normalized = hostname.toLowerCase();
|
||||
return HOSE_ASSIST_HOST_SUFFIXES.some((suffix) => normalized.endsWith(suffix));
|
||||
}
|
||||
|
||||
function normalizeHoseAssistLink(value: HoseAssistLink): HoseAssistLink {
|
||||
const corpId = typeof value?.corpId === "string" ? value.corpId.trim().toLowerCase() : "";
|
||||
const accessToken = typeof value?.accessToken === "string" ? value.accessToken.trim() : "";
|
||||
if (!CORP_ID_PATTERN.test(corpId) || !accessToken) {
|
||||
throw new Error("Invalid Hose assistance link: accessToken and ekbCorpId are required");
|
||||
}
|
||||
return {
|
||||
corpId,
|
||||
accessToken
|
||||
};
|
||||
}
|
||||
|
||||
function isFresh(entry: TokenEntry | undefined, refreshTtl = 60): entry is TokenEntry {
|
||||
return Boolean(entry && Date.now() < entry.expiresAt - refreshTtl * 1000);
|
||||
}
|
||||
|
||||
@@ -32,8 +32,12 @@ export async function createSealClient(corp: CorpConfig): Promise<KyInstance> {
|
||||
export async function createSealClientForToken(corp: CorpConfig, token: string): Promise<KyInstance> {
|
||||
const session = await getSealSessionForCorp(corp);
|
||||
|
||||
return createSealClientForUrl(session.enterprise.sealUrl, token);
|
||||
}
|
||||
|
||||
export function createSealClientForUrl(sealUrl: string, token: string): KyInstance {
|
||||
return ky.create({
|
||||
prefix: session.enterprise.sealUrl,
|
||||
prefix: sealUrl.replace(/\/+$/, ""),
|
||||
timeout: 30000,
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`
|
||||
|
||||
Reference in new issue
Block a user