mimo 0.1.3: two accounts signing on at once each keep their own session — one renewal was shared by every account, so an account could get another's session and show its plan and allowance
This commit is contained in:
+10
-5
@@ -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
|
||||
|
||||
@@ -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" })
|
||||
|
||||
Reference in New Issue
Block a user