@magpie-community/opencode-grok-auth 0.1.15 → 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.
Files changed (3) hide show
  1. package/README.md +5 -0
  2. package/index.mjs +53 -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) {
@@ -438,10 +464,10 @@ function modelOf(id) {
438
464
 
439
465
  // login runs `grok login --device-auth` in home and hands on the first link
440
466
  // it prints; done is settled when the command is.
441
- function login(exe, home, own) {
467
+ function login(exe, home) {
442
468
  const child = spawn(exe, ["login", "--device-auth"], {
443
469
  cwd: homedir(),
444
- env: own ? envFor(home) : process.env,
470
+ env: envFor(home),
445
471
  stdio: ["ignore", "pipe", "pipe"],
446
472
  })
447
473
  const tail = []
@@ -495,7 +521,7 @@ async function signIn() {
495
521
  if (!exe) throw new Error(NO_CLI)
496
522
  const own = !!credential(cliHome())
497
523
  const home = own ? newHome() : cliHome()
498
- const { child, link, done, tail } = login(exe, home, own)
524
+ const { child, link, done, tail } = login(exe, home)
499
525
  const url = await Promise.race([link, new Promise((r) => setTimeout(() => r(""), LINK_WAIT))])
500
526
  if (!url) {
501
527
  child.kill()
@@ -511,10 +537,19 @@ async function signIn() {
511
537
  method: "auto",
512
538
  callback: async () => {
513
539
  const r = await done
514
- const c = r.ok ? credential(home) : null
515
- if (!c || (own && !c.email)) {
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)) {
516
544
  if (own) rmSync(home, { recursive: true, force: true })
517
- return { type: "failed", error: r.ok ? "grok login finished without an account" : r.why }
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
+ }
518
553
  }
519
554
  return saved(home, c)
520
555
  },
@@ -596,7 +631,7 @@ async function importCPA(inputs) {
596
631
  // saved is the sign-in as OpenCode keeps it: refresh is the home the CLI
597
632
  // keeps it in, access the token it had then.
598
633
  function saved(home, c) {
599
- return { type: "success", refresh: home, access: c.key, expires: c.expires, accountId: c.email }
634
+ return { type: "success", refresh: home, access: c.key, expires: c.expires, accountId: c.who }
600
635
  }
601
636
 
602
637
  // current takes the sign-in the CLI already has, as it is.
@@ -751,7 +786,7 @@ export const GrokAuthPlugin = async ({ client }) => {
751
786
  const remember = async (auth, c) => {
752
787
  if (auth.access === c.key && auth.expires === c.expires) return
753
788
  try {
754
- await client.auth.set({ path: { id: PROVIDER }, body: { ...auth, access: c.key, expires: c.expires, accountId: c.email || auth.accountId } })
789
+ await client.auth.set({ path: { id: PROVIDER }, body: { ...auth, access: c.key, expires: c.expires, accountId: c.who || auth.accountId } })
755
790
  } catch {}
756
791
  }
757
792
 
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.16",
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",