sync: 鍚屾灞€鍩熺綉鏈€鏂颁唬鐮?(source master @ b69568e4)

This commit is contained in:
gozeros committed 2026-10-21 14:51:31 +08:00
1 parent 4f7f07504a
commit 8bc8b0e082
1651 files changed
+278599 -11416

No files matched your search

@@ -9,15 +9,19 @@ Hash format: ``$dfv<N>$<bcrypt_hash>`` where ``<N>`` is the version.
to the hash.
Verification auto-detects the version and falls back to v1 for hashes
without a prefix, so existing deployments upgrade transparently on next
login.
without a prefix, so existing credentials stay valid until their next explicit
password change writes the current format.
"""
import asyncio
import base64
import hashlib
import hmac
import re
import bcrypt
from deerflow.config import get_app_config
_CURRENT_VERSION = 2
_PREFIX_V2 = "$dfv2$"
@@ -79,3 +83,30 @@ async def verify_password_async(plain_password: str, hashed_password: str) -> bo
blocking the event loop during password verification.
"""
return await asyncio.to_thread(verify_password, plain_password, hashed_password)
def is_password_hash(value: str) -> bool:
"""Recognize current and historical hashes without exposing them in APIs."""
return bool(re.fullmatch(r"(?:\$dfv[12]\$)?\$2[aby]\$\d{2}\$[./A-Za-z0-9]{53}", value))
def has_account_password(user) -> bool:
return bool(getattr(user, "login_password", None) or getattr(user, "password_hash", None))
async def verify_account_password(password: str, user) -> bool:
"""The historical personal passphrase takes precedence over the old hash.
New credentials store the same hash in both columns. Legacy plaintext
passphrases remain accepted, including when an older password_hash differs.
Accounts without either credential use the deployment's shared passphrase
(the historical fallback is 123ewq), regardless of its old require flag.
"""
if getattr(user, "login_password", None):
if is_password_hash(user.login_password):
return await verify_password_async(password, user.login_password)
return hmac.compare_digest(password.encode("utf-8"), user.login_password.encode("utf-8"))
if getattr(user, "password_hash", None):
return await verify_password_async(password, user.password_hash)
fallback = get_app_config().auth_login.username_login.effective_password
return hmac.compare_digest(password.encode("utf-8"), fallback.encode("utf-8"))