@koishi-ce/plugin-auth 1.1.2 → 1.1.4
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 +2 -2
- package/dist/index.js +1 -1
- package/dist/style.css +1 -1
- package/lib/index.d.ts +46 -44
- package/lib/index.mjs +155 -158
- package/package.json +2 -2
- package/src/__tests__/helpers.ts +272 -0
- package/src/__tests__/intercept.test.ts +102 -0
- package/src/__tests__/login.test.ts +237 -0
- package/src/__tests__/user-events.test.ts +189 -0
- package/src/__tests__/verification.test.ts +122 -0
- package/src/crypto.ts +77 -0
- package/src/index.ts +19 -374
- package/src/login.ts +243 -0
- package/src/types.ts +52 -0
- package/src/__tests__/index.test.ts +0 -797
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
// SPDX-License-Identifier: AGPL-3.0-only
|
|
2
|
+
// Copyright (c) 2019-present Shigma and Koishijs contributors.
|
|
3
|
+
// Copyright (c) 2026-present Koishi-CE contributors.
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @koishi-ce/plugin-auth(控制台鉴权)行为测试的公共装配与桩件。
|
|
7
|
+
*
|
|
8
|
+
* 以内存数据库 + TestConsole(内存 WebSocket 客户端)驱动真实 RPC 链路,
|
|
9
|
+
* 供同目录 intercept / login / verification / user-events 四个主题测试文件
|
|
10
|
+
* 复用,共同覆盖:管理员账户初始化、密码登录(仅 PBKDF2 新格式,旧版
|
|
11
|
+
* SHA-256 哈希拒绝)、令牌续期登录、平台验证码两步登录与绑定改挂、权限拦截、
|
|
12
|
+
* 令牌删除 / 登出 / 资料更新 / 解绑等用户管理事件。
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import type { IncomingMessage } from "node:http";
|
|
16
|
+
import {
|
|
17
|
+
type Client,
|
|
18
|
+
Console,
|
|
19
|
+
type Entry,
|
|
20
|
+
} from "@koishi-ce/console";
|
|
21
|
+
import {
|
|
22
|
+
App,
|
|
23
|
+
Logger,
|
|
24
|
+
type Plugin,
|
|
25
|
+
Time,
|
|
26
|
+
type Universal,
|
|
27
|
+
} from "@koishi-ce/koishi";
|
|
28
|
+
import auth, { type Auth } from "@koishi-ce/plugin-auth";
|
|
29
|
+
import memory from "@koishi-ce/plugin-database-memory";
|
|
30
|
+
import mockClient from "@koishi-ce/plugin-mock";
|
|
31
|
+
|
|
32
|
+
/** 出站消息形状 */
|
|
33
|
+
interface SentMessage {
|
|
34
|
+
type: string;
|
|
35
|
+
body: {
|
|
36
|
+
id?: number;
|
|
37
|
+
key?: string;
|
|
38
|
+
value?: unknown;
|
|
39
|
+
error?: string;
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** 内存 WebSocket 桩 */
|
|
44
|
+
class FakeSocket {
|
|
45
|
+
sent: string[] = [];
|
|
46
|
+
// message 与 close 的监听器统一为同构签名(never 载荷),保证集合存取类型一致
|
|
47
|
+
private messageHandlers = new Set<
|
|
48
|
+
(event: never) => void
|
|
49
|
+
>();
|
|
50
|
+
private closeHandlers = new Set<(event: never) => void>();
|
|
51
|
+
|
|
52
|
+
send(data: string) {
|
|
53
|
+
this.sent.push(data);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
addEventListener(
|
|
57
|
+
type: string,
|
|
58
|
+
listener: (event: never) => void,
|
|
59
|
+
) {
|
|
60
|
+
if (type === "message")
|
|
61
|
+
this.messageHandlers.add(listener);
|
|
62
|
+
if (type === "close") this.closeHandlers.add(listener);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
removeEventListener(
|
|
66
|
+
type: string,
|
|
67
|
+
listener: (event: never) => void,
|
|
68
|
+
) {
|
|
69
|
+
if (type === "message")
|
|
70
|
+
this.messageHandlers.delete(listener);
|
|
71
|
+
if (type === "close")
|
|
72
|
+
this.closeHandlers.delete(listener);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
receive(text: string) {
|
|
76
|
+
for (const handler of this.messageHandlers) {
|
|
77
|
+
handler({ data: Buffer.from(text) } as never);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
shutdown() {
|
|
82
|
+
// close 监听器签名统一带 never 载荷,调用时补占位实参
|
|
83
|
+
for (const handler of this.closeHandlers)
|
|
84
|
+
handler(undefined as never);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
get socket(): Universal.WebSocket {
|
|
88
|
+
return this as unknown as Universal.WebSocket;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function fakeRequest(headers: Record<string, string> = {}) {
|
|
93
|
+
return {
|
|
94
|
+
headers,
|
|
95
|
+
socket: { remoteAddress: "127.0.0.1" },
|
|
96
|
+
} as unknown as IncomingMessage;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function tick(ms = 30) {
|
|
100
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** Console 抽象基类的最小实现 */
|
|
104
|
+
class TestConsole extends Console {
|
|
105
|
+
resolveEntry(files: Entry.Files, key: string): string[] {
|
|
106
|
+
const list =
|
|
107
|
+
typeof files === "string" || Array.isArray(files)
|
|
108
|
+
? files
|
|
109
|
+
: files.prod;
|
|
110
|
+
return [String(list), key];
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
acceptClient(
|
|
114
|
+
socket: Universal.WebSocket,
|
|
115
|
+
request: IncomingMessage,
|
|
116
|
+
): Client {
|
|
117
|
+
let accepted: Client | undefined;
|
|
118
|
+
const dispose = this.ctx.on(
|
|
119
|
+
"console/connection",
|
|
120
|
+
(client) => {
|
|
121
|
+
accepted = client;
|
|
122
|
+
},
|
|
123
|
+
);
|
|
124
|
+
this.accept(socket, request);
|
|
125
|
+
dispose();
|
|
126
|
+
if (!accepted) throw new Error("client not accepted");
|
|
127
|
+
return accepted;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** 测试客户端:包一层真实 Client,提供 RPC 调用与消息读取 */
|
|
132
|
+
export class TestClient {
|
|
133
|
+
readonly socket = new FakeSocket();
|
|
134
|
+
readonly client: Client;
|
|
135
|
+
private nextId = 0;
|
|
136
|
+
|
|
137
|
+
constructor(
|
|
138
|
+
service: TestConsole,
|
|
139
|
+
headers: Record<string, string> = {},
|
|
140
|
+
) {
|
|
141
|
+
this.client = service.acceptClient(
|
|
142
|
+
this.socket.socket,
|
|
143
|
+
fakeRequest(headers),
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** 发起一次 RPC 并轮询等待回执(PBKDF2 校验耗时较长,固定等待不可靠) */
|
|
148
|
+
async call(
|
|
149
|
+
type: string,
|
|
150
|
+
args: unknown[],
|
|
151
|
+
timeout = 5000,
|
|
152
|
+
) {
|
|
153
|
+
const id = ++this.nextId;
|
|
154
|
+
this.socket.receive(JSON.stringify({ type, args, id }));
|
|
155
|
+
const deadline = Date.now() + timeout;
|
|
156
|
+
for (;;) {
|
|
157
|
+
const response = this.messages().find(
|
|
158
|
+
(msg) =>
|
|
159
|
+
msg.type === "response" && msg.body.id === id,
|
|
160
|
+
);
|
|
161
|
+
if (response) return response.body;
|
|
162
|
+
if (Date.now() > deadline) {
|
|
163
|
+
throw new Error(`no response for ${type}`);
|
|
164
|
+
}
|
|
165
|
+
await tick(10);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** 最近一条 user 数据消息 */
|
|
170
|
+
lastUserData() {
|
|
171
|
+
const data = this.messages()
|
|
172
|
+
.filter(
|
|
173
|
+
(msg) =>
|
|
174
|
+
msg.type === "data" && msg.body.key === "user",
|
|
175
|
+
)
|
|
176
|
+
.at(-1);
|
|
177
|
+
return data?.body.value as
|
|
178
|
+
| (Auth & { tokens: unknown[] })
|
|
179
|
+
| null
|
|
180
|
+
| undefined;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
messages(): SentMessage[] {
|
|
184
|
+
return this.socket.sent.map(
|
|
185
|
+
(line) => JSON.parse(line) as SentMessage,
|
|
186
|
+
);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
close() {
|
|
190
|
+
this.socket.shutdown();
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
export type TestApp = InstanceType<typeof App>;
|
|
195
|
+
|
|
196
|
+
export let app: TestApp;
|
|
197
|
+
export let aliceId = 0;
|
|
198
|
+
export let bobId = 0;
|
|
199
|
+
|
|
200
|
+
export const service = () => app.console as TestConsole;
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* 全量重建应用装配。裸跑(bun test 无 --isolate)时多测试文件共享本模块
|
|
204
|
+
* 单例,stopApp 清理后由下一次 startApp 全量重建,避免对已停机实例二次
|
|
205
|
+
* start。
|
|
206
|
+
*/
|
|
207
|
+
function rebuild() {
|
|
208
|
+
app = new App();
|
|
209
|
+
// 同 admin:CJS 实现配 ESM 声明,nodenext 互操作视图多包一层 default,类型层穿透取真实类
|
|
210
|
+
app.plugin(memory);
|
|
211
|
+
app.plugin(mockClient);
|
|
212
|
+
// Console 基类的 static inject 是 cordis 3 旧形态,与 Plugin.Constructor 期待类型不兼容,仅做类型层转型
|
|
213
|
+
app.plugin(
|
|
214
|
+
TestConsole as unknown as Plugin.Constructor<App>,
|
|
215
|
+
);
|
|
216
|
+
app.plugin(auth, {
|
|
217
|
+
admin: {
|
|
218
|
+
enabled: true,
|
|
219
|
+
username: "root",
|
|
220
|
+
password: "admin-pass",
|
|
221
|
+
},
|
|
222
|
+
authTokenExpire: Time.week,
|
|
223
|
+
loginTokenExpire: Time.minute * 5,
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/** 各主题测试文件的 beforeAll 钩子体:重建并启动宿主,预置 alice / bob 账户。 */
|
|
228
|
+
export async function startApp() {
|
|
229
|
+
rebuild();
|
|
230
|
+
// 「creating admin account」是启动期一次性生命周期 info,收敛为仅错误级
|
|
231
|
+
(Logger.levels as Record<string, number>)["auth"] = 1;
|
|
232
|
+
await app.start();
|
|
233
|
+
// alice:用于平台验证码登录;bob:用于绑定改挂场景
|
|
234
|
+
const alice = await app.database.createUser(
|
|
235
|
+
"mock",
|
|
236
|
+
"111",
|
|
237
|
+
{
|
|
238
|
+
name: "alice",
|
|
239
|
+
authority: 2,
|
|
240
|
+
},
|
|
241
|
+
);
|
|
242
|
+
const bob = await app.database.createUser("mock", "222", {
|
|
243
|
+
name: "bob",
|
|
244
|
+
authority: 1,
|
|
245
|
+
});
|
|
246
|
+
aliceId = alice.id;
|
|
247
|
+
bobId = bob.id;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/** 各主题测试文件的 afterAll 钩子体:停机并恢复 auth 域日志阈值。 */
|
|
251
|
+
export async function stopApp() {
|
|
252
|
+
// 先注销全部入口并等异步刷新落地,避免停机期间异步回访已卸载的 console 服务
|
|
253
|
+
for (const entry of Object.values(service().entries)) {
|
|
254
|
+
entry.dispose();
|
|
255
|
+
}
|
|
256
|
+
await tick();
|
|
257
|
+
await app.stop();
|
|
258
|
+
delete (Logger.levels as Record<string, number>)["auth"];
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/** 以管理员身份登录并返回客户端 */
|
|
262
|
+
export async function loginAdmin() {
|
|
263
|
+
const client = new TestClient(service(), {
|
|
264
|
+
"user-agent": "test-agent",
|
|
265
|
+
"x-forwarded-for": "1.2.3.4",
|
|
266
|
+
});
|
|
267
|
+
await client.call("login/password", [
|
|
268
|
+
"root",
|
|
269
|
+
"admin-pass",
|
|
270
|
+
]);
|
|
271
|
+
return client;
|
|
272
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
// SPDX-License-Identifier: AGPL-3.0-only
|
|
2
|
+
// Copyright (c) 2019-present Shigma and Koishijs contributors.
|
|
3
|
+
// Copyright (c) 2026-present Koishi-CE contributors.
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
afterAll,
|
|
7
|
+
beforeAll,
|
|
8
|
+
describe,
|
|
9
|
+
expect,
|
|
10
|
+
it,
|
|
11
|
+
} from "bun:test";
|
|
12
|
+
import { Time } from "@koishi-ce/koishi";
|
|
13
|
+
import {
|
|
14
|
+
type Auth,
|
|
15
|
+
randomId,
|
|
16
|
+
} from "@koishi-ce/plugin-auth";
|
|
17
|
+
import {
|
|
18
|
+
app,
|
|
19
|
+
bobId,
|
|
20
|
+
loginAdmin,
|
|
21
|
+
service,
|
|
22
|
+
startApp,
|
|
23
|
+
stopApp,
|
|
24
|
+
TestClient,
|
|
25
|
+
} from "./helpers.ts";
|
|
26
|
+
|
|
27
|
+
// 声明测试专用事件(带权限门槛),驱动 console/intercept 鉴权链路
|
|
28
|
+
declare module "@koishi-ce/console" {
|
|
29
|
+
interface Events {
|
|
30
|
+
"test/admin-only"(): string;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
beforeAll(startApp);
|
|
35
|
+
afterAll(stopApp);
|
|
36
|
+
|
|
37
|
+
describe("@koishi-ce/plugin-auth", () => {
|
|
38
|
+
it("启动时创建管理员账户;randomId 生成随机令牌", async () => {
|
|
39
|
+
const [admin] = await app.database.get("user", {
|
|
40
|
+
id: 0,
|
|
41
|
+
});
|
|
42
|
+
expect(admin?.name).toBe("root");
|
|
43
|
+
expect(admin?.authority).toBe(5);
|
|
44
|
+
expect(admin?.password?.startsWith("pbkdf2$")).toBe(
|
|
45
|
+
true,
|
|
46
|
+
);
|
|
47
|
+
expect(randomId(8)).toMatch(/^[0-9a-zA-Z]{8}$/);
|
|
48
|
+
expect(randomId()).toHaveLength(40);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
describe("权限拦截(console/intercept)", () => {
|
|
52
|
+
it("按登录态与权限等级拦截事件", async () => {
|
|
53
|
+
app.console.addListener(
|
|
54
|
+
"test/admin-only",
|
|
55
|
+
() => "secret",
|
|
56
|
+
{
|
|
57
|
+
authority: 4,
|
|
58
|
+
},
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
// 未登录:拦截
|
|
62
|
+
const anonymous = new TestClient(service());
|
|
63
|
+
expect(
|
|
64
|
+
(await anonymous.call("test/admin-only", [])).error,
|
|
65
|
+
).toBe("unauthorized");
|
|
66
|
+
anonymous.close();
|
|
67
|
+
|
|
68
|
+
// 权限不足:拦截
|
|
69
|
+
const low = new TestClient(service());
|
|
70
|
+
low.client.auth = {
|
|
71
|
+
id: bobId,
|
|
72
|
+
authority: 1,
|
|
73
|
+
expiredAt: Date.now() + Time.hour,
|
|
74
|
+
token: "t",
|
|
75
|
+
} as Auth;
|
|
76
|
+
expect(
|
|
77
|
+
(await low.call("test/admin-only", [])).error,
|
|
78
|
+
).toBe("unauthorized");
|
|
79
|
+
low.close();
|
|
80
|
+
|
|
81
|
+
// 令牌过期:拦截
|
|
82
|
+
const expired = new TestClient(service());
|
|
83
|
+
expired.client.auth = {
|
|
84
|
+
id: 0,
|
|
85
|
+
authority: 5,
|
|
86
|
+
expiredAt: Date.now() - 1,
|
|
87
|
+
token: "t",
|
|
88
|
+
} as Auth;
|
|
89
|
+
expect(
|
|
90
|
+
(await expired.call("test/admin-only", [])).error,
|
|
91
|
+
).toBe("unauthorized");
|
|
92
|
+
expired.close();
|
|
93
|
+
|
|
94
|
+
// 管理员:放行
|
|
95
|
+
const admin = await loginAdmin();
|
|
96
|
+
expect(
|
|
97
|
+
(await admin.call("test/admin-only", [])).value,
|
|
98
|
+
).toBe("secret");
|
|
99
|
+
admin.close();
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
});
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
// SPDX-License-Identifier: AGPL-3.0-only
|
|
2
|
+
// Copyright (c) 2019-present Shigma and Koishijs contributors.
|
|
3
|
+
// Copyright (c) 2026-present Koishi-CE contributors.
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
afterAll,
|
|
7
|
+
beforeAll,
|
|
8
|
+
describe,
|
|
9
|
+
expect,
|
|
10
|
+
it,
|
|
11
|
+
} from "bun:test";
|
|
12
|
+
import {
|
|
13
|
+
createHash,
|
|
14
|
+
pbkdf2Sync,
|
|
15
|
+
randomBytes,
|
|
16
|
+
} from "node:crypto";
|
|
17
|
+
import { Time } from "@koishi-ce/koishi";
|
|
18
|
+
import {
|
|
19
|
+
app,
|
|
20
|
+
loginAdmin,
|
|
21
|
+
service,
|
|
22
|
+
startApp,
|
|
23
|
+
stopApp,
|
|
24
|
+
TestClient,
|
|
25
|
+
} from "./helpers.ts";
|
|
26
|
+
|
|
27
|
+
beforeAll(startApp);
|
|
28
|
+
afterAll(stopApp);
|
|
29
|
+
|
|
30
|
+
describe("@koishi-ce/plugin-auth", () => {
|
|
31
|
+
describe("密码登录", () => {
|
|
32
|
+
it("密码错误与账户缺失时拒绝", async () => {
|
|
33
|
+
const client = new TestClient(service());
|
|
34
|
+
const wrong = await client.call("login/password", [
|
|
35
|
+
"root",
|
|
36
|
+
"wrong-pass",
|
|
37
|
+
]);
|
|
38
|
+
expect(wrong.error).toContain("用户名或密码错误");
|
|
39
|
+
const missing = await client.call("login/password", [
|
|
40
|
+
"nobody",
|
|
41
|
+
"x",
|
|
42
|
+
]);
|
|
43
|
+
expect(missing.error).toContain("用户名或密码错误");
|
|
44
|
+
client.close();
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("登录成功签发令牌并下发登录态", async () => {
|
|
48
|
+
const client = new TestClient(service(), {
|
|
49
|
+
"user-agent": "test-agent",
|
|
50
|
+
"x-forwarded-for": "1.2.3.4",
|
|
51
|
+
});
|
|
52
|
+
const response = await client.call("login/password", [
|
|
53
|
+
"root",
|
|
54
|
+
"admin-pass",
|
|
55
|
+
]);
|
|
56
|
+
expect(response.error).toBeUndefined();
|
|
57
|
+
expect(client.client.auth?.id).toBe(0);
|
|
58
|
+
expect(client.client.auth?.token).toHaveLength(40);
|
|
59
|
+
|
|
60
|
+
// 令牌落库记录来源信息(auth 已由上方断言保证存在)
|
|
61
|
+
const [row] = await app.database.get("token", {
|
|
62
|
+
token: client.client.auth!.token,
|
|
63
|
+
});
|
|
64
|
+
expect(row?.userAgent).toBe("test-agent");
|
|
65
|
+
expect(row?.address).toBe("1.2.3.4");
|
|
66
|
+
expect(row?.type).toBe("password");
|
|
67
|
+
expect(row?.expiredAt).toBeGreaterThan(Date.now());
|
|
68
|
+
|
|
69
|
+
// 下发的 user 数据附带会话与绑定明细
|
|
70
|
+
const data = client.lastUserData();
|
|
71
|
+
expect(data?.name).toBe("root");
|
|
72
|
+
expect(Array.isArray(data?.tokens)).toBe(true);
|
|
73
|
+
client.close();
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("旧版无盐 SHA-256 哈希不再兼容,直接拒绝", async () => {
|
|
77
|
+
// 上游遗留的旧格式兼容已移除(CE 全新安装不存在旧格式),
|
|
78
|
+
// 存量旧哈希的账户须重置密码
|
|
79
|
+
const legacy = createHash("sha256")
|
|
80
|
+
.update("legacy-pass")
|
|
81
|
+
.digest("hex");
|
|
82
|
+
await app.database.set("user", 0, {
|
|
83
|
+
password: legacy,
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
const client = new TestClient(service());
|
|
87
|
+
const response = await client.call("login/password", [
|
|
88
|
+
"root",
|
|
89
|
+
"legacy-pass",
|
|
90
|
+
]);
|
|
91
|
+
expect(response.error).toContain("用户名或密码错误");
|
|
92
|
+
// 库中存储不被改写
|
|
93
|
+
const [row] = await app.database.get(
|
|
94
|
+
"user",
|
|
95
|
+
{ id: 0 },
|
|
96
|
+
["password"],
|
|
97
|
+
);
|
|
98
|
+
expect(row?.password).toBe(legacy);
|
|
99
|
+
client.close();
|
|
100
|
+
|
|
101
|
+
// 还原为合法管理员密码(与插件同格式的 PBKDF2 哈希)供后续用例使用
|
|
102
|
+
const salt = randomBytes(16);
|
|
103
|
+
const dk = pbkdf2Sync(
|
|
104
|
+
"admin-pass",
|
|
105
|
+
salt,
|
|
106
|
+
600_000,
|
|
107
|
+
32,
|
|
108
|
+
"sha256",
|
|
109
|
+
);
|
|
110
|
+
await app.database.set("user", 0, {
|
|
111
|
+
password: `pbkdf2$600000$${salt.toString("hex")}$${dk.toString("hex")}`,
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it("畸形哈希与空密码一律拒绝", async () => {
|
|
116
|
+
await app.database.set("user", 0, {
|
|
117
|
+
password: "pbkdf2$malformed",
|
|
118
|
+
});
|
|
119
|
+
const malformed = new TestClient(service());
|
|
120
|
+
expect(
|
|
121
|
+
(
|
|
122
|
+
await malformed.call("login/password", [
|
|
123
|
+
"root",
|
|
124
|
+
"x",
|
|
125
|
+
])
|
|
126
|
+
).error,
|
|
127
|
+
).toContain("用户名或密码错误");
|
|
128
|
+
malformed.close();
|
|
129
|
+
|
|
130
|
+
await app.database.set("user", 0, {
|
|
131
|
+
password: "not-a-hash-at-all",
|
|
132
|
+
});
|
|
133
|
+
const alien = new TestClient(service());
|
|
134
|
+
expect(
|
|
135
|
+
(await alien.call("login/password", ["root", "x"]))
|
|
136
|
+
.error,
|
|
137
|
+
).toContain("用户名或密码错误");
|
|
138
|
+
alien.close();
|
|
139
|
+
|
|
140
|
+
await app.database.set("user", 0, { password: "" });
|
|
141
|
+
const empty = new TestClient(service());
|
|
142
|
+
expect(
|
|
143
|
+
(await empty.call("login/password", ["root", "x"]))
|
|
144
|
+
.error,
|
|
145
|
+
).toContain("用户名或密码错误");
|
|
146
|
+
empty.close();
|
|
147
|
+
|
|
148
|
+
// 还原为合法管理员密码(与插件同格式的 PBKDF2 哈希)供后续用例使用
|
|
149
|
+
const salt = randomBytes(16);
|
|
150
|
+
const dk = pbkdf2Sync(
|
|
151
|
+
"admin-pass",
|
|
152
|
+
salt,
|
|
153
|
+
600_000,
|
|
154
|
+
32,
|
|
155
|
+
"sha256",
|
|
156
|
+
);
|
|
157
|
+
await app.database.set("user", 0, {
|
|
158
|
+
password: `pbkdf2$600000$${salt.toString("hex")}$${dk.toString("hex")}`,
|
|
159
|
+
});
|
|
160
|
+
});
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
describe("令牌登录", () => {
|
|
164
|
+
it("有效令牌恢复登录态并刷新最后访问时间", async () => {
|
|
165
|
+
const admin = await loginAdmin();
|
|
166
|
+
const token = admin.client.auth?.token;
|
|
167
|
+
expect(token).toBeTruthy();
|
|
168
|
+
|
|
169
|
+
const client = new TestClient(service());
|
|
170
|
+
const response = await client.call("login/token", [
|
|
171
|
+
0,
|
|
172
|
+
token,
|
|
173
|
+
]);
|
|
174
|
+
expect(response.error).toBeUndefined();
|
|
175
|
+
expect(client.client.auth?.id).toBe(0);
|
|
176
|
+
|
|
177
|
+
const [row] = await app.database.get("token", {
|
|
178
|
+
token: token!,
|
|
179
|
+
});
|
|
180
|
+
expect(row?.lastUsedAt?.valueOf()).toBeGreaterThan(
|
|
181
|
+
row?.createdAt?.valueOf() ?? 0,
|
|
182
|
+
);
|
|
183
|
+
admin.close();
|
|
184
|
+
client.close();
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
it("过期 / 不存在 / 用户缺失的令牌被拒绝", async () => {
|
|
188
|
+
await app.database.create("token", {
|
|
189
|
+
id: 0,
|
|
190
|
+
type: "password",
|
|
191
|
+
token: "expired-token",
|
|
192
|
+
expiredAt: Date.now() - 1000,
|
|
193
|
+
createdAt: new Date(),
|
|
194
|
+
lastUsedAt: new Date(),
|
|
195
|
+
userAgent: "ua",
|
|
196
|
+
address: "addr",
|
|
197
|
+
});
|
|
198
|
+
await app.database.create("token", {
|
|
199
|
+
id: 999,
|
|
200
|
+
type: "password",
|
|
201
|
+
token: "ghost-token",
|
|
202
|
+
expiredAt: Date.now() + Time.hour,
|
|
203
|
+
createdAt: new Date(),
|
|
204
|
+
lastUsedAt: new Date(),
|
|
205
|
+
userAgent: "ua",
|
|
206
|
+
address: "addr",
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
const client = new TestClient(service());
|
|
210
|
+
expect(
|
|
211
|
+
(
|
|
212
|
+
await client.call("login/token", [
|
|
213
|
+
0,
|
|
214
|
+
"expired-token",
|
|
215
|
+
])
|
|
216
|
+
).error,
|
|
217
|
+
).toContain("令牌已失效");
|
|
218
|
+
expect(
|
|
219
|
+
(
|
|
220
|
+
await client.call("login/token", [
|
|
221
|
+
0,
|
|
222
|
+
"no-such-token",
|
|
223
|
+
])
|
|
224
|
+
).error,
|
|
225
|
+
).toContain("令牌已失效");
|
|
226
|
+
expect(
|
|
227
|
+
(
|
|
228
|
+
await client.call("login/token", [
|
|
229
|
+
999,
|
|
230
|
+
"ghost-token",
|
|
231
|
+
])
|
|
232
|
+
).error,
|
|
233
|
+
).toContain("用户不存在");
|
|
234
|
+
client.close();
|
|
235
|
+
});
|
|
236
|
+
});
|
|
237
|
+
});
|