magpie-junie-auth 0.2.0 → 0.2.2

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 CHANGED
@@ -18,6 +18,7 @@
18
18
  1. **从本机 Junie CLI 导入登录态** —— 读取已登录 CLI 的 OAuth 凭据:macOS 从 Keychain(`security`,service `junie-cli`),Windows 从 `~/.junie/dpapi_credentials/*.dpapi`(DPAPI)。刷新令牌与过期时间会完整保存,便于自动续期。
19
19
  2. **浏览器登录(JetBrains 账号)** —— 复刻 CLI 的 PKCE 授权码流程,本地回调端口 62345–62364。
20
20
  3. **API key (`perm-*`)** —— 从 https://junie.jetbrains.com/cli 获取。
21
+ 4. **Refresh token (`oW66…` / `fHx…` / `8Sl…`)** —— 直接粘贴 JetBrains OAuth refresh token,插件自动换取 access token 并完整保存 refresh/expires/email,无需浏览器或本机 CLI。
21
22
 
22
23
  凭据存在 magpie 的 `plugin-auth.json`(权限 600)。`auth.refresh` 在到期前 90 秒自动续期;账号额度耗尽时 magpie 自动切到下一个。
23
24
 
package/index.mjs CHANGED
@@ -1,9 +1,10 @@
1
1
  // magpie-junie:把 JetBrains Junie 接入 magpie 供应商 `junie`。
2
2
  //
3
- // 三种登录方式(按优先级):
4
- // 1. 导入本机 CLI 登录态(~/.junie/dpapi_credentials,DPAPI 解密)——最快,不用浏览器
3
+ // 四种登录方式(按优先级):
4
+ // 1. 导入本机 CLI 登录态(macOS Keychain / Windows DPAPI)——最快,不用浏览器
5
5
  // 2. 浏览器 PKCE(Junie CLI 同款)——没有本地 CLI 时用
6
- // 3. API key(perm-*,从 junie.jetbrains.com/cli 获取)——兜底
6
+ // 3. API key(perm-*,从 junie.jetbrains.com/cli 获取)
7
+ // 4. 直接粘贴 OAuth refresh token(oW66… / fHx… / 8Sl…)——换取并完整保存
7
8
  //
8
9
  // 请求链路(两种,由 options.bridgeUrl 选择):
9
10
  // A. 直连上游:loader.fetch 把标准请求发到 ingrazzio-cloud-prod.labs.jb.gg
@@ -16,7 +17,7 @@
16
17
  import { PROVIDER, DISPLAY_NAME } from "./lib/constants.mjs"
17
18
  import { MODELS, configModels } from "./lib/models.mjs"
18
19
  import { readCliCredentials } from "./lib/credentials.mjs"
19
- import { startPkceLogin, refreshAccessToken } from "./lib/auth.mjs"
20
+ import { startPkceLogin, refreshAccessToken, signInWithRefreshToken } from "./lib/auth.mjs"
20
21
  import { makeFetch } from "./lib/fetch.mjs"
21
22
  import { makeDirectFetch, DIRECT_MODEL } from "./lib/direct.mjs"
22
23
  import { usageHook } from "./lib/usage.mjs"
@@ -111,6 +112,22 @@ export async function JuniePlugin(input, options = {}) {
111
112
  label: "Junie API key (perm-*)",
112
113
  placeholder: "perm-…",
113
114
  },
115
+ {
116
+ // 直接粘贴 OAuth refresh token(oW66… / fHx… / 8Sl…):
117
+ // prompts + authorize 让宿主标记 keyAsked,magpie 不再额外问 key。
118
+ type: "api",
119
+ label: "Refresh token (oW66… / fHx…)",
120
+ prompts: [
121
+ { type: "password", key: "refresh", message: "Junie refresh token" },
122
+ ],
123
+ async authorize(inputs = {}) {
124
+ try {
125
+ return await signInWithRefreshToken(inputs.refresh)
126
+ } catch (err) {
127
+ return { type: "failed", error: `refresh token 无效或上游拒绝:${String(err?.message ?? err).slice(0, 300)}` }
128
+ }
129
+ },
130
+ },
114
131
  ],
115
132
 
116
133
  async loader(getAuth, provider) {
package/lib/auth.mjs CHANGED
@@ -178,6 +178,30 @@ export async function startPkceLogin() {
178
178
  }
179
179
  }
180
180
 
181
+ /**
182
+ * 直接用 refresh token 换取完整账号(登录方法「Refresh token」用)。
183
+ * refresh token 来自 JetBrains CLI/OAuth,形如 oW66… / fHx… / 8Sl…。
184
+ */
185
+ export async function signInWithRefreshToken(refresh) {
186
+ const token = String(refresh ?? "").trim()
187
+ if (!token) throw new Error("refresh token 不能为空")
188
+ const t = await exchangeToken({ grant_type: "refresh_token", refresh_token: token })
189
+ const access = t.id_token ?? t.access_token
190
+ if (!access) throw new Error("上游没有返回 access/id token")
191
+ const accountId = await resolveAccountId(t.access_token ?? access)
192
+ return {
193
+ type: "success",
194
+ key: access,
195
+ access,
196
+ accessToken: t.access_token,
197
+ idToken: t.id_token,
198
+ refresh: t.refresh_token ?? token,
199
+ expires: t.expires_in ? Date.now() + t.expires_in * 1000 : 0,
200
+ accountId,
201
+ metadata: { source: "refresh-token", license: "free", email: accountId, freeGoogleApi: true },
202
+ }
203
+ }
204
+
181
205
  export const _internal = {
182
206
  generatePKCE,
183
207
  buildAuthUrl,
@@ -185,5 +209,6 @@ export const _internal = {
185
209
  refreshAccessToken,
186
210
  resolveAccountId,
187
211
  startCallbackServer,
212
+ signInWithRefreshToken,
188
213
  pending,
189
214
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "magpie-junie-auth",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "description": "magpie provider plugin: JetBrains Junie free tier (gpt-6-luna) via direct Responses API or CLI bridge; multi-account with browser PKCE / CLI login reuse",
5
5
  "type": "module",
6
6
  "main": "./index.mjs",