Keep recovery codes on screen and make two-factor optional.
Deploy Development / deploy (push) Successful in 51s
Test Suite / pytest-backend (push) Successful in 46s
Test Suite / lint-backend (push) Successful in 0s
Test Suite / build-frontend (push) Successful in 15s
Test Suite / k6 /health Baseline (push) Successful in 34s
Test Suite / playwright-tests (push) Failing after 4m10s
Deploy Development / deploy (push) Successful in 51s
Test Suite / pytest-backend (push) Successful in 46s
Test Suite / lint-backend (push) Successful in 0s
Test Suite / build-frontend (push) Successful in 15s
Test Suite / k6 /health Baseline (push) Successful in 34s
Test Suite / playwright-tests (push) Failing after 4m10s
Admins can replace missed codes or turn two-factor off without a forced enrollment on every login. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
32b28a93f3
commit
2e22a7c8e0
8 files changed
+145
-53
No files matched your search
+35
-15
@@ -85,12 +85,9 @@ limiter = Limiter(key_func=_client_ip)
|
||||
|
||||
|
||||
def _admin_totp_required() -> bool:
|
||||
"""Nur wenn REQUIRE_ADMIN_TOTP ausdrücklich an ist. Standard: der Nutzer entscheidet selbst."""
|
||||
raw = (os.getenv("REQUIRE_ADMIN_TOTP") or "").strip().lower()
|
||||
if raw in ("1", "true", "yes"):
|
||||
return True
|
||||
if raw in ("0", "false", "no"):
|
||||
return False
|
||||
return (os.getenv("ENVIRONMENT") or "development").strip().lower() in ("production", "prod")
|
||||
return raw in ("1", "true", "yes")
|
||||
|
||||
|
||||
def _issue_session(cur, profile_id: int, *, days: int, purpose: str = "full") -> tuple[str, datetime]:
|
||||
@@ -394,16 +391,7 @@ def totp_confirm(req: TotpCodeRequest, x_auth_token: Optional[str] = Header(defa
|
||||
""",
|
||||
(prof["totp_pending_secret"], pid),
|
||||
)
|
||||
cur.execute("DELETE FROM profile_recovery_codes WHERE profile_id=%s", (pid,))
|
||||
plain_codes = generate_recovery_codes()
|
||||
for code in plain_codes:
|
||||
cur.execute(
|
||||
"""
|
||||
INSERT INTO profile_recovery_codes (profile_id, code_hash)
|
||||
VALUES (%s, %s)
|
||||
""",
|
||||
(pid, hash_recovery_code(code)),
|
||||
)
|
||||
plain_codes = _replace_recovery_codes(cur, pid)
|
||||
prof = dict(prof)
|
||||
prof["totp_enabled"] = True
|
||||
prof["totp_secret"] = None
|
||||
@@ -414,6 +402,38 @@ def totp_confirm(req: TotpCodeRequest, x_auth_token: Optional[str] = Header(defa
|
||||
return payload
|
||||
|
||||
|
||||
def _replace_recovery_codes(cur, profile_id: int) -> list[str]:
|
||||
cur.execute("DELETE FROM profile_recovery_codes WHERE profile_id=%s", (profile_id,))
|
||||
plain_codes = generate_recovery_codes()
|
||||
for code in plain_codes:
|
||||
cur.execute(
|
||||
"""
|
||||
INSERT INTO profile_recovery_codes (profile_id, code_hash)
|
||||
VALUES (%s, %s)
|
||||
""",
|
||||
(profile_id, hash_recovery_code(code)),
|
||||
)
|
||||
return plain_codes
|
||||
|
||||
|
||||
@router.post("/totp/recovery-codes")
|
||||
def regenerate_recovery_codes(req: TotpDisableRequest, session: dict = Depends(require_auth)):
|
||||
"""Neue Codes ausgeben. Die bisherigen werden ungültig. Passwort und aktueller App-Code sind Pflicht."""
|
||||
pid = int(session["profile_id"])
|
||||
with get_db() as conn:
|
||||
cur = get_cursor(conn)
|
||||
cur.execute("SELECT * FROM profiles WHERE id=%s", (pid,))
|
||||
prof = cur.fetchone()
|
||||
if not prof or not prof.get("totp_enabled"):
|
||||
raise HTTPException(400, "Zwei-Faktor ist nicht aktiv")
|
||||
if not verify_pin(req.current_password, prof["pin_hash"]):
|
||||
raise HTTPException(400, "Aktuelles Passwort ist falsch")
|
||||
if not verify_totp(prof.get("totp_secret"), req.code):
|
||||
raise HTTPException(400, "Code aus der App stimmt nicht")
|
||||
plain_codes = _replace_recovery_codes(cur, pid)
|
||||
return {"recovery_codes": plain_codes}
|
||||
|
||||
|
||||
@router.post("/totp/disable")
|
||||
def totp_disable(req: TotpDisableRequest, session: dict = Depends(require_auth)):
|
||||
pid = int(session["profile_id"])
|
||||
|
||||
Reference in new issue
Block a user