增加用户验证及余额检查API接口
This commit is contained in:
parent
98ca47340b
commit
8d6b5ea81c
@ -1368,8 +1368,7 @@ def get_user_ssh_keys():
|
||||
)
|
||||
|
||||
|
||||
@jingrow.whitelist(allow_guest=True)
|
||||
# @rate_limit(limit=5, seconds=60 * 60)
|
||||
@jingrow.whitelist()
|
||||
def is_2fa_enabled(user):
|
||||
return jingrow.db.get_value("User 2FA", user, "enabled")
|
||||
|
||||
@ -1538,3 +1537,105 @@ def update_profile_email(email):
|
||||
"user": user_pg,
|
||||
"team": team
|
||||
}
|
||||
|
||||
@jingrow.whitelist()
|
||||
def verify_api_credentials_and_balance(api_key: str, api_secret: str, amount: float = 0):
|
||||
"""验证API密钥和检查余额
|
||||
|
||||
参数:
|
||||
api_key: 用户的API密钥
|
||||
api_secret: 用户的API密钥对应的密钥
|
||||
amount: 需要检查的余额金额,默认为0
|
||||
|
||||
返回:
|
||||
{
|
||||
"success": bool, # 验证是否成功
|
||||
"message": str # 错误信息
|
||||
}
|
||||
"""
|
||||
try:
|
||||
# 记录验证请求信息
|
||||
jingrow.log_error("API验证请求参数", f"api_key={api_key}, api_secret={api_secret}, amount={amount}")
|
||||
|
||||
# 获取当前用户(管理员)的API信息
|
||||
admin_user = jingrow.session.user
|
||||
jingrow.log_error("API验证管理员信息", f"当前用户: {admin_user}")
|
||||
|
||||
# 验证管理员权限
|
||||
if admin_user != "Administrator":
|
||||
return {
|
||||
"success": False,
|
||||
"message": "只有管理员用户才能访问此API"
|
||||
}
|
||||
|
||||
# 验证用户的API密钥
|
||||
user = jingrow.db.get_value(
|
||||
"User",
|
||||
{"api_key": api_key},
|
||||
["name", "enabled", "api_secret"]
|
||||
)
|
||||
|
||||
jingrow.log_error("API验证用户查询", f"用户查询结果: {user}")
|
||||
|
||||
if not user:
|
||||
return {
|
||||
"success": False,
|
||||
"message": "无效的API密钥"
|
||||
}
|
||||
|
||||
# 验证用户的API密钥
|
||||
stored_secret = get_decrypted_password("User", user[0], "api_secret")
|
||||
if stored_secret != api_secret:
|
||||
jingrow.log_error("API验证密钥不匹配", f"API密钥不匹配。数据库中的密钥: {stored_secret}")
|
||||
return {
|
||||
"success": False,
|
||||
"message": "无效的API密钥"
|
||||
}
|
||||
|
||||
if not user[1]: # 检查用户是否启用
|
||||
return {
|
||||
"success": False,
|
||||
"message": "用户账户已禁用"
|
||||
}
|
||||
|
||||
# 获取用户团队
|
||||
team = jingrow.db.get_value(
|
||||
"Team",
|
||||
{"user": user[0]},
|
||||
["name", "enabled"]
|
||||
)
|
||||
|
||||
jingrow.log_error("API验证团队信息", f"团队信息: {team}")
|
||||
|
||||
if not team:
|
||||
return {
|
||||
"success": False,
|
||||
"message": "未找到用户团队"
|
||||
}
|
||||
|
||||
if not team[1]: # 检查团队是否启用
|
||||
return {
|
||||
"success": False,
|
||||
"message": "团队账户已禁用"
|
||||
}
|
||||
|
||||
# 获取团队余额
|
||||
team_pg = jingrow.get_pg("Team", team[0])
|
||||
balance = team_pg.get_balance()
|
||||
|
||||
jingrow.log_error("API验证团队余额", f"团队余额: {balance}")
|
||||
|
||||
# 检查余额是否足够
|
||||
has_sufficient_balance = balance >= amount
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"message": "验证成功" if has_sufficient_balance else "余额不足"
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
jingrow.log_error("API验证系统错误", f"API验证失败: {str(e)}")
|
||||
return {
|
||||
"success": False,
|
||||
"message": f"验证过程发生错误: {str(e)}"
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user