@magpie-community/opencode-grok-auth 0.1.12 → 0.1.14
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/README.md +9 -2
- package/index.mjs +95 -6
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -22,13 +22,20 @@ irm https://x.ai/cli/install.ps1 | iex # Windows
|
|
|
22
22
|
`~/.config`). Grok Build's own sign-in stays as it is.
|
|
23
23
|
- **Use Grok Build's current sign-in** takes the account the CLI is
|
|
24
24
|
signed in to, with nothing to approve.
|
|
25
|
+
- **Import CPA Grok account JSON** copies one enabled xAI OAuth account from
|
|
26
|
+
a CLIProxyAPI (CPA) account JSON file (CPA keeps it as
|
|
27
|
+
`~/.cli-proxy-api/xai-<email>.json`) into a private home for this plugin.
|
|
28
|
+
CPA and Grok Build rotate the same refresh token, so stop using that
|
|
29
|
+
account in CPA before importing it. The file is read, never rewritten.
|
|
25
30
|
|
|
26
31
|
## Where the sign-in is kept
|
|
27
32
|
|
|
28
33
|
The CLI keeps the sign-in in its home, as `auth.json`. OpenCode (or
|
|
29
|
-
magpie) keeps where that home is, the token and its expiry.
|
|
34
|
+
magpie) keeps where that home is, the token and its expiry. Importing a CPA
|
|
35
|
+
account writes only its translated xAI OAuth fields to a private home owned
|
|
36
|
+
by this plugin; the selected CPA file is left unchanged.
|
|
30
37
|
|
|
31
|
-
The plugin
|
|
38
|
+
The plugin reads `auth.json`; Grok Build refreshes the token, which changes
|
|
32
39
|
each time. The plugin runs `grok models` in that home, so the CLI renews
|
|
33
40
|
it, then reads it again:
|
|
34
41
|
|
package/index.mjs
CHANGED
|
@@ -8,15 +8,17 @@
|
|
|
8
8
|
// backend, which speaks OpenAI's Responses API, signed as the CLI signs
|
|
9
9
|
// them.
|
|
10
10
|
import { spawn, execFile } from "node:child_process"
|
|
11
|
-
import { existsSync, mkdirSync, readFileSync, realpathSync, rmSync, statSync } from "node:fs"
|
|
11
|
+
import { existsSync, mkdirSync, readFileSync, realpathSync, rmSync, statSync, writeFileSync } from "node:fs"
|
|
12
12
|
import { homedir } from "node:os"
|
|
13
|
-
import { delimiter, dirname, isAbsolute, join } from "node:path"
|
|
13
|
+
import { delimiter, dirname, isAbsolute, join, resolve } from "node:path"
|
|
14
14
|
import { randomBytes } from "node:crypto"
|
|
15
15
|
import { STATUS_CODES } from "node:http"
|
|
16
16
|
|
|
17
17
|
const PROVIDER = "grok"
|
|
18
18
|
const BASE = "https://cli-chat-proxy.grok.com/v1"
|
|
19
19
|
const CLIENT_VERSION = "1.0.41"
|
|
20
|
+
const CPA_CLIENT_ID = "b1a00492-073a-47ea-816f-4c329264a828"
|
|
21
|
+
const CPA_SCOPE = `https://auth.x.ai::${CPA_CLIENT_ID}`
|
|
20
22
|
const REFRESH_MARGIN = 5 * 60 * 1000 // a token this close to its end is renewed before a request
|
|
21
23
|
const LEAD_MS = REFRESH_MARGIN // and this close, magpie renews it ahead of time (auth.refresh)
|
|
22
24
|
const LINK_WAIT = 30 * 1000
|
|
@@ -419,11 +421,12 @@ function windowsOf(d, efforts) {
|
|
|
419
421
|
// grok-4.7@500k is grok-4.7 at 500000 tokens. The header is what the CLI
|
|
420
422
|
// sends as x-grok-context-window. The proxy serves up to its largest
|
|
421
423
|
// window either way and refuses past it; the header names the window the
|
|
422
|
-
// caller asked to compact against.
|
|
424
|
+
// caller asked to compact against. A window no multiple of 1000 is named
|
|
425
|
+
// by its digits alone (grok-4.7@131072), as labelOf writes it.
|
|
423
426
|
function modelOf(id) {
|
|
424
|
-
const at = typeof id === "string" ? id.match(/^(
|
|
425
|
-
if (!at) return { model: id, window: 0 }
|
|
426
|
-
return { model: at[1], window: Number(at[2]) * 1000 }
|
|
427
|
+
const at = typeof id === "string" ? id.match(/^(.+)@(\d+)(k?)$/) : null
|
|
428
|
+
if (!at || !(Number(at[2]) > 0)) return { model: id, window: 0 }
|
|
429
|
+
return { model: at[1], window: Number(at[2]) * (at[3] ? 1000 : 1) }
|
|
427
430
|
}
|
|
428
431
|
|
|
429
432
|
// ---- sign-in ------------------------------------------------------------------
|
|
@@ -513,6 +516,78 @@ async function signIn() {
|
|
|
513
516
|
}
|
|
514
517
|
}
|
|
515
518
|
|
|
519
|
+
function cpaClaims(token) {
|
|
520
|
+
try {
|
|
521
|
+
const part = token.split(".")[1]
|
|
522
|
+
if (!part) return null
|
|
523
|
+
return JSON.parse(Buffer.from(part, "base64url").toString("utf8"))
|
|
524
|
+
} catch {
|
|
525
|
+
return null
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
function cpaAuthFromFile(file) {
|
|
530
|
+
let source
|
|
531
|
+
try {
|
|
532
|
+
source = JSON.parse(readFileSync(file, "utf8"))
|
|
533
|
+
} catch {
|
|
534
|
+
throw new Error("CPA Grok account file is not valid JSON")
|
|
535
|
+
}
|
|
536
|
+
if (!source || typeof source !== "object" || Array.isArray(source) || source.type !== "xai" || source.auth_kind !== "oauth" || source.disabled !== false) {
|
|
537
|
+
throw new Error("CPA file is not an enabled xAI OAuth account")
|
|
538
|
+
}
|
|
539
|
+
if (typeof source.access_token !== "string" || !source.access_token || typeof source.refresh_token !== "string" || !source.refresh_token || typeof source.sub !== "string" || !source.sub || typeof source.email !== "string" || !source.email) {
|
|
540
|
+
throw new Error("CPA xAI account is missing its tokens or identity")
|
|
541
|
+
}
|
|
542
|
+
if (typeof source.last_refresh !== "string" || typeof source.expired !== "string") {
|
|
543
|
+
throw new Error("CPA xAI account has invalid token timestamps")
|
|
544
|
+
}
|
|
545
|
+
const createTime = Date.parse(source.last_refresh)
|
|
546
|
+
const expiresAt = Date.parse(source.expired)
|
|
547
|
+
if (!Number.isFinite(createTime) || !Number.isFinite(expiresAt)) {
|
|
548
|
+
throw new Error("CPA xAI account has invalid token timestamps")
|
|
549
|
+
}
|
|
550
|
+
const claims = cpaClaims(source.access_token)
|
|
551
|
+
const audience = Array.isArray(claims?.aud) ? claims.aud : [claims?.aud]
|
|
552
|
+
if (claims?.iss !== "https://auth.x.ai" || !audience.includes(CPA_CLIENT_ID)) {
|
|
553
|
+
throw new Error("CPA token is not for the Grok Build xAI account")
|
|
554
|
+
}
|
|
555
|
+
return {
|
|
556
|
+
key: source.access_token,
|
|
557
|
+
auth_mode: "oidc",
|
|
558
|
+
create_time: new Date(createTime).toISOString(),
|
|
559
|
+
user_id: source.sub,
|
|
560
|
+
email: source.email,
|
|
561
|
+
refresh_token: source.refresh_token,
|
|
562
|
+
expires_at: new Date(expiresAt).toISOString(),
|
|
563
|
+
oidc_issuer: "https://auth.x.ai",
|
|
564
|
+
oidc_client_id: CPA_CLIENT_ID,
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
async function importCPA(inputs) {
|
|
569
|
+
if (!executable()) throw new Error(`install Grok Build first: ${INSTALL}`)
|
|
570
|
+
const rawPath = String(inputs?.path ?? "").trim()
|
|
571
|
+
const file = resolve(rawPath === "~" ? homedir() : rawPath.startsWith("~/") ? join(homedir(), rawPath.slice(2)) : rawPath)
|
|
572
|
+
if (!isFile(file)) throw new Error("choose a CPA Grok account JSON file")
|
|
573
|
+
const auth = cpaAuthFromFile(file)
|
|
574
|
+
return {
|
|
575
|
+
url: "",
|
|
576
|
+
instructions: "The account is ready to import. Stop using it in CPA before continuing; both clients rotate the same refresh token.",
|
|
577
|
+
method: "auto",
|
|
578
|
+
callback: async () => {
|
|
579
|
+
const home = newHome()
|
|
580
|
+
try {
|
|
581
|
+
writeFileSync(join(home, "auth.json"), JSON.stringify({ [CPA_SCOPE]: auth }, null, 2), { mode: 0o600 })
|
|
582
|
+
} catch (error) {
|
|
583
|
+
rmSync(home, { recursive: true, force: true })
|
|
584
|
+
throw error
|
|
585
|
+
}
|
|
586
|
+
return saved(home, credential(home))
|
|
587
|
+
},
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
|
|
516
591
|
// saved is the sign-in as OpenCode keeps it: refresh is the home the CLI
|
|
517
592
|
// keeps it in, access the token it had then.
|
|
518
593
|
function saved(home, c) {
|
|
@@ -770,6 +845,20 @@ export const GrokAuthPlugin = async ({ client }) => {
|
|
|
770
845
|
label: "Use Grok Build's current sign-in",
|
|
771
846
|
authorize: current,
|
|
772
847
|
},
|
|
848
|
+
{
|
|
849
|
+
type: "oauth",
|
|
850
|
+
label: "Import CPA Grok account JSON",
|
|
851
|
+
prompts: [
|
|
852
|
+
{
|
|
853
|
+
type: "text",
|
|
854
|
+
key: "path",
|
|
855
|
+
message: "Path to the CPA xAI account JSON. Stop using this account in CPA after import; its refresh token rotates.",
|
|
856
|
+
placeholder: "~/.cli-proxy-api/xai-<email>.json",
|
|
857
|
+
validate: (v) => (String(v ?? "").trim() ? undefined : "Enter the path to a CPA xAI account JSON file"),
|
|
858
|
+
},
|
|
859
|
+
],
|
|
860
|
+
authorize: importCPA,
|
|
861
|
+
},
|
|
773
862
|
],
|
|
774
863
|
// magpie's: how much of the subscription's allowance is gone; each
|
|
775
864
|
// read keeps the sign-in (signIn), as the built-in's marked no
|
package/package.json
CHANGED