Spaces:
Sleeping
Sleeping
Update src/auth.py
Browse files- src/auth.py +21 -44
src/auth.py
CHANGED
|
@@ -402,34 +402,22 @@ def _load_login_hero():
|
|
| 402 |
def render_signup_panel_inline():
|
| 403 |
st.subheader("Create your account")
|
| 404 |
|
|
|
|
| 405 |
CAPTCHA_KEY_SIGNUP = "_captcha_signup"
|
| 406 |
a2, b2, _sum2, nonce2 = get_captcha_and_nonce(CAPTCHA_KEY_SIGNUP)
|
| 407 |
log.debug("captcha.signup:init a=%d b=%d sum=%d nonce=%d", a2, b2, _sum2, nonce2)
|
| 408 |
|
| 409 |
with st.form("signup_form", clear_on_submit=True):
|
| 410 |
-
username = st.text_input("Username"
|
| 411 |
-
email2 = normalize_email(st.text_input("Email address"
|
| 412 |
-
cust = st.text_input("Customer ID"
|
| 413 |
|
| 414 |
-
# Passwords + live policy checklist
|
| 415 |
pcol1, pcol2 = st.columns(2)
|
| 416 |
with pcol1:
|
| 417 |
-
pw1 = st.text_input("Password", type="password"
|
| 418 |
with pcol2:
|
| 419 |
pw2 = st.text_input("Confirm Password", type="password")
|
| 420 |
|
| 421 |
-
# Live checklist
|
| 422 |
-
if pw1:
|
| 423 |
-
items = [
|
| 424 |
-
(len(pw1) >= PASS_MIN_LEN, f"Min length {PASS_MIN_LEN}"),
|
| 425 |
-
(_PASS_UPPER.search(pw1) is not None, "At least 1 uppercase"),
|
| 426 |
-
(_PASS_LOWER.search(pw1) is not None, "At least 1 lowercase"),
|
| 427 |
-
(_PASS_DIGIT.search(pw1) is not None, "At least 1 digit"),
|
| 428 |
-
(_PASS_SPECIAL.search(pw1) is not None, "At least 1 special char"),
|
| 429 |
-
]
|
| 430 |
-
checks = " \n".join([("✅ " if ok else "⬜ ") + txt for ok, txt in items])
|
| 431 |
-
st.caption(checks)
|
| 432 |
-
|
| 433 |
# CAPTCHA row: text input (blank by default) + refresh button
|
| 434 |
cS1, cS2 = st.columns([3, 1])
|
| 435 |
with cS1:
|
|
@@ -453,7 +441,7 @@ def render_signup_panel_inline():
|
|
| 453 |
log.debug("signup.click: email=%s user=%s cust=%s cap_entered_raw=%s expected=%s",
|
| 454 |
email2, username, cust, cap_raw, _sum2)
|
| 455 |
|
| 456 |
-
#
|
| 457 |
if cap_raw is None or cap_raw.strip() == "":
|
| 458 |
st.warning("Please answer the CAPTCHA.")
|
| 459 |
st.stop()
|
|
@@ -463,43 +451,32 @@ def render_signup_panel_inline():
|
|
| 463 |
st.error("CAPTCHA must be a number.")
|
| 464 |
st.stop()
|
| 465 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 466 |
try:
|
| 467 |
_validate_email(email2)
|
| 468 |
except EmailNotValidError:
|
| 469 |
-
st.error("Invalid email format.")
|
| 470 |
-
rotate_captcha(CAPTCHA_KEY_SIGNUP); st.stop()
|
| 471 |
-
|
| 472 |
if not is_valid_username(username):
|
| 473 |
-
st.error("Username must be 3–32 and letters/digits/_ only.")
|
| 474 |
-
rotate_captcha(CAPTCHA_KEY_SIGNUP); st.stop()
|
| 475 |
-
|
| 476 |
if not is_alnum(cust):
|
| 477 |
-
st.error("Customer ID must be alphanumeric.")
|
| 478 |
-
rotate_captcha(CAPTCHA_KEY_SIGNUP); st.stop()
|
| 479 |
-
|
| 480 |
if email_exists(email2):
|
| 481 |
-
st.error("Email already in use.")
|
| 482 |
-
rotate_captcha(CAPTCHA_KEY_SIGNUP); st.stop()
|
| 483 |
-
|
| 484 |
if username_exists(username):
|
| 485 |
-
st.error("Username already in use.")
|
| 486 |
-
rotate_captcha(CAPTCHA_KEY_SIGNUP); st.stop()
|
| 487 |
-
|
| 488 |
if pw1 != pw2:
|
| 489 |
-
st.error("Passwords do not match.")
|
| 490 |
-
rotate_captcha(CAPTCHA_KEY_SIGNUP); st.stop()
|
| 491 |
-
|
| 492 |
errs = password_policy_errors(pw1)
|
| 493 |
if errs:
|
| 494 |
-
st.error("Password policy: " + ", ".join(errs))
|
| 495 |
-
rotate_captcha(CAPTCHA_KEY_SIGNUP); st.stop()
|
| 496 |
-
|
| 497 |
-
if not check_captcha(cap_ans, CAPTCHA_KEY_SIGNUP):
|
| 498 |
-
log.info("captcha.signup: wrong answer email=%s entered=%s expected=%s", email2, cap_ans, _sum2)
|
| 499 |
-
st.error("CAPTCHA incorrect. Please try again.")
|
| 500 |
-
rotate_captcha(CAPTCHA_KEY_SIGNUP); st.stop()
|
| 501 |
|
| 502 |
-
#
|
| 503 |
now = datetime.utcnow().isoformat(timespec="seconds")
|
| 504 |
pwh = hash_password(pw1)
|
| 505 |
with get_connection() as conn:
|
|
|
|
| 402 |
def render_signup_panel_inline():
|
| 403 |
st.subheader("Create your account")
|
| 404 |
|
| 405 |
+
# Use same nonce-based CAPTCHA as login
|
| 406 |
CAPTCHA_KEY_SIGNUP = "_captcha_signup"
|
| 407 |
a2, b2, _sum2, nonce2 = get_captcha_and_nonce(CAPTCHA_KEY_SIGNUP)
|
| 408 |
log.debug("captcha.signup:init a=%d b=%d sum=%d nonce=%d", a2, b2, _sum2, nonce2)
|
| 409 |
|
| 410 |
with st.form("signup_form", clear_on_submit=True):
|
| 411 |
+
username = st.text_input("Username")
|
| 412 |
+
email2 = normalize_email(st.text_input("Email address"))
|
| 413 |
+
cust = st.text_input("Customer ID")
|
| 414 |
|
|
|
|
| 415 |
pcol1, pcol2 = st.columns(2)
|
| 416 |
with pcol1:
|
| 417 |
+
pw1 = st.text_input("Password", type="password")
|
| 418 |
with pcol2:
|
| 419 |
pw2 = st.text_input("Confirm Password", type="password")
|
| 420 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 421 |
# CAPTCHA row: text input (blank by default) + refresh button
|
| 422 |
cS1, cS2 = st.columns([3, 1])
|
| 423 |
with cS1:
|
|
|
|
| 441 |
log.debug("signup.click: email=%s user=%s cust=%s cap_entered_raw=%s expected=%s",
|
| 442 |
email2, username, cust, cap_raw, _sum2)
|
| 443 |
|
| 444 |
+
# --- CAPTCHA parsing & check (fix) ---
|
| 445 |
if cap_raw is None or cap_raw.strip() == "":
|
| 446 |
st.warning("Please answer the CAPTCHA.")
|
| 447 |
st.stop()
|
|
|
|
| 451 |
st.error("CAPTCHA must be a number.")
|
| 452 |
st.stop()
|
| 453 |
|
| 454 |
+
if not check_captcha(cap_ans, CAPTCHA_KEY_SIGNUP):
|
| 455 |
+
log.info("captcha.signup: wrong answer email=%s entered=%s expected=%s", email2, cap_ans, _sum2)
|
| 456 |
+
st.error("CAPTCHA incorrect. Please try again.")
|
| 457 |
+
rotate_captcha(CAPTCHA_KEY_SIGNUP)
|
| 458 |
+
st.stop()
|
| 459 |
+
|
| 460 |
+
# ---- keep your existing validations below ----
|
| 461 |
try:
|
| 462 |
_validate_email(email2)
|
| 463 |
except EmailNotValidError:
|
| 464 |
+
st.error("Invalid email format."); rotate_captcha(CAPTCHA_KEY_SIGNUP); st.stop()
|
|
|
|
|
|
|
| 465 |
if not is_valid_username(username):
|
| 466 |
+
st.error("Username must be 3–32 and letters/digits/_ only."); rotate_captcha(CAPTCHA_KEY_SIGNUP); st.stop()
|
|
|
|
|
|
|
| 467 |
if not is_alnum(cust):
|
| 468 |
+
st.error("Customer ID must be alphanumeric."); rotate_captcha(CAPTCHA_KEY_SIGNUP); st.stop()
|
|
|
|
|
|
|
| 469 |
if email_exists(email2):
|
| 470 |
+
st.error("Email already in use."); rotate_captcha(CAPTCHA_KEY_SIGNUP); st.stop()
|
|
|
|
|
|
|
| 471 |
if username_exists(username):
|
| 472 |
+
st.error("Username already in use."); rotate_captcha(CAPTCHA_KEY_SIGNUP); st.stop()
|
|
|
|
|
|
|
| 473 |
if pw1 != pw2:
|
| 474 |
+
st.error("Passwords do not match."); rotate_captcha(CAPTCHA_KEY_SIGNUP); st.stop()
|
|
|
|
|
|
|
| 475 |
errs = password_policy_errors(pw1)
|
| 476 |
if errs:
|
| 477 |
+
st.error("Password policy: " + ", ".join(errs)); rotate_captcha(CAPTCHA_KEY_SIGNUP); st.stop()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 478 |
|
| 479 |
+
# --- create user (unchanged) ---
|
| 480 |
now = datetime.utcnow().isoformat(timespec="seconds")
|
| 481 |
pwh = hash_password(pw1)
|
| 482 |
with get_connection() as conn:
|