fix: 管理员默认口令改为六位默认值
This commit is contained in:
+1
-1
@@ -4,7 +4,7 @@ EUNIV_SECRET_KEY=
|
|||||||
EUNIV_SECRET_KEY_OLD=
|
EUNIV_SECRET_KEY_OLD=
|
||||||
# 下面这行只对直接启动的进程生效。docker compose 里数据库路径写死为 /data/euniv.db。
|
# 下面这行只对直接启动的进程生效。docker compose 里数据库路径写死为 /data/euniv.db。
|
||||||
EUNIV_DB_PATH=data/euniv.db
|
EUNIV_DB_PATH=data/euniv.db
|
||||||
# 不设置时,首次启动会在日志里打印一次随机管理员口令
|
# 不设置时,首次启动的管理员口令是 242520
|
||||||
EUNIV_ADMIN_PASSWORD=
|
EUNIV_ADMIN_PASSWORD=
|
||||||
# 设为 1 时,启动按上面的口令重置管理员密码
|
# 设为 1 时,启动按上面的口令重置管理员密码
|
||||||
EUNIV_ADMIN_RESET=
|
EUNIV_ADMIN_RESET=
|
||||||
|
|||||||
@@ -10,7 +10,8 @@ _N = 2**14
|
|||||||
_R = 8
|
_R = 8
|
||||||
_P = 1
|
_P = 1
|
||||||
_PREFIX = "scrypt$"
|
_PREFIX = "scrypt$"
|
||||||
_LEGACY_PASSWORD = "242520"
|
_DEFAULT_PASSWORD = "242520"
|
||||||
|
_MIN_LENGTH = 6
|
||||||
|
|
||||||
|
|
||||||
def hash_password(password: str) -> str:
|
def hash_password(password: str) -> str:
|
||||||
@@ -34,5 +35,9 @@ def is_hashed(stored: str) -> bool:
|
|||||||
return stored.startswith(_PREFIX)
|
return stored.startswith(_PREFIX)
|
||||||
|
|
||||||
|
|
||||||
def legacy_password() -> str:
|
def default_password() -> str:
|
||||||
return _LEGACY_PASSWORD
|
return _DEFAULT_PASSWORD
|
||||||
|
|
||||||
|
|
||||||
|
def min_length() -> int:
|
||||||
|
return _MIN_LENGTH
|
||||||
|
|||||||
@@ -2,9 +2,7 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import logging
|
|
||||||
import os
|
import os
|
||||||
import secrets
|
|
||||||
from datetime import datetime, timezone
|
from datetime import datetime, timezone
|
||||||
|
|
||||||
from sqlalchemy import select
|
from sqlalchemy import select
|
||||||
@@ -12,11 +10,10 @@ from sqlalchemy.orm import Session
|
|||||||
|
|
||||||
from admin.identity import identity
|
from admin.identity import identity
|
||||||
from admin.models import SystemConfig
|
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
|
from euniv.crypto import decrypt_secret
|
||||||
|
|
||||||
_INITIAL_USERNAME = "admin"
|
_INITIAL_USERNAME = "admin"
|
||||||
_LOG = logging.getLogger("admin.settings")
|
|
||||||
|
|
||||||
|
|
||||||
class SettingsError(ValueError):
|
class SettingsError(ValueError):
|
||||||
@@ -36,8 +33,8 @@ def _configured_admin_password() -> str | None:
|
|||||||
value = os.environ.get("EUNIV_ADMIN_PASSWORD", "").strip()
|
value = os.environ.get("EUNIV_ADMIN_PASSWORD", "").strip()
|
||||||
if not value:
|
if not value:
|
||||||
return None
|
return None
|
||||||
if len(value) < 8:
|
if len(value) < min_length():
|
||||||
raise RuntimeError("EUNIV_ADMIN_PASSWORD 至少需要 8 位")
|
raise RuntimeError(f"EUNIV_ADMIN_PASSWORD 至少需要 {min_length()} 位")
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
|
||||||
@@ -45,9 +42,7 @@ def _new_admin_password() -> str:
|
|||||||
configured = _configured_admin_password()
|
configured = _configured_admin_password()
|
||||||
if configured is not None:
|
if configured is not None:
|
||||||
return configured
|
return configured
|
||||||
generated = secrets.token_urlsafe(12)
|
return default_password()
|
||||||
_LOG.warning("已生成随机管理员口令,请立即保存:%s", generated)
|
|
||||||
return generated
|
|
||||||
|
|
||||||
|
|
||||||
def _reversible_password(stored: str) -> str | None:
|
def _reversible_password(stored: str) -> str | None:
|
||||||
@@ -90,8 +85,7 @@ def ensure_admin_seed() -> None:
|
|||||||
plain = _reversible_password(row.admin_password_cipher)
|
plain = _reversible_password(row.admin_password_cipher)
|
||||||
if plain is None:
|
if plain is None:
|
||||||
return
|
return
|
||||||
replacement = _new_admin_password() if plain == legacy_password() else plain
|
row.admin_password_cipher = hash_password(plain)
|
||||||
row.admin_password_cipher = hash_password(replacement)
|
|
||||||
db.commit()
|
db.commit()
|
||||||
|
|
||||||
|
|
||||||
@@ -216,8 +210,8 @@ def save_settings(db: Session, payload: dict) -> SystemConfig:
|
|||||||
raise SettingsError("自动滑动次数须为 1 到 10 的整数")
|
raise SettingsError("自动滑动次数须为 1 到 10 的整数")
|
||||||
password = payload.get("admin_password")
|
password = payload.get("admin_password")
|
||||||
if password is not None and password != "":
|
if password is not None and password != "":
|
||||||
if len(str(password)) < 8:
|
if len(str(password)) < min_length():
|
||||||
raise SettingsError("管理员密码至少 8 位")
|
raise SettingsError(f"管理员密码至少 {min_length()} 位")
|
||||||
if len(str(password)) > 128:
|
if len(str(password)) > 128:
|
||||||
raise SettingsError("管理员密码过长")
|
raise SettingsError("管理员密码过长")
|
||||||
|
|
||||||
|
|||||||
@@ -107,7 +107,7 @@ def test_admin_login_succeeds_and_wrong_password_skips_euniv(monkeypatch) -> Non
|
|||||||
event.remove(engine, "before_cursor_execute", hear)
|
event.remove(engine, "before_cursor_execute", hear)
|
||||||
|
|
||||||
|
|
||||||
def test_legacy_default_password_is_replaced_and_failures_are_limited() -> None:
|
def test_default_password_is_kept_when_hashed_and_failures_are_limited() -> None:
|
||||||
from admin.settings import ensure_admin_seed
|
from admin.settings import ensure_admin_seed
|
||||||
|
|
||||||
with SessionLocal() as db:
|
with SessionLocal() as db:
|
||||||
@@ -119,14 +119,22 @@ def test_legacy_default_password_is_replaced_and_failures_are_limited() -> None:
|
|||||||
load_admin_identity()
|
load_admin_identity()
|
||||||
api = _app()
|
api = _app()
|
||||||
client = TestClient(api.app)
|
client = TestClient(api.app)
|
||||||
assert client.post("/api/login", json={"employee_id": "admin", "password": "242520"}).status_code == 400
|
assert client.post("/api/login", json={"employee_id": "admin", "password": "242520"}).status_code == 200
|
||||||
assert client.post("/api/login", json={"employee_id": "admin", "password": INITIAL_PASSWORD}).status_code == 200
|
assert client.post("/api/login", json={"employee_id": "admin", "password": INITIAL_PASSWORD}).status_code == 400
|
||||||
short = client.put(
|
short = client.put(
|
||||||
"/api/admin/settings",
|
"/api/admin/settings",
|
||||||
json=_settings_body(admin_password="short", admin_password_current=INITIAL_PASSWORD),
|
json=_settings_body(admin_password="short", admin_password_current="242520"),
|
||||||
)
|
)
|
||||||
assert short.status_code == 400
|
assert short.status_code == 400
|
||||||
|
accepted = client.put(
|
||||||
|
"/api/admin/settings",
|
||||||
|
json=_settings_body(admin_password="242520", admin_password_current="242520"),
|
||||||
|
)
|
||||||
|
assert accepted.status_code == 200
|
||||||
|
|
||||||
|
from admin.login_limit import reset
|
||||||
|
|
||||||
|
reset()
|
||||||
locked = TestClient(api.app)
|
locked = TestClient(api.app)
|
||||||
for _ in range(4):
|
for _ in range(4):
|
||||||
assert locked.post("/api/login", json={"employee_id": "admin", "password": "wrong-pass"}).status_code == 400
|
assert locked.post("/api/login", json={"employee_id": "admin", "password": "wrong-pass"}).status_code == 400
|
||||||
|
|||||||
+2
-2
@@ -16,9 +16,9 @@ python -c "import secrets;print(secrets.token_urlsafe(48))"
|
|||||||
|
|
||||||
## 管理员
|
## 管理员
|
||||||
|
|
||||||
首次启动如果没有设置 `EUNIV_ADMIN_PASSWORD`,会生成随机口令,并用 WARNING 打一次日志。错过这次日志就无法登录。可以设置 `EUNIV_ADMIN_RESET=1`,并同时给出 `EUNIV_ADMIN_PASSWORD`,启动时按这个口令重置一次。重置后把 `EUNIV_ADMIN_RESET` 改回空。
|
首次启动如果没有设置 `EUNIV_ADMIN_PASSWORD`,管理员用户名是 `admin`,口令是 `242520`。可以设置 `EUNIV_ADMIN_RESET=1`,并同时给出 `EUNIV_ADMIN_PASSWORD`,启动时按这个口令重置一次。重置后把 `EUNIV_ADMIN_RESET` 改回空。
|
||||||
|
|
||||||
管理员用户名不能和已有工号相同。新口令至少 8 位。
|
管理员用户名不能和已有工号相同。新口令至少 6 位。
|
||||||
|
|
||||||
## 反向代理
|
## 反向代理
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user