@shad70w0 on X: "magpie + Trae 插件实测没办法走企业订阅" - a Trae enterprise
subscription couldn't be used through this plugin. The sign-in URL sent
hide_saas_login=true (copied from the relays in 6c048c7), which hides the
enterprise/SSO sign-in on www.trae.cn/authorization and
www.trae.ai/authorization, so an enterprise user could only sign in as a
personal account.
What changed:
- the authorization URL sends hide_saas_login=false.
- traeHost: a host a sign-in names is taken only when it is a well-formed
https host (no port, credentials, query) on trae.cn, trae.ai or
mchost.guru; anything else is ignored and the consumer host used, as
before.
- Trae CN: a sign-in host on mchost.guru / trae-api-*, or another trae.cn
host than www/api/grow*, is the account's model host. Trae Global: a
trae.ai host other than www/api-*/grow* is too, after the existing
US/coresg rules. Personal hosts (api.trae.cn, api-us-east, api-sg-central,
growsg-normal, coresg-normal) route as before.
- the sign-in's host (host), a token host the callback names (authHost /
auth_host / userInfo.AuthHost, on the realm's own domain only) and the
userInfo's tenant/enterprise/login-type fields (account; no field that
looks like a secret) are kept in the auth record. Renewal (sign-in and
refresh) goes to authHost when one is kept. Nothing new is sent.
- README: enterprise sign-in is offered, not tried with a real enterprise
account.
Run: bun test in packages/trae (103 pass, 6 new in enterprise.test.mjs);
the whole repo's bun test with HOME under a scratch TMPDIR (1168 pass, 0
fail); bun scripts/check.mjs. Each change broken by hand turns
enterprise.test.mjs red: hide_saas_login back to true, no enterprise CN
host, no domain check, http allowed, refresh at the consumer host, no
account kept, api.trae.cn taken as a model host, a secret-looking field kept.
Not tried: no real enterprise account. Which host(s) and fields an
enterprise sign-in sends back is unknown; the hosts in the tests are made
up, and whether the callback ever names an auth host is a guess.
111 lines
5.8 KiB
JavaScript
111 lines
5.8 KiB
JavaScript
// An enterprise (SaaS) subscription signs in on the same authorization
|
|
// page as a personal one, when the page is not told to hide that sign-in
|
|
// (@shad70w0 on X). A host the sign-in names on Trae's own domains is kept
|
|
// and used; one anywhere else is not, and a personal sign-in is as before.
|
|
// Not tried with a real enterprise account: the hosts below are made up.
|
|
import "./nonet.mjs"
|
|
import { afterEach, expect, test } from "bun:test"
|
|
import { TraeCNAuthPlugin, _internal } from "./index.mjs"
|
|
import { fakeTrae, signedIn } from "./fake.mjs"
|
|
|
|
const CN = _internal.SITES["trae-cn"]
|
|
const GLOBAL = _internal.SITES["trae-global"]
|
|
|
|
let f
|
|
afterEach(() => f?.close())
|
|
|
|
async function start() {
|
|
const hooks = await TraeCNAuthPlugin({ client: {} })
|
|
const a = await hooks.auth.methods[0].authorize()
|
|
return { a, q: new URL(a.url).searchParams, cb: new URL(new URL(a.url).searchParams.get("auth_callback_url")) }
|
|
}
|
|
|
|
async function signIn(info, extra = {}) {
|
|
const { a, cb } = await start()
|
|
const jwt = { Token: "jwt-1", RefreshToken: "r-1", TokenExpireAt: Math.floor(Date.now() / 1000) + 7200, ClientID: "ono9krqynydwx5" }
|
|
const url = new URL(cb)
|
|
url.searchParams.set("userJwt", JSON.stringify(jwt))
|
|
url.searchParams.set("userInfo", JSON.stringify(info))
|
|
for (const [k, v] of Object.entries(extra)) url.searchParams.set(k, v)
|
|
await fetch(url)
|
|
return a.callback()
|
|
}
|
|
|
|
test("the sign-in page doesn't hide the enterprise (SaaS) sign-in", async () => {
|
|
f = fakeTrae()
|
|
const { a, q } = await start()
|
|
expect(q.get("hide_saas_login")).not.toBe("true")
|
|
await fetch(new URL(q.get("auth_callback_url")) + "?error=x")
|
|
await a.callback()
|
|
})
|
|
|
|
test("only an https host on Trae's own domains is taken", () => {
|
|
const t = _internal.traeHost
|
|
expect(t("https://trae-api-cn.mchost.guru")).toBe("https://trae-api-cn.mchost.guru")
|
|
expect(t("https://ent-acme.trae.cn/")).toBe("https://ent-acme.trae.cn")
|
|
expect(t("https://ent.trae.ai")).toBe("https://ent.trae.ai")
|
|
for (const bad of ["http://ent.trae.cn", "https://trae.cn.evil.com", "https://eviltrae.cn", "https://trae-api-cn.evil.com", "https://u:p@ent.trae.cn", "https://ent.trae.cn:8443", "https://ent.trae.cn/?x=1", "ent.trae.cn", "", undefined]) {
|
|
expect(t(bad)).toBe("")
|
|
}
|
|
})
|
|
|
|
test("a sign-in naming an enterprise Trae CN host keeps it, and requests go there", async () => {
|
|
f = fakeTrae()
|
|
const info = { UserID: "u-e", ScreenName: "Acme", Host: "https://ent-acme.trae.cn", TenantID: "t-1", LoginType: "saas", TenantToken: "no" }
|
|
const got = await signIn(info)
|
|
expect(got.type).toBe("success")
|
|
expect(got.api).toBe("https://ent-acme.trae.cn")
|
|
expect(got.host).toBe("https://ent-acme.trae.cn")
|
|
// the account's kind is kept, never a field that looks like a secret
|
|
expect(got.account).toEqual({ TenantID: "t-1", LoginType: "saas" })
|
|
expect(CN.apiOf(got)).toBe("https://ent-acme.trae.cn")
|
|
// a request goes to that host (nonet stops it before it leaves)
|
|
const hooks = await TraeCNAuthPlugin({ client: {} })
|
|
const opts = await hooks.auth.loader(async () => signedIn({ api: got.api }))
|
|
const e = await opts.fetch(opts.baseURL + "/chat/completions", { method: "POST", body: JSON.stringify({ model: "glm-5", messages: [{ role: "user", content: "hi" }] }) }).catch((e) => e)
|
|
expect(String(e?.message)).toContain("https://ent-acme.trae.cn/api/agent/v3/llm_utils_chat")
|
|
})
|
|
|
|
test("a personal sign-in is as before", async () => {
|
|
f = fakeTrae()
|
|
const got = await signIn({ UserID: "u-1", ScreenName: "Ann", Region: "CN", Host: "https://api.trae.cn" })
|
|
expect(got.api).toBeUndefined()
|
|
expect(got.authHost).toBeUndefined()
|
|
expect(got.account).toBeUndefined()
|
|
expect(CN.apiOf(got)).toBe(CN.hosts.api)
|
|
expect(CN.apiOf({ api: "https://trae-api-cn.mchost.guru" })).toBe("https://trae-api-cn.mchost.guru")
|
|
for (const h of ["https://www.trae.cn", "https://api.trae.cn", "https://growcn-normal.trae.cn"]) expect(CN.modelHost(h)).toBe(false)
|
|
// Global's personal hosts route as they did
|
|
expect(GLOBAL.apiOf({ api: "https://api-us-east.trae.ai" })).toBe(GLOBAL.hosts.us)
|
|
expect(GLOBAL.apiOf({ api: "https://api-sg-central.trae.ai" })).toBe(GLOBAL.hosts.api)
|
|
expect(GLOBAL.apiOf({ api: "https://growsg-normal.trae.ai" })).toBe(GLOBAL.hosts.api)
|
|
expect(GLOBAL.apiOf({ api: "https://coresg-normal.trae.ai" })).toBe("https://coresg-normal.trae.ai")
|
|
expect(GLOBAL.apiOf({ api: "https://ent-acme.trae.ai" })).toBe("https://ent-acme.trae.ai")
|
|
})
|
|
|
|
test("a host off Trae's domains is refused, and the consumer one is used", async () => {
|
|
f = fakeTrae()
|
|
const got = await signIn({ UserID: "u-x", ScreenName: "X", Host: "https://trae-api-cn.evil.com" }, { authHost: "https://api.evil.com" })
|
|
expect(got.type).toBe("success")
|
|
expect(got.api).toBeUndefined()
|
|
expect(got.host).toBeUndefined()
|
|
expect(got.authHost).toBeUndefined()
|
|
expect(CN.apiOf({ api: "https://trae-api-cn.evil.com" })).toBe(CN.hosts.api)
|
|
expect(CN.apiOf({ api: "http://ent-acme.trae.cn" })).toBe(CN.hosts.api)
|
|
expect(GLOBAL.apiOf({ api: "https://coresg.evil.com" })).toBe(GLOBAL.hosts.api)
|
|
expect(GLOBAL.modelHost("https://trae.ai.evil.com")).toBe(false)
|
|
})
|
|
|
|
test("a token host the sign-in named is where the token is renewed", async () => {
|
|
f = fakeTrae()
|
|
const got = await signIn({ UserID: "u-e", ScreenName: "Acme", Host: "https://ent-acme.trae.cn" }, { authHost: "https://auth-acme.trae.cn" })
|
|
expect(got.authHost).toBe("https://auth-acme.trae.cn")
|
|
const hooks = await TraeCNAuthPlugin({ client: {} })
|
|
// nonet stops the renewal before it leaves, naming where it went
|
|
const e = await hooks.auth.refresh(signedIn({ authHost: got.authHost, refresh: "r-ent" })).catch((e) => e)
|
|
expect(String(e?.message)).toContain("https://auth-acme.trae.cn/")
|
|
// a token host off the site's domain is not taken
|
|
const g = await signIn({ UserID: "u-e", ScreenName: "Acme" }, { authHost: "https://auth.trae.ai" })
|
|
expect(g.authHost).toBeUndefined()
|
|
})
|