@koishi-ce/plugin-auth 1.1.1 → 1.1.3
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/lib/index.d.ts +46 -44
- package/lib/index.mjs +155 -158
- package/package.json +4 -4
- 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,189 @@
|
|
|
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
|
+
aliceId,
|
|
14
|
+
app,
|
|
15
|
+
bobId,
|
|
16
|
+
loginAdmin,
|
|
17
|
+
service,
|
|
18
|
+
startApp,
|
|
19
|
+
stopApp,
|
|
20
|
+
TestClient,
|
|
21
|
+
tick,
|
|
22
|
+
} from "./helpers.ts";
|
|
23
|
+
|
|
24
|
+
beforeAll(startApp);
|
|
25
|
+
afterAll(stopApp);
|
|
26
|
+
|
|
27
|
+
describe("@koishi-ce/plugin-auth", () => {
|
|
28
|
+
describe("用户管理事件", () => {
|
|
29
|
+
it("user/delete-token 删除指定会话", async () => {
|
|
30
|
+
const anonymous = new TestClient(service());
|
|
31
|
+
expect(
|
|
32
|
+
(await anonymous.call("user/delete-token", [1]))
|
|
33
|
+
.error,
|
|
34
|
+
).toContain("请先登录");
|
|
35
|
+
anonymous.close();
|
|
36
|
+
|
|
37
|
+
const admin = await loginAdmin();
|
|
38
|
+
const [row] = await app.database.get("token", {
|
|
39
|
+
token: admin.client.auth!.token,
|
|
40
|
+
});
|
|
41
|
+
const bad = await admin.call(
|
|
42
|
+
"user/delete-token",
|
|
43
|
+
[99999],
|
|
44
|
+
);
|
|
45
|
+
expect(bad.error).toContain("令牌不存在");
|
|
46
|
+
|
|
47
|
+
const ok = await admin.call("user/delete-token", [
|
|
48
|
+
row?.inc,
|
|
49
|
+
]);
|
|
50
|
+
expect(ok.error).toBeUndefined();
|
|
51
|
+
const [removed] = await app.database.get("token", {
|
|
52
|
+
inc: row!.inc,
|
|
53
|
+
});
|
|
54
|
+
expect(removed).toBeUndefined();
|
|
55
|
+
admin.close();
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it("user/logout 删除当前令牌并重发登录态", async () => {
|
|
59
|
+
const admin = await loginAdmin();
|
|
60
|
+
const token = admin.client.auth?.token;
|
|
61
|
+
const response = await admin.call("user/logout", []);
|
|
62
|
+
expect(response.error).toBeUndefined();
|
|
63
|
+
// 令牌已被删除,其它设备无法再用它续期(loginAdmin 成功后令牌必存在)
|
|
64
|
+
const [row] = await app.database.get("token", {
|
|
65
|
+
token: token!,
|
|
66
|
+
});
|
|
67
|
+
expect(row).toBeUndefined();
|
|
68
|
+
// 移植偏差说明:上游 logout 以 setAuth(this, null) 显式清空登录态,
|
|
69
|
+
// 本仓改为传 undefined,命中 setAuth 的默认参数(沿用 client.auth),
|
|
70
|
+
// 故当前行为是重发 user 数据而非下发 null;此处按实际行为断言
|
|
71
|
+
expect(admin.client.auth?.id).toBe(0);
|
|
72
|
+
const data = admin.lastUserData();
|
|
73
|
+
expect(data?.name).toBe("root");
|
|
74
|
+
admin.close();
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it("setAuth 对匿名客户端下发 null(登出数据通道)", async () => {
|
|
78
|
+
const anonymous = new TestClient(service());
|
|
79
|
+
await app.auth.setAuth(anonymous.client);
|
|
80
|
+
await tick();
|
|
81
|
+
expect(anonymous.lastUserData()).toBeNull();
|
|
82
|
+
anonymous.close();
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it("user/update 修改资料(密码加哈希、配置透传)", async () => {
|
|
86
|
+
const anonymous = new TestClient(service());
|
|
87
|
+
expect(
|
|
88
|
+
(
|
|
89
|
+
await anonymous.call("user/update", [
|
|
90
|
+
{ name: "x" },
|
|
91
|
+
])
|
|
92
|
+
).error,
|
|
93
|
+
).toContain("请先登录");
|
|
94
|
+
anonymous.close();
|
|
95
|
+
|
|
96
|
+
const admin = await loginAdmin();
|
|
97
|
+
const response = await admin.call("user/update", [
|
|
98
|
+
{
|
|
99
|
+
name: "renamed",
|
|
100
|
+
password: "new-pass",
|
|
101
|
+
config: { theme: "dark" },
|
|
102
|
+
},
|
|
103
|
+
]);
|
|
104
|
+
expect(response.error).toBeUndefined();
|
|
105
|
+
// passive 更新:本地登录态同步但不推送数据
|
|
106
|
+
expect(admin.client.auth?.name).toBe("renamed");
|
|
107
|
+
const [row] = await app.database.get("user", {
|
|
108
|
+
id: 0,
|
|
109
|
+
});
|
|
110
|
+
expect(row?.name).toBe("renamed");
|
|
111
|
+
expect(row?.password?.startsWith("pbkdf2$")).toBe(
|
|
112
|
+
true,
|
|
113
|
+
);
|
|
114
|
+
expect(row?.config).toEqual({ theme: "dark" });
|
|
115
|
+
admin.close();
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it("user/unbind 解绑平台账号的三种分支", async () => {
|
|
119
|
+
const anonymous = new TestClient(service());
|
|
120
|
+
expect(
|
|
121
|
+
(
|
|
122
|
+
await anonymous.call("user/unbind", [
|
|
123
|
+
"mock",
|
|
124
|
+
"111",
|
|
125
|
+
])
|
|
126
|
+
).error,
|
|
127
|
+
).toContain("请先登录");
|
|
128
|
+
anonymous.close();
|
|
129
|
+
|
|
130
|
+
// 以 alice 身份登录
|
|
131
|
+
const alice = new TestClient(service());
|
|
132
|
+
const grant = await alice.call("login/platform", [
|
|
133
|
+
"mock",
|
|
134
|
+
"111",
|
|
135
|
+
]);
|
|
136
|
+
const code = (grant.value as { token: string }).token;
|
|
137
|
+
await app.mock.client("111").receive(code);
|
|
138
|
+
await tick();
|
|
139
|
+
expect(alice.client.auth?.id).toBe(aliceId);
|
|
140
|
+
|
|
141
|
+
// 绑定不存在
|
|
142
|
+
expect(
|
|
143
|
+
(await alice.call("user/unbind", ["mock", "000"]))
|
|
144
|
+
.error,
|
|
145
|
+
).toContain("绑定不存在");
|
|
146
|
+
|
|
147
|
+
// 仅剩一个自绑定(主账号):拒绝解绑
|
|
148
|
+
expect(
|
|
149
|
+
(await alice.call("user/unbind", ["mock", "111"]))
|
|
150
|
+
.error,
|
|
151
|
+
).toContain("无法解除绑定");
|
|
152
|
+
|
|
153
|
+
// 追加第二个自绑定后可解绑
|
|
154
|
+
await app.database.create("binding", {
|
|
155
|
+
aid: aliceId,
|
|
156
|
+
bid: aliceId,
|
|
157
|
+
platform: "mock",
|
|
158
|
+
pid: "333",
|
|
159
|
+
});
|
|
160
|
+
expect(
|
|
161
|
+
(await alice.call("user/unbind", ["mock", "333"]))
|
|
162
|
+
.error,
|
|
163
|
+
).toBeUndefined();
|
|
164
|
+
const [gone] = await app.database.get("binding", {
|
|
165
|
+
platform: "mock",
|
|
166
|
+
pid: "333",
|
|
167
|
+
});
|
|
168
|
+
expect(gone).toBeUndefined();
|
|
169
|
+
|
|
170
|
+
// 绑到他人名下(aid !== bid):解绑时改回其主账号
|
|
171
|
+
await app.database.create("binding", {
|
|
172
|
+
aid: aliceId,
|
|
173
|
+
bid: bobId,
|
|
174
|
+
platform: "mock",
|
|
175
|
+
pid: "444",
|
|
176
|
+
});
|
|
177
|
+
expect(
|
|
178
|
+
(await alice.call("user/unbind", ["mock", "444"]))
|
|
179
|
+
.error,
|
|
180
|
+
).toBeUndefined();
|
|
181
|
+
const [moved] = await app.database.get("binding", {
|
|
182
|
+
platform: "mock",
|
|
183
|
+
pid: "444",
|
|
184
|
+
});
|
|
185
|
+
expect(moved?.aid).toBe(bobId);
|
|
186
|
+
alice.close();
|
|
187
|
+
});
|
|
188
|
+
});
|
|
189
|
+
});
|
|
@@ -0,0 +1,122 @@
|
|
|
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 type { Auth } from "@koishi-ce/plugin-auth";
|
|
13
|
+
import {
|
|
14
|
+
aliceId,
|
|
15
|
+
app,
|
|
16
|
+
service,
|
|
17
|
+
startApp,
|
|
18
|
+
stopApp,
|
|
19
|
+
TestClient,
|
|
20
|
+
tick,
|
|
21
|
+
} from "./helpers.ts";
|
|
22
|
+
|
|
23
|
+
beforeAll(startApp);
|
|
24
|
+
afterAll(stopApp);
|
|
25
|
+
|
|
26
|
+
describe("@koishi-ce/plugin-auth", () => {
|
|
27
|
+
describe("平台验证码登录", () => {
|
|
28
|
+
it("账户不存在或已绑定同一账户时拒绝", async () => {
|
|
29
|
+
const client = new TestClient(service());
|
|
30
|
+
expect(
|
|
31
|
+
(
|
|
32
|
+
await client.call("login/platform", [
|
|
33
|
+
"mock",
|
|
34
|
+
"404",
|
|
35
|
+
])
|
|
36
|
+
).error,
|
|
37
|
+
).toContain("找不到此账户");
|
|
38
|
+
|
|
39
|
+
client.client.auth = { id: aliceId } as Auth;
|
|
40
|
+
expect(
|
|
41
|
+
(
|
|
42
|
+
await client.call("login/platform", [
|
|
43
|
+
"mock",
|
|
44
|
+
"111",
|
|
45
|
+
])
|
|
46
|
+
).error,
|
|
47
|
+
).toContain("你已经绑定了此账户");
|
|
48
|
+
client.close();
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("未登录客户端经验证码完成登录", async () => {
|
|
52
|
+
const client = new TestClient(service());
|
|
53
|
+
const result = await client.call("login/platform", [
|
|
54
|
+
"mock",
|
|
55
|
+
"111",
|
|
56
|
+
]);
|
|
57
|
+
expect(result.error).toBeUndefined();
|
|
58
|
+
expect(result.value).toMatchObject({
|
|
59
|
+
id: aliceId,
|
|
60
|
+
name: "alice",
|
|
61
|
+
});
|
|
62
|
+
const code = (result.value as { token: string })
|
|
63
|
+
.token;
|
|
64
|
+
expect(code).toMatch(/^\d{6}$/);
|
|
65
|
+
|
|
66
|
+
// 无关消息不消费验证码状态
|
|
67
|
+
const bot = app.mock.client("999", "888");
|
|
68
|
+
await bot.receive("hello-world");
|
|
69
|
+
|
|
70
|
+
// 用户把验证码发给机器人 → 为对应账户签发令牌
|
|
71
|
+
const user = app.mock.client("111");
|
|
72
|
+
await user.receive(code);
|
|
73
|
+
await tick();
|
|
74
|
+
expect(client.client.auth?.id).toBe(aliceId);
|
|
75
|
+
const data = client.lastUserData();
|
|
76
|
+
expect(data?.name).toBe("alice");
|
|
77
|
+
client.close();
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it("已登录客户端经验证码改绑平台账户", async () => {
|
|
81
|
+
const client = new TestClient(service());
|
|
82
|
+
// 已登录为 alice,把 bob 的平台账号绑到 alice 名下
|
|
83
|
+
client.client.auth = { id: aliceId } as Auth;
|
|
84
|
+
const result = await client.call("login/platform", [
|
|
85
|
+
"mock",
|
|
86
|
+
"222",
|
|
87
|
+
]);
|
|
88
|
+
expect(result.error).toBeUndefined();
|
|
89
|
+
const code = (result.value as { token: string })
|
|
90
|
+
.token;
|
|
91
|
+
|
|
92
|
+
const user = app.mock.client("222");
|
|
93
|
+
await user.receive(code);
|
|
94
|
+
await tick();
|
|
95
|
+
const [binding] = await app.database.get("binding", {
|
|
96
|
+
platform: "mock",
|
|
97
|
+
pid: "222",
|
|
98
|
+
});
|
|
99
|
+
expect(binding?.aid).toBe(aliceId);
|
|
100
|
+
client.close();
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it("验证码超时后触发状态清理回调", async () => {
|
|
104
|
+
// Schema 要 loginTokenExpire >= 1min,构造后直接改字段绕开校验,
|
|
105
|
+
// 用 50ms 时效快速触发超时回调
|
|
106
|
+
const keep = app.auth.config.loginTokenExpire;
|
|
107
|
+
app.auth.config.loginTokenExpire = 50;
|
|
108
|
+
const client = new TestClient(service());
|
|
109
|
+
const result = await client.call("login/platform", [
|
|
110
|
+
"mock",
|
|
111
|
+
"111",
|
|
112
|
+
]);
|
|
113
|
+
expect(result.error).toBeUndefined();
|
|
114
|
+
// 等待超时回调执行(状态清理与否取决于到期判定,此处验证回调不抛错)
|
|
115
|
+
await new Promise((resolve) =>
|
|
116
|
+
setTimeout(resolve, 150),
|
|
117
|
+
);
|
|
118
|
+
client.close();
|
|
119
|
+
app.auth.config.loginTokenExpire = keep;
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
});
|
package/src/crypto.ts
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
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
|
+
// upstream: koishijs/webui plugins/auth/src/index.ts L66-L70(randomId)、L79-L81(toHash);PBKDF2_ROUNDS 与 verifyPassword 为本仓新增(上游无对应段);上游为单文件,同步时以其整体 diff 对照本目录。
|
|
6
|
+
// 本仓分歧(E 类,勿回移):上游监听器内联保留的旧版无盐 SHA-256 密码兼容校验已在本仓移除——CE 全新安装一律 pbkdf2$ 格式,旧格式不可达(2026-09-13 用户拍板)。
|
|
7
|
+
|
|
8
|
+
import {
|
|
9
|
+
pbkdf2Sync,
|
|
10
|
+
randomBytes,
|
|
11
|
+
timingSafeEqual,
|
|
12
|
+
} from "node:crypto";
|
|
13
|
+
|
|
14
|
+
// 随机令牌的字符表(数字 + 大小写字母)
|
|
15
|
+
const letters =
|
|
16
|
+
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* 生成指定长度的随机令牌字符串。
|
|
20
|
+
* @param length 令牌长度,默认 40
|
|
21
|
+
* @returns 从字符表随机取字符拼接成的字符串
|
|
22
|
+
*/
|
|
23
|
+
export function randomId(length = 40) {
|
|
24
|
+
// 登录令牌的安全敏感源:randomBytes(CSPRNG)+ 拒绝采样消除模偏差
|
|
25
|
+
// (256 % 62 != 0),格式契约(字符表与长度)保持不变
|
|
26
|
+
let out = "";
|
|
27
|
+
while (out.length < length) {
|
|
28
|
+
// 两倍余量覆盖拒绝采样损耗,极端涨落由外层 while 兜底补取
|
|
29
|
+
for (const byte of randomBytes(length * 2)) {
|
|
30
|
+
if (out.length >= length) break;
|
|
31
|
+
if (byte >= 248) continue;
|
|
32
|
+
out += letters[byte % letters.length] ?? "";
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return out;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** PBKDF2-HMAC-SHA256 迭代次数(OWASP 2023 建议 600k;登录低频,开销可接受) */
|
|
39
|
+
const PBKDF2_ROUNDS = 600_000;
|
|
40
|
+
|
|
41
|
+
/** 新格式密码哈希:`pbkdf2$<rounds>$<salt-hex>$<dk-hex>`(加盐 + 慢哈希)。 */
|
|
42
|
+
export function toHash(password: string) {
|
|
43
|
+
const salt = randomBytes(16);
|
|
44
|
+
const dk = pbkdf2Sync(
|
|
45
|
+
password,
|
|
46
|
+
salt,
|
|
47
|
+
PBKDF2_ROUNDS,
|
|
48
|
+
32,
|
|
49
|
+
"sha256",
|
|
50
|
+
);
|
|
51
|
+
return `pbkdf2$${PBKDF2_ROUNDS}$${salt.toString("hex")}$${dk.toString("hex")}`;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* 校验明文密码与库中存储是否匹配。
|
|
56
|
+
*
|
|
57
|
+
* 仅接受 `pbkdf2$...` 格式:按存储的盐与迭代次数重派生,恒定时间比较;
|
|
58
|
+
* 其余形态(含历史上游遗留的无盐 SHA-256 十六进制)一律不匹配。
|
|
59
|
+
*/
|
|
60
|
+
export function verifyPassword(
|
|
61
|
+
password: string,
|
|
62
|
+
stored: string,
|
|
63
|
+
): boolean {
|
|
64
|
+
if (!stored.startsWith("pbkdf2$")) return false;
|
|
65
|
+
const [, rounds, saltHex, dkHex] = stored.split("$");
|
|
66
|
+
if (!rounds || !saltHex || !dkHex) return false;
|
|
67
|
+
const expected = Buffer.from(dkHex, "hex");
|
|
68
|
+
const actual = pbkdf2Sync(
|
|
69
|
+
password,
|
|
70
|
+
Buffer.from(saltHex, "hex"),
|
|
71
|
+
Number(rounds),
|
|
72
|
+
expected.length,
|
|
73
|
+
"sha256",
|
|
74
|
+
);
|
|
75
|
+
// 长度已按 expected.length 派生,恒定时间比较不会抛错
|
|
76
|
+
return timingSafeEqual(actual, expected);
|
|
77
|
+
}
|