Archived
为什么:单文件 1100~1900 行的巨石模块让「改一处要通读全文」;同时注释里残留 大量已删台账的编号,以及「Flask/FastAPI 双栈迁移期」这类已失实的架构描述, 会误导后来者按不复存在的架构去改代码。 拆分(对外导入路径不变,行为零变更): - music_downloader.py -> music_downloader/(models/downloader/cover/_http/_log/_concurrency) - auth_system.py -> auth_system/(db/security/users/credits/ledger/mail/cards/backup...) - music_api.py -> music_api/(api/client/crypto/qr/cookies/compat/constants/errors) - services/runtime.py -> services/(api_service/_shared/_taskcore/downloads/batch/podcast/ streaming/tasks);services/runtime.py 保留为兼容门面 兼容性做法:各包用 __init__.py 以 __all__ 原样再导出全部符号,既有调用点 (auth_system.xxx / services.xxx / from music_api import Y)零改动;路径常量改为经包属性 动态读取(auth_system.DB_PATH),否则测试的 monkeypatch 会静默失效并写到生产库。 修复 9 处代码损伤(上一轮注释清理把 `()` 当「空括号残留」删掉所致;用「与拆分前逐字 等价」校验逐一定位,静态检查只会报出其中 4 处): - core/config.py ).strip() or '*' - core/state.py threading.Lock() x2 - qr_login.py datetime.now() / .strip() - music_downloader/downloader.py .lower() - services/podcast.py set() / time.time() x2 清理与删除: - 删除 部署文档.md / 功能介绍.md / README_私有包警告.txt - 清理指向已删台账与文档的过时编号注释;把「迁移期 / 两栈 / 双框架 / 已迁移」等 失实描述改写为现状描述(保留 `<-> Flask /path` 映射表等仍有价值的契约来源说明) - _mkpkg.py:公开包剔除清单去掉已不存在的文档名;私有包警告改为自包含 - pyproject.toml:补 pythonpath = ["."],使裸跑 pytest 不再 import core 失败 验证:ruff 全通过;pyright 0 errors / 0 warnings(102 文件);pytest 144 passed; 真实 uvicorn 冒烟 6 条路由符合预期且生产 data/ 指纹未变;忽略注释的逐字等价性校验通过。
82 lines
3.0 KiB
Python
82 lines
3.0 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""Flask 兼容的会话 Cookie 编解码(itsdangerous 签名格式)。
|
||
|
||
Flask 默认会话即 itsdangerous 签名 Cookie(`session` cookie,格式 `payload.timestamp.hmac`,
|
||
payload 为会话字典的 compact JSON;bytes/datetime/tuple 等类型会被 Flask 打上
|
||
单字符标签再包一层)。本站早期由 Flask 签发会话,为让**存量 Cookie 无需重新登录即可继续使用**,
|
||
本模块复刻同一套编解码(同格式、同密钥、同摘要算法)。
|
||
|
||
密钥同源:取自 auth_system.get_secret_key()(data/secret.key)。
|
||
"""
|
||
import base64
|
||
import hashlib
|
||
import hmac
|
||
import json
|
||
import time
|
||
from typing import Any
|
||
|
||
# 与 Flask 默认一致:SHA1 + itsdangerous 的 key derivation(HMAC(digest, salt))
|
||
_SALT = 'cookie-session'
|
||
_DIGEST = hashlib.sha1
|
||
|
||
SESSION_KEY = 'site_uid'
|
||
|
||
|
||
def _derive_key(secret: str, salt: str = _SALT) -> bytes:
|
||
"""itsdangerous 的密钥派生:HMAC(secret, salt)。"""
|
||
mac = hmac.new(secret.encode('utf-8'), salt.encode('utf-8'), _DIGEST)
|
||
return mac.digest()
|
||
|
||
|
||
def _int_to_bytes(num: int) -> bytes:
|
||
"""itsdangerous 时间戳编码:8 字节大端并去前导零(struct.pack('>Q') + lstrip)。"""
|
||
return num.to_bytes(8, 'big').lstrip(b'\x00') or b'\x00'
|
||
|
||
|
||
def dumps(secret: str, session: dict[str, Any]) -> str:
|
||
"""把会话字典序列化为 Flask/itsdangerous 兼容的签名 Cookie 值。"""
|
||
payload = base64.b64encode(
|
||
json.dumps(session, separators=(',', ':')).encode('utf-8')).decode('ascii')
|
||
ts = base64.b64encode(_int_to_bytes(int(time.time()))).decode('ascii').rstrip('=')
|
||
value = f'{payload}.{ts}'
|
||
sig = hmac.new(_derive_key(secret), value.encode('utf-8'), _DIGEST).digest()
|
||
return f'{value}.{base64.b64encode(sig).decode("ascii").rstrip("=")}'
|
||
|
||
|
||
def loads(secret: str, value: str) -> dict[str, Any] | None:
|
||
"""校验签名并解出会话字典;无效/被篡改返回 None。
|
||
|
||
注:Flask 的会话 JSON 采用「带空格」的 compact 编码(`{"a": 1}`),
|
||
解码时直接 json.loads 即可,两种空白形式都能解析。
|
||
"""
|
||
if not value:
|
||
return None
|
||
try:
|
||
payload, ts, sig = value.rsplit('.', 2)
|
||
except ValueError:
|
||
return None
|
||
value_no_sig = f'{payload}.{ts}'
|
||
expected = hmac.new(_derive_key(secret), value_no_sig.encode('utf-8'), _DIGEST).digest()
|
||
pad = '=' * (-len(sig) % 4)
|
||
try:
|
||
actual = base64.urlsafe_b64decode(sig + pad)
|
||
except Exception:
|
||
return None
|
||
if not hmac.compare_digest(expected, actual):
|
||
return None
|
||
pad_p = '=' * (-len(payload) % 4)
|
||
try:
|
||
raw = base64.urlsafe_b64decode(payload + pad_p)
|
||
data = json.loads(raw.decode('utf-8'))
|
||
except Exception:
|
||
return None
|
||
return data if isinstance(data, dict) else None
|
||
|
||
|
||
def get_uid(session: dict[str, Any] | None) -> int | None:
|
||
"""取会话中的站点用户 id(Flask 存的是 int,直接可用)。"""
|
||
if not session:
|
||
return None
|
||
val = session.get(SESSION_KEY)
|
||
return int(val) if val else None
|