befly 3.70.0 → 3.71.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/apis/admin/insert.js +2 -4
- package/apis/admin/update.js +4 -1
- package/apis/auth/login.js +2 -1
- package/lib/dbHelper.js +0 -4
- package/package.json +1 -1
- package/plugins/tool.js +1 -41
- package/sync/dev.js +2 -1
- package/utils/crypto.js +62 -0
package/apis/admin/insert.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import adminTable from "#befly/tables/admin.json";
|
|
2
|
-
import {
|
|
2
|
+
import { hashPassword } from "#befly/utils/crypto.js";
|
|
3
3
|
|
|
4
4
|
export default {
|
|
5
5
|
name: "添加管理员",
|
|
@@ -43,9 +43,7 @@ export default {
|
|
|
43
43
|
return befly.tool.No("角色不存在");
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
-
const
|
|
47
|
-
const isSha256Hex = /^[a-f0-9]{64}$/.test(passwordSource);
|
|
48
|
-
const hashedPassword = isSha256Hex ? befly.tool.sha256(passwordSource) : befly.tool.sha256(befly.tool.sha256(ctx.body.password));
|
|
46
|
+
const hashedPassword = await hashPassword(ctx.body.password);
|
|
49
47
|
|
|
50
48
|
const adminId = await befly.mysql.insData({
|
|
51
49
|
table: "beflyAdmin",
|
package/apis/admin/update.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import adminTable from "#befly/tables/admin.json";
|
|
2
|
+
import { hashPassword } from "#befly/utils/crypto.js";
|
|
2
3
|
|
|
3
4
|
export default {
|
|
4
5
|
name: "更新管理员",
|
|
@@ -74,7 +75,9 @@ export default {
|
|
|
74
75
|
if (ctx.body.email !== undefined) updateData.email = ctx.body.email;
|
|
75
76
|
if (ctx.body.lastLoginIp !== undefined) updateData.lastLoginIp = ctx.body.lastLoginIp;
|
|
76
77
|
if (ctx.body.lastLoginTime !== undefined) updateData.lastLoginTime = ctx.body.lastLoginTime;
|
|
77
|
-
if (ctx.body.password !== undefined)
|
|
78
|
+
if (ctx.body.password !== undefined) {
|
|
79
|
+
updateData.password = await hashPassword(ctx.body.password);
|
|
80
|
+
}
|
|
78
81
|
if (ctx.body.phone !== undefined) updateData.phone = ctx.body.phone;
|
|
79
82
|
if (ctx.body.roleType !== undefined) updateData.roleType = ctx.body.roleType;
|
|
80
83
|
if (ctx.body.username !== undefined) updateData.username = ctx.body.username;
|
package/apis/auth/login.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import adminTable from "#befly/tables/admin.json";
|
|
2
2
|
import loginLogTable from "#befly/tables/loginLog.json";
|
|
3
|
+
import { verifyPassword } from "#befly/utils/crypto.js";
|
|
3
4
|
import { toSessionTtlSeconds } from "#befly/utils/util.js";
|
|
4
5
|
|
|
5
6
|
export default {
|
|
@@ -79,7 +80,7 @@ export default {
|
|
|
79
80
|
logData.username = admin.data.username;
|
|
80
81
|
logData.nickname = admin.data.nickname;
|
|
81
82
|
|
|
82
|
-
if (
|
|
83
|
+
if (!(await verifyPassword(ctx.body.password, admin.data.password))) {
|
|
83
84
|
logData.failReason = "密码错误";
|
|
84
85
|
await befly.mysql.insData({ table: "beflyLoginLog", data: logData });
|
|
85
86
|
return befly.tool.No("账号或密码错误");
|
package/lib/dbHelper.js
CHANGED
|
@@ -79,10 +79,6 @@ function safeStringify(value) {
|
|
|
79
79
|
}
|
|
80
80
|
|
|
81
81
|
function getExecuteErrorMessage(error) {
|
|
82
|
-
if (error instanceof Error && typeof error.message === "string" && error.message.trim().length > 0) {
|
|
83
|
-
return error.message.trim();
|
|
84
|
-
}
|
|
85
|
-
|
|
86
82
|
if (!error || typeof error !== "object") {
|
|
87
83
|
return String(error);
|
|
88
84
|
}
|
package/package.json
CHANGED
package/plugins/tool.js
CHANGED
|
@@ -103,53 +103,13 @@ export function Raw(ctx, data, options = {}) {
|
|
|
103
103
|
});
|
|
104
104
|
}
|
|
105
105
|
|
|
106
|
-
/**
|
|
107
|
-
* 计算 md5(hex)
|
|
108
|
-
* @param value 输入字符串
|
|
109
|
-
* @returns md5 hex
|
|
110
|
-
*/
|
|
111
|
-
export function md5(value) {
|
|
112
|
-
if (!isString(value) || value.length < 1) {
|
|
113
|
-
throw new Error("md5 输入必须是非空字符串", {
|
|
114
|
-
cause: null,
|
|
115
|
-
code: "validation",
|
|
116
|
-
subsystem: "tool",
|
|
117
|
-
operation: "md5"
|
|
118
|
-
});
|
|
119
|
-
}
|
|
120
|
-
const hasher = new Bun.CryptoHasher("md5");
|
|
121
|
-
hasher.update(value);
|
|
122
|
-
return hasher.digest("hex");
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
/**
|
|
126
|
-
* 计算 sha256(hex)
|
|
127
|
-
* @param value 输入字符串
|
|
128
|
-
* @returns sha256 hex
|
|
129
|
-
*/
|
|
130
|
-
export function sha256(value) {
|
|
131
|
-
if (!isString(value) || value.length < 1) {
|
|
132
|
-
throw new Error("sha256 输入必须是非空字符串", {
|
|
133
|
-
cause: null,
|
|
134
|
-
code: "validation",
|
|
135
|
-
subsystem: "tool",
|
|
136
|
-
operation: "sha256"
|
|
137
|
-
});
|
|
138
|
-
}
|
|
139
|
-
const hasher = new Bun.CryptoHasher("sha256");
|
|
140
|
-
hasher.update(value);
|
|
141
|
-
return hasher.digest("hex");
|
|
142
|
-
}
|
|
143
|
-
|
|
144
106
|
export default {
|
|
145
107
|
order: 3,
|
|
146
108
|
handler: () => {
|
|
147
109
|
return {
|
|
148
110
|
Yes: Yes,
|
|
149
111
|
No: No,
|
|
150
|
-
Raw: Raw
|
|
151
|
-
md5: md5,
|
|
152
|
-
sha256: sha256
|
|
112
|
+
Raw: Raw
|
|
153
113
|
};
|
|
154
114
|
}
|
|
155
115
|
};
|
package/sync/dev.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { hashPassword } from "../utils/crypto.js";
|
|
1
2
|
import { isNumber } from "../utils/is.js";
|
|
2
3
|
|
|
3
4
|
const MENU_TABLE_NAME = "beflyMenu";
|
|
@@ -72,7 +73,7 @@ export async function syncDev(ctx) {
|
|
|
72
73
|
});
|
|
73
74
|
}
|
|
74
75
|
|
|
75
|
-
const password =
|
|
76
|
+
const password = await hashPassword(ctx.config.devPassword);
|
|
76
77
|
|
|
77
78
|
const devAdminData = {
|
|
78
79
|
nickname: "开发者",
|
package/utils/crypto.js
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 通用加密/哈希工具
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { isString } from "./is.js";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* 计算 md5(hex)
|
|
9
|
+
* @param value 输入字符串
|
|
10
|
+
* @returns md5 hex
|
|
11
|
+
*/
|
|
12
|
+
export function md5(value) {
|
|
13
|
+
if (!isString(value) || value.length < 1) {
|
|
14
|
+
throw new Error("md5 输入必须是非空字符串", {
|
|
15
|
+
cause: null,
|
|
16
|
+
code: "validation",
|
|
17
|
+
subsystem: "crypto",
|
|
18
|
+
operation: "md5"
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
const hasher = new Bun.CryptoHasher("md5");
|
|
22
|
+
hasher.update(value);
|
|
23
|
+
return hasher.digest("hex");
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* 计算 sha256(hex)
|
|
28
|
+
* @param value 输入字符串
|
|
29
|
+
* @returns sha256 hex
|
|
30
|
+
*/
|
|
31
|
+
export function sha256(value) {
|
|
32
|
+
if (!isString(value) || value.length < 1) {
|
|
33
|
+
throw new Error("sha256 输入必须是非空字符串", {
|
|
34
|
+
cause: null,
|
|
35
|
+
code: "validation",
|
|
36
|
+
subsystem: "crypto",
|
|
37
|
+
operation: "sha256"
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
const hasher = new Bun.CryptoHasher("sha256");
|
|
41
|
+
hasher.update(value);
|
|
42
|
+
return hasher.digest("hex");
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* 使用 bcrypt 对密码进行哈希
|
|
47
|
+
* @param password 原始密码
|
|
48
|
+
* @returns bcrypt hash 字符串
|
|
49
|
+
*/
|
|
50
|
+
export async function hashPassword(password) {
|
|
51
|
+
return Bun.password.hash(password, { algorithm: "bcrypt" });
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* 校验密码与 bcrypt hash 是否匹配
|
|
56
|
+
* @param password 原始密码
|
|
57
|
+
* @param hashedPassword 数据库中存储的 bcrypt hash
|
|
58
|
+
* @returns 是否匹配
|
|
59
|
+
*/
|
|
60
|
+
export async function verifyPassword(password, hashedPassword) {
|
|
61
|
+
return Bun.password.verify(password, hashedPassword);
|
|
62
|
+
}
|