qoder 0.2.7: the account's fetch sends Qoder's campaigns pages (the daily credits claim) as the account — GET <openapi>/sash/api/v1/me/campaigns and POST …/{id}/claim, on the device token as usage is read (a refused one rotated once), for qoder (openapi.qoder.sh) and qoder-cn (openapi.qoder.com.cn); any other page there is still refused. For magpie's Qoder check-in (Discord, ARNO, after wallechfox/qoder-checkin). checkin.test.mjs is new (4 of its 5 fail on 0.2.6); bun test (54 pass) and check.mjs qoder pass. Not tried against Qoder itself (no Qoder account here).
This commit is contained in:
@@ -151,6 +151,27 @@ Neither magpie's built-in Qoder CN nor this plugin has been checked against
|
||||
every kind of qoder.cn account. If a sign-in or a chat fails, please open
|
||||
an issue with the error.
|
||||
|
||||
## Daily check-in
|
||||
|
||||
Qoder gives credits for a daily claim (the Qoder client's campaign
|
||||
`CLAIM_BENEFIT`, 100 Credits a day as reported), on both sites. magpie can
|
||||
press it once a day for each account when its Qoder check-in switch is on
|
||||
(Settings). It asks these pages through this plugin's fetch (since 0.2.7),
|
||||
which sends them as the account, on the device token (`Bearer`,
|
||||
`Cosy-ClientType: 10`) as usage is read; a refused device token is
|
||||
rotated once and the page asked again.
|
||||
|
||||
- **List:** `GET <openapi>/sash/api/v1/me/campaigns`: `campaigns[]` with
|
||||
`campaignId`, `actionType`, `claimStatus` (`CLAIMABLE`, `CLAIMED`),
|
||||
`startAt`, `endAt`, `benefit.amount`.
|
||||
- **Claim:** `POST <openapi>/sash/api/v1/me/campaigns/{id}/claim`, body
|
||||
`{}`: `status` `CLAIMED` (or under `data`).
|
||||
- `<openapi>` is `openapi.qoder.sh` (Qoder) or `openapi.qoder.com.cn`
|
||||
(Qoder CN). Other pages on those hosts are still refused.
|
||||
|
||||
Taken from [wallechfox/qoder-checkin](https://github.com/wallechfox/qoder-checkin),
|
||||
which a user reports works. Not yet tried here with a real Qoder account.
|
||||
|
||||
## Not here
|
||||
|
||||
- Qoder's usage and quota display.
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
// The account's fetch sends Qoder's campaigns pages (the daily check-in) as
|
||||
// the account, on the device token, for both sites; anything else on those
|
||||
// hosts is still refused as not a chat.
|
||||
import { afterEach, expect, test } from "bun:test"
|
||||
import { QoderAuthPlugin, QoderCNAuthPlugin, _internal } from "./index.mjs"
|
||||
|
||||
const real = globalThis.fetch
|
||||
afterEach(() => (globalThis.fetch = real))
|
||||
|
||||
const account = () => ({
|
||||
type: "oauth",
|
||||
access: "jt-one",
|
||||
refresh: "rt-one",
|
||||
expires: Date.now() + 3_600_000,
|
||||
accountId: "one@x",
|
||||
uid: "u1",
|
||||
deviceToken: "dt-old",
|
||||
deviceRefresh: "drt-old",
|
||||
})
|
||||
|
||||
const json = (v, status = 200) => new Response(JSON.stringify(v), { status })
|
||||
|
||||
async function loader(make, serve) {
|
||||
let auth = account()
|
||||
const seen = []
|
||||
globalThis.fetch = async (url, init = {}) => {
|
||||
const u = new URL(String(url))
|
||||
seen.push({ origin: u.origin, path: u.pathname, method: init.method ?? "GET", auth: init.headers?.Authorization, body: init.body })
|
||||
return serve(u, init)
|
||||
}
|
||||
const client = { auth: { set: async ({ body }) => (auth = body) } }
|
||||
const hooks = await make({ client })
|
||||
const l = await hooks.auth.loader(async () => auth)
|
||||
return { fetch: l.fetch, seen, auth: () => auth }
|
||||
}
|
||||
|
||||
const LIST = { campaigns: [{ campaignId: "c1", actionType: "CLAIM_BENEFIT", claimStatus: "CLAIMABLE", benefit: { amount: 100 } }] }
|
||||
|
||||
for (const [make, origin] of [
|
||||
[QoderAuthPlugin, "https://openapi.qoder.sh"],
|
||||
[QoderCNAuthPlugin, "https://openapi.qoder.com.cn"],
|
||||
]) {
|
||||
test(`${origin}: the campaigns list and a claim go out on the device token`, async () => {
|
||||
const p = await loader(make, (u, init) => {
|
||||
if (u.pathname === "/sash/api/v1/me/campaigns") return json(LIST)
|
||||
if (u.pathname === "/sash/api/v1/me/campaigns/c1/claim") return json({ data: { status: "CLAIMED", benefit: { amount: 100 } } })
|
||||
return new Response("", { status: 404 })
|
||||
})
|
||||
const list = await p.fetch(origin + "/sash/api/v1/me/campaigns", { method: "GET" })
|
||||
expect(list.status).toBe(200)
|
||||
expect(await list.json()).toEqual(LIST)
|
||||
const claim = await p.fetch(origin + "/sash/api/v1/me/campaigns/c1/claim", { method: "POST", body: "{}" })
|
||||
expect(claim.status).toBe(200)
|
||||
expect((await claim.json()).data.status).toBe("CLAIMED")
|
||||
expect(p.seen).toEqual([
|
||||
{ origin, path: "/sash/api/v1/me/campaigns", method: "GET", auth: "Bearer dt-old", body: undefined },
|
||||
{ origin, path: "/sash/api/v1/me/campaigns/c1/claim", method: "POST", auth: "Bearer dt-old", body: "{}" },
|
||||
])
|
||||
})
|
||||
}
|
||||
|
||||
test("a refused device token is rotated, saved, and the claim asked again", async () => {
|
||||
const p = await loader(QoderAuthPlugin, (u, init) => {
|
||||
if (u.pathname === "/api/v1/deviceToken/refresh") return json({ device_token: "dt-new", refresh_token: "drt-new" })
|
||||
if (u.pathname === "/sash/api/v1/me/campaigns/c1/claim")
|
||||
return init.headers.Authorization === "Bearer dt-new" ? json({ status: "CLAIMED" }) : new Response("", { status: 401 })
|
||||
return new Response("", { status: 404 })
|
||||
})
|
||||
const res = await p.fetch("https://openapi.qoder.sh/sash/api/v1/me/campaigns/c1/claim", { method: "POST", body: "{}" })
|
||||
expect(res.status).toBe(200)
|
||||
expect(p.auth().deviceToken).toBe("dt-new")
|
||||
expect(p.auth().deviceRefresh).toBe("drt-new")
|
||||
})
|
||||
|
||||
test("only the campaigns pages of the account's own site pass", () => {
|
||||
const { campaignPage, SITES } = _internal
|
||||
expect(campaignPage(SITES.qoder, "https://openapi.qoder.sh/sash/api/v1/me/campaigns")).toBe(true)
|
||||
expect(campaignPage(SITES.qoder, "https://openapi.qoder.sh/sash/api/v1/me/campaigns/x-1/claim")).toBe(true)
|
||||
expect(campaignPage(SITES.qoder, "https://openapi.qoder.com.cn/sash/api/v1/me/campaigns")).toBe(false)
|
||||
expect(campaignPage(SITES.qoder, "https://openapi.qoder.sh/sash/api/v2/me/usage")).toBe(false)
|
||||
expect(campaignPage(SITES.qoder, "https://openapi.qoder.sh/sash/api/v1/me/campaigns/a/b/claim")).toBe(false)
|
||||
expect(campaignPage(SITES.qoder, "https://evil.example/sash/api/v1/me/campaigns")).toBe(false)
|
||||
expect(campaignPage(SITES["qoder-cn"], "https://openapi.qoder.com.cn/sash/api/v1/me/campaigns/c/claim")).toBe(true)
|
||||
})
|
||||
|
||||
test("another page on the openapi host is still refused", async () => {
|
||||
const p = await loader(QoderAuthPlugin, () => json({}))
|
||||
const res = await p.fetch("https://openapi.qoder.sh/api/v1/userinfo", { method: "GET" })
|
||||
expect(res.status).toBe(404)
|
||||
expect(p.seen).toEqual([])
|
||||
})
|
||||
@@ -953,6 +953,33 @@ async function fetchUsage(site, deviceToken) {
|
||||
return env
|
||||
}
|
||||
|
||||
// CAMPAIGNS is where Qoder lists the account's campaigns — the daily
|
||||
// credits among them (actionType CLAIM_BENEFIT) — and claims one
|
||||
// (POST …/{id}/claim), as Qoder's client does, on the device token.
|
||||
const CAMPAIGNS = "/sash/api/v1/me/campaigns"
|
||||
|
||||
// campaignPage: url is the site's campaigns page or a claim under it, which
|
||||
// the account's fetch sends as the account rather than as a chat.
|
||||
function campaignPage(site, url) {
|
||||
try {
|
||||
const u = new URL(url)
|
||||
return u.origin === new URL(site.openapi).origin && (u.pathname === CAMPAIGNS || /^\/sash\/api\/v1\/me\/campaigns\/[^/]+\/claim$/.test(u.pathname))
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// campaignCall is url asked with the device token; Qoder's answer comes back
|
||||
// as it is.
|
||||
async function campaignCall(url, method, body, deviceToken, signal) {
|
||||
return fetch(url, {
|
||||
method,
|
||||
headers: { Accept: "application/json", "Content-Type": "application/json", Authorization: `Bearer ${deviceToken}`, "Cosy-ClientType": "10", "User-Agent": "Qoder" },
|
||||
body: method === "GET" ? undefined : body || "{}",
|
||||
signal: signal ?? AbortSignal.timeout(20_000),
|
||||
})
|
||||
}
|
||||
|
||||
// refreshDevice trades the device refresh token for a new pair; it rotates
|
||||
// too, so the caller saves it.
|
||||
async function refreshDevice(site, refresh, chat = false) {
|
||||
@@ -1174,6 +1201,27 @@ const makePlugin = (site) => async ({ client }) => {
|
||||
}
|
||||
}
|
||||
|
||||
// campaign is a campaigns page (the daily check-in) asked as the account:
|
||||
// a refused device token is rotated once, as usage does, and asked again
|
||||
const campaign = async (getAuth, input, url, init) => {
|
||||
const body = await bodyText(input, init)
|
||||
let cred
|
||||
try {
|
||||
cred = await fresh(getAuth)
|
||||
} catch (e) {
|
||||
return signedInError(e)
|
||||
}
|
||||
const method = String(init.method ?? input?.method ?? "GET").toUpperCase()
|
||||
try {
|
||||
let res = await campaignCall(url, method, body, cred.deviceToken, init.signal)
|
||||
if (res.status === 401 || res.status === 403) res = await campaignCall(url, method, body, await deviceToken(getAuth, cred.deviceToken), init.signal)
|
||||
return signed(res, renewed(cred))
|
||||
} catch (e) {
|
||||
if (e?.expired) return signedInError(e)
|
||||
return errorResponse({ status: 502, message: e?.message ?? String(e) })
|
||||
}
|
||||
}
|
||||
|
||||
const signedInError = (e) =>
|
||||
errorResponse({
|
||||
status: e instanceof SignInGone ? 401 : 502,
|
||||
@@ -1244,6 +1292,7 @@ const makePlugin = (site) => async ({ client }) => {
|
||||
// every chat completion written as Qoder's own request
|
||||
async fetch(input, init = {}) {
|
||||
const url = typeof input === "string" ? input : input instanceof URL ? input.href : input.url
|
||||
if (campaignPage(site, url)) return campaign(getAuth, input, url, init)
|
||||
if (!/\/chat\/completions$/.test(new URL(url).pathname))
|
||||
return errorResponse({ status: 404, message: "only chat completions are served" })
|
||||
let chat
|
||||
@@ -1309,4 +1358,4 @@ export const QoderAuthPlugin = makePlugin(SITES.qoder)
|
||||
export const QoderCNAuthPlugin = makePlugin(SITES["qoder-cn"])
|
||||
|
||||
// for tests
|
||||
export const _internal = { parseUsage, gfmt, when, failure, modelInfo, qoderBody, SITES, decodeBody, events }
|
||||
export const _internal = { campaignPage, parseUsage, gfmt, when, failure, modelInfo, qoderBody, SITES, decodeBody, events }
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@magpie-community/opencode-qoder-auth",
|
||||
"version": "0.2.6",
|
||||
"version": "0.2.7",
|
||||
"description": "Qoder subscriptions in OpenCode and magpie: Qoder (qoder.com) and Qoder CN (qoder.cn)",
|
||||
"type": "module",
|
||||
"main": "./index.mjs",
|
||||
|
||||
Reference in New Issue
Block a user