refactor: 移除旧版单文件管理后台兼容
This commit is contained in:
+1
-1
@@ -20,7 +20,7 @@ async def admin_audit_middleware(request: Request, call_next):
|
||||
if should_audit:
|
||||
try:
|
||||
principal = getattr(request.state, "admin_principal", None)
|
||||
token = request.headers.get("authorization", "") or request.headers.get("X-Admin-Token", "")
|
||||
token = request.headers.get("authorization", "")
|
||||
conn = db.get_conn()
|
||||
conn.execute(
|
||||
"""INSERT INTO admin_audit_logs
|
||||
|
||||
+1
-23
@@ -165,16 +165,6 @@ def principal_from_user_row(row, auth_type: str = "jwt") -> AdminPrincipal:
|
||||
)
|
||||
|
||||
|
||||
def legacy_admin_principal() -> AdminPrincipal:
|
||||
return AdminPrincipal(
|
||||
username="legacy-admin-token",
|
||||
display_name="兼容管理员令牌",
|
||||
roles=["super_admin"],
|
||||
permissions=role_permissions(["super_admin"]),
|
||||
auth_type="legacy_token",
|
||||
)
|
||||
|
||||
|
||||
def create_jwt_token(username: str, token_type: str, expires_delta: timedelta, roles: list[str] | None = None) -> tuple[str, str, datetime]:
|
||||
now = utc_now()
|
||||
expires_at = now + expires_delta
|
||||
@@ -271,21 +261,11 @@ def persist_env_value(path: Path, key: str, value: str) -> bool:
|
||||
return True
|
||||
|
||||
|
||||
def legacy_admin_auth(X_Admin_Token: str = Header("")):
|
||||
# Backward-compatible token-only auth for old scripts.
|
||||
token = X_Admin_Token if isinstance(X_Admin_Token, str) else ""
|
||||
if token and secrets.compare_digest(token, settings.admin_token):
|
||||
return legacy_admin_principal()
|
||||
raise HTTPException(status_code=403, detail="后台密钥错误,禁止访问")
|
||||
|
||||
|
||||
def current_admin(
|
||||
request: Request,
|
||||
Authorization: str = Header(""),
|
||||
X_Admin_Token: str = Header(""),
|
||||
) -> AdminPrincipal:
|
||||
authorization = Authorization if isinstance(Authorization, str) else ""
|
||||
admin_token = X_Admin_Token if isinstance(X_Admin_Token, str) else ""
|
||||
if authorization.lower().startswith("bearer "):
|
||||
payload = decode_jwt_token(authorization[7:].strip(), "access")
|
||||
username = str(payload.get("sub") or "")
|
||||
@@ -295,9 +275,7 @@ def current_admin(
|
||||
principal = principal_from_user_row(row)
|
||||
request.state.admin_principal = principal
|
||||
return principal
|
||||
principal = legacy_admin_auth(admin_token)
|
||||
request.state.admin_principal = principal
|
||||
return principal
|
||||
raise HTTPException(status_code=401, detail="请先登录管理后台")
|
||||
|
||||
|
||||
admin_auth = current_admin
|
||||
|
||||
Reference in New Issue
Block a user