New pages and APIs for cross-platform jobs, YouTube OAuth health, mobile ops, settings audit, optional DB backup, quota/disk alerts, and clip archive replay. All new behavior defaults off to avoid changing production pipelines. Co-authored-by: Cursor <cursoragent@cursor.com>
95 lines
2.9 KiB
Python
95 lines
2.9 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""Role-based access: admin (read/write) vs readonly (read-only)."""
|
|
from __future__ import annotations
|
|
|
|
import secrets
|
|
from typing import Any, Optional
|
|
|
|
from starlette.requests import Request
|
|
|
|
from .session_auth import hash_password, verify_session_payload
|
|
|
|
|
|
def get_readonly_credentials() -> tuple[str, str]:
|
|
from . import db
|
|
|
|
m = db.get_settings_map()
|
|
user = str(m.get("readonly_username") or "").strip()
|
|
pw_hash = str(m.get("readonly_password_hash") or "").strip()
|
|
return user, pw_hash
|
|
|
|
|
|
def readonly_enabled() -> bool:
|
|
user, pw_hash = get_readonly_credentials()
|
|
return bool(user and pw_hash)
|
|
|
|
|
|
def verify_readonly(username: str, password: str) -> bool:
|
|
user, pw_hash = get_readonly_credentials()
|
|
if not user or not pw_hash:
|
|
return False
|
|
if not secrets.compare_digest(str(username or ""), user):
|
|
return False
|
|
return secrets.compare_digest(hash_password(str(password or "")), pw_hash)
|
|
|
|
|
|
def set_readonly_password(username: str, password: str) -> None:
|
|
from . import db
|
|
|
|
user = str(username or "").strip()
|
|
if not user:
|
|
db.update_settings_map({"readonly_username": "", "readonly_password_hash": ""})
|
|
return
|
|
pw = str(password or "")
|
|
if len(pw) < 8:
|
|
raise ValueError("只读账号密码至少 8 位")
|
|
db.update_settings_map(
|
|
{
|
|
"readonly_username": user,
|
|
"readonly_password_hash": hash_password(pw),
|
|
}
|
|
)
|
|
|
|
|
|
def session_payload_from_request(request: Request) -> Optional[dict[str, Any]]:
|
|
from .config import settings
|
|
from .session_auth import SESSION_COOKIE, verify_session_token
|
|
|
|
if not settings.auth_enabled:
|
|
return {"u": str(settings.admin_username or "admin"), "role": "admin"}
|
|
token = (request.cookies.get(SESSION_COOKIE) or "").strip()
|
|
if not token:
|
|
auth = request.headers.get("Authorization") or ""
|
|
if auth.lower().startswith("bearer "):
|
|
token = auth[7:].strip()
|
|
if not token:
|
|
return None
|
|
payload = verify_session_payload(token)
|
|
if not payload:
|
|
# Legacy token without role — treat as admin if username verifies
|
|
user = verify_session_token(token)
|
|
if user:
|
|
return {"u": user, "role": "admin"}
|
|
return None
|
|
return payload
|
|
|
|
|
|
def session_role_from_request(request: Request) -> str:
|
|
payload = session_payload_from_request(request)
|
|
if not payload:
|
|
return ""
|
|
role = str(payload.get("role") or "admin").strip().lower()
|
|
return role if role in {"admin", "readonly"} else "admin"
|
|
|
|
|
|
def assert_write_access(request: Request) -> None:
|
|
from fastapi import HTTPException
|
|
|
|
role = session_role_from_request(request)
|
|
if role == "readonly":
|
|
raise HTTPException(403, "只读账号无写入权限")
|
|
|
|
|
|
def is_write_method(method: str) -> bool:
|
|
return method.upper() in {"POST", "PUT", "PATCH", "DELETE"}
|