@magpie-community/opencode-grok-auth 0.1.15 → 0.1.17

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.
Files changed (3) hide show
  1. package/README.md +5 -0
  2. package/index.mjs +67 -18
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -27,6 +27,11 @@ the installer does, before a sign-in that needs it.
27
27
  - If it is signed in, the sign-in goes to a home of this plugin's,
28
28
  `$XDG_CONFIG_HOME/opencode-grok-auth/<random>` (else under
29
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.
30
35
  - **Use Grok Build's current sign-in** takes the account the CLI is
31
36
  signed in to, with nothing to approve.
32
37
  - **Import CPA Grok account JSON** copies one enabled xAI OAuth account from
package/index.mjs CHANGED
@@ -97,34 +97,60 @@ function executable() {
97
97
  return ""
98
98
  }
99
99
 
100
- // 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.
101
104
  function envFor(home) {
102
105
  const env = {}
103
106
  for (const [k, v] of Object.entries(process.env)) {
104
- 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
105
108
  env[k] = v
106
109
  }
107
110
  env.GROK_HOME = home
108
111
  return env
109
112
  }
110
113
 
111
- // credential is the sign-in the CLI keeps in home: the first, by key, with a token.
112
- function credential(home) {
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
+ }
113
131
  let all
114
132
  try {
115
- all = JSON.parse(readFileSync(join(home, "auth.json"), "utf8"))
133
+ all = JSON.parse(text)
116
134
  } catch {
117
- return null
135
+ return { c: null, why: `${file} isn't JSON` }
118
136
  }
119
- if (!all || typeof all !== "object") return null
120
- for (const k of Object.keys(all).sort()) {
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) {
121
140
  const c = all[k]
122
141
  if (c && typeof c.key === "string" && c.key) {
123
142
  const t = Date.parse(c.expires_at ?? "")
124
- return { key: c.key, email: c.email ?? "", expires: Number.isFinite(t) ? t : 0, issuer: c.oidc_issuer ?? "" }
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: "" }
125
147
  }
126
148
  }
127
- 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
128
154
  }
129
155
 
130
156
  function run(file, args, opts) {
@@ -436,12 +462,24 @@ function modelOf(id) {
436
462
 
437
463
  // ---- sign-in ------------------------------------------------------------------
438
464
 
465
+ // opensPage is whether `grok login --device-auth` opens its page in the
466
+ // browser itself, which it does wherever it can: on macOS (NSWorkspace) and
467
+ // Windows (ShellExecute) always, with nothing to turn it off; on Linux and
468
+ // the BSDs through $BROWSER, else xdg-open where a display is. There it is
469
+ // given a $BROWSER that opens nothing, and the one who asked (magpie, which
470
+ // knows where the user's browser is) opens it; here the sign-in says the
471
+ // page is open already, so it isn't opened a second time (andyfmyth on X:
472
+ // "每次都会弹两次验证").
473
+ const opensPage = () => process.platform === "darwin" || process.platform === "win32"
474
+
439
475
  // login runs `grok login --device-auth` in home and hands on the first link
440
476
  // it prints; done is settled when the command is.
441
- function login(exe, home, own) {
477
+ function login(exe, home) {
478
+ const env = envFor(home)
479
+ if (!opensPage()) env.BROWSER = "true"
442
480
  const child = spawn(exe, ["login", "--device-auth"], {
443
481
  cwd: homedir(),
444
- env: own ? envFor(home) : process.env,
482
+ env,
445
483
  stdio: ["ignore", "pipe", "pipe"],
446
484
  })
447
485
  const tail = []
@@ -495,7 +533,7 @@ async function signIn() {
495
533
  if (!exe) throw new Error(NO_CLI)
496
534
  const own = !!credential(cliHome())
497
535
  const home = own ? newHome() : cliHome()
498
- const { child, link, done, tail } = login(exe, home, own)
536
+ const { child, link, done, tail } = login(exe, home)
499
537
  const url = await Promise.race([link, new Promise((r) => setTimeout(() => r(""), LINK_WAIT))])
500
538
  if (!url) {
501
539
  child.kill()
@@ -507,14 +545,25 @@ async function signIn() {
507
545
  }
508
546
  return {
509
547
  url,
548
+ // not OpenCode's: the page is open already, by Grok Build itself
549
+ opened: opensPage(),
510
550
  instructions: "Approve the sign-in on the page; Grok Build finishes it.",
511
551
  method: "auto",
512
552
  callback: async () => {
513
553
  const r = await done
514
- const c = r.ok ? credential(home) : null
515
- if (!c || (own && !c.email)) {
554
+ const { c, why } = r.ok ? look(home) : { c: null, why: "" }
555
+ // a sign-in beside the CLI's own needs to know whose it is, so the
556
+ // same account signed in again replaces it instead of adding a second
557
+ if (!c || (own && !c.who)) {
516
558
  if (own) rmSync(home, { recursive: true, force: true })
517
- return { type: "failed", error: r.ok ? "grok login finished without an account" : r.why }
559
+ if (!r.ok) return { type: "failed", error: r.why }
560
+ const said = tail.at(-1) ? ` (it said "${tail.at(-1)}")` : ""
561
+ return {
562
+ type: "failed",
563
+ error: !c
564
+ ? `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"`
565
+ : `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"`,
566
+ }
518
567
  }
519
568
  return saved(home, c)
520
569
  },
@@ -596,7 +645,7 @@ async function importCPA(inputs) {
596
645
  // saved is the sign-in as OpenCode keeps it: refresh is the home the CLI
597
646
  // keeps it in, access the token it had then.
598
647
  function saved(home, c) {
599
- return { type: "success", refresh: home, access: c.key, expires: c.expires, accountId: c.email }
648
+ return { type: "success", refresh: home, access: c.key, expires: c.expires, accountId: c.who }
600
649
  }
601
650
 
602
651
  // current takes the sign-in the CLI already has, as it is.
@@ -751,7 +800,7 @@ export const GrokAuthPlugin = async ({ client }) => {
751
800
  const remember = async (auth, c) => {
752
801
  if (auth.access === c.key && auth.expires === c.expires) return
753
802
  try {
754
- await client.auth.set({ path: { id: PROVIDER }, body: { ...auth, access: c.key, expires: c.expires, accountId: c.email || auth.accountId } })
803
+ await client.auth.set({ path: { id: PROVIDER }, body: { ...auth, access: c.key, expires: c.expires, accountId: c.who || auth.accountId } })
755
804
  } catch {}
756
805
  }
757
806
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magpie-community/opencode-grok-auth",
3
- "version": "0.1.15",
3
+ "version": "0.1.17",
4
4
  "description": "OpenCode provider plugin: Grok (SuperGrok / X Premium+) through the Grok Build CLI's sign-in",
5
5
  "type": "module",
6
6
  "main": "./index.mjs",