opencode-cau-maas-auth 0.1.0 → 0.1.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.
Files changed (3) hide show
  1. package/README.md +1 -4
  2. package/package.json +6 -2
  3. package/src/sso.ts +3 -5
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # CAU MaaS — Magpie 插件
2
2
 
3
- 将 [maas.cau.edu.cn](https://maas.cau.edu.cn)(中国农业大学 MaaS 平台,New API 套壳,OpenAI 兼容)接入 Magpie,支持统一身份认证登录与用量显示。
3
+ 将 [maas.cau.edu.cn](https://maas.cau.edu.cn) (中国农业大学 MaaS 平台) 接入 Magpie,支持统一身份认证登录与用量显示。
4
4
 
5
5
  ## 安装
6
6
 
@@ -17,14 +17,11 @@ magpie quota
17
17
  1. **统一身份认证登录(推荐)**:输入学号/工号和 CAS 密码,零浏览器零粘贴。插件纯 HTTP 跑完 SSO 链(OIDC → CAS → ticket 回跳)拿 `jwt_token`,自动复用默认 API Key(没有则以 `magpie` 为名创建)。
18
18
  2. **直接粘贴 API Key**:已有 Key 时直接使用。
19
19
 
20
- > 保存的凭证是 API Key(长期有效);`jwt_token` 仅约 3 天有效,只作登录中间态。
21
-
22
20
  ## 实现要点
23
21
 
24
22
  - `src/index.ts` — 插件主体:`config` 注册 provider、`auth.methods` 两种登录、`loader` 注入 Bearer、`usage` 读 `/api/budget/me`、`provider.models` 列模型
25
23
  - `src/sso.ts` — CAS 纯 HTTP 登录:`/api/auth/cau/start` → OIDC → `onecas.cau.edu.cn` CAS 表单(`strEnc(学号+密码+lt)` 三重 DES)→ ticket 回跳 → `Set-Cookie: jwt_token`
26
24
  - `src/des-loader.ts` — 平台原版 `des.js`(Guapo DES 变体)内含隐式全局循环变量,ESM 严格模式会炸,故用 `node:vm` 沙箱加载
27
- - CAS 密码仅在登录请求期间存在于内存,不落盘
28
25
 
29
26
  平台对 Magpie 暴露的接口全部可用 Bearer API Key 访问:
30
27
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-cau-maas-auth",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "main": "./src/index.ts",
5
5
  "devDependencies": {
6
6
  "@types/bun": "^1.4.4",
@@ -22,7 +22,11 @@
22
22
  "opencode-plugin",
23
23
  "magpie",
24
24
  "cau",
25
- "new-api"
25
+ "maas",
26
+ "china-agricultural-university",
27
+ "中国农业大学",
28
+ "中国农大",
29
+ "中农"
26
30
  ],
27
31
  "magpie": {
28
32
  "name": "CAU MaaS",
package/src/sso.ts CHANGED
@@ -125,7 +125,7 @@ export async function loginWithCas(username: string, password: string): Promise<
125
125
  const { jwt } = await followRedirects(jar, callbackUrl)
126
126
  if (!jwt) throw new Error("回跳链未捕获到 jwt_token")
127
127
 
128
- // 5. 换 API Key:优先复用默认 ACTIVE,否则以 "magpie" 为名创建
128
+ // 5. 换 API Key:只认名为 "magpie" 的,没有则以 "magpie" 为名创建
129
129
  const headers = { cookie: `jwt_token=${jwt}`, accept: "application/json" }
130
130
  const user = await fetch(`${BASE}/api/user/current`, { headers }).then((r) =>
131
131
  asJson<{ user_name: string; user_id: number }>(r),
@@ -133,11 +133,9 @@ export async function loginWithCas(username: string, password: string): Promise<
133
133
  const { keys } = await fetch(`${BASE}/api/user/api_keys/?user_id=${user.user_id}`, {
134
134
  headers,
135
135
  }).then((r) =>
136
- asJson<{ keys?: { api_key: string; is_default: boolean; status: string }[] }>(r),
136
+ asJson<{ keys?: { api_key: string; name: string; status: string }[] }>(r),
137
137
  )
138
- const existing = keys?.find((k) => k.is_default && k.status === "ACTIVE") ?? keys?.[0]
139
-
140
- let apiKey = existing?.api_key
138
+ let apiKey = keys?.find((k) => k.name === "magpie" && k.status === "ACTIVE")?.api_key
141
139
  if (!apiKey) {
142
140
  const created = await fetch(`${BASE}/api/user/api_keys/`, {
143
141
  method: "POST",