From d28f9dfa09d2f7eaa27d2ec581d108f333096beb Mon Sep 17 00:00:00 2001 From: yetone Date: Thu, 1 Oct 2026 02:45:10 +0800 Subject: [PATCH] =?UTF-8?q?mimo=200.1.3:=20two=20accounts=20signing=20on?= =?UTF-8?q?=20at=20once=20each=20keep=20their=20own=20session=20=E2=80=94?= =?UTF-8?q?=20one=20renewal=20was=20shared=20by=20every=20account,=20so=20?= =?UTF-8?q?an=20account=20could=20get=20another's=20session=20and=20show?= =?UTF-8?q?=20its=20plan=20and=20allowance?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/mimo/index.mjs | 15 ++++++++++----- packages/mimo/usage.test.mjs | 28 ++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/packages/mimo/index.mjs b/packages/mimo/index.mjs index bf3b7e4..f50b5d3 100644 --- a/packages/mimo/index.mjs +++ b/packages/mimo/index.mjs @@ -324,7 +324,9 @@ function serverTime(v) { // ---- the plugin ------------------------------------------------------------- export const MimoAuthPlugin = async ({ client }) => { - let renewing = null + // renewals under way, one to an account: two accounts signing on at + // once each keep their own session + const renewing = new Map() const save = async (auth) => { try { @@ -339,8 +341,10 @@ export const MimoAuthPlugin = async ({ client }) => { const a = fromAuth(await getAuth()) if (!a) throw new Error("Xiaomi MiMo: not signed in") if (!force && Object.keys(a.cookies).length && Date.now() < a.expires) return a - if (!renewing) { - renewing = (async () => { + const who = String(a.creds.userId ?? "") + let r = renewing.get(who) + if (!r) { + r = (async () => { try { const s = await session(a.creds, a.creds.base) const issued = Date.now() @@ -352,10 +356,11 @@ export const MimoAuthPlugin = async ({ client }) => { throw e } })().finally(() => { - renewing = null + renewing.delete(who) }) + renewing.set(who, r) } - return renewing + return r } // page asks the MiMo server for one of the account's pages, signing on diff --git a/packages/mimo/usage.test.mjs b/packages/mimo/usage.test.mjs index 9a99924..099c021 100644 --- a/packages/mimo/usage.test.mjs +++ b/packages/mimo/usage.test.mjs @@ -76,6 +76,34 @@ test("the allowance unread leaves the plan alone; a refused sign-in says so", as expect(await p.usage()).toEqual({ error: "42: the Xiaomi MiMo sign-in has expired — sign in again" }) }) +test("two accounts signing on at once each keep their own session", async () => { + const stale = (uid, host) => ({ + type: "oauth", + refresh: JSON.stringify({ userId: uid, passToken: "pt-" + uid, deviceId: "d", base: `https://${host}/api` }), + access: JSON.stringify({ serviceToken: "old-" + uid }), + expires: 0, + accountId: uid, + }) + const asked = [] + globalThis.fetch = async (url, init = {}) => { + const u = new URL(String(url)) + if (u.pathname.endsWith("/user/xiaomi/me")) { + await new Promise((r) => setTimeout(r, 30)) + const uid = u.host[0].toUpperCase() + return new Response(JSON.stringify({ code: 0, data: { userId: uid } }), { headers: { "set-cookie": `serviceToken=st-${uid}; Path=/` } }) + } + asked.push(`${u.host} ${new Headers(init.headers).get("Cookie")}`) + return u.pathname.endsWith("/self") ? json({ code: 0, data: { current: { title: "plan of " + u.host } } }) : json(USAGE) + } + const hooks = await MimoAuthPlugin({ client: { auth: { set: async () => {} } } }) + const A = stale("A", "a.example"), + B = stale("B", "b.example") + const [a, b] = await Promise.all([hooks.auth.usage(async () => A), hooks.auth.usage(async () => B)]) + expect(a.plan).toBe("plan of a.example") + expect(b.plan).toBe("plan of b.example") + for (const x of asked) expect(x).toContain(x.startsWith("a.") ? "serviceToken=st-A" : "serviceToken=st-B") +}) + test("a failure is the page's and the server's word", async () => { let p = await plugin(account(), () => json({ code: 10001, msg: "no such user" })) expect(await p.usage()).toEqual({ error: "Xiaomi MiMo /user/xiaomi/subscription/self: code 10001 no such user" })