@magpie-community/opencode-grok-auth 0.1.14 → 0.1.16
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 +12 -0
- package/index.mjs +61 -21
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -10,6 +10,13 @@ curl -fsSL https://x.ai/cli/install.sh | bash # macOS, Linux
|
|
|
10
10
|
irm https://x.ai/cli/install.ps1 | iex # Windows
|
|
11
11
|
```
|
|
12
12
|
|
|
13
|
+
It is found in the installer's folder (`GROK_BIN_DIR`, `GROK_HOME/bin`,
|
|
14
|
+
`~/.grok/bin`), or on `PATH` when it lives under a `.grok` folder, so that
|
|
15
|
+
another program named `grok` isn't taken for it. Where the installer can't
|
|
16
|
+
run, such as a container without curl, put the binary in a folder of your
|
|
17
|
+
own and set `GROK_BIN_DIR` to it. magpie installs Grok Build itself, as
|
|
18
|
+
the installer does, before a sign-in that needs it.
|
|
19
|
+
|
|
13
20
|
## Sign in
|
|
14
21
|
|
|
15
22
|
- **Sign in with Grok Build (device code)** runs `grok login --device-auth`
|
|
@@ -20,6 +27,11 @@ irm https://x.ai/cli/install.ps1 | iex # Windows
|
|
|
20
27
|
- If it is signed in, the sign-in goes to a home of this plugin's,
|
|
21
28
|
`$XDG_CONFIG_HOME/opencode-grok-auth/<random>` (else under
|
|
22
29
|
`~/.config`). Grok Build's own sign-in stays as it is.
|
|
30
|
+
- The account is told apart by its email, else its xAI user id (an
|
|
31
|
+
account signed in through X can have no email). `GROK_AUTH_PATH` is
|
|
32
|
+
left out of the CLI's run, so the sign-in lands in that home.
|
|
33
|
+
- When the CLI finishes but leaves no sign-in there, the failure says
|
|
34
|
+
which file it read and what it found.
|
|
23
35
|
- **Use Grok Build's current sign-in** takes the account the CLI is
|
|
24
36
|
signed in to, with nothing to approve.
|
|
25
37
|
- **Import CPA Grok account JSON** copies one enabled xAI OAuth account from
|
package/index.mjs
CHANGED
|
@@ -23,6 +23,11 @@ const REFRESH_MARGIN = 5 * 60 * 1000 // a token this close to its end is renewed
|
|
|
23
23
|
const LEAD_MS = REFRESH_MARGIN // and this close, magpie renews it ahead of time (auth.refresh)
|
|
24
24
|
const LINK_WAIT = 30 * 1000
|
|
25
25
|
const INSTALL = process.platform === "win32" ? "irm https://x.ai/cli/install.ps1 | iex" : "curl -fsSL https://x.ai/cli/install.sh | bash"
|
|
26
|
+
// NO_CLI is what a sign-in without Grok Build says: how to install it, and
|
|
27
|
+
// that one put somewhere else by hand (a container with no shell to run the
|
|
28
|
+
// installer) is taken from GROK_BIN_DIR, as the installer's own folder is,
|
|
29
|
+
// while one found on PATH has to be under a .grok folder (yetone/magpie#2223)
|
|
30
|
+
const NO_CLI = `install Grok Build first: ${INSTALL}; or set GROK_BIN_DIR to the folder holding its grok binary`
|
|
26
31
|
|
|
27
32
|
// the tool types Grok's backend takes; it turns the whole request away over another
|
|
28
33
|
const TOOLS = new Set(["function", "web_search", "x_search", "image_generation", "collections_search", "file_search",
|
|
@@ -92,34 +97,60 @@ function executable() {
|
|
|
92
97
|
return ""
|
|
93
98
|
}
|
|
94
99
|
|
|
95
|
-
// envFor runs the CLI as the user runs it, in the given home.
|
|
100
|
+
// envFor runs the CLI as the user runs it, in the given home. GROK_AUTH_PATH
|
|
101
|
+
// goes too: grok 1.0.62 keeps its sign-in at that file instead of
|
|
102
|
+
// home/auth.json, where this plugin reads it, so a login with it set
|
|
103
|
+
// finished signed in and left nothing here.
|
|
96
104
|
function envFor(home) {
|
|
97
105
|
const env = {}
|
|
98
106
|
for (const [k, v] of Object.entries(process.env)) {
|
|
99
|
-
if (["GROK_HOME", "GROK_AUTH_PROVIDER_COMMAND", "GROK_AUTH_EXPIRED"].includes(k.toUpperCase())) continue
|
|
107
|
+
if (["GROK_HOME", "GROK_AUTH_PATH", "GROK_AUTH_PROVIDER_COMMAND", "GROK_AUTH_EXPIRED"].includes(k.toUpperCase())) continue
|
|
100
108
|
env[k] = v
|
|
101
109
|
}
|
|
102
110
|
env.GROK_HOME = home
|
|
103
111
|
return env
|
|
104
112
|
}
|
|
105
113
|
|
|
106
|
-
//
|
|
107
|
-
|
|
114
|
+
// look reads the sign-in the CLI keeps in home: the first, by key, with a
|
|
115
|
+
// token. With none, why says what the file held instead.
|
|
116
|
+
//
|
|
117
|
+
// who is the account it is: its email, else its xAI user id (the token's
|
|
118
|
+
// subject when the file has none). grok 1.0.62 takes both from the
|
|
119
|
+
// sign-in's id_token and writes "email": null when that
|
|
120
|
+
// has no email claim (an xAI account signed in through X has none), and
|
|
121
|
+
// still says "✓ Signed in" (llllleo9 on 𝕏; seen with grok 1.0.62 in a
|
|
122
|
+
// sandbox, signed in against a stand-in auth.x.ai).
|
|
123
|
+
function look(home) {
|
|
124
|
+
const file = join(home, "auth.json")
|
|
125
|
+
let text
|
|
126
|
+
try {
|
|
127
|
+
text = readFileSync(file, "utf8")
|
|
128
|
+
} catch (e) {
|
|
129
|
+
return { c: null, why: e?.code === "ENOENT" ? `there is no ${file}` : `${file} can't be read (${e?.code ?? e?.message})` }
|
|
130
|
+
}
|
|
108
131
|
let all
|
|
109
132
|
try {
|
|
110
|
-
all = JSON.parse(
|
|
133
|
+
all = JSON.parse(text)
|
|
111
134
|
} catch {
|
|
112
|
-
return null
|
|
135
|
+
return { c: null, why: `${file} isn't JSON` }
|
|
113
136
|
}
|
|
114
|
-
if (!all || typeof all !== "object") return null
|
|
115
|
-
|
|
137
|
+
if (!all || typeof all !== "object" || Array.isArray(all)) return { c: null, why: `${file} holds no sign-in` }
|
|
138
|
+
const keys = Object.keys(all).sort()
|
|
139
|
+
for (const k of keys) {
|
|
116
140
|
const c = all[k]
|
|
117
141
|
if (c && typeof c.key === "string" && c.key) {
|
|
118
142
|
const t = Date.parse(c.expires_at ?? "")
|
|
119
|
-
|
|
143
|
+
const email = typeof c.email === "string" ? c.email : ""
|
|
144
|
+
const sub = cpaClaims(c.key)?.sub
|
|
145
|
+
const user = (typeof c.user_id === "string" && c.user_id) || (typeof sub === "string" ? sub : "")
|
|
146
|
+
return { c: { key: c.key, email, user, who: email || user, expires: Number.isFinite(t) ? t : 0, issuer: c.oidc_issuer ?? "" }, why: "" }
|
|
120
147
|
}
|
|
121
148
|
}
|
|
122
|
-
return null
|
|
149
|
+
return { c: null, why: keys.length ? `${file} has ${keys.join(", ")}, none with a token` : `${file} holds no sign-in` }
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function credential(home) {
|
|
153
|
+
return look(home).c
|
|
123
154
|
}
|
|
124
155
|
|
|
125
156
|
function run(file, args, opts) {
|
|
@@ -433,10 +464,10 @@ function modelOf(id) {
|
|
|
433
464
|
|
|
434
465
|
// login runs `grok login --device-auth` in home and hands on the first link
|
|
435
466
|
// it prints; done is settled when the command is.
|
|
436
|
-
function login(exe, home
|
|
467
|
+
function login(exe, home) {
|
|
437
468
|
const child = spawn(exe, ["login", "--device-auth"], {
|
|
438
469
|
cwd: homedir(),
|
|
439
|
-
env:
|
|
470
|
+
env: envFor(home),
|
|
440
471
|
stdio: ["ignore", "pipe", "pipe"],
|
|
441
472
|
})
|
|
442
473
|
const tail = []
|
|
@@ -487,10 +518,10 @@ function newHome() {
|
|
|
487
518
|
// sign-in stays as it is.
|
|
488
519
|
async function signIn() {
|
|
489
520
|
const exe = executable()
|
|
490
|
-
if (!exe) throw new Error(
|
|
521
|
+
if (!exe) throw new Error(NO_CLI)
|
|
491
522
|
const own = !!credential(cliHome())
|
|
492
523
|
const home = own ? newHome() : cliHome()
|
|
493
|
-
const { child, link, done, tail } = login(exe, home
|
|
524
|
+
const { child, link, done, tail } = login(exe, home)
|
|
494
525
|
const url = await Promise.race([link, new Promise((r) => setTimeout(() => r(""), LINK_WAIT))])
|
|
495
526
|
if (!url) {
|
|
496
527
|
child.kill()
|
|
@@ -506,10 +537,19 @@ async function signIn() {
|
|
|
506
537
|
method: "auto",
|
|
507
538
|
callback: async () => {
|
|
508
539
|
const r = await done
|
|
509
|
-
const c = r.ok ?
|
|
510
|
-
|
|
540
|
+
const { c, why } = r.ok ? look(home) : { c: null, why: "" }
|
|
541
|
+
// a sign-in beside the CLI's own needs to know whose it is, so the
|
|
542
|
+
// same account signed in again replaces it instead of adding a second
|
|
543
|
+
if (!c || (own && !c.who)) {
|
|
511
544
|
if (own) rmSync(home, { recursive: true, force: true })
|
|
512
|
-
return { type: "failed", error: r.
|
|
545
|
+
if (!r.ok) return { type: "failed", error: r.why }
|
|
546
|
+
const said = tail.at(-1) ? ` (it said "${tail.at(-1)}")` : ""
|
|
547
|
+
return {
|
|
548
|
+
type: "failed",
|
|
549
|
+
error: !c
|
|
550
|
+
? `grok login finished${said}, but left no sign-in where this plugin reads it: ${why}. Run \`grok login\` in a terminal, then add Grok with "Use Grok Build's current sign-in"`
|
|
551
|
+
: `grok login finished${said}, but its sign-in in ${join(home, "auth.json")} names no account (no email, user id or token subject), so it can't be told from the Grok Build sign-in in ${cliHome()}. Sign in to that one with \`grok login\` and add it with "Use Grok Build's current sign-in"`,
|
|
552
|
+
}
|
|
513
553
|
}
|
|
514
554
|
return saved(home, c)
|
|
515
555
|
},
|
|
@@ -566,7 +606,7 @@ function cpaAuthFromFile(file) {
|
|
|
566
606
|
}
|
|
567
607
|
|
|
568
608
|
async function importCPA(inputs) {
|
|
569
|
-
if (!executable()) throw new Error(
|
|
609
|
+
if (!executable()) throw new Error(NO_CLI)
|
|
570
610
|
const rawPath = String(inputs?.path ?? "").trim()
|
|
571
611
|
const file = resolve(rawPath === "~" ? homedir() : rawPath.startsWith("~/") ? join(homedir(), rawPath.slice(2)) : rawPath)
|
|
572
612
|
if (!isFile(file)) throw new Error("choose a CPA Grok account JSON file")
|
|
@@ -591,12 +631,12 @@ async function importCPA(inputs) {
|
|
|
591
631
|
// saved is the sign-in as OpenCode keeps it: refresh is the home the CLI
|
|
592
632
|
// keeps it in, access the token it had then.
|
|
593
633
|
function saved(home, c) {
|
|
594
|
-
return { type: "success", refresh: home, access: c.key, expires: c.expires, accountId: c.
|
|
634
|
+
return { type: "success", refresh: home, access: c.key, expires: c.expires, accountId: c.who }
|
|
595
635
|
}
|
|
596
636
|
|
|
597
637
|
// current takes the sign-in the CLI already has, as it is.
|
|
598
638
|
async function current() {
|
|
599
|
-
if (!executable()) throw new Error(
|
|
639
|
+
if (!executable()) throw new Error(NO_CLI)
|
|
600
640
|
const home = cliHome()
|
|
601
641
|
if (!credential(home)) throw new Error("Grok Build is not signed in; run `grok login`, or sign in with the device code")
|
|
602
642
|
const c = await token(home, false)
|
|
@@ -746,7 +786,7 @@ export const GrokAuthPlugin = async ({ client }) => {
|
|
|
746
786
|
const remember = async (auth, c) => {
|
|
747
787
|
if (auth.access === c.key && auth.expires === c.expires) return
|
|
748
788
|
try {
|
|
749
|
-
await client.auth.set({ path: { id: PROVIDER }, body: { ...auth, access: c.key, expires: c.expires, accountId: c.
|
|
789
|
+
await client.auth.set({ path: { id: PROVIDER }, body: { ...auth, access: c.key, expires: c.expires, accountId: c.who || auth.accountId } })
|
|
750
790
|
} catch {}
|
|
751
791
|
}
|
|
752
792
|
|
package/package.json
CHANGED