Files
plugins/packages/orcaterm/refresh-token.test.mjs
T
yetone db307844e7 orcaterm 0.1.6: keep the WeChat sign-in's refresh token and renew from it (#675)
orinsid's desktop-client logs (#675, 11-04) show OrcaTerm 1.6.8 renewing
from a long-lived oauth_refresh_token_v2 on a 110-minute schedule, while
the plugin dropped the exchange's refresh token (`refresh: r.accessToken`)
and renewed from the two-hour access token, which OrcaTerm answers
SESSION_EXPIRED for 30 minutes into an idle sign-in.

OrcaTerm's own bundle (orcaterm.cloud.tencent.com/terminal,
orcaterm_components/orcaterm_app, fetched 11-04) settles the protocol:
- the WeChat exchange sends {code, state, client: "desktop"} from the
  desktop client, and reads data.refreshToken from the reply;
- the client keeps a refresh token only when it matches
  /^<uuid>\.\d{1,6}\.[A-Za-z0-9_-]{43}$/ and its uuid is the access
  token's jti (persistLoginRefreshToken);
- OAuthRefreshToken sends {refreshToken} when it has one, else
  {accessToken}.

The plugin now does the same: the exchange sends client "desktop", a
matching refresh token is the sign-in's `refresh`, renewal sends it (an
access token past its expiry no longer stops a renewal that has one),
and a reply's RefreshToken replaces it, else it is kept. A sign-in from
before 0.1.6 has the access token in `refresh` and renews as before
until the next scan.

Run (mote): packages/orcaterm `bun test`, 121 pass / 0 fail, including
the new refresh-token.test.mjs (6 tests).
Broken by hand: dropped client "desktop", sent {accessToken} always and
saved `refresh: r.accessToken` again: 4 of the 6 new tests went red (the
two that stay green check a mismatched token isn't kept and the
pre-0.1.6 path).
Not tried: a real WeChat sign-in. No OrcaTerm account here, so whether
the reply to client "desktop" carries refreshToken for this flow, and
whether renewal rotates it, is from the vendor's bundle, not a capture.
If the reply has none, the sign-in falls back to the 0.1.5 behaviour.
2026-11-04 12:37:29 +08:00

110 lines
4.9 KiB
JavaScript

// #675 (orinsid, 2026-11-04): OrcaTerm's desktop client keeps a long-lived
// refresh token (oauth_refresh_token_v2) and renews from it, while the
// plugin threw the exchange's refresh token away and renewed from the
// two-hour access token, which OrcaTerm stops renewing once its web session
// is gone. The client's own bundle sends client "desktop" to
// /oauth/wechat/exchange, keeps data.refreshToken when its jti prefix is the
// access token's jti, and calls OAuthRefreshToken with {refreshToken} when it
// has one, {accessToken} otherwise.
import { test, afterEach } from "bun:test"
import assert from "node:assert/strict"
import { OrcaTermAuthPlugin } from "./index.mjs"
const realFetch = globalThis.fetch
afterEach(() => { globalThis.fetch = realFetch })
const json = data => Response.json({ code: 0, data })
const JTI = "0d4c2f7e-9b1a-4c3d-8e5f-6a7b8c9d0e1f"
const tail = "A".repeat(43)
const access = (jti, exp = Math.floor(Date.now() / 1000) + 7200) =>
`eyJhbGciOiJub25lIn0.${Buffer.from(JSON.stringify({ userId: "u1", jti, exp })).toString("base64url")}.test`
const refreshOf = (jti, n = 1) => `${jti}.${n}.${tail}`
async function scan(data, seen) {
globalThis.fetch = async (url, init) => {
if (String(url).endsWith("/oauth/wechat/config")) return json({
appid: "rt-test", redirect_uri: "https://orcaterm.com/oauth/wechat/bridge",
state: "rt-state", scope: "snsapi_login",
})
assert.ok(String(url).endsWith("/oauth/wechat/exchange"))
seen.push(JSON.parse(init.body))
return json(data)
}
const plugin = await OrcaTermAuthPlugin()
const flow = await plugin.auth.methods[0].authorize()
try {
const res = await realFetch(new URL("/cb", flow.url), {
method: "POST", headers: { "content-type": "application/json", origin: new URL(flow.url).origin, connection: "close" },
body: JSON.stringify({ code: "c", state: "rt-state", error: null }),
})
assert.equal(res.status, 200)
return await flow.callback()
} finally { flow.__cap.close() }
}
test("QR sign-in asks for the desktop client's refresh token and keeps it as the sign-in's refresh", async () => {
const seen = []
const r = await scan({ userId: "u1", providerUsername: "A", accessToken: access(JTI), refreshToken: refreshOf(JTI), expiresIn: 7200 }, seen)
assert.equal(r.type, "success")
assert.equal(seen[0].client, "desktop")
assert.equal(r.refresh, refreshOf(JTI))
assert.equal(r.access, access(JTI))
})
test("a refresh token issued with another access token, or none, isn't kept", async () => {
for (const refreshToken of [refreshOf("11111111-2222-3333-4444-555555555555"), "not-a-token", undefined]) {
const r = await scan({ userId: "u1", providerUsername: "A", accessToken: access(JTI), refreshToken, expiresIn: 7200 }, [])
assert.equal(r.type, "success")
assert.equal(r.refresh, r.access)
}
})
async function renew(stored, reply) {
const sent = []
globalThis.fetch = async (url, init) => {
assert.equal(String(url), "https://orcaterm.com/cgi/api?action=OAuthRefreshToken")
sent.push(JSON.parse(init.body).data)
return json(reply)
}
const plugin = await OrcaTermAuthPlugin()
return { next: await plugin.auth.refresh(stored), sent }
}
test("renewal sends the refresh token, not the access token, and keeps the one the reply rotates to", async () => {
const next2 = "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"
const { next, sent } = await renew(
{ type: "oauth", access: access(JTI), refresh: refreshOf(JTI), expires: Date.now() + 60e3 },
{ AccessToken: access(next2), RefreshToken: refreshOf(next2, 2), ExpiresIn: 7200 },
)
assert.deepEqual(sent, [{ refreshToken: refreshOf(JTI) }])
assert.equal(next.access, access(next2))
assert.equal(next.refresh, refreshOf(next2, 2))
})
test("a reply with no refresh token keeps the one the sign-in has", async () => {
const { next } = await renew(
{ type: "oauth", access: access(JTI), refresh: refreshOf(JTI), expires: Date.now() + 60e3 },
{ accessToken: access("aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"), expiresIn: 7200 },
)
assert.equal(next.refresh, refreshOf(JTI))
})
test("an access token past its expiry still renews from the refresh token", async () => {
const old = access(JTI, Math.floor(Date.now() / 1000) - 3600)
const { next, sent } = await renew(
{ type: "oauth", access: old, refresh: refreshOf(JTI), expires: Date.now() - 3600e3 },
{ accessToken: access("aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"), expiresIn: 7200 },
)
assert.deepEqual(sent, [{ refreshToken: refreshOf(JTI) }])
assert.ok(next?.access)
})
test("a sign-in from before 0.1.6 renews from its access token as it did", async () => {
const before = access(JTI)
const { next, sent } = await renew(
{ type: "oauth", access: before, refresh: before, expires: Date.now() + 60e3 },
{ accessToken: access("aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"), expiresIn: 7200 },
)
assert.deepEqual(sent, [{ accessToken: before }])
assert.equal(next.refresh, next.access)
})