diff --git a/packages/trae/README.md b/packages/trae/README.md index 8276c27..0d86a51 100644 --- a/packages/trae/README.md +++ b/packages/trae/README.md @@ -30,8 +30,9 @@ A fix to that machinery reaches both. ## Sign-in -One way per provider, **Trae CN account (browser)** or **Trae Global account -(browser)**. The plugin opens that realm's authorization page +A personal account signs in with **Trae CN account (browser)** or **Trae +Global account (browser)**; an enterprise seat has its own way (see +Enterprise below). The plugin opens that realm's authorization page (`www.trae.cn/authorization` or `www.trae.ai/authorization`, the IDE's client `ono9krqynydwx5`). Sign in there and allow the sign-in; the page then sends the browser back to a callback on `http://127.0.0.1:/authorize` — it accepts @@ -51,6 +52,21 @@ Trae Global's stays 19. The daily check-in goes on a 15-digit id made from it ### Enterprise (SaaS) accounts +Each provider has a second way, **Trae CN enterprise account (browser)** or +**Trae Global enterprise account (browser)**, since 0.2.46. It opens the +realm's enterprise console's own authorization page +(`console.enterprise.trae.cn/authorization` or +`console.enterprise.trae.ai/authorization`) with the same parameters and +callback. There the seat signs in with its TRAE enterprise email and +password, the Doubao/Feishu tab, or the company's SSO. Since November 2026 +trae.cn's own page sends a signed-out browser to a Doubao/Feishu account +sign-in (accounts.doubao.com) that has no way to an enterprise seat, so an +enterprise account signing in again there was not recognised (@yalifesign +on X). The console's page calls back with `userJwt`, `userInfo`, +`refreshToken` and `host` (its Api host), read as below; a callback naming +no host is taken as the console's seat. Read from the consoles' page code; +**not tried with a real enterprise seat**. + Since 0.2.10 the authorization page also offers Trae's enterprise (SaaS / SSO) sign-in; before, the plugin asked the page to hide it (`hide_saas_login`), so an enterprise subscription could only sign in as a personal account. A host diff --git a/packages/trae/enterprise.test.mjs b/packages/trae/enterprise.test.mjs index dc298fb..ab0238b 100644 --- a/packages/trae/enterprise.test.mjs +++ b/packages/trae/enterprise.test.mjs @@ -5,7 +5,7 @@ // 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 { TraeCNAuthPlugin, TraeGlobalAuthPlugin, _internal } from "./index.mjs" import { fakeTrae, json, signedIn, sse } from "./fake.mjs" // the console's own answer, as a real enterprise account's is: the seat @@ -67,6 +67,83 @@ test("the sign-in page doesn't hide the enterprise (SaaS) sign-in", async () => await a.callback() }) +// Since November 2026 trae.cn's authorization page sends a signed-out +// browser to a Doubao/Feishu account sign-in with no way to an enterprise +// one (@yalifesign on X). The enterprise way starts on the realm's console's +// own authorization page, which takes the same parameters and calls back the +// same way, naming its Api host (console.enterprise.trae.cn's code, read +// 2026-11-19; not tried with a real seat). +test("the enterprise way opens the console's authorization page with the IDE's parameters", async () => { + for (const [make, site] of [[TraeCNAuthPlugin, CN], [TraeGlobalAuthPlugin, GLOBAL]]) { + const hooks = await make({ client: {} }) + const m = hooks.auth.methods[1] + expect(m.type).toBe("oauth") + expect(m.label).toBe(`${site.name} enterprise account (browser)`) + const a = await m.authorize() + const u = new URL(a.url) + expect(u.origin).toBe(site.hosts.console) + expect(u.pathname).toBe("/authorization") + // what the console's page checks before it calls back + for (const k of ["auth_from", "login_trace_id", "device_id", "auth_callback_url"]) expect(u.searchParams.get(k)).toBeTruthy() + expect(["trae", "solo"]).toContain(u.searchParams.get("auth_from")) + expect(new URL(u.searchParams.get("auth_callback_url")).hostname).toBe("127.0.0.1") + await fetch(u.searchParams.get("auth_callback_url") + "?error=x") + expect(await a.callback()).toEqual({ type: "failed", error: "x" }) + // the personal way is still the consumer site's + const p = await hooks.auth.methods[0].authorize() + expect(new URL(p.url).origin).toBe(site.hosts.web) + await fetch(new URL(p.url).searchParams.get("auth_callback_url") + "?error=x") + await p.callback() + } +}) + +test("the console's callback signs in an enterprise seat on its console", async () => { + f = fakeTrae() + const hooks = await TraeCNAuthPlugin({ client: {} }) + const a = await hooks.auth.methods[1].authorize() + const q = new URL(a.url).searchParams + // the console's callback: _t() in its authorization page + const url = new URL(q.get("auth_callback_url")) + const exp = Math.floor(Date.now() / 1000) + 7200 + for (const [k, v] of Object.entries({ + scope: "saas", host: ENT, consoleHost: ENT, coreHost: ENT, isRedirect: "true", loginTraceID: q.get("login_trace_id"), + userJwt: JSON.stringify({ Token: "jwt-e", TokenExpireAt: exp }), + userInfo: JSON.stringify({ UserID: "u-e", ScreenName: "Acme", TenantID: "t-1" }), + refreshToken: "r-e", refreshExpireAt: String(exp + 86400), data: "r-e", + })) url.searchParams.set(k, v) + await fetch(url) + const got = await a.callback() + expect(got.type).toBe("success") + expect(got.access).toBe("jwt-e") + expect(got.refresh).toBe("r-e") + expect(got.host).toBe(ENT) + expect(_internal.isEnterprise(got, CN)).toBe(true) + expect(CN.apiOf(got)).toBe(CN.hosts.console) + expect(got.deviceId).toBe(q.get("device_id")) +}) + +test("a console callback naming no host keeps its console as the seat's host", async () => { + f = fakeTrae() + const hooks = await TraeCNAuthPlugin({ client: {} }) + const a = await hooks.auth.methods[1].authorize() + const url = new URL(new URL(a.url).searchParams.get("auth_callback_url")) + url.searchParams.set("consoleHost", ENT) + url.searchParams.set("userJwt", JSON.stringify({ Token: "jwt-e", TokenExpireAt: Math.floor(Date.now() / 1000) + 7200 })) + url.searchParams.set("refreshToken", "r-e") + await fetch(url) + const got = await a.callback() + expect(got.host).toBe(ENT) + expect(_internal.isEnterprise(got, CN)).toBe(true) + // the consumer page's callback is not read that way + const p = await hooks.auth.methods[0].authorize() + const pu = new URL(new URL(p.url).searchParams.get("auth_callback_url")) + pu.searchParams.set("consoleHost", ENT) + pu.searchParams.set("userJwt", JSON.stringify({ Token: "jwt-p", TokenExpireAt: Math.floor(Date.now() / 1000) + 7200 })) + pu.searchParams.set("refreshToken", "r-p") + await fetch(pu) + expect(_internal.isEnterprise(await p.callback(), CN)).toBe(false) +}) + 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") diff --git a/packages/trae/index.mjs b/packages/trae/index.mjs index 47a06ae..1a9ea87 100644 --- a/packages/trae/index.mjs +++ b/packages/trae/index.mjs @@ -755,10 +755,12 @@ const page = (ok, title, text) => ` done, } @@ -4383,7 +4397,10 @@ const makePlugin = (site) => async ({ client }) => { }, } }, - methods: [{ type: "oauth", label: `${site.name} account (browser)`, authorize: () => browserSignIn(site) }], + methods: [ + { type: "oauth", label: `${site.name} account (browser)`, authorize: () => browserSignIn(site) }, + { type: "oauth", label: `${site.name} enterprise account (browser)`, authorize: () => browserSignIn(site, { enterprise: true }) }, + ], }, } } diff --git a/packages/trae/package.json b/packages/trae/package.json index affe229..f43b1d5 100644 --- a/packages/trae/package.json +++ b/packages/trae/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-trae-auth", - "version": "0.2.45", + "version": "0.2.46", "description": "OpenCode provider plugins for Trae: Trae CN (trae.cn) and Trae Global (trae.ai), free tier included, with Trae's own browser sign-in", "type": "module", "main": "./opencode.mjs",