opencode-cau-maas-auth 0.1.0 → 0.1.1
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 +1 -4
- package/package.json +1 -1
- 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
|
|
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
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
|
|
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;
|
|
136
|
+
asJson<{ keys?: { api_key: string; name: string; status: string }[] }>(r),
|
|
137
137
|
)
|
|
138
|
-
|
|
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",
|