1
This commit is contained in:
@@ -38,6 +38,7 @@ module.exports = {
|
||||
id: result.row.id,
|
||||
user_id: result.row.user_id,
|
||||
plan_id: result.row.plan_id,
|
||||
key: result.row.key,
|
||||
token_name: result.row.token_name,
|
||||
expire_at: result.row.expire_at,
|
||||
plain_token: result.plain_token,
|
||||
|
||||
@@ -65,8 +65,11 @@ function buildProxyRoutes() {
|
||||
ctx.fail(authResult.message || "鉴权失败");
|
||||
return;
|
||||
}
|
||||
// 3. 组装 query
|
||||
// 3. 组装 query,并注入 token 对应 key(上游要求参数名为 key)
|
||||
const query = { ...ctx.query };
|
||||
if (!query.key && authResult.context && authResult.context.token_key) {
|
||||
query.key = authResult.context.token_key;
|
||||
}
|
||||
|
||||
// 4. 转发到上游
|
||||
const result = await proxy.forwardRequest({
|
||||
|
||||
@@ -18,6 +18,10 @@ module.exports = (db) => {
|
||||
allowNull: false,
|
||||
defaultValue: "",
|
||||
},
|
||||
key: {
|
||||
type: Sequelize.STRING(128),
|
||||
allowNull: true,
|
||||
},
|
||||
token_hash: {
|
||||
type: Sequelize.STRING(64),
|
||||
allowNull: false,
|
||||
@@ -42,6 +46,6 @@ module.exports = (db) => {
|
||||
underscored: true,
|
||||
}
|
||||
);
|
||||
// biz_api_token.sync({ force: true });
|
||||
//biz_api_token.sync({ force: true });
|
||||
return biz_api_token;
|
||||
};
|
||||
|
||||
@@ -109,6 +109,7 @@ async function verifyRequest(body) {
|
||||
plan_id: sub.plan_id,
|
||||
subscription_id: sub.id,
|
||||
token_id: row.id,
|
||||
token_key: row.key || "",
|
||||
stat_month: statMonth,
|
||||
usage_snapshot: {
|
||||
msg_count: usageSvc.num(usageRow.msg_count),
|
||||
|
||||
@@ -36,7 +36,7 @@ async function findActiveSubscriptionForUser(userId) {
|
||||
}
|
||||
|
||||
async function createToken(body) {
|
||||
const { user_id, token_name, expire_at } = body;
|
||||
const { user_id, token_name, expire_at, key } = body;
|
||||
if (!user_id || !expire_at) throw new Error("缺少 user_id 或 expire_at");
|
||||
const u = await baseModel.biz_user.findByPk(user_id);
|
||||
if (!u) throw new Error("用户不存在");
|
||||
@@ -60,6 +60,7 @@ async function createToken(body) {
|
||||
user_id,
|
||||
plan_id,
|
||||
token_name: token_name || "default",
|
||||
key: key || null,
|
||||
token_hash,
|
||||
secret_cipher,
|
||||
status: "active",
|
||||
|
||||
Reference in New Issue
Block a user