Spaces:
Sleeping
Sleeping
Update src/auth.py
Browse files- src/auth.py +13 -11
src/auth.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
# src/auth.py
|
| 2 |
-
import os, re, time, uuid, sqlite3, secrets
|
| 3 |
from datetime import datetime, timedelta
|
| 4 |
from pathlib import Path
|
| 5 |
|
|
@@ -10,17 +10,16 @@ from PIL import Image
|
|
| 10 |
|
| 11 |
from src.utils import get_connection
|
| 12 |
|
| 13 |
-
|
| 14 |
LOG_LEVEL = os.getenv("LOG_LEVEL", "INFO").upper()
|
| 15 |
logging.basicConfig(
|
| 16 |
stream=sys.stdout,
|
| 17 |
level=getattr(logging, LOG_LEVEL, logging.INFO),
|
| 18 |
format="%(asctime)s %(levelname)s [auth] %(message)s",
|
| 19 |
-
force=True,
|
| 20 |
)
|
| 21 |
log = logging.getLogger("auth")
|
| 22 |
|
| 23 |
-
|
| 24 |
# =========================
|
| 25 |
# Config (ENV-driven)
|
| 26 |
# =========================
|
|
@@ -68,6 +67,10 @@ def hash_password(pw: str) -> str:
|
|
| 68 |
return "bcrypt$" + bcrypt.hashpw(raw, bcrypt.gensalt(rounds=12)).decode("utf-8")
|
| 69 |
|
| 70 |
def verify_password_migrating(email: str, plaintext_pw: str) -> tuple[bool, dict | None]:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
em = normalize_email(email)
|
| 72 |
with get_connection() as conn:
|
| 73 |
conn.row_factory = sqlite3.Row
|
|
@@ -137,7 +140,6 @@ def verify_password_migrating(email: str, plaintext_pw: str) -> tuple[bool, dict
|
|
| 137 |
log.info("login: uid=%s password mismatch", row["id"])
|
| 138 |
return (False, None)
|
| 139 |
|
| 140 |
-
|
| 141 |
# =========================
|
| 142 |
# CAPTCHA helpers (rotate with nonce so widget remounts)
|
| 143 |
# =========================
|
|
@@ -400,7 +402,7 @@ def render_signup_panel_inline():
|
|
| 400 |
ok2 = st.form_submit_button("Create Account")
|
| 401 |
|
| 402 |
if refresh_signup:
|
| 403 |
-
log.info("captcha: manual refresh pressed
|
| 404 |
rotate_captcha(CAPTCHA_KEY_SIGNUP)
|
| 405 |
st.stop()
|
| 406 |
|
|
@@ -423,8 +425,9 @@ def render_signup_panel_inline():
|
|
| 423 |
if errs:
|
| 424 |
st.error("Password policy: " + ", ".join(errs)); rotate_captcha(CAPTCHA_KEY_SIGNUP); st.stop()
|
| 425 |
if not check_captcha(cap2, CAPTCHA_KEY_SIGNUP):
|
| 426 |
-
log.info("captcha: wrong answer (
|
| 427 |
-
st.error("CAPTCHA incorrect. Please try again.")
|
|
|
|
| 428 |
|
| 429 |
now = datetime.utcnow().isoformat(timespec="seconds")
|
| 430 |
pwh = hash_password(pw1)
|
|
@@ -514,21 +517,20 @@ def render_login_signup_gate():
|
|
| 514 |
f"CAPTCHA: What is {a} + {b}?",
|
| 515 |
min_value=0, max_value=999, step=1, format="%d", key=f"cap_login_{nonce}"
|
| 516 |
)
|
| 517 |
-
#refresh_captcha = st.form_submit_button("↻ New CAPTCHA")
|
| 518 |
with cB:
|
| 519 |
refresh_captcha = st.form_submit_button("↻ New CAPTCHA")
|
| 520 |
|
| 521 |
login_clicked = st.form_submit_button("LOGIN", use_container_width=True)
|
| 522 |
|
| 523 |
if refresh_captcha:
|
| 524 |
-
|
| 525 |
rotate_captcha(CAPTCHA_KEY_LOGIN)
|
| 526 |
st.stop()
|
| 527 |
|
| 528 |
if login_clicked:
|
| 529 |
# 1) CAPTCHA
|
| 530 |
if not check_captcha(cap_ans, CAPTCHA_KEY_LOGIN):
|
| 531 |
-
log.info("captcha: wrong answer (
|
| 532 |
st.error("CAPTCHA incorrect. Please try again.")
|
| 533 |
rotate_captcha(CAPTCHA_KEY_LOGIN); st.stop()
|
| 534 |
|
|
|
|
| 1 |
# src/auth.py
|
| 2 |
+
import os, re, time, uuid, sqlite3, secrets, logging, sys
|
| 3 |
from datetime import datetime, timedelta
|
| 4 |
from pathlib import Path
|
| 5 |
|
|
|
|
| 10 |
|
| 11 |
from src.utils import get_connection
|
| 12 |
|
| 13 |
+
# -------- logging to HF Runtime logs --------
|
| 14 |
LOG_LEVEL = os.getenv("LOG_LEVEL", "INFO").upper()
|
| 15 |
logging.basicConfig(
|
| 16 |
stream=sys.stdout,
|
| 17 |
level=getattr(logging, LOG_LEVEL, logging.INFO),
|
| 18 |
format="%(asctime)s %(levelname)s [auth] %(message)s",
|
| 19 |
+
force=True,
|
| 20 |
)
|
| 21 |
log = logging.getLogger("auth")
|
| 22 |
|
|
|
|
| 23 |
# =========================
|
| 24 |
# Config (ENV-driven)
|
| 25 |
# =========================
|
|
|
|
| 67 |
return "bcrypt$" + bcrypt.hashpw(raw, bcrypt.gensalt(rounds=12)).decode("utf-8")
|
| 68 |
|
| 69 |
def verify_password_migrating(email: str, plaintext_pw: str) -> tuple[bool, dict | None]:
|
| 70 |
+
"""
|
| 71 |
+
Try current PEPPER, then any fallbacks, then legacy (no pepper).
|
| 72 |
+
On fallback/legacy match, rehash with CURRENT pepper and update DB.
|
| 73 |
+
"""
|
| 74 |
em = normalize_email(email)
|
| 75 |
with get_connection() as conn:
|
| 76 |
conn.row_factory = sqlite3.Row
|
|
|
|
| 140 |
log.info("login: uid=%s password mismatch", row["id"])
|
| 141 |
return (False, None)
|
| 142 |
|
|
|
|
| 143 |
# =========================
|
| 144 |
# CAPTCHA helpers (rotate with nonce so widget remounts)
|
| 145 |
# =========================
|
|
|
|
| 402 |
ok2 = st.form_submit_button("Create Account")
|
| 403 |
|
| 404 |
if refresh_signup:
|
| 405 |
+
log.info("captcha: manual refresh pressed (signup)")
|
| 406 |
rotate_captcha(CAPTCHA_KEY_SIGNUP)
|
| 407 |
st.stop()
|
| 408 |
|
|
|
|
| 425 |
if errs:
|
| 426 |
st.error("Password policy: " + ", ".join(errs)); rotate_captcha(CAPTCHA_KEY_SIGNUP); st.stop()
|
| 427 |
if not check_captcha(cap2, CAPTCHA_KEY_SIGNUP):
|
| 428 |
+
log.info("captcha: wrong answer (signup email=%s)", email2)
|
| 429 |
+
st.error("CAPTCHA incorrect. Please try again.")
|
| 430 |
+
rotate_captcha(CAPTCHA_KEY_SIGNUP); st.stop()
|
| 431 |
|
| 432 |
now = datetime.utcnow().isoformat(timespec="seconds")
|
| 433 |
pwh = hash_password(pw1)
|
|
|
|
| 517 |
f"CAPTCHA: What is {a} + {b}?",
|
| 518 |
min_value=0, max_value=999, step=1, format="%d", key=f"cap_login_{nonce}"
|
| 519 |
)
|
|
|
|
| 520 |
with cB:
|
| 521 |
refresh_captcha = st.form_submit_button("↻ New CAPTCHA")
|
| 522 |
|
| 523 |
login_clicked = st.form_submit_button("LOGIN", use_container_width=True)
|
| 524 |
|
| 525 |
if refresh_captcha:
|
| 526 |
+
log.info("captcha: manual refresh pressed")
|
| 527 |
rotate_captcha(CAPTCHA_KEY_LOGIN)
|
| 528 |
st.stop()
|
| 529 |
|
| 530 |
if login_clicked:
|
| 531 |
# 1) CAPTCHA
|
| 532 |
if not check_captcha(cap_ans, CAPTCHA_KEY_LOGIN):
|
| 533 |
+
log.info("captcha: wrong answer (login email=%s)", email)
|
| 534 |
st.error("CAPTCHA incorrect. Please try again.")
|
| 535 |
rotate_captcha(CAPTCHA_KEY_LOGIN); st.stop()
|
| 536 |
|