mintonight found the cause on #808: a JWT renewed at /cloudide/api/v3/trae/oauth/ExchangeToken carries no device, and Trae CN answers its check-in claim 9074 every time, while the client's own token is let through. Trae CN now renews as its clients do: POST /trae/api/v3/oauth/ExchangeToken with DeviceInfo (the device id, machine id and a per-account ECDSA P-256 public key, PEM) and DeviceProof (the key's ECDSA-SHA256 signature, base64, of POST, the path, ClientID, RefreshToken, Timestamp and Nonce, one to a line), as TRAE SOLO CN (en1oxy7wnw8j9n, SOLO_PC) first, then Trae CN's IDE (ono9krqynydwx5, IDE_PC). That renewal keeps the refresh token. The key pair is made at sign-in, or at the first renewal of an account signed in before, and kept with the sign-in through auth.refresh's fields; a sign-in's token is bound at once. When Trae turns the bound renewal away, the plugin renews at /cloudide/ as before. Since the refresh token can stay the same, a renewal made here is told from the sign-in by its token rather than by the refresh token. Trae Global is unchanged. device.test.mjs is new: 5 of its 6 tests fail on 0.2.2 (the Trae Global one passes on both). No Trae account to test with; mintonight is asked to verify on #808.
62 lines
2.9 KiB
JavaScript
62 lines
2.9 KiB
JavaScript
// Renewing the JWT with the refresh token, once at a time (Trae spends a
|
|
// refresh token once): magpie's auth.refresh, and the check before a
|
|
// request.
|
|
import "./nonet.mjs"
|
|
import { afterEach, expect, test } from "bun:test"
|
|
import { TraeCNAuthPlugin } from "./index.mjs"
|
|
import { fakeTrae, json, signedIn, sse } from "./fake.mjs"
|
|
|
|
let f
|
|
afterEach(() => f?.close())
|
|
|
|
test("auth.refresh exchanges the refresh token, once for two asking", async () => {
|
|
f = fakeTrae()
|
|
let n = 0
|
|
f.route("POST /cloudide/api/v3/trae/oauth/ExchangeToken", async () => {
|
|
n++
|
|
await Bun.sleep(20)
|
|
return json({ Result: { Token: "jwt-2", RefreshToken: "r-2", TokenExpireAt: 2000000000 } })
|
|
})
|
|
const hooks = await TraeCNAuthPlugin({ client: {} })
|
|
expect(hooks.auth.refreshLead).toBe(10 * 60 * 1000)
|
|
const auth = signedIn({ expires: Date.now() + 60_000 })
|
|
const [x, y] = await Promise.all([hooks.auth.refresh(auth), hooks.auth.refresh(auth)])
|
|
expect(n).toBe(1)
|
|
// the account had no device key: the one made for it comes back with it
|
|
expect(x).toMatchObject({ access: "jwt-2", refresh: "r-2", expires: 2000000000 * 1000 })
|
|
expect(x.devicePublicKey).toContain("BEGIN PUBLIC KEY")
|
|
expect(y).toEqual(x)
|
|
// asked again with the sign-in from before, it gives the one it got
|
|
expect(await hooks.auth.refresh(auth)).toEqual(x)
|
|
expect(n).toBe(1)
|
|
})
|
|
|
|
test("a refresh token Trae turns away is signIn expired", async () => {
|
|
f = fakeTrae()
|
|
f.route("POST /cloudide/api/v3/trae/oauth/ExchangeToken", () => json({ ResponseMetadata: { Error: { Code: "10101", Message: "refresh token is not matched to the client" } } }, 400))
|
|
const hooks = await TraeCNAuthPlugin({ client: {} })
|
|
const e = await hooks.auth.refresh(signedIn()).catch((e) => e)
|
|
expect(e.signIn).toBe("expired")
|
|
expect(e.message).toContain("not matched")
|
|
})
|
|
|
|
test("a request with a token about to end renews it first and saves it", async () => {
|
|
f = fakeTrae()
|
|
f.route("POST /cloudide/api/v3/trae/oauth/ExchangeToken", () => json({ Result: { Token: "jwt-2", RefreshToken: "r-2", TokenExpireAt: Date.now() + 7200_000 } }))
|
|
let auth = null
|
|
f.route("POST /api/agent/v3/llm_utils_chat", (r) => {
|
|
auth = r.headers.get("authorization")
|
|
return sse([["output", { response: "hi" }], ["done", {}]])
|
|
})
|
|
const saved = []
|
|
const hooks = await TraeCNAuthPlugin({ client: { auth: { set: async (x) => saved.push(x) } } })
|
|
const opts = await hooks.auth.loader(async () => signedIn({ expires: Date.now() + 30_000 }))
|
|
const res = await opts.fetch(opts.baseURL + "/chat/completions", { method: "POST", body: JSON.stringify({ model: "glm-5", messages: [{ role: "user", content: "hi" }] }) })
|
|
expect(res.status).toBe(200)
|
|
expect(res.headers.get("X-Magpie-Sign-In")).toBe("renewed")
|
|
expect(auth).toBe("Cloud-IDE-JWT jwt-2")
|
|
expect(saved[0].body.access).toBe("jwt-2")
|
|
expect(saved[0].body.refresh).toBe("r-2")
|
|
expect(saved[0].body.deviceId).toBe("1234567890123456789")
|
|
})
|