@koishi-ce/plugin-auth 1.0.1 → 1.1.0
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/dist/index.js +1 -1
- package/lib/index.d.ts +6 -6
- package/locales/de-DE.yml +7 -9
- package/locales/en-US.yml +7 -9
- package/locales/fr-FR.yml +7 -9
- package/locales/ja-JP.yml +7 -9
- package/locales/ru-RU.yml +7 -9
- package/package.json +2 -2
- package/src/__tests__/index.test.ts +282 -87
- package/src/index.ts +271 -141
|
@@ -11,12 +11,35 @@
|
|
|
11
11
|
* 令牌删除 / 登出 / 资料更新 / 解绑等用户管理事件。
|
|
12
12
|
*/
|
|
13
13
|
|
|
14
|
-
import {
|
|
15
|
-
|
|
14
|
+
import {
|
|
15
|
+
afterAll,
|
|
16
|
+
beforeAll,
|
|
17
|
+
describe,
|
|
18
|
+
expect,
|
|
19
|
+
it,
|
|
20
|
+
} from "bun:test";
|
|
21
|
+
import {
|
|
22
|
+
createHash,
|
|
23
|
+
pbkdf2Sync,
|
|
24
|
+
randomBytes,
|
|
25
|
+
} from "node:crypto";
|
|
16
26
|
import type { IncomingMessage } from "node:http";
|
|
17
|
-
import {
|
|
18
|
-
|
|
19
|
-
|
|
27
|
+
import {
|
|
28
|
+
type Client,
|
|
29
|
+
Console,
|
|
30
|
+
type Entry,
|
|
31
|
+
} from "@koishi-ce/console";
|
|
32
|
+
import {
|
|
33
|
+
App,
|
|
34
|
+
Logger,
|
|
35
|
+
type Plugin,
|
|
36
|
+
Time,
|
|
37
|
+
type Universal,
|
|
38
|
+
} from "@koishi-ce/koishi";
|
|
39
|
+
import auth, {
|
|
40
|
+
type Auth,
|
|
41
|
+
randomId,
|
|
42
|
+
} from "@koishi-ce/plugin-auth";
|
|
20
43
|
import mockClient from "@koishi-ce/plugin-mock";
|
|
21
44
|
import memory from "@koishijs/plugin-database-memory";
|
|
22
45
|
|
|
@@ -42,21 +65,32 @@ interface SentMessage {
|
|
|
42
65
|
class FakeSocket {
|
|
43
66
|
sent: string[] = [];
|
|
44
67
|
// message 与 close 的监听器统一为同构签名(never 载荷),保证集合存取类型一致
|
|
45
|
-
private messageHandlers = new Set<
|
|
68
|
+
private messageHandlers = new Set<
|
|
69
|
+
(event: never) => void
|
|
70
|
+
>();
|
|
46
71
|
private closeHandlers = new Set<(event: never) => void>();
|
|
47
72
|
|
|
48
73
|
send(data: string) {
|
|
49
74
|
this.sent.push(data);
|
|
50
75
|
}
|
|
51
76
|
|
|
52
|
-
addEventListener(
|
|
53
|
-
|
|
77
|
+
addEventListener(
|
|
78
|
+
type: string,
|
|
79
|
+
listener: (event: never) => void,
|
|
80
|
+
) {
|
|
81
|
+
if (type === "message")
|
|
82
|
+
this.messageHandlers.add(listener);
|
|
54
83
|
if (type === "close") this.closeHandlers.add(listener);
|
|
55
84
|
}
|
|
56
85
|
|
|
57
|
-
removeEventListener(
|
|
58
|
-
|
|
59
|
-
|
|
86
|
+
removeEventListener(
|
|
87
|
+
type: string,
|
|
88
|
+
listener: (event: never) => void,
|
|
89
|
+
) {
|
|
90
|
+
if (type === "message")
|
|
91
|
+
this.messageHandlers.delete(listener);
|
|
92
|
+
if (type === "close")
|
|
93
|
+
this.closeHandlers.delete(listener);
|
|
60
94
|
}
|
|
61
95
|
|
|
62
96
|
receive(text: string) {
|
|
@@ -67,7 +101,8 @@ class FakeSocket {
|
|
|
67
101
|
|
|
68
102
|
shutdown() {
|
|
69
103
|
// close 监听器签名统一带 never 载荷,调用时补占位实参
|
|
70
|
-
for (const handler of this.closeHandlers)
|
|
104
|
+
for (const handler of this.closeHandlers)
|
|
105
|
+
handler(undefined as never);
|
|
71
106
|
}
|
|
72
107
|
|
|
73
108
|
get socket(): Universal.WebSocket {
|
|
@@ -90,15 +125,23 @@ function tick(ms = 30) {
|
|
|
90
125
|
class TestConsole extends Console {
|
|
91
126
|
resolveEntry(files: Entry.Files, key: string): string[] {
|
|
92
127
|
const list =
|
|
93
|
-
typeof files === "string" || Array.isArray(files)
|
|
128
|
+
typeof files === "string" || Array.isArray(files)
|
|
129
|
+
? files
|
|
130
|
+
: files.prod;
|
|
94
131
|
return [String(list), key];
|
|
95
132
|
}
|
|
96
133
|
|
|
97
|
-
acceptClient(
|
|
134
|
+
acceptClient(
|
|
135
|
+
socket: Universal.WebSocket,
|
|
136
|
+
request: IncomingMessage,
|
|
137
|
+
): Client {
|
|
98
138
|
let accepted: Client | undefined;
|
|
99
|
-
const dispose = this.ctx.on(
|
|
100
|
-
|
|
101
|
-
|
|
139
|
+
const dispose = this.ctx.on(
|
|
140
|
+
"console/connection",
|
|
141
|
+
(client) => {
|
|
142
|
+
accepted = client;
|
|
143
|
+
},
|
|
144
|
+
);
|
|
102
145
|
this.accept(socket, request);
|
|
103
146
|
dispose();
|
|
104
147
|
if (!accepted) throw new Error("client not accepted");
|
|
@@ -112,7 +155,10 @@ class TestClient {
|
|
|
112
155
|
readonly client: Client;
|
|
113
156
|
private nextId = 0;
|
|
114
157
|
|
|
115
|
-
constructor(
|
|
158
|
+
constructor(
|
|
159
|
+
service: TestConsole,
|
|
160
|
+
headers: Record<string, string> = {},
|
|
161
|
+
) {
|
|
116
162
|
this.client = service.acceptClient(
|
|
117
163
|
this.socket.socket,
|
|
118
164
|
fakeRequest(headers),
|
|
@@ -120,13 +166,18 @@ class TestClient {
|
|
|
120
166
|
}
|
|
121
167
|
|
|
122
168
|
/** 发起一次 RPC 并轮询等待回执(PBKDF2 校验耗时较长,固定等待不可靠) */
|
|
123
|
-
async call(
|
|
169
|
+
async call(
|
|
170
|
+
type: string,
|
|
171
|
+
args: unknown[],
|
|
172
|
+
timeout = 5000,
|
|
173
|
+
) {
|
|
124
174
|
const id = ++this.nextId;
|
|
125
175
|
this.socket.receive(JSON.stringify({ type, args, id }));
|
|
126
176
|
const deadline = Date.now() + timeout;
|
|
127
177
|
for (;;) {
|
|
128
178
|
const response = this.messages().find(
|
|
129
|
-
(msg) =>
|
|
179
|
+
(msg) =>
|
|
180
|
+
msg.type === "response" && msg.body.id === id,
|
|
130
181
|
);
|
|
131
182
|
if (response) return response.body;
|
|
132
183
|
if (Date.now() > deadline) {
|
|
@@ -139,7 +190,10 @@ class TestClient {
|
|
|
139
190
|
/** 最近一条 user 数据消息 */
|
|
140
191
|
lastUserData() {
|
|
141
192
|
const data = this.messages()
|
|
142
|
-
.filter(
|
|
193
|
+
.filter(
|
|
194
|
+
(msg) =>
|
|
195
|
+
msg.type === "data" && msg.body.key === "user",
|
|
196
|
+
)
|
|
143
197
|
.at(-1);
|
|
144
198
|
return data?.body.value as
|
|
145
199
|
| (Auth & { tokens: unknown[] })
|
|
@@ -148,7 +202,9 @@ class TestClient {
|
|
|
148
202
|
}
|
|
149
203
|
|
|
150
204
|
messages(): SentMessage[] {
|
|
151
|
-
return this.socket.sent.map(
|
|
205
|
+
return this.socket.sent.map(
|
|
206
|
+
(line) => JSON.parse(line) as SentMessage,
|
|
207
|
+
);
|
|
152
208
|
}
|
|
153
209
|
|
|
154
210
|
close() {
|
|
@@ -161,9 +217,15 @@ const app = new App();
|
|
|
161
217
|
app.plugin(memory as unknown as typeof memory.default);
|
|
162
218
|
app.plugin(mockClient);
|
|
163
219
|
// Console 基类的 static inject 是 cordis 3 旧形态,与 Plugin.Constructor 期待类型不兼容,仅做类型层转型
|
|
164
|
-
app.plugin(
|
|
220
|
+
app.plugin(
|
|
221
|
+
TestConsole as unknown as Plugin.Constructor<App>,
|
|
222
|
+
);
|
|
165
223
|
app.plugin(auth, {
|
|
166
|
-
admin: {
|
|
224
|
+
admin: {
|
|
225
|
+
enabled: true,
|
|
226
|
+
username: "root",
|
|
227
|
+
password: "admin-pass",
|
|
228
|
+
},
|
|
167
229
|
authTokenExpire: Time.week,
|
|
168
230
|
loginTokenExpire: Time.minute * 5,
|
|
169
231
|
});
|
|
@@ -173,12 +235,18 @@ let aliceId = 0;
|
|
|
173
235
|
let bobId = 0;
|
|
174
236
|
|
|
175
237
|
beforeAll(async () => {
|
|
238
|
+
// 「creating admin account」是启动期一次性生命周期 info,收敛为仅错误级
|
|
239
|
+
(Logger.levels as Record<string, number>)["auth"] = 1;
|
|
176
240
|
await app.start();
|
|
177
241
|
// alice:用于平台验证码登录;bob:用于绑定改挂场景
|
|
178
|
-
const alice = await app.database.createUser(
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
242
|
+
const alice = await app.database.createUser(
|
|
243
|
+
"mock",
|
|
244
|
+
"111",
|
|
245
|
+
{
|
|
246
|
+
name: "alice",
|
|
247
|
+
authority: 2,
|
|
248
|
+
},
|
|
249
|
+
);
|
|
182
250
|
const bob = await app.database.createUser("mock", "222", {
|
|
183
251
|
name: "bob",
|
|
184
252
|
authority: 1,
|
|
@@ -194,6 +262,7 @@ afterAll(async () => {
|
|
|
194
262
|
}
|
|
195
263
|
await tick();
|
|
196
264
|
await app.stop();
|
|
265
|
+
delete (Logger.levels as Record<string, number>)["auth"];
|
|
197
266
|
});
|
|
198
267
|
|
|
199
268
|
/** 以管理员身份登录并返回客户端 */
|
|
@@ -202,16 +271,23 @@ async function loginAdmin() {
|
|
|
202
271
|
"user-agent": "test-agent",
|
|
203
272
|
"x-forwarded-for": "1.2.3.4",
|
|
204
273
|
});
|
|
205
|
-
await client.call("login/password", [
|
|
274
|
+
await client.call("login/password", [
|
|
275
|
+
"root",
|
|
276
|
+
"admin-pass",
|
|
277
|
+
]);
|
|
206
278
|
return client;
|
|
207
279
|
}
|
|
208
280
|
|
|
209
281
|
describe("@koishi-ce/plugin-auth", () => {
|
|
210
282
|
it("启动时创建管理员账户;randomId 生成随机令牌", async () => {
|
|
211
|
-
const [admin] = await app.database.get("user", {
|
|
283
|
+
const [admin] = await app.database.get("user", {
|
|
284
|
+
id: 0,
|
|
285
|
+
});
|
|
212
286
|
expect(admin?.name).toBe("root");
|
|
213
287
|
expect(admin?.authority).toBe(5);
|
|
214
|
-
expect(admin?.password?.startsWith("pbkdf2$")).toBe(
|
|
288
|
+
expect(admin?.password?.startsWith("pbkdf2$")).toBe(
|
|
289
|
+
true,
|
|
290
|
+
);
|
|
215
291
|
expect(randomId(8)).toMatch(/^[0-9a-zA-Z]{8}$/);
|
|
216
292
|
expect(randomId()).toHaveLength(40);
|
|
217
293
|
});
|
|
@@ -219,9 +295,15 @@ describe("@koishi-ce/plugin-auth", () => {
|
|
|
219
295
|
describe("密码登录", () => {
|
|
220
296
|
it("密码错误与账户缺失时拒绝", async () => {
|
|
221
297
|
const client = new TestClient(service());
|
|
222
|
-
const wrong = await client.call("login/password", [
|
|
298
|
+
const wrong = await client.call("login/password", [
|
|
299
|
+
"root",
|
|
300
|
+
"wrong-pass",
|
|
301
|
+
]);
|
|
223
302
|
expect(wrong.error).toContain("用户名或密码错误");
|
|
224
|
-
const missing = await client.call("login/password", [
|
|
303
|
+
const missing = await client.call("login/password", [
|
|
304
|
+
"nobody",
|
|
305
|
+
"x",
|
|
306
|
+
]);
|
|
225
307
|
expect(missing.error).toContain("用户名或密码错误");
|
|
226
308
|
client.close();
|
|
227
309
|
});
|
|
@@ -256,8 +338,12 @@ describe("@koishi-ce/plugin-auth", () => {
|
|
|
256
338
|
});
|
|
257
339
|
|
|
258
340
|
it("旧版无盐 SHA-256 哈希校验通过后透明升级", async () => {
|
|
259
|
-
const legacy = createHash("sha256")
|
|
260
|
-
|
|
341
|
+
const legacy = createHash("sha256")
|
|
342
|
+
.update("legacy-pass")
|
|
343
|
+
.digest("hex");
|
|
344
|
+
await app.database.set("user", 0, {
|
|
345
|
+
password: legacy,
|
|
346
|
+
});
|
|
261
347
|
|
|
262
348
|
const client = new TestClient(service());
|
|
263
349
|
const response = await client.call("login/password", [
|
|
@@ -265,36 +351,59 @@ describe("@koishi-ce/plugin-auth", () => {
|
|
|
265
351
|
"legacy-pass",
|
|
266
352
|
]);
|
|
267
353
|
expect(response.error).toBeUndefined();
|
|
268
|
-
const [row] = await app.database.get(
|
|
269
|
-
|
|
354
|
+
const [row] = await app.database.get(
|
|
355
|
+
"user",
|
|
356
|
+
{ id: 0 },
|
|
357
|
+
["password"],
|
|
358
|
+
);
|
|
359
|
+
expect(row?.password?.startsWith("pbkdf2$")).toBe(
|
|
360
|
+
true,
|
|
361
|
+
);
|
|
270
362
|
client.close();
|
|
271
363
|
});
|
|
272
364
|
|
|
273
365
|
it("畸形哈希与空密码一律拒绝", async () => {
|
|
274
|
-
await app.database.set("user", 0, {
|
|
366
|
+
await app.database.set("user", 0, {
|
|
367
|
+
password: "pbkdf2$malformed",
|
|
368
|
+
});
|
|
275
369
|
const malformed = new TestClient(service());
|
|
276
370
|
expect(
|
|
277
|
-
(
|
|
371
|
+
(
|
|
372
|
+
await malformed.call("login/password", [
|
|
373
|
+
"root",
|
|
374
|
+
"x",
|
|
375
|
+
])
|
|
376
|
+
).error,
|
|
278
377
|
).toContain("用户名或密码错误");
|
|
279
378
|
malformed.close();
|
|
280
379
|
|
|
281
|
-
await app.database.set("user", 0, {
|
|
380
|
+
await app.database.set("user", 0, {
|
|
381
|
+
password: "not-a-hash-at-all",
|
|
382
|
+
});
|
|
282
383
|
const alien = new TestClient(service());
|
|
283
384
|
expect(
|
|
284
|
-
(await alien.call("login/password", ["root", "x"]))
|
|
385
|
+
(await alien.call("login/password", ["root", "x"]))
|
|
386
|
+
.error,
|
|
285
387
|
).toContain("用户名或密码错误");
|
|
286
388
|
alien.close();
|
|
287
389
|
|
|
288
390
|
await app.database.set("user", 0, { password: "" });
|
|
289
391
|
const empty = new TestClient(service());
|
|
290
392
|
expect(
|
|
291
|
-
(await empty.call("login/password", ["root", "x"]))
|
|
393
|
+
(await empty.call("login/password", ["root", "x"]))
|
|
394
|
+
.error,
|
|
292
395
|
).toContain("用户名或密码错误");
|
|
293
396
|
empty.close();
|
|
294
397
|
|
|
295
398
|
// 还原为合法管理员密码(与插件同格式的 PBKDF2 哈希)供后续用例使用
|
|
296
399
|
const salt = randomBytes(16);
|
|
297
|
-
const dk = pbkdf2Sync(
|
|
400
|
+
const dk = pbkdf2Sync(
|
|
401
|
+
"admin-pass",
|
|
402
|
+
salt,
|
|
403
|
+
600_000,
|
|
404
|
+
32,
|
|
405
|
+
"sha256",
|
|
406
|
+
);
|
|
298
407
|
await app.database.set("user", 0, {
|
|
299
408
|
password: `pbkdf2$600000$${salt.toString("hex")}$${dk.toString("hex")}`,
|
|
300
409
|
});
|
|
@@ -308,11 +417,16 @@ describe("@koishi-ce/plugin-auth", () => {
|
|
|
308
417
|
expect(token).toBeTruthy();
|
|
309
418
|
|
|
310
419
|
const client = new TestClient(service());
|
|
311
|
-
const response = await client.call("login/token", [
|
|
420
|
+
const response = await client.call("login/token", [
|
|
421
|
+
0,
|
|
422
|
+
token,
|
|
423
|
+
]);
|
|
312
424
|
expect(response.error).toBeUndefined();
|
|
313
425
|
expect(client.client.auth?.id).toBe(0);
|
|
314
426
|
|
|
315
|
-
const [row] = await app.database.get("token", {
|
|
427
|
+
const [row] = await app.database.get("token", {
|
|
428
|
+
token: token!,
|
|
429
|
+
});
|
|
316
430
|
expect(row?.lastUsedAt?.valueOf()).toBeGreaterThan(
|
|
317
431
|
row?.createdAt?.valueOf() ?? 0,
|
|
318
432
|
);
|
|
@@ -344,13 +458,28 @@ describe("@koishi-ce/plugin-auth", () => {
|
|
|
344
458
|
|
|
345
459
|
const client = new TestClient(service());
|
|
346
460
|
expect(
|
|
347
|
-
(
|
|
461
|
+
(
|
|
462
|
+
await client.call("login/token", [
|
|
463
|
+
0,
|
|
464
|
+
"expired-token",
|
|
465
|
+
])
|
|
466
|
+
).error,
|
|
348
467
|
).toContain("令牌已失效");
|
|
349
468
|
expect(
|
|
350
|
-
(
|
|
469
|
+
(
|
|
470
|
+
await client.call("login/token", [
|
|
471
|
+
0,
|
|
472
|
+
"no-such-token",
|
|
473
|
+
])
|
|
474
|
+
).error,
|
|
351
475
|
).toContain("令牌已失效");
|
|
352
476
|
expect(
|
|
353
|
-
(
|
|
477
|
+
(
|
|
478
|
+
await client.call("login/token", [
|
|
479
|
+
999,
|
|
480
|
+
"ghost-token",
|
|
481
|
+
])
|
|
482
|
+
).error,
|
|
354
483
|
).toContain("用户不存在");
|
|
355
484
|
client.close();
|
|
356
485
|
});
|
|
@@ -360,22 +489,39 @@ describe("@koishi-ce/plugin-auth", () => {
|
|
|
360
489
|
it("账户不存在或已绑定同一账户时拒绝", async () => {
|
|
361
490
|
const client = new TestClient(service());
|
|
362
491
|
expect(
|
|
363
|
-
(
|
|
492
|
+
(
|
|
493
|
+
await client.call("login/platform", [
|
|
494
|
+
"mock",
|
|
495
|
+
"404",
|
|
496
|
+
])
|
|
497
|
+
).error,
|
|
364
498
|
).toContain("找不到此账户");
|
|
365
499
|
|
|
366
500
|
client.client.auth = { id: aliceId } as Auth;
|
|
367
501
|
expect(
|
|
368
|
-
(
|
|
502
|
+
(
|
|
503
|
+
await client.call("login/platform", [
|
|
504
|
+
"mock",
|
|
505
|
+
"111",
|
|
506
|
+
])
|
|
507
|
+
).error,
|
|
369
508
|
).toContain("你已经绑定了此账户");
|
|
370
509
|
client.close();
|
|
371
510
|
});
|
|
372
511
|
|
|
373
512
|
it("未登录客户端经验证码完成登录", async () => {
|
|
374
513
|
const client = new TestClient(service());
|
|
375
|
-
const result = await client.call("login/platform", [
|
|
514
|
+
const result = await client.call("login/platform", [
|
|
515
|
+
"mock",
|
|
516
|
+
"111",
|
|
517
|
+
]);
|
|
376
518
|
expect(result.error).toBeUndefined();
|
|
377
|
-
expect(result.value).toMatchObject({
|
|
378
|
-
|
|
519
|
+
expect(result.value).toMatchObject({
|
|
520
|
+
id: aliceId,
|
|
521
|
+
name: "alice",
|
|
522
|
+
});
|
|
523
|
+
const code = (result.value as { token: string })
|
|
524
|
+
.token;
|
|
379
525
|
expect(code).toMatch(/^\d{6}$/);
|
|
380
526
|
|
|
381
527
|
// 无关消息不消费验证码状态
|
|
@@ -396,9 +542,13 @@ describe("@koishi-ce/plugin-auth", () => {
|
|
|
396
542
|
const client = new TestClient(service());
|
|
397
543
|
// 已登录为 alice,把 bob 的平台账号绑到 alice 名下
|
|
398
544
|
client.client.auth = { id: aliceId } as Auth;
|
|
399
|
-
const result = await client.call("login/platform", [
|
|
545
|
+
const result = await client.call("login/platform", [
|
|
546
|
+
"mock",
|
|
547
|
+
"222",
|
|
548
|
+
]);
|
|
400
549
|
expect(result.error).toBeUndefined();
|
|
401
|
-
const code = (result.value as { token: string })
|
|
550
|
+
const code = (result.value as { token: string })
|
|
551
|
+
.token;
|
|
402
552
|
|
|
403
553
|
const user = app.mock.client("222");
|
|
404
554
|
await user.receive(code);
|
|
@@ -417,10 +567,15 @@ describe("@koishi-ce/plugin-auth", () => {
|
|
|
417
567
|
const keep = app.auth.config.loginTokenExpire;
|
|
418
568
|
app.auth.config.loginTokenExpire = 50;
|
|
419
569
|
const client = new TestClient(service());
|
|
420
|
-
const result = await client.call("login/platform", [
|
|
570
|
+
const result = await client.call("login/platform", [
|
|
571
|
+
"mock",
|
|
572
|
+
"111",
|
|
573
|
+
]);
|
|
421
574
|
expect(result.error).toBeUndefined();
|
|
422
575
|
// 等待超时回调执行(状态清理与否取决于到期判定,此处验证回调不抛错)
|
|
423
|
-
await new Promise((resolve) =>
|
|
576
|
+
await new Promise((resolve) =>
|
|
577
|
+
setTimeout(resolve, 150),
|
|
578
|
+
);
|
|
424
579
|
client.close();
|
|
425
580
|
app.auth.config.loginTokenExpire = keep;
|
|
426
581
|
});
|
|
@@ -428,15 +583,19 @@ describe("@koishi-ce/plugin-auth", () => {
|
|
|
428
583
|
|
|
429
584
|
describe("权限拦截(console/intercept)", () => {
|
|
430
585
|
it("按登录态与权限等级拦截事件", async () => {
|
|
431
|
-
app.console.addListener(
|
|
432
|
-
|
|
433
|
-
|
|
586
|
+
app.console.addListener(
|
|
587
|
+
"test/admin-only",
|
|
588
|
+
() => "secret",
|
|
589
|
+
{
|
|
590
|
+
authority: 4,
|
|
591
|
+
},
|
|
592
|
+
);
|
|
434
593
|
|
|
435
594
|
// 未登录:拦截
|
|
436
595
|
const anonymous = new TestClient(service());
|
|
437
|
-
expect(
|
|
438
|
-
"
|
|
439
|
-
);
|
|
596
|
+
expect(
|
|
597
|
+
(await anonymous.call("test/admin-only", [])).error,
|
|
598
|
+
).toBe("unauthorized");
|
|
440
599
|
anonymous.close();
|
|
441
600
|
|
|
442
601
|
// 权限不足:拦截
|
|
@@ -447,9 +606,9 @@ describe("@koishi-ce/plugin-auth", () => {
|
|
|
447
606
|
expiredAt: Date.now() + Time.hour,
|
|
448
607
|
token: "t",
|
|
449
608
|
} as Auth;
|
|
450
|
-
expect(
|
|
451
|
-
"
|
|
452
|
-
);
|
|
609
|
+
expect(
|
|
610
|
+
(await low.call("test/admin-only", [])).error,
|
|
611
|
+
).toBe("unauthorized");
|
|
453
612
|
low.close();
|
|
454
613
|
|
|
455
614
|
// 令牌过期:拦截
|
|
@@ -460,14 +619,16 @@ describe("@koishi-ce/plugin-auth", () => {
|
|
|
460
619
|
expiredAt: Date.now() - 1,
|
|
461
620
|
token: "t",
|
|
462
621
|
} as Auth;
|
|
463
|
-
expect(
|
|
464
|
-
"
|
|
465
|
-
);
|
|
622
|
+
expect(
|
|
623
|
+
(await expired.call("test/admin-only", [])).error,
|
|
624
|
+
).toBe("unauthorized");
|
|
466
625
|
expired.close();
|
|
467
626
|
|
|
468
627
|
// 管理员:放行
|
|
469
628
|
const admin = await loginAdmin();
|
|
470
|
-
expect(
|
|
629
|
+
expect(
|
|
630
|
+
(await admin.call("test/admin-only", [])).value,
|
|
631
|
+
).toBe("secret");
|
|
471
632
|
admin.close();
|
|
472
633
|
});
|
|
473
634
|
});
|
|
@@ -475,21 +636,29 @@ describe("@koishi-ce/plugin-auth", () => {
|
|
|
475
636
|
describe("用户管理事件", () => {
|
|
476
637
|
it("user/delete-token 删除指定会话", async () => {
|
|
477
638
|
const anonymous = new TestClient(service());
|
|
478
|
-
expect(
|
|
479
|
-
"
|
|
480
|
-
|
|
639
|
+
expect(
|
|
640
|
+
(await anonymous.call("user/delete-token", [1]))
|
|
641
|
+
.error,
|
|
642
|
+
).toContain("请先登录");
|
|
481
643
|
anonymous.close();
|
|
482
644
|
|
|
483
645
|
const admin = await loginAdmin();
|
|
484
646
|
const [row] = await app.database.get("token", {
|
|
485
647
|
token: admin.client.auth!.token,
|
|
486
648
|
});
|
|
487
|
-
const bad = await admin.call(
|
|
649
|
+
const bad = await admin.call(
|
|
650
|
+
"user/delete-token",
|
|
651
|
+
[99999],
|
|
652
|
+
);
|
|
488
653
|
expect(bad.error).toContain("令牌不存在");
|
|
489
654
|
|
|
490
|
-
const ok = await admin.call("user/delete-token", [
|
|
655
|
+
const ok = await admin.call("user/delete-token", [
|
|
656
|
+
row?.inc,
|
|
657
|
+
]);
|
|
491
658
|
expect(ok.error).toBeUndefined();
|
|
492
|
-
const [removed] = await app.database.get("token", {
|
|
659
|
+
const [removed] = await app.database.get("token", {
|
|
660
|
+
inc: row!.inc,
|
|
661
|
+
});
|
|
493
662
|
expect(removed).toBeUndefined();
|
|
494
663
|
admin.close();
|
|
495
664
|
});
|
|
@@ -500,7 +669,9 @@ describe("@koishi-ce/plugin-auth", () => {
|
|
|
500
669
|
const response = await admin.call("user/logout", []);
|
|
501
670
|
expect(response.error).toBeUndefined();
|
|
502
671
|
// 令牌已被删除,其它设备无法再用它续期(loginAdmin 成功后令牌必存在)
|
|
503
|
-
const [row] = await app.database.get("token", {
|
|
672
|
+
const [row] = await app.database.get("token", {
|
|
673
|
+
token: token!,
|
|
674
|
+
});
|
|
504
675
|
expect(row).toBeUndefined();
|
|
505
676
|
// 移植偏差说明:上游 logout 以 setAuth(this, null) 显式清空登录态,
|
|
506
677
|
// 本仓改为传 undefined,命中 setAuth 的默认参数(沿用 client.auth),
|
|
@@ -522,20 +693,32 @@ describe("@koishi-ce/plugin-auth", () => {
|
|
|
522
693
|
it("user/update 修改资料(密码加哈希、配置透传)", async () => {
|
|
523
694
|
const anonymous = new TestClient(service());
|
|
524
695
|
expect(
|
|
525
|
-
(
|
|
696
|
+
(
|
|
697
|
+
await anonymous.call("user/update", [
|
|
698
|
+
{ name: "x" },
|
|
699
|
+
])
|
|
700
|
+
).error,
|
|
526
701
|
).toContain("请先登录");
|
|
527
702
|
anonymous.close();
|
|
528
703
|
|
|
529
704
|
const admin = await loginAdmin();
|
|
530
705
|
const response = await admin.call("user/update", [
|
|
531
|
-
{
|
|
706
|
+
{
|
|
707
|
+
name: "renamed",
|
|
708
|
+
password: "new-pass",
|
|
709
|
+
config: { theme: "dark" },
|
|
710
|
+
},
|
|
532
711
|
]);
|
|
533
712
|
expect(response.error).toBeUndefined();
|
|
534
713
|
// passive 更新:本地登录态同步但不推送数据
|
|
535
714
|
expect(admin.client.auth?.name).toBe("renamed");
|
|
536
|
-
const [row] = await app.database.get("user", {
|
|
715
|
+
const [row] = await app.database.get("user", {
|
|
716
|
+
id: 0,
|
|
717
|
+
});
|
|
537
718
|
expect(row?.name).toBe("renamed");
|
|
538
|
-
expect(row?.password?.startsWith("pbkdf2$")).toBe(
|
|
719
|
+
expect(row?.password?.startsWith("pbkdf2$")).toBe(
|
|
720
|
+
true,
|
|
721
|
+
);
|
|
539
722
|
expect(row?.config).toEqual({ theme: "dark" });
|
|
540
723
|
admin.close();
|
|
541
724
|
});
|
|
@@ -543,13 +726,21 @@ describe("@koishi-ce/plugin-auth", () => {
|
|
|
543
726
|
it("user/unbind 解绑平台账号的三种分支", async () => {
|
|
544
727
|
const anonymous = new TestClient(service());
|
|
545
728
|
expect(
|
|
546
|
-
(
|
|
729
|
+
(
|
|
730
|
+
await anonymous.call("user/unbind", [
|
|
731
|
+
"mock",
|
|
732
|
+
"111",
|
|
733
|
+
])
|
|
734
|
+
).error,
|
|
547
735
|
).toContain("请先登录");
|
|
548
736
|
anonymous.close();
|
|
549
737
|
|
|
550
738
|
// 以 alice 身份登录
|
|
551
739
|
const alice = new TestClient(service());
|
|
552
|
-
const grant = await alice.call("login/platform", [
|
|
740
|
+
const grant = await alice.call("login/platform", [
|
|
741
|
+
"mock",
|
|
742
|
+
"111",
|
|
743
|
+
]);
|
|
553
744
|
const code = (grant.value as { token: string }).token;
|
|
554
745
|
await app.mock.client("111").receive(code);
|
|
555
746
|
await tick();
|
|
@@ -557,12 +748,14 @@ describe("@koishi-ce/plugin-auth", () => {
|
|
|
557
748
|
|
|
558
749
|
// 绑定不存在
|
|
559
750
|
expect(
|
|
560
|
-
(await alice.call("user/unbind", ["mock", "000"]))
|
|
751
|
+
(await alice.call("user/unbind", ["mock", "000"]))
|
|
752
|
+
.error,
|
|
561
753
|
).toContain("绑定不存在");
|
|
562
754
|
|
|
563
755
|
// 仅剩一个自绑定(主账号):拒绝解绑
|
|
564
756
|
expect(
|
|
565
|
-
(await alice.call("user/unbind", ["mock", "111"]))
|
|
757
|
+
(await alice.call("user/unbind", ["mock", "111"]))
|
|
758
|
+
.error,
|
|
566
759
|
).toContain("无法解除绑定");
|
|
567
760
|
|
|
568
761
|
// 追加第二个自绑定后可解绑
|
|
@@ -573,7 +766,8 @@ describe("@koishi-ce/plugin-auth", () => {
|
|
|
573
766
|
pid: "333",
|
|
574
767
|
});
|
|
575
768
|
expect(
|
|
576
|
-
(await alice.call("user/unbind", ["mock", "333"]))
|
|
769
|
+
(await alice.call("user/unbind", ["mock", "333"]))
|
|
770
|
+
.error,
|
|
577
771
|
).toBeUndefined();
|
|
578
772
|
const [gone] = await app.database.get("binding", {
|
|
579
773
|
platform: "mock",
|
|
@@ -589,7 +783,8 @@ describe("@koishi-ce/plugin-auth", () => {
|
|
|
589
783
|
pid: "444",
|
|
590
784
|
});
|
|
591
785
|
expect(
|
|
592
|
-
(await alice.call("user/unbind", ["mock", "444"]))
|
|
786
|
+
(await alice.call("user/unbind", ["mock", "444"]))
|
|
787
|
+
.error,
|
|
593
788
|
).toBeUndefined();
|
|
594
789
|
const [moved] = await app.database.get("binding", {
|
|
595
790
|
platform: "mock",
|