feat: add first-run setup security flow
This commit is contained in:
1 parent
1b6d8e7277
commit
cdaf3638b8
24 files changed
+1127
-64
No files matched your search
@@ -12,6 +12,13 @@ from core.settings import settings
|
||||
from apps.admin.services import FileService, ConfigService, LocalFileService
|
||||
|
||||
|
||||
def _get_jwt_secret() -> bytes:
|
||||
secret = getattr(settings, "jwt_secret", "")
|
||||
if not secret:
|
||||
raise RuntimeError("JWT签名密钥未初始化")
|
||||
return secret.encode()
|
||||
|
||||
|
||||
def create_token(data: dict, expires_in: int = 3600 * 24 * 30) -> str:
|
||||
"""
|
||||
创建JWT token
|
||||
@@ -25,9 +32,7 @@ def create_token(data: dict, expires_in: int = 3600 * 24 * 30) -> str:
|
||||
json.dumps({**data, "exp": int(time.time()) + expires_in}).encode()
|
||||
).decode()
|
||||
|
||||
signature = hmac.new(
|
||||
settings.admin_token.encode(), f"{header}.{payload}".encode(), "sha256"
|
||||
).digest()
|
||||
signature = hmac.new(_get_jwt_secret(), f"{header}.{payload}".encode(), "sha256").digest()
|
||||
signature = base64.b64encode(signature).decode()
|
||||
|
||||
return f"{header}.{payload}.{signature}"
|
||||
@@ -44,7 +49,7 @@ def verify_token(token: str) -> dict:
|
||||
|
||||
# 验证签名
|
||||
expected_signature = hmac.new(
|
||||
settings.admin_token.encode(),
|
||||
_get_jwt_secret(),
|
||||
f"{header_b64}.{payload_b64}".encode(),
|
||||
"sha256",
|
||||
).digest()
|
||||
|
||||
+16
-2
@@ -8,6 +8,7 @@ from core.response import APIResponse
|
||||
from core.storage import FileStorageInterface, storages
|
||||
from core.settings import settings
|
||||
from core.config import refresh_settings
|
||||
from core.security import INTERNAL_CONFIG_KEYS, generate_jwt_secret
|
||||
from apps.base.models import FileCodes, KeyValue
|
||||
from apps.base.utils import get_expire_info, get_file_path_name
|
||||
from fastapi import HTTPException
|
||||
@@ -1514,20 +1515,30 @@ class ConfigService:
|
||||
FLOAT_FIELDS = {"opacity"}
|
||||
|
||||
def get_config(self):
|
||||
return dict(settings.items())
|
||||
config = dict(settings.items())
|
||||
config["admin_token"] = ""
|
||||
for key in INTERNAL_CONFIG_KEYS:
|
||||
config.pop(key, None)
|
||||
return config
|
||||
|
||||
async def update_config(self, data: dict):
|
||||
current_config = dict(settings.items())
|
||||
next_config = dict(current_config)
|
||||
update_data = {
|
||||
key: value for key, value in data.items() if key in settings.default_config
|
||||
key: value
|
||||
for key, value in data.items()
|
||||
if key in settings.default_config and key not in INTERNAL_CONFIG_KEYS
|
||||
}
|
||||
|
||||
admin_token = update_data.get("admin_token")
|
||||
admin_password_changed = False
|
||||
if admin_token is None or admin_token == "":
|
||||
update_data.pop("admin_token", None)
|
||||
elif not is_password_hashed(admin_token):
|
||||
update_data["admin_token"] = hash_password(admin_token)
|
||||
admin_password_changed = True
|
||||
else:
|
||||
admin_password_changed = True
|
||||
|
||||
for key, value in update_data.items():
|
||||
if value == "" and key in self.INT_FIELDS | self.FLOAT_FIELDS:
|
||||
@@ -1543,6 +1554,9 @@ class ConfigService:
|
||||
except (TypeError, ValueError):
|
||||
raise HTTPException(status_code=400, detail=f"{key} 配置值格式错误")
|
||||
|
||||
if admin_password_changed:
|
||||
next_config["jwt_secret"] = generate_jwt_secret()
|
||||
|
||||
await KeyValue.update_or_create(key="settings", defaults={"value": next_config})
|
||||
await refresh_settings()
|
||||
|
||||
|
||||
Reference in new issue
Block a user