opencode-cau-maas-auth 0.0.0-stage → 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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 kanata
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
19
+ DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20
+ OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
21
+ OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,43 @@
1
- # Temporary Holding Version
1
+ # CAU MaaS — Magpie 插件
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ 将 [maas.cau.edu.cn](https://maas.cau.edu.cn) (中国农业大学 MaaS 平台) 接入 Magpie,支持统一身份认证登录与用量显示。
4
+
5
+ ## 安装
6
+
7
+ ```bash
8
+ magpie plugin add github:YuZiOuO/cau-maas
9
+ magpie plugin login cau-maas
10
+ magpie quota
11
+ ```
12
+
13
+ 本地开发:`magpie plugin add /path/to/cau-maas`(Windows 可用 `\\wsl.localhost\Ubuntu\...` UNC 路径直挂 WSL 目录)。
14
+
15
+ ## 登录方式
16
+
17
+ 1. **统一身份认证登录(推荐)**:输入学号/工号和 CAS 密码,零浏览器零粘贴。插件纯 HTTP 跑完 SSO 链(OIDC → CAS → ticket 回跳)拿 `jwt_token`,自动复用默认 API Key(没有则以 `magpie` 为名创建)。
18
+ 2. **直接粘贴 API Key**:已有 Key 时直接使用。
19
+
20
+ ## 实现要点
21
+
22
+ - `src/index.ts` — 插件主体:`config` 注册 provider、`auth.methods` 两种登录、`loader` 注入 Bearer、`usage` 读 `/api/budget/me`、`provider.models` 列模型
23
+ - `src/sso.ts` — CAS 纯 HTTP 登录:`/api/auth/cau/start` → OIDC → `onecas.cau.edu.cn` CAS 表单(`strEnc(学号+密码+lt)` 三重 DES)→ ticket 回跳 → `Set-Cookie: jwt_token`
24
+ - `src/des-loader.ts` — 平台原版 `des.js`(Guapo DES 变体)内含隐式全局循环变量,ESM 严格模式会炸,故用 `node:vm` 沙箱加载
25
+
26
+ 平台对 Magpie 暴露的接口全部可用 Bearer API Key 访问:
27
+
28
+ | 接口 | 用途 |
29
+ |---|---|
30
+ | `GET /v1/models` | 模型列表 |
31
+ | `POST /v1/chat/completions` | 聊天(OpenAI 兼容) |
32
+ | `GET /api/budget/me` | 本月用量 / 预算 |
33
+ | `GET /api/user/current` | 用户信息 |
34
+ | `POST /api/auth/login` `{api_key}` | Key 换会话 cookie(验证 Key) |
35
+
36
+ ## 开发
37
+
38
+ ```bash
39
+ bun install
40
+ bunx tsc --noEmit
41
+ ```
42
+
43
+ 入口 `src/index.ts`,Magpie 由 Bun 直跑 TS,无需构建。修改后重新 `magpie plugin add .` 生效。
package/package.json CHANGED
@@ -1,6 +1,37 @@
1
1
  {
2
2
  "name": "opencode-cau-maas-auth",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.1",
4
+ "main": "./src/index.ts",
5
+ "devDependencies": {
6
+ "@types/bun": "^1.4.4",
7
+ "typescript": "^5.9.0"
8
+ },
9
+ "description": "CAU MaaS (maas.cau.edu.cn) provider for Magpie — 统一身份认证登录 + 用量显示",
10
+ "license": "MIT",
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "git+https://github.com/YuZiOuO/magpie-cau-maas.git"
14
+ },
15
+ "files": [
16
+ "src",
17
+ "README.md",
18
+ "LICENSE"
19
+ ],
20
+ "keywords": [
21
+ "opencode",
22
+ "opencode-plugin",
23
+ "magpie",
24
+ "cau",
25
+ "new-api"
26
+ ],
27
+ "magpie": {
28
+ "name": "CAU MaaS",
29
+ "icon": "https://www.cau.edu.cn/static2022/images/favicon.ico",
30
+ "summary": {
31
+ "en": "Sign in to maas.cau.edu.cn via CAU SSO and show monthly usage",
32
+ "zh": "通过 CAU 统一身份认证接入 maas.cau.edu.cn 并显示用量"
33
+ },
34
+ "maxConcurrency": 8
35
+ },
36
+ "type": "module"
37
+ }
@@ -0,0 +1,9 @@
1
+ // des.js 是 CAS 平台原版 Guapo DES,内含隐式全局循环变量(非严格模式),
2
+ // ESM 严格模式会 ReferenceError,故用 node:vm 沙箱加载。
3
+ import vm from "node:vm"
4
+
5
+ const sandbox: { strEnc?: (data: string, k1: string, k2: string, k3: string) => string } = {}
6
+ vm.createContext(sandbox)
7
+ vm.runInContext(await Bun.file(new URL("./des.js", import.meta.url)).text(), sandbox)
8
+
9
+ export const strEnc = sandbox.strEnc!
package/src/des.js ADDED
@@ -0,0 +1,867 @@
1
+ /**
2
+ * DES加密解密
3
+ * @Copyright Copyright (c) 2006
4
+ * @author Guapo
5
+ * @see DESCore
6
+ */
7
+
8
+ /*
9
+ * encrypt the string to string made up of hex
10
+ * return the encrypted string
11
+ */
12
+ function strEnc(data,firstKey,secondKey,thirdKey){
13
+
14
+ var leng = data.length;
15
+ var encData = "";
16
+ var firstKeyBt,secondKeyBt,thirdKeyBt,firstLength,secondLength,thirdLength;
17
+ if(firstKey != null && firstKey != ""){
18
+ firstKeyBt = getKeyBytes(firstKey);
19
+ firstLength = firstKeyBt.length;
20
+ }
21
+ if(secondKey != null && secondKey != ""){
22
+ secondKeyBt = getKeyBytes(secondKey);
23
+ secondLength = secondKeyBt.length;
24
+ }
25
+ if(thirdKey != null && thirdKey != ""){
26
+ thirdKeyBt = getKeyBytes(thirdKey);
27
+ thirdLength = thirdKeyBt.length;
28
+ }
29
+
30
+ if(leng > 0){
31
+ if(leng < 4){
32
+ var bt = strToBt(data);
33
+ var encByte ;
34
+ if(firstKey != null && firstKey !="" && secondKey != null && secondKey != "" && thirdKey != null && thirdKey != ""){
35
+ var tempBt;
36
+ var x,y,z;
37
+ tempBt = bt;
38
+ for(x = 0;x < firstLength ;x ++){
39
+ tempBt = enc(tempBt,firstKeyBt[x]);
40
+ }
41
+ for(y = 0;y < secondLength ;y ++){
42
+ tempBt = enc(tempBt,secondKeyBt[y]);
43
+ }
44
+ for(z = 0;z < thirdLength ;z ++){
45
+ tempBt = enc(tempBt,thirdKeyBt[z]);
46
+ }
47
+ encByte = tempBt;
48
+ }else{
49
+ if(firstKey != null && firstKey !="" && secondKey != null && secondKey != ""){
50
+ var tempBt;
51
+ var x,y;
52
+ tempBt = bt;
53
+ for(x = 0;x < firstLength ;x ++){
54
+ tempBt = enc(tempBt,firstKeyBt[x]);
55
+ }
56
+ for(y = 0;y < secondLength ;y ++){
57
+ tempBt = enc(tempBt,secondKeyBt[y]);
58
+ }
59
+ encByte = tempBt;
60
+ }else{
61
+ if(firstKey != null && firstKey !=""){
62
+ var tempBt;
63
+ var x = 0;
64
+ tempBt = bt;
65
+ for(x = 0;x < firstLength ;x ++){
66
+ tempBt = enc(tempBt,firstKeyBt[x]);
67
+ }
68
+ encByte = tempBt;
69
+ }
70
+ }
71
+ }
72
+ encData = bt64ToHex(encByte);
73
+ }else{
74
+ var iterator = parseInt(leng/4);
75
+ var remainder = leng%4;
76
+ var i=0;
77
+ for(i = 0;i < iterator;i++){
78
+ var tempData = data.substring(i*4+0,i*4+4);
79
+ var tempByte = strToBt(tempData);
80
+ var encByte ;
81
+ if(firstKey != null && firstKey !="" && secondKey != null && secondKey != "" && thirdKey != null && thirdKey != ""){
82
+ var tempBt;
83
+ var x,y,z;
84
+ tempBt = tempByte;
85
+ for(x = 0;x < firstLength ;x ++){
86
+ tempBt = enc(tempBt,firstKeyBt[x]);
87
+ }
88
+ for(y = 0;y < secondLength ;y ++){
89
+ tempBt = enc(tempBt,secondKeyBt[y]);
90
+ }
91
+ for(z = 0;z < thirdLength ;z ++){
92
+ tempBt = enc(tempBt,thirdKeyBt[z]);
93
+ }
94
+ encByte = tempBt;
95
+ }else{
96
+ if(firstKey != null && firstKey !="" && secondKey != null && secondKey != ""){
97
+ var tempBt;
98
+ var x,y;
99
+ tempBt = tempByte;
100
+ for(x = 0;x < firstLength ;x ++){
101
+ tempBt = enc(tempBt,firstKeyBt[x]);
102
+ }
103
+ for(y = 0;y < secondLength ;y ++){
104
+ tempBt = enc(tempBt,secondKeyBt[y]);
105
+ }
106
+ encByte = tempBt;
107
+ }else{
108
+ if(firstKey != null && firstKey !=""){
109
+ var tempBt;
110
+ var x;
111
+ tempBt = tempByte;
112
+ for(x = 0;x < firstLength ;x ++){
113
+ tempBt = enc(tempBt,firstKeyBt[x]);
114
+ }
115
+ encByte = tempBt;
116
+ }
117
+ }
118
+ }
119
+ encData += bt64ToHex(encByte);
120
+ }
121
+ if(remainder > 0){
122
+ var remainderData = data.substring(iterator*4+0,leng);
123
+ var tempByte = strToBt(remainderData);
124
+ var encByte ;
125
+ if(firstKey != null && firstKey !="" && secondKey != null && secondKey != "" && thirdKey != null && thirdKey != ""){
126
+ var tempBt;
127
+ var x,y,z;
128
+ tempBt = tempByte;
129
+ for(x = 0;x < firstLength ;x ++){
130
+ tempBt = enc(tempBt,firstKeyBt[x]);
131
+ }
132
+ for(y = 0;y < secondLength ;y ++){
133
+ tempBt = enc(tempBt,secondKeyBt[y]);
134
+ }
135
+ for(z = 0;z < thirdLength ;z ++){
136
+ tempBt = enc(tempBt,thirdKeyBt[z]);
137
+ }
138
+ encByte = tempBt;
139
+ }else{
140
+ if(firstKey != null && firstKey !="" && secondKey != null && secondKey != ""){
141
+ var tempBt;
142
+ var x,y;
143
+ tempBt = tempByte;
144
+ for(x = 0;x < firstLength ;x ++){
145
+ tempBt = enc(tempBt,firstKeyBt[x]);
146
+ }
147
+ for(y = 0;y < secondLength ;y ++){
148
+ tempBt = enc(tempBt,secondKeyBt[y]);
149
+ }
150
+ encByte = tempBt;
151
+ }else{
152
+ if(firstKey != null && firstKey !=""){
153
+ var tempBt;
154
+ var x;
155
+ tempBt = tempByte;
156
+ for(x = 0;x < firstLength ;x ++){
157
+ tempBt = enc(tempBt,firstKeyBt[x]);
158
+ }
159
+ encByte = tempBt;
160
+ }
161
+ }
162
+ }
163
+ encData += bt64ToHex(encByte);
164
+ }
165
+ }
166
+ }
167
+ return encData;
168
+ }
169
+
170
+ /*
171
+ * decrypt the encrypted string to the original string
172
+ *
173
+ * return the original string
174
+ */
175
+ function strDec(data,firstKey,secondKey,thirdKey){
176
+ var leng = data.length;
177
+ var decStr = "";
178
+ var firstKeyBt,secondKeyBt,thirdKeyBt,firstLength,secondLength,thirdLength;
179
+ if(firstKey != null && firstKey != ""){
180
+ firstKeyBt = getKeyBytes(firstKey);
181
+ firstLength = firstKeyBt.length;
182
+ }
183
+ if(secondKey != null && secondKey != ""){
184
+ secondKeyBt = getKeyBytes(secondKey);
185
+ secondLength = secondKeyBt.length;
186
+ }
187
+ if(thirdKey != null && thirdKey != ""){
188
+ thirdKeyBt = getKeyBytes(thirdKey);
189
+ thirdLength = thirdKeyBt.length;
190
+ }
191
+
192
+ var iterator = parseInt(leng/16);
193
+ var i=0;
194
+ for(i = 0;i < iterator;i++){
195
+ var tempData = data.substring(i*16+0,i*16+16);
196
+ var strByte = hexToBt64(tempData);
197
+ var intByte = new Array(64);
198
+ var j = 0;
199
+ for(j = 0;j < 64; j++){
200
+ intByte[j] = parseInt(strByte.substring(j,j+1));
201
+ }
202
+ var decByte;
203
+ if(firstKey != null && firstKey !="" && secondKey != null && secondKey != "" && thirdKey != null && thirdKey != ""){
204
+ var tempBt;
205
+ var x,y,z;
206
+ tempBt = intByte;
207
+ for(x = thirdLength - 1;x >= 0;x --){
208
+ tempBt = dec(tempBt,thirdKeyBt[x]);
209
+ }
210
+ for(y = secondLength - 1;y >= 0;y --){
211
+ tempBt = dec(tempBt,secondKeyBt[y]);
212
+ }
213
+ for(z = firstLength - 1;z >= 0 ;z --){
214
+ tempBt = dec(tempBt,firstKeyBt[z]);
215
+ }
216
+ decByte = tempBt;
217
+ }else{
218
+ if(firstKey != null && firstKey !="" && secondKey != null && secondKey != ""){
219
+ var tempBt;
220
+ var x,y,z;
221
+ tempBt = intByte;
222
+ for(x = secondLength - 1;x >= 0 ;x --){
223
+ tempBt = dec(tempBt,secondKeyBt[x]);
224
+ }
225
+ for(y = firstLength - 1;y >= 0 ;y --){
226
+ tempBt = dec(tempBt,firstKeyBt[y]);
227
+ }
228
+ decByte = tempBt;
229
+ }else{
230
+ if(firstKey != null && firstKey !=""){
231
+ var tempBt;
232
+ var x,y,z;
233
+ tempBt = intByte;
234
+ for(x = firstLength - 1;x >= 0 ;x --){
235
+ tempBt = dec(tempBt,firstKeyBt[x]);
236
+ }
237
+ decByte = tempBt;
238
+ }
239
+ }
240
+ }
241
+ decStr += byteToString(decByte);
242
+ }
243
+ return decStr;
244
+ }
245
+ /*
246
+ * chang the string into the bit array
247
+ *
248
+ * return bit array(it's length % 64 = 0)
249
+ */
250
+ function getKeyBytes(key){
251
+ var keyBytes = new Array();
252
+ var leng = key.length;
253
+ var iterator = parseInt(leng/4);
254
+ var remainder = leng%4;
255
+ var i = 0;
256
+ for(i = 0;i < iterator; i ++){
257
+ keyBytes[i] = strToBt(key.substring(i*4+0,i*4+4));
258
+ }
259
+ if(remainder > 0){
260
+ keyBytes[i] = strToBt(key.substring(i*4+0,leng));
261
+ }
262
+ return keyBytes;
263
+ }
264
+
265
+ /*
266
+ * chang the string(it's length <= 4) into the bit array
267
+ *
268
+ * return bit array(it's length = 64)
269
+ */
270
+ function strToBt(str){
271
+ var leng = str.length;
272
+ var bt = new Array(64);
273
+ if(leng < 4){
274
+ var i=0,j=0,p=0,q=0;
275
+ for(i = 0;i<leng;i++){
276
+ var k = str.charCodeAt(i);
277
+ for(j=0;j<16;j++){
278
+ var pow=1,m=0;
279
+ for(m=15;m>j;m--){
280
+ pow *= 2;
281
+ }
282
+ bt[16*i+j]=parseInt(k/pow)%2;
283
+ }
284
+ }
285
+ for(p = leng;p<4;p++){
286
+ var k = 0;
287
+ for(q=0;q<16;q++){
288
+ var pow=1,m=0;
289
+ for(m=15;m>q;m--){
290
+ pow *= 2;
291
+ }
292
+ bt[16*p+q]=parseInt(k/pow)%2;
293
+ }
294
+ }
295
+ }else{
296
+ for(i = 0;i<4;i++){
297
+ var k = str.charCodeAt(i);
298
+ for(j=0;j<16;j++){
299
+ var pow=1;
300
+ for(m=15;m>j;m--){
301
+ pow *= 2;
302
+ }
303
+ bt[16*i+j]=parseInt(k/pow)%2;
304
+ }
305
+ }
306
+ }
307
+ return bt;
308
+ }
309
+
310
+ /*
311
+ * chang the bit(it's length = 4) into the hex
312
+ *
313
+ * return hex
314
+ */
315
+ function bt4ToHex(binary) {
316
+ var hex;
317
+ switch (binary) {
318
+ case "0000" : hex = "0"; break;
319
+ case "0001" : hex = "1"; break;
320
+ case "0010" : hex = "2"; break;
321
+ case "0011" : hex = "3"; break;
322
+ case "0100" : hex = "4"; break;
323
+ case "0101" : hex = "5"; break;
324
+ case "0110" : hex = "6"; break;
325
+ case "0111" : hex = "7"; break;
326
+ case "1000" : hex = "8"; break;
327
+ case "1001" : hex = "9"; break;
328
+ case "1010" : hex = "A"; break;
329
+ case "1011" : hex = "B"; break;
330
+ case "1100" : hex = "C"; break;
331
+ case "1101" : hex = "D"; break;
332
+ case "1110" : hex = "E"; break;
333
+ case "1111" : hex = "F"; break;
334
+ }
335
+ return hex;
336
+ }
337
+
338
+ /*
339
+ * chang the hex into the bit(it's length = 4)
340
+ *
341
+ * return the bit(it's length = 4)
342
+ */
343
+ function hexToBt4(hex) {
344
+ var binary;
345
+ switch (hex) {
346
+ case "0" : binary = "0000"; break;
347
+ case "1" : binary = "0001"; break;
348
+ case "2" : binary = "0010"; break;
349
+ case "3" : binary = "0011"; break;
350
+ case "4" : binary = "0100"; break;
351
+ case "5" : binary = "0101"; break;
352
+ case "6" : binary = "0110"; break;
353
+ case "7" : binary = "0111"; break;
354
+ case "8" : binary = "1000"; break;
355
+ case "9" : binary = "1001"; break;
356
+ case "A" : binary = "1010"; break;
357
+ case "B" : binary = "1011"; break;
358
+ case "C" : binary = "1100"; break;
359
+ case "D" : binary = "1101"; break;
360
+ case "E" : binary = "1110"; break;
361
+ case "F" : binary = "1111"; break;
362
+ }
363
+ return binary;
364
+ }
365
+
366
+ /*
367
+ * chang the bit(it's length = 64) into the string
368
+ *
369
+ * return string
370
+ */
371
+ function byteToString(byteData){
372
+ var str="";
373
+ for(i = 0;i<4;i++){
374
+ var count=0;
375
+ for(j=0;j<16;j++){
376
+ var pow=1;
377
+ for(m=15;m>j;m--){
378
+ pow*=2;
379
+ }
380
+ count+=byteData[16*i+j]*pow;
381
+ }
382
+ if(count != 0){
383
+ str+=String.fromCharCode(count);
384
+ }
385
+ }
386
+ return str;
387
+ }
388
+
389
+ function bt64ToHex(byteData){
390
+ var hex = "";
391
+ for(i = 0;i<16;i++){
392
+ var bt = "";
393
+ for(j=0;j<4;j++){
394
+ bt += byteData[i*4+j];
395
+ }
396
+ hex+=bt4ToHex(bt);
397
+ }
398
+ return hex;
399
+ }
400
+
401
+ function hexToBt64(hex){
402
+ var binary = "";
403
+ for(i = 0;i<16;i++){
404
+ binary+=hexToBt4(hex.substring(i,i+1));
405
+ }
406
+ return binary;
407
+ }
408
+
409
+ /*
410
+ * the 64 bit des core arithmetic
411
+ */
412
+
413
+ function enc(dataByte,keyByte){
414
+ var keys = generateKeys(keyByte);
415
+ var ipByte = initPermute(dataByte);
416
+ var ipLeft = new Array(32);
417
+ var ipRight = new Array(32);
418
+ var tempLeft = new Array(32);
419
+ var i = 0,j = 0,k = 0,m = 0, n = 0;
420
+ for(k = 0;k < 32;k ++){
421
+ ipLeft[k] = ipByte[k];
422
+ ipRight[k] = ipByte[32+k];
423
+ }
424
+ for(i = 0;i < 16;i ++){
425
+ for(j = 0;j < 32;j ++){
426
+ tempLeft[j] = ipLeft[j];
427
+ ipLeft[j] = ipRight[j];
428
+ }
429
+ var key = new Array(48);
430
+ for(m = 0;m < 48;m ++){
431
+ key[m] = keys[i][m];
432
+ }
433
+ var tempRight = xor(pPermute(sBoxPermute(xor(expandPermute(ipRight),key))), tempLeft);
434
+ for(n = 0;n < 32;n ++){
435
+ ipRight[n] = tempRight[n];
436
+ }
437
+
438
+ }
439
+
440
+
441
+ var finalData =new Array(64);
442
+ for(i = 0;i < 32;i ++){
443
+ finalData[i] = ipRight[i];
444
+ finalData[32+i] = ipLeft[i];
445
+ }
446
+ return finallyPermute(finalData);
447
+ }
448
+
449
+ function dec(dataByte,keyByte){
450
+ var keys = generateKeys(keyByte);
451
+ var ipByte = initPermute(dataByte);
452
+ var ipLeft = new Array(32);
453
+ var ipRight = new Array(32);
454
+ var tempLeft = new Array(32);
455
+ var i = 0,j = 0,k = 0,m = 0, n = 0;
456
+ for(k = 0;k < 32;k ++){
457
+ ipLeft[k] = ipByte[k];
458
+ ipRight[k] = ipByte[32+k];
459
+ }
460
+ for(i = 15;i >= 0;i --){
461
+ for(j = 0;j < 32;j ++){
462
+ tempLeft[j] = ipLeft[j];
463
+ ipLeft[j] = ipRight[j];
464
+ }
465
+ var key = new Array(48);
466
+ for(m = 0;m < 48;m ++){
467
+ key[m] = keys[i][m];
468
+ }
469
+
470
+ var tempRight = xor(pPermute(sBoxPermute(xor(expandPermute(ipRight),key))), tempLeft);
471
+ for(n = 0;n < 32;n ++){
472
+ ipRight[n] = tempRight[n];
473
+ }
474
+ }
475
+
476
+
477
+ var finalData =new Array(64);
478
+ for(i = 0;i < 32;i ++){
479
+ finalData[i] = ipRight[i];
480
+ finalData[32+i] = ipLeft[i];
481
+ }
482
+ return finallyPermute(finalData);
483
+ }
484
+
485
+ function initPermute(originalData){
486
+ var ipByte = new Array(64);
487
+ for (i = 0, m = 1, n = 0; i < 4; i++, m += 2, n += 2) {
488
+ for (j = 7, k = 0; j >= 0; j--, k++) {
489
+ ipByte[i * 8 + k] = originalData[j * 8 + m];
490
+ ipByte[i * 8 + k + 32] = originalData[j * 8 + n];
491
+ }
492
+ }
493
+ return ipByte;
494
+ }
495
+
496
+ function expandPermute(rightData){
497
+ var epByte = new Array(48);
498
+ for (i = 0; i < 8; i++) {
499
+ if (i == 0) {
500
+ epByte[i * 6 + 0] = rightData[31];
501
+ } else {
502
+ epByte[i * 6 + 0] = rightData[i * 4 - 1];
503
+ }
504
+ epByte[i * 6 + 1] = rightData[i * 4 + 0];
505
+ epByte[i * 6 + 2] = rightData[i * 4 + 1];
506
+ epByte[i * 6 + 3] = rightData[i * 4 + 2];
507
+ epByte[i * 6 + 4] = rightData[i * 4 + 3];
508
+ if (i == 7) {
509
+ epByte[i * 6 + 5] = rightData[0];
510
+ } else {
511
+ epByte[i * 6 + 5] = rightData[i * 4 + 4];
512
+ }
513
+ }
514
+ return epByte;
515
+ }
516
+
517
+ function xor(byteOne,byteTwo){
518
+ var xorByte = new Array(byteOne.length);
519
+ for(i = 0;i < byteOne.length; i ++){
520
+ xorByte[i] = byteOne[i] ^ byteTwo[i];
521
+ }
522
+ return xorByte;
523
+ }
524
+
525
+ function sBoxPermute(expandByte){
526
+
527
+ var sBoxByte = new Array(32);
528
+ var binary = "";
529
+ var s1 = [
530
+ [14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7],
531
+ [0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8],
532
+ [4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0],
533
+ [15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13 ]];
534
+
535
+ /* Table - s2 */
536
+ var s2 = [
537
+ [15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10],
538
+ [3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5],
539
+ [0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15],
540
+ [13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9 ]];
541
+
542
+ /* Table - s3 */
543
+ var s3= [
544
+ [10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8],
545
+ [13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1],
546
+ [13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7],
547
+ [1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12 ]];
548
+ /* Table - s4 */
549
+ var s4 = [
550
+ [7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15],
551
+ [13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9],
552
+ [10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4],
553
+ [3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14 ]];
554
+
555
+ /* Table - s5 */
556
+ var s5 = [
557
+ [2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9],
558
+ [14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6],
559
+ [4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14],
560
+ [11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3 ]];
561
+
562
+ /* Table - s6 */
563
+ var s6 = [
564
+ [12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11],
565
+ [10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8],
566
+ [9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6],
567
+ [4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13 ]];
568
+
569
+ /* Table - s7 */
570
+ var s7 = [
571
+ [4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1],
572
+ [13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6],
573
+ [1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2],
574
+ [6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12]];
575
+
576
+ /* Table - s8 */
577
+ var s8 = [
578
+ [13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7],
579
+ [1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2],
580
+ [7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8],
581
+ [2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11]];
582
+
583
+ for(m=0;m<8;m++){
584
+ var i=0,j=0;
585
+ i = expandByte[m*6+0]*2+expandByte[m*6+5];
586
+ j = expandByte[m * 6 + 1] * 2 * 2 * 2
587
+ + expandByte[m * 6 + 2] * 2* 2
588
+ + expandByte[m * 6 + 3] * 2
589
+ + expandByte[m * 6 + 4];
590
+ switch (m) {
591
+ case 0 :
592
+ binary = getBoxBinary(s1[i][j]);
593
+ break;
594
+ case 1 :
595
+ binary = getBoxBinary(s2[i][j]);
596
+ break;
597
+ case 2 :
598
+ binary = getBoxBinary(s3[i][j]);
599
+ break;
600
+ case 3 :
601
+ binary = getBoxBinary(s4[i][j]);
602
+ break;
603
+ case 4 :
604
+ binary = getBoxBinary(s5[i][j]);
605
+ break;
606
+ case 5 :
607
+ binary = getBoxBinary(s6[i][j]);
608
+ break;
609
+ case 6 :
610
+ binary = getBoxBinary(s7[i][j]);
611
+ break;
612
+ case 7 :
613
+ binary = getBoxBinary(s8[i][j]);
614
+ break;
615
+ }
616
+ sBoxByte[m*4+0] = parseInt(binary.substring(0,1));
617
+ sBoxByte[m*4+1] = parseInt(binary.substring(1,2));
618
+ sBoxByte[m*4+2] = parseInt(binary.substring(2,3));
619
+ sBoxByte[m*4+3] = parseInt(binary.substring(3,4));
620
+ }
621
+ return sBoxByte;
622
+ }
623
+
624
+ function pPermute(sBoxByte){
625
+ var pBoxPermute = new Array(32);
626
+ pBoxPermute[ 0] = sBoxByte[15];
627
+ pBoxPermute[ 1] = sBoxByte[ 6];
628
+ pBoxPermute[ 2] = sBoxByte[19];
629
+ pBoxPermute[ 3] = sBoxByte[20];
630
+ pBoxPermute[ 4] = sBoxByte[28];
631
+ pBoxPermute[ 5] = sBoxByte[11];
632
+ pBoxPermute[ 6] = sBoxByte[27];
633
+ pBoxPermute[ 7] = sBoxByte[16];
634
+ pBoxPermute[ 8] = sBoxByte[ 0];
635
+ pBoxPermute[ 9] = sBoxByte[14];
636
+ pBoxPermute[10] = sBoxByte[22];
637
+ pBoxPermute[11] = sBoxByte[25];
638
+ pBoxPermute[12] = sBoxByte[ 4];
639
+ pBoxPermute[13] = sBoxByte[17];
640
+ pBoxPermute[14] = sBoxByte[30];
641
+ pBoxPermute[15] = sBoxByte[ 9];
642
+ pBoxPermute[16] = sBoxByte[ 1];
643
+ pBoxPermute[17] = sBoxByte[ 7];
644
+ pBoxPermute[18] = sBoxByte[23];
645
+ pBoxPermute[19] = sBoxByte[13];
646
+ pBoxPermute[20] = sBoxByte[31];
647
+ pBoxPermute[21] = sBoxByte[26];
648
+ pBoxPermute[22] = sBoxByte[ 2];
649
+ pBoxPermute[23] = sBoxByte[ 8];
650
+ pBoxPermute[24] = sBoxByte[18];
651
+ pBoxPermute[25] = sBoxByte[12];
652
+ pBoxPermute[26] = sBoxByte[29];
653
+ pBoxPermute[27] = sBoxByte[ 5];
654
+ pBoxPermute[28] = sBoxByte[21];
655
+ pBoxPermute[29] = sBoxByte[10];
656
+ pBoxPermute[30] = sBoxByte[ 3];
657
+ pBoxPermute[31] = sBoxByte[24];
658
+ return pBoxPermute;
659
+ }
660
+
661
+ function finallyPermute(endByte){
662
+ var fpByte = new Array(64);
663
+ fpByte[ 0] = endByte[39];
664
+ fpByte[ 1] = endByte[ 7];
665
+ fpByte[ 2] = endByte[47];
666
+ fpByte[ 3] = endByte[15];
667
+ fpByte[ 4] = endByte[55];
668
+ fpByte[ 5] = endByte[23];
669
+ fpByte[ 6] = endByte[63];
670
+ fpByte[ 7] = endByte[31];
671
+ fpByte[ 8] = endByte[38];
672
+ fpByte[ 9] = endByte[ 6];
673
+ fpByte[10] = endByte[46];
674
+ fpByte[11] = endByte[14];
675
+ fpByte[12] = endByte[54];
676
+ fpByte[13] = endByte[22];
677
+ fpByte[14] = endByte[62];
678
+ fpByte[15] = endByte[30];
679
+ fpByte[16] = endByte[37];
680
+ fpByte[17] = endByte[ 5];
681
+ fpByte[18] = endByte[45];
682
+ fpByte[19] = endByte[13];
683
+ fpByte[20] = endByte[53];
684
+ fpByte[21] = endByte[21];
685
+ fpByte[22] = endByte[61];
686
+ fpByte[23] = endByte[29];
687
+ fpByte[24] = endByte[36];
688
+ fpByte[25] = endByte[ 4];
689
+ fpByte[26] = endByte[44];
690
+ fpByte[27] = endByte[12];
691
+ fpByte[28] = endByte[52];
692
+ fpByte[29] = endByte[20];
693
+ fpByte[30] = endByte[60];
694
+ fpByte[31] = endByte[28];
695
+ fpByte[32] = endByte[35];
696
+ fpByte[33] = endByte[ 3];
697
+ fpByte[34] = endByte[43];
698
+ fpByte[35] = endByte[11];
699
+ fpByte[36] = endByte[51];
700
+ fpByte[37] = endByte[19];
701
+ fpByte[38] = endByte[59];
702
+ fpByte[39] = endByte[27];
703
+ fpByte[40] = endByte[34];
704
+ fpByte[41] = endByte[ 2];
705
+ fpByte[42] = endByte[42];
706
+ fpByte[43] = endByte[10];
707
+ fpByte[44] = endByte[50];
708
+ fpByte[45] = endByte[18];
709
+ fpByte[46] = endByte[58];
710
+ fpByte[47] = endByte[26];
711
+ fpByte[48] = endByte[33];
712
+ fpByte[49] = endByte[ 1];
713
+ fpByte[50] = endByte[41];
714
+ fpByte[51] = endByte[ 9];
715
+ fpByte[52] = endByte[49];
716
+ fpByte[53] = endByte[17];
717
+ fpByte[54] = endByte[57];
718
+ fpByte[55] = endByte[25];
719
+ fpByte[56] = endByte[32];
720
+ fpByte[57] = endByte[ 0];
721
+ fpByte[58] = endByte[40];
722
+ fpByte[59] = endByte[ 8];
723
+ fpByte[60] = endByte[48];
724
+ fpByte[61] = endByte[16];
725
+ fpByte[62] = endByte[56];
726
+ fpByte[63] = endByte[24];
727
+ return fpByte;
728
+ }
729
+
730
+ function getBoxBinary(i) {
731
+ var binary = "";
732
+ switch (i) {
733
+ case 0 :binary = "0000";break;
734
+ case 1 :binary = "0001";break;
735
+ case 2 :binary = "0010";break;
736
+ case 3 :binary = "0011";break;
737
+ case 4 :binary = "0100";break;
738
+ case 5 :binary = "0101";break;
739
+ case 6 :binary = "0110";break;
740
+ case 7 :binary = "0111";break;
741
+ case 8 :binary = "1000";break;
742
+ case 9 :binary = "1001";break;
743
+ case 10 :binary = "1010";break;
744
+ case 11 :binary = "1011";break;
745
+ case 12 :binary = "1100";break;
746
+ case 13 :binary = "1101";break;
747
+ case 14 :binary = "1110";break;
748
+ case 15 :binary = "1111";break;
749
+ }
750
+ return binary;
751
+ }
752
+ /*
753
+ * generate 16 keys for xor
754
+ *
755
+ */
756
+ function generateKeys(keyByte){
757
+ var key = new Array(56);
758
+ var keys = new Array();
759
+
760
+ keys[ 0] = new Array();
761
+ keys[ 1] = new Array();
762
+ keys[ 2] = new Array();
763
+ keys[ 3] = new Array();
764
+ keys[ 4] = new Array();
765
+ keys[ 5] = new Array();
766
+ keys[ 6] = new Array();
767
+ keys[ 7] = new Array();
768
+ keys[ 8] = new Array();
769
+ keys[ 9] = new Array();
770
+ keys[10] = new Array();
771
+ keys[11] = new Array();
772
+ keys[12] = new Array();
773
+ keys[13] = new Array();
774
+ keys[14] = new Array();
775
+ keys[15] = new Array();
776
+ var loop = [1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1];
777
+
778
+ for(i=0;i<7;i++){
779
+ for(j=0,k=7;j<8;j++,k--){
780
+ key[i*8+j]=keyByte[8*k+i];
781
+ }
782
+ }
783
+
784
+ var i = 0;
785
+ for(i = 0;i < 16;i ++){
786
+ var tempLeft=0;
787
+ var tempRight=0;
788
+ for(j = 0; j < loop[i];j ++){
789
+ tempLeft = key[0];
790
+ tempRight = key[28];
791
+ for(k = 0;k < 27 ;k ++){
792
+ key[k] = key[k+1];
793
+ key[28+k] = key[29+k];
794
+ }
795
+ key[27]=tempLeft;
796
+ key[55]=tempRight;
797
+ }
798
+ var tempKey = new Array(48);
799
+ tempKey[ 0] = key[13];
800
+ tempKey[ 1] = key[16];
801
+ tempKey[ 2] = key[10];
802
+ tempKey[ 3] = key[23];
803
+ tempKey[ 4] = key[ 0];
804
+ tempKey[ 5] = key[ 4];
805
+ tempKey[ 6] = key[ 2];
806
+ tempKey[ 7] = key[27];
807
+ tempKey[ 8] = key[14];
808
+ tempKey[ 9] = key[ 5];
809
+ tempKey[10] = key[20];
810
+ tempKey[11] = key[ 9];
811
+ tempKey[12] = key[22];
812
+ tempKey[13] = key[18];
813
+ tempKey[14] = key[11];
814
+ tempKey[15] = key[ 3];
815
+ tempKey[16] = key[25];
816
+ tempKey[17] = key[ 7];
817
+ tempKey[18] = key[15];
818
+ tempKey[19] = key[ 6];
819
+ tempKey[20] = key[26];
820
+ tempKey[21] = key[19];
821
+ tempKey[22] = key[12];
822
+ tempKey[23] = key[ 1];
823
+ tempKey[24] = key[40];
824
+ tempKey[25] = key[51];
825
+ tempKey[26] = key[30];
826
+ tempKey[27] = key[36];
827
+ tempKey[28] = key[46];
828
+ tempKey[29] = key[54];
829
+ tempKey[30] = key[29];
830
+ tempKey[31] = key[39];
831
+ tempKey[32] = key[50];
832
+ tempKey[33] = key[44];
833
+ tempKey[34] = key[32];
834
+ tempKey[35] = key[47];
835
+ tempKey[36] = key[43];
836
+ tempKey[37] = key[48];
837
+ tempKey[38] = key[38];
838
+ tempKey[39] = key[55];
839
+ tempKey[40] = key[33];
840
+ tempKey[41] = key[52];
841
+ tempKey[42] = key[45];
842
+ tempKey[43] = key[41];
843
+ tempKey[44] = key[49];
844
+ tempKey[45] = key[35];
845
+ tempKey[46] = key[28];
846
+ tempKey[47] = key[31];
847
+ switch(i){
848
+ case 0: for(m=0;m < 48 ;m++){ keys[ 0][m] = tempKey[m]; } break;
849
+ case 1: for(m=0;m < 48 ;m++){ keys[ 1][m] = tempKey[m]; } break;
850
+ case 2: for(m=0;m < 48 ;m++){ keys[ 2][m] = tempKey[m]; } break;
851
+ case 3: for(m=0;m < 48 ;m++){ keys[ 3][m] = tempKey[m]; } break;
852
+ case 4: for(m=0;m < 48 ;m++){ keys[ 4][m] = tempKey[m]; } break;
853
+ case 5: for(m=0;m < 48 ;m++){ keys[ 5][m] = tempKey[m]; } break;
854
+ case 6: for(m=0;m < 48 ;m++){ keys[ 6][m] = tempKey[m]; } break;
855
+ case 7: for(m=0;m < 48 ;m++){ keys[ 7][m] = tempKey[m]; } break;
856
+ case 8: for(m=0;m < 48 ;m++){ keys[ 8][m] = tempKey[m]; } break;
857
+ case 9: for(m=0;m < 48 ;m++){ keys[ 9][m] = tempKey[m]; } break;
858
+ case 10: for(m=0;m < 48 ;m++){ keys[10][m] = tempKey[m]; } break;
859
+ case 11: for(m=0;m < 48 ;m++){ keys[11][m] = tempKey[m]; } break;
860
+ case 12: for(m=0;m < 48 ;m++){ keys[12][m] = tempKey[m]; } break;
861
+ case 13: for(m=0;m < 48 ;m++){ keys[13][m] = tempKey[m]; } break;
862
+ case 14: for(m=0;m < 48 ;m++){ keys[14][m] = tempKey[m]; } break;
863
+ case 15: for(m=0;m < 48 ;m++){ keys[15][m] = tempKey[m]; } break;
864
+ }
865
+ }
866
+ return keys;
867
+ }
package/src/index.ts ADDED
@@ -0,0 +1,137 @@
1
+ // CAU MaaS (https://maas.cau.edu.cn) — New API 套壳,OpenAI 兼容 /v1。
2
+ // API Key 做 Bearer 即可覆盖聊天、模型列表、用量。
3
+ // 登录方式一:学号 + CAS 密码,纯 HTTP 跑完 SSO 链(sso.ts)后复用/创建 API Key。
4
+ // 登录方式二:直接粘贴 API Key。
5
+
6
+ import { loginWithCas } from "./sso"
7
+
8
+ const BASE = "https://maas.cau.edu.cn"
9
+ const PROVIDER_ID = "cau-maas"
10
+
11
+ const asJson = async <T>(res: Response): Promise<T> => {
12
+ if (!res.ok) throw new Error(`HTTP ${res.status}`)
13
+ return (await res.json()) as T
14
+ }
15
+
16
+ export const CauMaasPlugin = async () => ({
17
+ async config(cfg: any) {
18
+ cfg.provider ??= {}
19
+ cfg.provider[PROVIDER_ID] ??= {
20
+ name: "CAU MaaS",
21
+ npm: "@ai-sdk/openai-compatible",
22
+ api: `${BASE}/v1`,
23
+ models: {},
24
+ }
25
+ },
26
+
27
+ auth: {
28
+ provider: PROVIDER_ID,
29
+ icon: "https://www.cau.edu.cn/static2022/images/favicon.ico",
30
+ methods: [
31
+ {
32
+ type: "api",
33
+ label: "统一身份认证登录(学号 + CAS 密码)",
34
+ keyless: true,
35
+ prompts: [
36
+ { type: "text", key: "username", message: "学号/工号" },
37
+ { type: "password", key: "password", message: "统一身份认证密码" },
38
+ ],
39
+ async authorize(inputs: { username?: string; password?: string } | undefined) {
40
+ try {
41
+ if (!inputs?.username || !inputs.password)
42
+ return { type: "failed" as const, error: "请输入学号和密码" }
43
+ const r = await loginWithCas(inputs.username, inputs.password)
44
+ return {
45
+ type: "success" as const,
46
+ key: r.apiKey,
47
+ accountId: r.accountId,
48
+ metadata: { email: r.userName },
49
+ }
50
+ } catch (e) {
51
+ return {
52
+ type: "failed" as const,
53
+ error: e instanceof Error ? e.message : "SSO 登录失败",
54
+ }
55
+ }
56
+ },
57
+ },
58
+ { type: "api", label: "直接粘贴 API Key", placeholder: "MaaS 个人中心 → API Key" },
59
+ ],
60
+
61
+ async loader(getAuth: () => Promise<{ type?: string; key?: string } | undefined>) {
62
+ const auth = await getAuth()
63
+ if (auth?.type !== "api" || !auth.key) return {}
64
+ const key = auth.key
65
+ return {
66
+ baseURL: `${BASE}/v1`,
67
+ async fetch(input: RequestInfo | URL, init?: RequestInit) {
68
+ const headers = new Headers(init?.headers)
69
+ headers.set("authorization", `Bearer ${key}`)
70
+ return fetch(input, { ...init, headers })
71
+ },
72
+ }
73
+ },
74
+
75
+ async usage(getAuth: () => Promise<{ type?: string; key?: string } | undefined>) {
76
+ const auth = await getAuth()
77
+ if (auth?.type !== "api" || !auth.key) return { error: "未登录", signIn: "expired" as const }
78
+
79
+ const headers = { authorization: `Bearer ${auth.key}`, accept: "application/json" }
80
+ try {
81
+ const [budget, user] = await Promise.all([
82
+ fetch(`${BASE}/api/budget/me`, { headers }).then((r) =>
83
+ asJson<{
84
+ month_window?: { month_end_time_secs: number }
85
+ member_monthly_used?: string
86
+ effective_member_monthly_budget?: string
87
+ }>(r),
88
+ ),
89
+ fetch(`${BASE}/api/user/current`, { headers }).then((r) =>
90
+ asJson<{ user_name: string }>(r),
91
+ ),
92
+ ])
93
+
94
+ const used = parseFloat(budget.member_monthly_used ?? "0")
95
+ const limit = parseFloat(budget.effective_member_monthly_budget ?? "0")
96
+ const until = budget.month_window?.month_end_time_secs
97
+
98
+ return {
99
+ user: user.user_name,
100
+ plan: "CAU MaaS",
101
+ until: until ? new Date(until * 1000).toISOString() : undefined,
102
+ windows: [
103
+ {
104
+ name: "本月",
105
+ // 金额为人民币元;limit=0 表示未设上限,仅展示已用
106
+ display: limit > 0 ? `¥${used.toFixed(4)} / ¥${limit}` : `已用 ¥${used.toFixed(4)}`,
107
+ ...(limit > 0 ? { amount: used, limit, unit: "¥" } : {}),
108
+ resetsAt: until ? new Date(until * 1000).toISOString() : undefined,
109
+ },
110
+ ],
111
+ signIn: "renewed" as const,
112
+ }
113
+ } catch (e) {
114
+ const expired = /HTTP 401|HTTP 403/.test(String(e))
115
+ return {
116
+ error: expired ? "登录已过期" : "用量读取失败",
117
+ signIn: expired ? ("expired" as const) : ("kept" as const),
118
+ }
119
+ }
120
+ },
121
+ },
122
+
123
+ provider: {
124
+ id: PROVIDER_ID,
125
+ async models(provider: any, { auth }: { auth?: { type?: string; key?: string } } = {}) {
126
+ if (auth?.type !== "api" || !auth.key) return provider.models
127
+ const res = await fetch(`${BASE}/v1/models`, {
128
+ headers: { authorization: `Bearer ${auth.key}` },
129
+ })
130
+ if (!res.ok) return provider.models
131
+ const { data } = (await res.json()) as { data: { id: string; name?: string }[] }
132
+ return Object.fromEntries(data.map((m) => [m.id, { id: m.id, name: m.name ?? m.id }]))
133
+ },
134
+ },
135
+ })
136
+
137
+ export default CauMaasPlugin
package/src/sso.ts ADDED
@@ -0,0 +1,155 @@
1
+ // 纯 HTTP CAS 统一身份认证登录(无浏览器):
2
+ // 1. /api/auth/cau/start → OIDC → CAS 登录页(拿 lt/execution)
3
+ // 2. rsa = strEnc(学号+密码+lt,'1','2','3')(平台原版 des.js,见 des-loader.ts)
4
+ // 3. POST CAS 登录 → 302 ticket 回跳 → Set-Cookie jwt_token
5
+ // 4. 用 jwt 复用/创建 API Key;jwt 仅作中间态,不返回
6
+
7
+ import { strEnc } from "./des-loader"
8
+
9
+ const BASE = "https://maas.cau.edu.cn"
10
+ const BROWSER_UA =
11
+ "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
12
+
13
+ const asJson = async <T>(res: Response): Promise<T> => {
14
+ if (!res.ok) throw new Error(`HTTP ${res.status}`)
15
+ return (await res.json()) as T
16
+ }
17
+
18
+ // cookie jar:domain → Map(name → value)
19
+ class CookieJar {
20
+ private store = new Map<string, Map<string, string>>()
21
+
22
+ set(domain: string, setCookies: string[]) {
23
+ if (!this.store.has(domain)) this.store.set(domain, new Map())
24
+ const jar = this.store.get(domain)!
25
+ for (const sc of setCookies) {
26
+ const [pair] = sc.split(";")
27
+ const eq = pair!.indexOf("=")
28
+ if (eq > 0) jar.set(pair!.slice(0, eq).trim(), pair!.slice(eq + 1).trim())
29
+ }
30
+ }
31
+
32
+ header(domain: string): string {
33
+ const jar = this.store.get(domain)
34
+ if (!jar || jar.size === 0) return ""
35
+ return [...jar].map(([k, v]) => `${k}=${v}`).join("; ")
36
+ }
37
+
38
+ get(domain: string, name: string): string | undefined {
39
+ return this.store.get(domain)?.get(name)
40
+ }
41
+ }
42
+
43
+ async function fetchWithJar(
44
+ jar: CookieJar,
45
+ url: string,
46
+ init: RequestInit = {},
47
+ redirect: "follow" | "manual" = "manual",
48
+ ): Promise<Response> {
49
+ const domain = new URL(url).hostname
50
+ const headers = new Headers(init.headers)
51
+ headers.set("user-agent", BROWSER_UA)
52
+ const cookie = jar.header(domain)
53
+ if (cookie) headers.set("cookie", cookie)
54
+ const res = await fetch(url, { ...init, headers, redirect })
55
+ jar.set(domain, res.headers.getSetCookie())
56
+ return res
57
+ }
58
+
59
+ // 跟踪重定向链,拿到 maas 的 jwt_token 即停
60
+ async function followRedirects(
61
+ jar: CookieJar,
62
+ startUrl: string,
63
+ ): Promise<{ finalUrl: string; jwt: string | null }> {
64
+ let url = startUrl
65
+ for (let i = 0; i < 20; i++) {
66
+ const res = await fetchWithJar(jar, url)
67
+ const jwt = jar.get("maas.cau.edu.cn", "jwt_token")
68
+ if (jwt) return { finalUrl: url, jwt }
69
+ const loc = res.headers.get("location")
70
+ if (!loc) break
71
+ url = loc.startsWith("http") ? loc : new URL(loc, url).href
72
+ }
73
+ return { finalUrl: url, jwt: jar.get("maas.cau.edu.cn", "jwt_token") ?? null }
74
+ }
75
+
76
+ export interface SsoLoginResult {
77
+ userName: string
78
+ apiKey: string
79
+ accountId: string
80
+ }
81
+
82
+ export async function loginWithCas(username: string, password: string): Promise<SsoLoginResult> {
83
+ const jar = new CookieJar()
84
+
85
+ // 1. 走 OIDC 链到 CAS 登录页
86
+ const { finalUrl: casLoginUrl } = await followRedirects(jar, `${BASE}/api/auth/cau/start`)
87
+ if (!casLoginUrl.includes("onecas.cau.edu.cn/tpass/login")) {
88
+ throw new Error(`未到达 CAS 登录页,停在 ${casLoginUrl}`)
89
+ }
90
+
91
+ // 2. 提取 lt/execution
92
+ const html = await (await fetchWithJar(jar, casLoginUrl, {}, "follow")).text()
93
+ const lt = html.match(/name="lt"\s+value="([^"]+)"/)?.[1]
94
+ const execution = html.match(/name="execution"\s+value="([^"]+)"/)?.[1]
95
+ if (!lt || !execution) throw new Error("CAS 登录页缺少 lt/execution")
96
+
97
+ // 3. 加密并提交;失败返回 200 错误页,成功 302 带 ticket
98
+ const submitRes = await fetchWithJar(
99
+ jar,
100
+ casLoginUrl,
101
+ {
102
+ method: "POST",
103
+ headers: { "content-type": "application/x-www-form-urlencoded" },
104
+ body: new URLSearchParams({
105
+ rsa: strEnc(username + password + lt, "1", "2", "3"),
106
+ ul: String(username.length),
107
+ pl: String(password.length),
108
+ sl: "0",
109
+ lt,
110
+ execution,
111
+ _eventId: "submit",
112
+ }).toString(),
113
+ },
114
+ "manual",
115
+ )
116
+ if (submitRes.status !== 302) {
117
+ const errMsg =
118
+ (await submitRes.text()).match(/id="errormsg"[^>]*>([^<]+)</)?.[1] ?? "账号或密码错误"
119
+ throw new Error(`CAS 登录失败: ${errMsg}`)
120
+ }
121
+ const callbackUrl = submitRes.headers.get("location")
122
+ if (!callbackUrl?.includes("ticket=")) throw new Error("CAS 登录成功但未返回 ticket")
123
+
124
+ // 4. 跟回跳链拿 jwt
125
+ const { jwt } = await followRedirects(jar, callbackUrl)
126
+ if (!jwt) throw new Error("回跳链未捕获到 jwt_token")
127
+
128
+ // 5. 换 API Key:只认名为 "magpie" 的,没有则以 "magpie" 为名创建
129
+ const headers = { cookie: `jwt_token=${jwt}`, accept: "application/json" }
130
+ const user = await fetch(`${BASE}/api/user/current`, { headers }).then((r) =>
131
+ asJson<{ user_name: string; user_id: number }>(r),
132
+ )
133
+ const { keys } = await fetch(`${BASE}/api/user/api_keys/?user_id=${user.user_id}`, {
134
+ headers,
135
+ }).then((r) =>
136
+ asJson<{ keys?: { api_key: string; name: string; status: string }[] }>(r),
137
+ )
138
+ let apiKey = keys?.find((k) => k.name === "magpie" && k.status === "ACTIVE")?.api_key
139
+ if (!apiKey) {
140
+ const created = await fetch(`${BASE}/api/user/api_keys/`, {
141
+ method: "POST",
142
+ headers: { ...headers, "content-type": "application/json" },
143
+ body: JSON.stringify({
144
+ user_id: user.user_id,
145
+ name: "magpie",
146
+ allowed_ip_networks: [],
147
+ allowed_ip_networks_enabled: false,
148
+ }),
149
+ }).then((r) => asJson<{ api_key?: string; key?: string }>(r))
150
+ apiKey = created.api_key ?? created.key
151
+ }
152
+ if (!apiKey) throw new Error("未能获取或创建 API Key")
153
+
154
+ return { userName: user.user_name, apiKey, accountId: user.user_name }
155
+ }