feat(вход): TOTP и recovery
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
"""P4.5b1: TOTP, recovery, login challenge, отзыв старых сессий."""
|
||||
import importlib
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
|
||||
import pytest
|
||||
|
||||
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
if ROOT not in sys.path:
|
||||
sys.path.insert(0, ROOT)
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def env(tmp_path, monkeypatch):
|
||||
monkeypatch.setenv("DB_PATH", str(tmp_path / "t.db"))
|
||||
monkeypatch.setenv("SESSION_SECRET", "test-secret")
|
||||
monkeypatch.setenv("AI_BACKEND", "claude")
|
||||
monkeypatch.chdir(ROOT)
|
||||
import auth
|
||||
import db
|
||||
import main
|
||||
import totp
|
||||
for m in (auth, db, main, totp):
|
||||
importlib.reload(m)
|
||||
db.init_db()
|
||||
db.create_user("boss@x", auth.hash_password("Passw0rd!ok"), role="admin")
|
||||
db.admin_verify_user("boss@x")
|
||||
from fastapi.testclient import TestClient
|
||||
client = TestClient(main.app)
|
||||
token = client.post("/api/login", json={"username": "boss@x",
|
||||
"password": "Passw0rd!ok"}).json()["token"]
|
||||
h = {"Authorization": "Bearer " + token}
|
||||
return auth, db, totp, client, h, token
|
||||
|
||||
|
||||
def test_totp_rfc_vector():
|
||||
import totp
|
||||
assert totp.code_at("GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ", 1) == "287082"
|
||||
assert totp.verify("GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ", "287082", at=59)
|
||||
assert not totp.verify("GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ", "000000", at=59)
|
||||
|
||||
|
||||
def test_enable_login_challenge_and_recovery(env):
|
||||
auth, db, totp, client, h, old = env
|
||||
s = client.post("/api/admin/2fa/setup", headers=h).json()
|
||||
code = totp.code_at(s["secret"], int(time.time() // 30))
|
||||
e = client.post("/api/admin/2fa/enable", json={"code": code}, headers=h)
|
||||
assert e.status_code == 200, e.text
|
||||
codes = e.json()["recovery_codes"]
|
||||
assert len(codes) == 8
|
||||
# включение отзывает текущую сессию
|
||||
assert client.get("/api/admin/v2/ping", headers=h).status_code == 401
|
||||
|
||||
r = client.post("/api/login", json={"username": "boss@x", "password": "Passw0rd!ok"})
|
||||
assert r.status_code == 200 and r.json().get("totp_required") is True
|
||||
assert "token" not in r.json()
|
||||
bad = client.post("/api/login", json={"username": "boss@x", "password": "Passw0rd!ok",
|
||||
"code": "000000"})
|
||||
assert bad.status_code == 401
|
||||
good = client.post("/api/login", json={"username": "boss@x", "password": "Passw0rd!ok",
|
||||
"code": totp.code_at(s["secret"], int(time.time() // 30))})
|
||||
assert good.status_code == 200 and good.json().get("token")
|
||||
recovery = client.post("/api/login", json={"username": "boss@x",
|
||||
"password": "Passw0rd!ok",
|
||||
"recovery": codes[0]})
|
||||
assert recovery.status_code == 200 and recovery.json().get("token")
|
||||
|
||||
|
||||
def test_env_admin_door_closes_when_any_admin_has_2fa(env, monkeypatch):
|
||||
auth, db, totp, client, h, token = env
|
||||
with db._conn() as c:
|
||||
c.execute("UPDATE users SET totp_enabled=1 WHERE email='boss@x'")
|
||||
monkeypatch.setenv("SMETA_USER", "envboss")
|
||||
monkeypatch.setenv("SMETA_PASS", "envpass")
|
||||
import importlib
|
||||
import auth as a2
|
||||
importlib.reload(a2)
|
||||
assert a2.authenticate("envboss", "envpass") is None
|
||||
refresh = a2._make_refresh_token("envboss", "admin")
|
||||
assert a2.refresh_access_token(refresh) is None
|
||||
Reference in New Issue
Block a user