Add Seal-native enterprise support
This commit is contained in:
1 parent
2611ce4c85
commit
997ece006a
10 files changed
+176
-9
No files matched your search
+91
@@ -117,6 +117,11 @@ async function main() {
|
||||
return;
|
||||
}
|
||||
|
||||
if (area === "corps" && action === "add-seal") {
|
||||
await addSealCorp(jsonObject(args.json));
|
||||
return;
|
||||
}
|
||||
|
||||
const corp = selectCorp(String(args.options.corp ?? ""));
|
||||
|
||||
if (area === "source" && action === "config") {
|
||||
@@ -655,6 +660,75 @@ async function addHoseCorp(input: Record<string, unknown>) {
|
||||
});
|
||||
}
|
||||
|
||||
async function addSealCorp(input: Record<string, unknown>) {
|
||||
const token = requiredString(input, "token");
|
||||
const sealUrl = (optionalString(input, "sealUrl") ?? optionalString(input, "url") ?? "").replace(/\/+$/, "");
|
||||
if (!sealUrl) throw new Error("Missing required JSON field: sealUrl");
|
||||
|
||||
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;
|
||||
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 id = optionalString(input, "id") ?? tenantSlug ?? tenantId;
|
||||
if (!id) {
|
||||
throw new Error("Missing required JSON field: id. Seal whoami did not return tenantSlug or tenantId.");
|
||||
}
|
||||
const staffId = optionalString(input, "staffId") ??
|
||||
stringValue(remoteUser?.providerUserId) ??
|
||||
stringValue(remoteUser?.id) ??
|
||||
"seal";
|
||||
|
||||
const config: CorpConfig = {
|
||||
id,
|
||||
name: tenantName ?? id,
|
||||
seal: {
|
||||
url: sealUrl,
|
||||
tenantSlug,
|
||||
endpoints: {
|
||||
approvalStylePreferences: "api/v1/agent/ai-approval/config"
|
||||
}
|
||||
},
|
||||
source: {
|
||||
type: "seal",
|
||||
token,
|
||||
sealUrl,
|
||||
expiresIn,
|
||||
staffId,
|
||||
corpId: tenantSlug ?? tenantId ?? id,
|
||||
tenantId,
|
||||
tenantSlug,
|
||||
tenantName,
|
||||
providerUserId: stringValue(remoteUser?.providerUserId)
|
||||
},
|
||||
auth: {
|
||||
refreshTtl: 300
|
||||
}
|
||||
};
|
||||
|
||||
const enterprisesDir = resolveWritableEnterprisesDir();
|
||||
mkdirSync(enterprisesDir, { recursive: true, mode: 0o700 });
|
||||
const file = join(enterprisesDir, `${safeFileName(id)}.json`);
|
||||
if (existsSync(file) && input.overwrite !== true) {
|
||||
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,
|
||||
name: config.name,
|
||||
tenantId,
|
||||
tenantSlug,
|
||||
verified: verify,
|
||||
path: file
|
||||
});
|
||||
}
|
||||
|
||||
async function diagnoseAuth(corp: CorpConfig) {
|
||||
if (corp.source.type !== "hose") {
|
||||
const client = await createSealClient(corp);
|
||||
@@ -871,6 +945,17 @@ async function getJson(url: string, headers: Record<string, string> = {}): Promi
|
||||
return parseJsonResponse(response);
|
||||
}
|
||||
|
||||
async function resolveSealWhoami(sealUrl: string, token: string) {
|
||||
const json = await getJson(`${sealUrl}/api/v1/auth/me`, {
|
||||
authorization: `Bearer ${token}`
|
||||
});
|
||||
const data = objectValue(json.data);
|
||||
return {
|
||||
user: objectValue(data.user ?? json.user),
|
||||
tenant: objectValue(data.tenant ?? json.tenant)
|
||||
};
|
||||
}
|
||||
|
||||
async function parseJsonResponse(response: Response): Promise<Record<string, unknown>> {
|
||||
const body = await response.text();
|
||||
if (!response.ok) {
|
||||
@@ -931,6 +1016,11 @@ function optionalString(input: Record<string, unknown>, key: string): string | u
|
||||
return typeof value === "string" && value.trim() ? value.trim() : undefined;
|
||||
}
|
||||
|
||||
function optionalNumber(input: Record<string, unknown>, key: string): number | undefined {
|
||||
const value = input[key];
|
||||
return typeof value === "number" && Number.isFinite(value) ? value : undefined;
|
||||
}
|
||||
|
||||
function inferCorpIdFromStaffId(staffId: string): string | undefined {
|
||||
const [corpId] = staffId.split(":", 1);
|
||||
return corpId || undefined;
|
||||
@@ -1145,6 +1235,7 @@ Usage:
|
||||
seal-home corps current
|
||||
seal-home corps switch <corpId>
|
||||
seal-home corps add-hose --json '{"name":"企业名称","domain":"https://app.ekuaibao.com","appKey":"...","appSecurity":"...","proxyStaffBizId":"corpId:staffId"}'
|
||||
seal-home corps add-seal --json '{"sealUrl":"https://tenant.sealai.cc","token":"seal-bearer-token"}'
|
||||
seal-home auth diagnose [--corp <corpId>]
|
||||
seal-home auth hose-link [--corp <corpId>] [--expire 7200]
|
||||
seal-home source config [--corp <corpId>]
|
||||
|
||||
Reference in new issue
Block a user