fix: 管理员口令改为哈希并限制连续失败
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import json
|
||||
import os
|
||||
import threading
|
||||
from datetime import datetime, timezone
|
||||
from pathlib import Path
|
||||
@@ -10,12 +11,13 @@ from admin.identity import identity
|
||||
from admin.models import SystemConfig
|
||||
from admin.settings import ensure_admin_seed, load_admin_identity, refresh_interval_seconds
|
||||
from euniv.client import EunivClient, JigsawRejected, ManualJigsaw
|
||||
from admin.passwords import verify_password
|
||||
from euniv.crypto import decrypt_secret, encrypt_secret
|
||||
from euniv.db import SessionLocal, engine
|
||||
from euniv.models import Account
|
||||
from euniv.session import COOKIE_NAME
|
||||
|
||||
INITIAL_PASSWORD = "242520"
|
||||
INITIAL_PASSWORD = os.environ["EUNIV_ADMIN_PASSWORD"]
|
||||
CREDENTIAL = {
|
||||
"ucs_token": "ucs-1",
|
||||
"ucs_account_id": "acct",
|
||||
@@ -106,6 +108,30 @@ def test_admin_login_succeeds_and_wrong_password_skips_euniv() -> None:
|
||||
event.remove(engine, "before_cursor_execute", hear)
|
||||
|
||||
|
||||
def test_legacy_default_password_is_replaced_and_failures_are_limited() -> None:
|
||||
from admin.settings import ensure_admin_seed
|
||||
|
||||
with SessionLocal() as db:
|
||||
row = db.get(SystemConfig, 1)
|
||||
assert row is not None
|
||||
row.admin_password_cipher = encrypt_secret("242520")
|
||||
db.commit()
|
||||
ensure_admin_seed()
|
||||
load_admin_identity()
|
||||
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": INITIAL_PASSWORD}).status_code == 200
|
||||
short = client.put("/api/admin/settings", json=_settings_body(admin_password="short"))
|
||||
assert short.status_code == 400
|
||||
|
||||
locked = TestClient(api.app)
|
||||
for _ in range(4):
|
||||
assert locked.post("/api/login", json={"employee_id": "admin", "password": "wrong-pass"}).status_code == 400
|
||||
blocked = locked.post("/api/login", json={"employee_id": "admin", "password": "wrong-pass"})
|
||||
assert blocked.status_code == 429
|
||||
|
||||
|
||||
def test_admin_identity_uses_memory_until_reload() -> None:
|
||||
api = _app()
|
||||
calls = []
|
||||
@@ -162,7 +188,7 @@ def test_seed_does_not_reset_saved_admin() -> None:
|
||||
with SessionLocal() as db:
|
||||
row = db.get(SystemConfig, 1)
|
||||
assert row is not None
|
||||
assert decrypt_secret(row.admin_password_cipher) == "next-secret"
|
||||
assert verify_password(row.admin_password_cipher, "next-secret")
|
||||
|
||||
|
||||
def test_login_config_changes_meaning_with_visibility() -> None:
|
||||
@@ -525,6 +551,7 @@ def test_credentials_and_delete_user() -> None:
|
||||
assert admin.post("/api/login", json={"employee_id": "admin", "password": INITIAL_PASSWORD}).status_code == 200
|
||||
secret = admin.get("/api/admin/users/6650000146/credentials")
|
||||
assert secret.status_code == 200
|
||||
assert secret.headers["cache-control"] == "no-store"
|
||||
assert secret.json()["password"] == "secret-pass"
|
||||
assert secret.json()["ucs_token"] == "ucs-1"
|
||||
assert secret.json()["yxt_token"] == "yxt-1"
|
||||
@@ -658,7 +685,7 @@ def test_settings_do_not_expose_process_switches() -> None:
|
||||
}
|
||||
assert body["hours_source"] == "api"
|
||||
assert identity.password_cipher()
|
||||
assert decrypt_secret(identity.password_cipher()) == INITIAL_PASSWORD
|
||||
assert verify_password(identity.password_cipher(), INITIAL_PASSWORD)
|
||||
|
||||
|
||||
def test_admin_can_set_and_clear_proxy_for_selected_users() -> None:
|
||||
|
||||
Reference in New Issue
Block a user