fix: 注册接口不再接受客户端 app,管理员身份改按 role 判定
客户端可在 /user/register 的 app 入参写入 wxapp-escort-admin 键自封管理员;改为注册忽略 app,管理员身份统一由 utils 判定 role。conf.json 含微信密钥与库口令,移出版本控制。
This commit is contained in:
@@ -1,3 +1,5 @@
|
|||||||
/node_modules/
|
/node_modules/
|
||||||
/logs/
|
/logs/
|
||||||
.env
|
.env
|
||||||
|
# 含微信 AppSecret 与数据库口令,只允许外置在服务器上,禁止入库
|
||||||
|
conf.json
|
||||||
|
|||||||
@@ -1,28 +0,0 @@
|
|||||||
{
|
|
||||||
"port": 9010,
|
|
||||||
"app": {
|
|
||||||
"wxapp-escort": {
|
|
||||||
"appid": "wxf73c79e16837af07",
|
|
||||||
"secret": "5a061d65f4d35d19e62e83b98f6c63cf"
|
|
||||||
},
|
|
||||||
"wxapp-escort-admin": {
|
|
||||||
"appid": "wxf94dd97b91af09a2",
|
|
||||||
"secret": "79ba226684740830af0029763d3045f0"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"mongodb": {
|
|
||||||
"str": "mongodb://huashengtec.com:6000",
|
|
||||||
"host": "huashengtec.com",
|
|
||||||
"db": "health",
|
|
||||||
"option": {
|
|
||||||
"user": "ehason",
|
|
||||||
"pass": "Ehason_dbuser_2026",
|
|
||||||
"dbName": "eiot_user",
|
|
||||||
"authSource": "admin",
|
|
||||||
"autoIndex": true,
|
|
||||||
"socketTimeoutMS": 3000,
|
|
||||||
"serverSelectionTimeoutMS": 30000
|
|
||||||
},
|
|
||||||
"debug": false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+6
-4
@@ -2,7 +2,7 @@ import Joi from 'joi';
|
|||||||
import { DBModel } from "../models/index.js";
|
import { DBModel } from "../models/index.js";
|
||||||
import ResponseUtil from "../utils/api_response.js";
|
import ResponseUtil from "../utils/api_response.js";
|
||||||
import { hashPassword, verifyPassword, hashToken } from "../utils/crypto.js";
|
import { hashPassword, verifyPassword, hashToken } from "../utils/crypto.js";
|
||||||
import { sanitizeUser } from "../middleware/auth.js";
|
import { sanitizeUser, isAdmin } from "../middleware/auth.js";
|
||||||
import config from "../conf.json" with { type: "json" };
|
import config from "../conf.json" with { type: "json" };
|
||||||
|
|
||||||
// 微信 access_token 进程内缓存(有效期 7200s,提前 5 分钟刷新)
|
// 微信 access_token 进程内缓存(有效期 7200s,提前 5 分钟刷新)
|
||||||
@@ -48,7 +48,8 @@ const registerSchema = Joi.object({
|
|||||||
district: Joi.string().allow(''),
|
district: Joi.string().allow(''),
|
||||||
}).optional(),
|
}).optional(),
|
||||||
addresses: Joi.array().optional(),
|
addresses: Joi.array().optional(),
|
||||||
app: Joi.object().optional(),
|
// 不接受客户端 app:app 里的角色决定管理端权限,只能由服务端/库内维护,
|
||||||
|
// 否则任何人都能在注册时自封管理员(历史漏洞)。
|
||||||
}).required(),
|
}).required(),
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -146,7 +147,8 @@ class HandlerUser {
|
|||||||
status: {
|
status: {
|
||||||
account: "normal",
|
account: "normal",
|
||||||
},
|
},
|
||||||
app: userInfo.app || {},
|
// 注册一律不带 app:管理端角色只能由服务端在库内授予
|
||||||
|
app: {},
|
||||||
};
|
};
|
||||||
|
|
||||||
const user = await DBModel.User.setUser(newUser);
|
const user = await DBModel.User.setUser(newUser);
|
||||||
@@ -280,7 +282,7 @@ class HandlerUser {
|
|||||||
return ResponseUtil.unauthorized(ctx, "用户未登录或 token 无效");
|
return ResponseUtil.unauthorized(ctx, "用户未登录或 token 无效");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!("wxapp-escort-admin" in (user.app || {}))) {
|
if (!isAdmin(user)) {
|
||||||
return ResponseUtil.unauthorized(ctx, "用户无管理员权限");
|
return ResponseUtil.unauthorized(ctx, "用户无管理员权限");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+28
-1
@@ -87,6 +87,33 @@ function auth(options = {}) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 管理端身份判定(与 health 服务 utils/permissions.js 同一口径)
|
||||||
|
*
|
||||||
|
* 不再用「app 里存在 wxapp-escort-admin 键」:该键历史上可由 /user/register 的
|
||||||
|
* 客户端入参写入,任何人都能据此自封管理员。现在注册侧拒绝写 app,
|
||||||
|
* 但库里可能残留历史账号,因此要求显式 role === 'admin';
|
||||||
|
* role 缺失的历史账号暂按管理员放行并打告警,待库中补齐 role 后收紧。
|
||||||
|
*
|
||||||
|
* @param {Object} user 用户文档或脱敏后的用户对象
|
||||||
|
* @returns {boolean}
|
||||||
|
*/
|
||||||
|
function isAdmin(user) {
|
||||||
|
const entry = user?.app?.['wxapp-escort-admin'];
|
||||||
|
if (!entry) return false;
|
||||||
|
|
||||||
|
const roles = Array.isArray(entry.role) ? entry.role : [entry.role].filter(Boolean);
|
||||||
|
if (roles.includes('admin')) return true;
|
||||||
|
|
||||||
|
if (entry.role === undefined) {
|
||||||
|
console.warn(
|
||||||
|
"[auth] 管理员账号缺少 role 字段,请补齐 app.wxapp-escort-admin.role = ['admin']"
|
||||||
|
);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 返回用户安全对象(去除密码、token 等敏感字段)
|
* 返回用户安全对象(去除密码、token 等敏感字段)
|
||||||
* 注意:登录/注册响应中的 token 由 handler 单独附加
|
* 注意:登录/注册响应中的 token 由 handler 单独附加
|
||||||
@@ -102,4 +129,4 @@ function sanitizeUser(user) {
|
|||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
export { auth, extractToken, sanitizeUser, findUserByToken };
|
export { auth, extractToken, sanitizeUser, findUserByToken, isAdmin };
|
||||||
|
|||||||
Reference in New Issue
Block a user