fix: 管理员默认口令改为六位默认值
This commit is contained in:
@@ -10,7 +10,8 @@ _N = 2**14
|
||||
_R = 8
|
||||
_P = 1
|
||||
_PREFIX = "scrypt$"
|
||||
_LEGACY_PASSWORD = "242520"
|
||||
_DEFAULT_PASSWORD = "242520"
|
||||
_MIN_LENGTH = 6
|
||||
|
||||
|
||||
def hash_password(password: str) -> str:
|
||||
@@ -34,5 +35,9 @@ def is_hashed(stored: str) -> bool:
|
||||
return stored.startswith(_PREFIX)
|
||||
|
||||
|
||||
def legacy_password() -> str:
|
||||
return _LEGACY_PASSWORD
|
||||
def default_password() -> str:
|
||||
return _DEFAULT_PASSWORD
|
||||
|
||||
|
||||
def min_length() -> int:
|
||||
return _MIN_LENGTH
|
||||
|
||||
@@ -2,9 +2,7 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import os
|
||||
import secrets
|
||||
from datetime import datetime, timezone
|
||||
|
||||
from sqlalchemy import select
|
||||
@@ -12,11 +10,10 @@ from sqlalchemy.orm import Session
|
||||
|
||||
from admin.identity import identity
|
||||
from admin.models import SystemConfig
|
||||
from admin.passwords import hash_password, is_hashed, legacy_password
|
||||
from admin.passwords import default_password, hash_password, is_hashed, min_length
|
||||
from euniv.crypto import decrypt_secret
|
||||
|
||||
_INITIAL_USERNAME = "admin"
|
||||
_LOG = logging.getLogger("admin.settings")
|
||||
|
||||
|
||||
class SettingsError(ValueError):
|
||||
@@ -36,8 +33,8 @@ def _configured_admin_password() -> str | None:
|
||||
value = os.environ.get("EUNIV_ADMIN_PASSWORD", "").strip()
|
||||
if not value:
|
||||
return None
|
||||
if len(value) < 8:
|
||||
raise RuntimeError("EUNIV_ADMIN_PASSWORD 至少需要 8 位")
|
||||
if len(value) < min_length():
|
||||
raise RuntimeError(f"EUNIV_ADMIN_PASSWORD 至少需要 {min_length()} 位")
|
||||
return value
|
||||
|
||||
|
||||
@@ -45,9 +42,7 @@ def _new_admin_password() -> str:
|
||||
configured = _configured_admin_password()
|
||||
if configured is not None:
|
||||
return configured
|
||||
generated = secrets.token_urlsafe(12)
|
||||
_LOG.warning("已生成随机管理员口令,请立即保存:%s", generated)
|
||||
return generated
|
||||
return default_password()
|
||||
|
||||
|
||||
def _reversible_password(stored: str) -> str | None:
|
||||
@@ -90,8 +85,7 @@ def ensure_admin_seed() -> None:
|
||||
plain = _reversible_password(row.admin_password_cipher)
|
||||
if plain is None:
|
||||
return
|
||||
replacement = _new_admin_password() if plain == legacy_password() else plain
|
||||
row.admin_password_cipher = hash_password(replacement)
|
||||
row.admin_password_cipher = hash_password(plain)
|
||||
db.commit()
|
||||
|
||||
|
||||
@@ -216,8 +210,8 @@ def save_settings(db: Session, payload: dict) -> SystemConfig:
|
||||
raise SettingsError("自动滑动次数须为 1 到 10 的整数")
|
||||
password = payload.get("admin_password")
|
||||
if password is not None and password != "":
|
||||
if len(str(password)) < 8:
|
||||
raise SettingsError("管理员密码至少 8 位")
|
||||
if len(str(password)) < min_length():
|
||||
raise SettingsError(f"管理员密码至少 {min_length()} 位")
|
||||
if len(str(password)) > 128:
|
||||
raise SettingsError("管理员密码过长")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user