Open Maopaoya M0-M2: 用户体系+权限开关+后台 / 渠道管理 / 单角色扮演(记忆可篡改+世界书+TTS) / 动态小说管线(实体抽取→建档→立绘→世界书注册) / AES-GCM数据加密+登录解密迁移 / 液态玻璃UI

This commit is contained in:
zhiqui
2026-11-14 23:23:34 +08:00
commit f8faed9ea7
29 changed files with 3412 additions and 0 deletions
View File
+29
View File
@@ -0,0 +1,29 @@
from pydantic_settings import BaseSettings
class Settings(BaseSettings):
app_name: str = "Open Maopaoya"
database_url: str = "sqlite+aiosqlite:///./data.db"
jwt_secret: str = "change-me-in-env"
jwt_expire_minutes: int = 60 * 24 * 7
first_admin_user: str = "admin"
first_admin_pass: str = "maopaoya-admin"
class Config:
env_file = ".env"
env_prefix = "OMO_"
settings = Settings()
# 四个模型槽位的默认预设(Step Plan)
STEP_PRESET = {
"name": "Step Plan (阶跃星辰)",
"base_url": "https://api.stepfun.com/v1",
"models": {
"narrative": "step-3.5-flash", # 主叙事/角色扮演:现役最便宜,角色扮演最强
"extract": "step-3.5-flash", # 实体抽取
"tts": "stepaudio-2.5-tts", # 语音合成
"image": "", # 生图:阶跃已全下线,留槽位接任意 OpenAI 兼容生图
},
}
+69
View File
@@ -0,0 +1,69 @@
import base64
import os
import bcrypt
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
from cryptography.hazmat.primitives import hashes
from jose import jwt, JWTError
from .config import settings
_JWT_KEY = settings.jwt_secret.encode()
def hash_password(pw: str) -> str:
return bcrypt.hashpw(pw.encode(), bcrypt.gensalt()).decode()
def verify_password(pw: str, hashed: str) -> bool:
return bcrypt.checkpw(pw.encode(), hashed.encode())
def make_token(user_id: int, is_admin: bool) -> str:
return jwt.encode(
{"uid": user_id, "adm": is_admin},
_JWT_KEY, algorithm="HS256")
def parse_token(token: str) -> dict | None:
try:
return jwt.decode(token, _JWT_KEY, algorithms=["HS256"])
except JWTError:
return None
# ---------- 用户数据加密(管理员不可见模式) ----------
# 密钥从用户密码派生,服务器不落盘。登录时派生,缓存在进程内 session map。
_ENC_PREFIX = "ENC::"
_PBKDF2_ITERS = 200_000
def derive_key(password: str, salt: bytes) -> bytes:
kdf = PBKDF2HMAC(algorithm=hashes.SHA256(), length=32,
salt=salt, iterations=_PBKDF2_ITERS)
return kdf.derive(password.encode())
def encrypt_text(key: bytes, plaintext: str) -> str:
if not plaintext or plaintext.startswith(_ENC_PREFIX):
return plaintext
nonce = os.urandom(12)
ct = AESGCM(key).encrypt(nonce, plaintext.encode(), None)
return _ENC_PREFIX + base64.b64encode(nonce + ct).decode()
def decrypt_text(key: bytes, stored: str) -> str:
if not stored or not stored.startswith(_ENC_PREFIX):
return stored
raw = base64.b64decode(stored[len(_ENC_PREFIX):])
return AESGCM(key).decrypt(raw[:12], raw[12:], None).decode()
def is_encrypted(stored: str | None) -> bool:
return bool(stored) and stored.startswith(_ENC_PREFIX)
# 进程内派生密钥缓存 {user_id: key}(重启后需重新登录)
key_cache: dict[int, bytes] = {}