cmvr-visual/backend/app/core/security.py
zhanghao 88cf14d794 feat: consolidate story studio and editable automatic production
Restructure backend and frontend modules; add story, library, and editor schema migrations.

Support editable automatic production, manual confirmation modes, account and user lifecycle management, compact container scrolling, consistent forms, and per-task result previews.

Update inference and TTS scheduling, deployment guidance, and regression coverage; remove obsolete standalone pages and temporary acceptance artifacts.
2026-11-14 14:07:26 +08:00

56 lines
1.4 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from dataclasses import dataclass, field
import hashlib, hmac, secrets
from app.core.errors import fail
COOKIE = "cmvr_session"
def token_hash(token):
return hashlib.sha256(token.encode()).hexdigest()
def hash_password(password):
salt = secrets.token_bytes(16)
key = hashlib.scrypt(
password.encode(), salt=salt, n=32768, r=8, p=1, dklen=64, maxmem=128 * 1024 * 1024
)
return f"scrypt$32768$8$1${salt.hex()}${key.hex()}"
def verify_password(password, encoded):
try:
_, n, r, p, salt, expected = encoded.split("$")
key = hashlib.scrypt(
password.encode(),
salt=bytes.fromhex(salt),
n=int(n),
r=int(r),
p=int(p),
dklen=64,
maxmem=128 * 1024 * 1024,
)
return hmac.compare_digest(key.hex(), expected)
except (ValueError, TypeError):
hashlib.scrypt(
password.encode(), salt=b"\0" * 16, n=32768, r=8, p=1, maxmem=128 * 1024 * 1024
)
return False
def validate_password(password, *, minimum=12):
if not minimum <= len(password) <= 256:
fail(422, "weak_password", f"密码长度须为 {minimum}–256 个字符")
@dataclass
class Actor:
id: str
username: str
display_name: str
role: str
csrf_token: str
token_hash: str
auth_kind: str = "session"
scopes: list[str] = field(default_factory=list)
must_change_password: bool = False