@magpie-community/opencode-grok-auth 0.1.12 → 0.1.13
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 +90 -2
- 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
|
|
@@ -513,6 +515,78 @@ async function signIn() {
|
|
|
513
515
|
}
|
|
514
516
|
}
|
|
515
517
|
|
|
518
|
+
function cpaClaims(token) {
|
|
519
|
+
try {
|
|
520
|
+
const part = token.split(".")[1]
|
|
521
|
+
if (!part) return null
|
|
522
|
+
return JSON.parse(Buffer.from(part, "base64url").toString("utf8"))
|
|
523
|
+
} catch {
|
|
524
|
+
return null
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
function cpaAuthFromFile(file) {
|
|
529
|
+
let source
|
|
530
|
+
try {
|
|
531
|
+
source = JSON.parse(readFileSync(file, "utf8"))
|
|
532
|
+
} catch {
|
|
533
|
+
throw new Error("CPA Grok account file is not valid JSON")
|
|
534
|
+
}
|
|
535
|
+
if (!source || typeof source !== "object" || Array.isArray(source) || source.type !== "xai" || source.auth_kind !== "oauth" || source.disabled !== false) {
|
|
536
|
+
throw new Error("CPA file is not an enabled xAI OAuth account")
|
|
537
|
+
}
|
|
538
|
+
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) {
|
|
539
|
+
throw new Error("CPA xAI account is missing its tokens or identity")
|
|
540
|
+
}
|
|
541
|
+
if (typeof source.last_refresh !== "string" || typeof source.expired !== "string") {
|
|
542
|
+
throw new Error("CPA xAI account has invalid token timestamps")
|
|
543
|
+
}
|
|
544
|
+
const createTime = Date.parse(source.last_refresh)
|
|
545
|
+
const expiresAt = Date.parse(source.expired)
|
|
546
|
+
if (!Number.isFinite(createTime) || !Number.isFinite(expiresAt)) {
|
|
547
|
+
throw new Error("CPA xAI account has invalid token timestamps")
|
|
548
|
+
}
|
|
549
|
+
const claims = cpaClaims(source.access_token)
|
|
550
|
+
const audience = Array.isArray(claims?.aud) ? claims.aud : [claims?.aud]
|
|
551
|
+
if (claims?.iss !== "https://auth.x.ai" || !audience.includes(CPA_CLIENT_ID)) {
|
|
552
|
+
throw new Error("CPA token is not for the Grok Build xAI account")
|
|
553
|
+
}
|
|
554
|
+
return {
|
|
555
|
+
key: source.access_token,
|
|
556
|
+
auth_mode: "oidc",
|
|
557
|
+
create_time: new Date(createTime).toISOString(),
|
|
558
|
+
user_id: source.sub,
|
|
559
|
+
email: source.email,
|
|
560
|
+
refresh_token: source.refresh_token,
|
|
561
|
+
expires_at: new Date(expiresAt).toISOString(),
|
|
562
|
+
oidc_issuer: "https://auth.x.ai",
|
|
563
|
+
oidc_client_id: CPA_CLIENT_ID,
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
async function importCPA(inputs) {
|
|
568
|
+
if (!executable()) throw new Error(`install Grok Build first: ${INSTALL}`)
|
|
569
|
+
const rawPath = String(inputs?.path ?? "").trim()
|
|
570
|
+
const file = resolve(rawPath === "~" ? homedir() : rawPath.startsWith("~/") ? join(homedir(), rawPath.slice(2)) : rawPath)
|
|
571
|
+
if (!isFile(file)) throw new Error("choose a CPA Grok account JSON file")
|
|
572
|
+
const auth = cpaAuthFromFile(file)
|
|
573
|
+
return {
|
|
574
|
+
url: "",
|
|
575
|
+
instructions: "The account is ready to import. Stop using it in CPA before continuing; both clients rotate the same refresh token.",
|
|
576
|
+
method: "auto",
|
|
577
|
+
callback: async () => {
|
|
578
|
+
const home = newHome()
|
|
579
|
+
try {
|
|
580
|
+
writeFileSync(join(home, "auth.json"), JSON.stringify({ [CPA_SCOPE]: auth }, null, 2), { mode: 0o600 })
|
|
581
|
+
} catch (error) {
|
|
582
|
+
rmSync(home, { recursive: true, force: true })
|
|
583
|
+
throw error
|
|
584
|
+
}
|
|
585
|
+
return saved(home, credential(home))
|
|
586
|
+
},
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
|
|
516
590
|
// saved is the sign-in as OpenCode keeps it: refresh is the home the CLI
|
|
517
591
|
// keeps it in, access the token it had then.
|
|
518
592
|
function saved(home, c) {
|
|
@@ -770,6 +844,20 @@ export const GrokAuthPlugin = async ({ client }) => {
|
|
|
770
844
|
label: "Use Grok Build's current sign-in",
|
|
771
845
|
authorize: current,
|
|
772
846
|
},
|
|
847
|
+
{
|
|
848
|
+
type: "oauth",
|
|
849
|
+
label: "Import CPA Grok account JSON",
|
|
850
|
+
prompts: [
|
|
851
|
+
{
|
|
852
|
+
type: "text",
|
|
853
|
+
key: "path",
|
|
854
|
+
message: "Path to the CPA xAI account JSON. Stop using this account in CPA after import; its refresh token rotates.",
|
|
855
|
+
placeholder: "~/.cli-proxy-api/xai-<email>.json",
|
|
856
|
+
validate: (v) => (String(v ?? "").trim() ? undefined : "Enter the path to a CPA xAI account JSON file"),
|
|
857
|
+
},
|
|
858
|
+
],
|
|
859
|
+
authorize: importCPA,
|
|
860
|
+
},
|
|
773
861
|
],
|
|
774
862
|
// magpie's: how much of the subscription's allowance is gone; each
|
|
775
863
|
// read keeps the sign-in (signIn), as the built-in's marked no
|
package/package.json
CHANGED