@magpie-community/opencode-qoder-auth 0.2.56 → 0.2.57
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.mjs +78 -9
- package/package.json +1 -1
package/index.mjs
CHANGED
|
@@ -223,7 +223,9 @@ const b64url = (buf) => buf.toString("base64url")
|
|
|
223
223
|
async function openapi(site, path, { method = "GET", token, body, query } = {}) {
|
|
224
224
|
const url = new URL(site.openapi + path)
|
|
225
225
|
for (const [k, v] of Object.entries(query ?? {})) url.searchParams.set(k, v)
|
|
226
|
-
|
|
226
|
+
// a site whose own client names itself on these calls (QwenWork) sends
|
|
227
|
+
// the same names
|
|
228
|
+
const headers = { Accept: "application/json", ...(site.clientHeaders?.() ?? {}) }
|
|
227
229
|
if (body) headers["Content-Type"] = "application/json"
|
|
228
230
|
if (token) headers.Authorization = `Bearer ${token}`
|
|
229
231
|
const res = await fetch(url, { method, headers, body: body ? JSON.stringify(body) : undefined, signal: AbortSignal.timeout(20_000) })
|
|
@@ -615,7 +617,8 @@ function siteOf(site, a) {
|
|
|
615
617
|
// enterpriseHost, not the site's vpcDomain (Qoder CN's VPC suffix rule)
|
|
616
618
|
if (a?.enterpriseHost) {
|
|
617
619
|
const base = `https://${a.enterpriseHost}`
|
|
618
|
-
|
|
620
|
+
// and with no redirect_uri, as its sign-in had none
|
|
621
|
+
return { ...site, web: base, openapi: base, api: base, vpc: a.enterpriseHost, redirect: "" }
|
|
619
622
|
}
|
|
620
623
|
if (a?.dedicated && a.lingma) {
|
|
621
624
|
const s = lingmaSite(site, a.dedicated)
|
|
@@ -676,10 +679,12 @@ async function deviceSignIn(site) {
|
|
|
676
679
|
let name = ""
|
|
677
680
|
let uid = dt.user_id
|
|
678
681
|
let user = null
|
|
682
|
+
let identity = {}
|
|
679
683
|
try {
|
|
680
684
|
const ui = await openapi(site, site.userInfoPath ?? "/api/v1/userinfo", { token: dt.token })
|
|
681
685
|
if (ui.status >= 200 && ui.status < 300) {
|
|
682
686
|
user = site.normalizeUser ? site.normalizeUser(ui.json) : ui.json
|
|
687
|
+
if (site.deviceRefreshTarget) identity = accountIdentity(ui.json)
|
|
683
688
|
;[email, name] = [user?.email ?? "", user?.name ?? ""]
|
|
684
689
|
if (site.deviceOnly) uid = String(user?.id ?? user?.user_id ?? user?.uid ?? uid ?? "") || undefined
|
|
685
690
|
// a VPC poll may give the token alone: the user is then the one
|
|
@@ -716,6 +721,7 @@ async function deviceSignIn(site) {
|
|
|
716
721
|
machineId,
|
|
717
722
|
deviceToken: dt.token,
|
|
718
723
|
deviceRefresh: dt.refresh_token ?? "",
|
|
724
|
+
...identity,
|
|
719
725
|
...(site.vpc ? { vpc: site.vpc } : {}),
|
|
720
726
|
...(site.enterpriseDomain ? { vpc: site.enterpriseDomain, enterpriseHost: site.enterpriseDomain } : {}),
|
|
721
727
|
...(site.dedicated ? { dedicated: site.dedicated, ...(site.api !== dedicatedHost(site.web, "-gateway") ? { dedicatedApi: site.api } : {}) } : {}),
|
|
@@ -1019,7 +1025,14 @@ async function fetchListing(site, cred) {
|
|
|
1019
1025
|
// flagship answers 503 "Model catalog is unavailable") falls back to
|
|
1020
1026
|
// the list its site ships
|
|
1021
1027
|
if (res.status === 503 && site.fallbackModels) return site.fallbackModels
|
|
1022
|
-
|
|
1028
|
+
// QwenWork's "User identity could not be verified" (tnt1223 on
|
|
1029
|
+
// Discord): before 0.2.57 a biz account's token was refreshed as
|
|
1030
|
+
// personal, and no gateway call named the client. A sign-in records
|
|
1031
|
+
// the account's identity, which the refresh then asks for.
|
|
1032
|
+
const again = res.status === 403 && site.deviceRefreshTarget && /identity could not be verified/i.test(text)
|
|
1033
|
+
? ` — ${site.name} didn't recognise this sign-in's identity; sign in to the account again`
|
|
1034
|
+
: ""
|
|
1035
|
+
throw new Error(`${site.name} models: HTTP ${res.status}: ${text.trim().slice(0, 512)}${again}`)
|
|
1023
1036
|
}
|
|
1024
1037
|
return JSON.parse(text)
|
|
1025
1038
|
}
|
|
@@ -2553,16 +2566,30 @@ async function campaignCall(url, method, body, deviceToken, machine, signal) {
|
|
|
2553
2566
|
})
|
|
2554
2567
|
}
|
|
2555
2568
|
|
|
2569
|
+
// deviceRefreshBody is what the device token's refresh is asked with. A
|
|
2570
|
+
// site with a refresh target (QwenWork) asks as its client does
|
|
2571
|
+
// (refreshDeviceToken): a biz account for its own team, any other for
|
|
2572
|
+
// "c", and with the client id and redirect_uri when the site signs in with
|
|
2573
|
+
// that pair. Asked for "c", a biz account's token was rotated to its
|
|
2574
|
+
// personal identity.
|
|
2575
|
+
function deviceRefreshBody(site, refresh, a = {}) {
|
|
2576
|
+
if (!site.deviceRefreshTarget) return { refresh_token: refresh }
|
|
2577
|
+
const body = a?.identity === "biz" && a?.teamId
|
|
2578
|
+
? { refresh_token: refresh, target: "biz", team_id: a.teamId }
|
|
2579
|
+
: { refresh_token: refresh, target: site.deviceRefreshTarget }
|
|
2580
|
+
return site.clientId && site.redirect ? { ...body, client_id: site.clientId, redirect_uri: site.redirect } : body
|
|
2581
|
+
}
|
|
2582
|
+
|
|
2556
2583
|
// refreshDevice trades the device refresh token for a new pair; it rotates
|
|
2557
2584
|
// too, so the caller saves it.
|
|
2558
|
-
async function refreshDevice(site, refresh, chat = false) {
|
|
2585
|
+
async function refreshDevice(site, refresh, chat = false, a = {}) {
|
|
2559
2586
|
if (!String(refresh ?? "").trim()) {
|
|
2560
2587
|
if (chat) throw new SignInGone(`${site.name}: the sign-in lapsed; sign in again`)
|
|
2561
2588
|
throw new Error("qoder device token refresh: missing refresh token; sign in again")
|
|
2562
2589
|
}
|
|
2563
2590
|
let r
|
|
2564
2591
|
try {
|
|
2565
|
-
r = await openapi(site, "/api/v1/deviceToken/refresh", { method: "POST", body:
|
|
2592
|
+
r = await openapi(site, "/api/v1/deviceToken/refresh", { method: "POST", body: deviceRefreshBody(site, refresh, a) })
|
|
2566
2593
|
} catch (e) {
|
|
2567
2594
|
throw new Error(`qoder device token refresh: request failed: ${e?.message ?? e}`)
|
|
2568
2595
|
}
|
|
@@ -2780,6 +2807,47 @@ const parseAccountContext = (ctx, wallets) => {
|
|
|
2780
2807
|
return out
|
|
2781
2808
|
}
|
|
2782
2809
|
|
|
2810
|
+
// QWENWORK_APP is the QwenWork desktop release these calls name, as
|
|
2811
|
+
// QwenWorkCN.app 1.2.1's package.json has it (version, and
|
|
2812
|
+
// qoderworkUpgradeVersion for the release and its build).
|
|
2813
|
+
const QWENWORK_APP = { version: "1.2.1", release: "1.2.1-26092107" }
|
|
2814
|
+
|
|
2815
|
+
// qwenworkClientHeaders are the names QwenWork's own client puts on every
|
|
2816
|
+
// gateway call it makes outside a chat (openApiJsonRequest: buildUserAgent,
|
|
2817
|
+
// a fresh X-Request-Id and getQwenWorkClientHeaders): the sign-in poll, the
|
|
2818
|
+
// account-context, the device token's refresh. Without them every such
|
|
2819
|
+
// call went out as no client at all.
|
|
2820
|
+
const qwenworkClientHeaders = () => ({
|
|
2821
|
+
"User-Agent": `qoderwork/${QWENWORK_APP.version}`,
|
|
2822
|
+
"X-Request-Id": randomUUID(),
|
|
2823
|
+
"X-QwenWork-Version": QWENWORK_APP.version,
|
|
2824
|
+
"X-QwenWork-Release-Version": QWENWORK_APP.release,
|
|
2825
|
+
"X-QwenWork-Build": QWENWORK_APP.release.split("-")[1],
|
|
2826
|
+
"X-QwenWork-Platform": process.platform,
|
|
2827
|
+
"X-QwenWork-Arch": process.arch,
|
|
2828
|
+
"X-QwenWork-Channel": "stable",
|
|
2829
|
+
})
|
|
2830
|
+
|
|
2831
|
+
// accountIdentity is the identity QwenWork's client reads off the
|
|
2832
|
+
// account-context (fetchUserInfo over normalizeAccountContext: the
|
|
2833
|
+
// envelope's data with data.user spread over it; getIdentityFromUser): a
|
|
2834
|
+
// biz account names its team, which the device token's refresh is asked
|
|
2835
|
+
// for; anything else is the personal "c" identity, and gives nothing.
|
|
2836
|
+
function accountIdentity(v) {
|
|
2837
|
+
const e = v?.data ?? v
|
|
2838
|
+
if (!e || typeof e !== "object") return {}
|
|
2839
|
+
const u = e.user && typeof e.user === "object" && !Array.isArray(e.user) ? { ...e, ...e.user } : e
|
|
2840
|
+
const str = (o, keys) => {
|
|
2841
|
+
for (const k of keys) if (typeof o?.[k] === "string" && o[k].length > 0) return o[k]
|
|
2842
|
+
return null
|
|
2843
|
+
}
|
|
2844
|
+
const org = u.organization && typeof u.organization === "object" ? u.organization : null
|
|
2845
|
+
const isBiz = typeof u.is_biz === "boolean" ? u.is_biz : u.isBiz === true
|
|
2846
|
+
const orgId = str(u, ["orgId", "organization_id", "organizationId"]) ?? str(org, ["org_id", "orgId", "id"])
|
|
2847
|
+
const teamId = str(u, ["team_id", "teamId"]) ?? str(org, ["team_id", "teamId"])
|
|
2848
|
+
return isBiz && orgId && teamId ? { identity: "biz", teamId } : {}
|
|
2849
|
+
}
|
|
2850
|
+
|
|
2783
2851
|
const QWENWORK_COMMON = {
|
|
2784
2852
|
clientId: "e883ade2-e6e3-4d6d-adf7-f92ceff5fdcb", // the desktop client's
|
|
2785
2853
|
rsaKey: QWENWORK_RSA_KEY,
|
|
@@ -2789,6 +2857,7 @@ const QWENWORK_COMMON = {
|
|
|
2789
2857
|
redirect: "qwenwork-cn://",
|
|
2790
2858
|
deviceOnly: true, // the device token is itself the chat token
|
|
2791
2859
|
deviceRefreshTarget: "c",
|
|
2860
|
+
clientHeaders: qwenworkClientHeaders,
|
|
2792
2861
|
modelScene: "qwork", listAllModels: true,
|
|
2793
2862
|
clientType: "6", businessProduct: "qoder_work", sessionType: "qoder_work",
|
|
2794
2863
|
userInfoPath: "/api/v1/adapter/user/account-context?include=user,plan,quota,page,data_sharing",
|
|
@@ -2942,7 +3011,7 @@ const makePlugin = (site) => async ({ client }) => {
|
|
|
2942
3011
|
if (a.deviceChat) {
|
|
2943
3012
|
// the device token is the chat token: renewed as a device token,
|
|
2944
3013
|
// both pairs being the one pair
|
|
2945
|
-
const dt = await refreshDevice(s, a.refresh, true)
|
|
3014
|
+
const dt = await refreshDevice(s, a.refresh, true, a)
|
|
2946
3015
|
return spend(a.refresh, { access: dt.token, refresh: dt.refresh, expires: dt.expires, deviceToken: dt.token, deviceRefresh: dt.refresh })
|
|
2947
3016
|
}
|
|
2948
3017
|
let jt
|
|
@@ -3053,7 +3122,7 @@ const makePlugin = (site) => async ({ client }) => {
|
|
|
3053
3122
|
locked(async () => {
|
|
3054
3123
|
const a = latest(await getAuth())
|
|
3055
3124
|
if (a?.deviceToken !== attempted) return a?.deviceToken
|
|
3056
|
-
const dt = await refreshDevice(siteOf(site, a), a.deviceRefresh, !!a.deviceChat)
|
|
3125
|
+
const dt = await refreshDevice(siteOf(site, a), a.deviceRefresh, !!a.deviceChat, a)
|
|
3057
3126
|
const next = { ...a, deviceToken: dt.token, deviceRefresh: dt.refresh }
|
|
3058
3127
|
// the pair it spent was the chat pair too
|
|
3059
3128
|
if (a.deviceChat) Object.assign(next, spend(a.refresh, { access: dt.token, refresh: dt.refresh, expires: dt.expires, deviceToken: dt.token, deviceRefresh: dt.refresh }))
|
|
@@ -3101,7 +3170,7 @@ const makePlugin = (site) => async ({ client }) => {
|
|
|
3101
3170
|
const read = async (token, path = site.usagePath) => {
|
|
3102
3171
|
const s = siteOf(site, cred)
|
|
3103
3172
|
const res = await fetch(`${s.openapi}${path}`, {
|
|
3104
|
-
headers: { Accept: "application/json", Authorization: `Bearer ${token}` },
|
|
3173
|
+
headers: { Accept: "application/json", ...(s.clientHeaders?.() ?? {}), Authorization: `Bearer ${token}` },
|
|
3105
3174
|
signal: AbortSignal.timeout(20_000),
|
|
3106
3175
|
})
|
|
3107
3176
|
if (res.status !== 200) throw new UsageStatus(res.status)
|
|
@@ -3753,4 +3822,4 @@ export async function QwenWorkEnterpriseAuthPlugin({ client }) {
|
|
|
3753
3822
|
export const protocol = { makePlugin, deviceSignIn, openapi, siteOf }
|
|
3754
3823
|
|
|
3755
3824
|
// for tests
|
|
3756
|
-
export const _internal = { freeView, maskPhone, phoneLabelOf, realEmailOf, chatHeaders, machineOS, machineHostname, cosyHeaders, typedUnions, Splitter, campaignRuntime, RUNTIMES, campaignPage, campaignTarget, vpcInstance, vpcSite, dedicatedOrigin, dedicatedHost, dedicatedSite, lingmaRoots, lingmaDecode, lingmaSite, lingmaHeaders, LINGMA_MODELS, parseUsage, gfmt, when, failure, modelInfo, qoderBody, qoderMessages, cacheMarked, sessionOf, namedSession, taskOf, TASKS, TASK_MAX, TASK_IDLE, SESSION, SITES, decodeBody, events, QUEUE, queueState }
|
|
3825
|
+
export const _internal = { fetchListing, accountIdentity, deviceRefreshBody, qwenworkClientHeaders, freeView, maskPhone, phoneLabelOf, realEmailOf, chatHeaders, machineOS, machineHostname, cosyHeaders, typedUnions, Splitter, campaignRuntime, RUNTIMES, campaignPage, campaignTarget, vpcInstance, vpcSite, dedicatedOrigin, dedicatedHost, dedicatedSite, lingmaRoots, lingmaDecode, lingmaSite, lingmaHeaders, LINGMA_MODELS, parseUsage, gfmt, when, failure, modelInfo, qoderBody, qoderMessages, cacheMarked, sessionOf, namedSession, taskOf, TASKS, TASK_MAX, TASK_IDLE, SESSION, SITES, decodeBody, events, QUEUE, queueState }
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@magpie-community/opencode-qoder-auth",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.57",
|
|
4
4
|
"description": "Qoder and QwenWork (千问办公) subscriptions in OpenCode and magpie: qoder.com, qoder.cn, gateway.qwenwork.cn and the enterprise flagship, VPC and dedicated domains",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./index.mjs",
|