mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-10-04 03:10:01 +08:00
🎨 代码优化 (#1582)
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import asyncio
|
||||
import secrets
|
||||
|
||||
import nonebot
|
||||
from fastapi import APIRouter, FastAPI
|
||||
@@ -6,7 +7,7 @@ from nonebot.log import default_filter, default_format
|
||||
from nonebot.plugin import PluginMetadata
|
||||
|
||||
from zhenxun.configs.config import Config as gConfig
|
||||
from zhenxun.configs.utils import PluginExtraData
|
||||
from zhenxun.configs.utils import PluginExtraData, RegisterConfig
|
||||
from zhenxun.services.log import logger, logger_
|
||||
from zhenxun.utils.enum import PluginType
|
||||
|
||||
@@ -28,15 +29,40 @@ __plugin_meta__ = PluginMetadata(
|
||||
usage="""
|
||||
""".strip(),
|
||||
extra=PluginExtraData(
|
||||
author="HibiKier", version="0.1", plugin_type=PluginType.HIDDEN
|
||||
author="HibiKier",
|
||||
version="0.1",
|
||||
plugin_type=PluginType.HIDDEN,
|
||||
Configs=[
|
||||
RegisterConfig(
|
||||
module="web-ui",
|
||||
key="username",
|
||||
value="admin",
|
||||
help="前端管理用户名",
|
||||
type=str,
|
||||
default_value="admin",
|
||||
),
|
||||
RegisterConfig(
|
||||
module="web-ui",
|
||||
key="password",
|
||||
value=None,
|
||||
help="前端管理密码",
|
||||
type=str,
|
||||
default_value=None,
|
||||
),
|
||||
RegisterConfig(
|
||||
module="web-ui",
|
||||
key="secret",
|
||||
value=secrets.token_urlsafe(32),
|
||||
help="JWT密钥",
|
||||
type=str,
|
||||
default_value=None,
|
||||
),
|
||||
],
|
||||
).dict(),
|
||||
)
|
||||
|
||||
driver = nonebot.get_driver()
|
||||
|
||||
gConfig.add_plugin_config("web-ui", "username", "admin", help="前端管理用户名")
|
||||
|
||||
gConfig.add_plugin_config("web-ui", "password", None, help="前端管理密码")
|
||||
|
||||
gConfig.set_name("web-ui", "web-ui")
|
||||
|
||||
|
||||
@@ -28,8 +28,6 @@ if token_file.exists():
|
||||
token_data = json.load(open(token_file, "r", encoding="utf8"))
|
||||
except json.JSONDecodeError:
|
||||
pass
|
||||
if not token_data.get("secret"):
|
||||
token_data["secret"] = secrets.token_hex(64)
|
||||
|
||||
|
||||
def get_user(uname: str) -> User | None:
|
||||
@@ -57,7 +55,7 @@ def create_token(user: User, expires_delta: timedelta | None = None):
|
||||
expire = datetime.utcnow() + (expires_delta or timedelta(minutes=15))
|
||||
return jwt.encode(
|
||||
claims={"sub": user.username, "exp": expire},
|
||||
key=token_data["secret"],
|
||||
key=Config.get_config("web-ui", "secret"),
|
||||
algorithm=ALGORITHM,
|
||||
)
|
||||
|
||||
@@ -73,7 +71,7 @@ def authentication():
|
||||
# if token not in token_data["token"]:
|
||||
def inner(token: str = Depends(oauth2_scheme)):
|
||||
try:
|
||||
payload = jwt.decode(token, token_data["secret"], algorithms=[ALGORITHM])
|
||||
payload = jwt.decode(token, Config.get_config("web-ui", "secret"), algorithms=[ALGORITHM])
|
||||
username, expire = payload.get("sub"), payload.get("exp")
|
||||
user = get_user(username) # type: ignore
|
||||
if user is None:
|
||||
|
||||
Reference in New Issue
Block a user