Spaces:
Sleeping
Sleeping
Update src/auth.py
Browse files- src/auth.py +68 -21
src/auth.py
CHANGED
|
@@ -404,57 +404,102 @@ def render_signup_panel_inline():
|
|
| 404 |
|
| 405 |
CAPTCHA_KEY_SIGNUP = "_captcha_signup"
|
| 406 |
a2, b2, _sum2, nonce2 = get_captcha_and_nonce(CAPTCHA_KEY_SIGNUP)
|
| 407 |
-
# --- DEBUG ---
|
| 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 |
-
|
| 416 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 417 |
cS1, cS2 = st.columns([3, 1])
|
| 418 |
with cS1:
|
| 419 |
-
|
| 420 |
f"CAPTCHA: What is {a2} + {b2}?",
|
| 421 |
-
|
|
|
|
| 422 |
)
|
| 423 |
with cS2:
|
| 424 |
refresh_signup = st.form_submit_button("↻ New CAPTCHA")
|
| 425 |
|
| 426 |
-
ok2 = st.form_submit_button("Create Account")
|
| 427 |
|
|
|
|
| 428 |
if refresh_signup:
|
| 429 |
log.info("captcha.signup: manual refresh pressed (old a=%d b=%d sum=%d nonce=%d)", a2, b2, _sum2, nonce2)
|
| 430 |
rotate_captcha(CAPTCHA_KEY_SIGNUP)
|
| 431 |
st.stop()
|
| 432 |
|
| 433 |
if ok2:
|
| 434 |
-
log.debug("signup.click: email=%s user=%s cust=%s
|
| 435 |
-
email2, username, cust,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 436 |
try:
|
| 437 |
_validate_email(email2)
|
| 438 |
except EmailNotValidError:
|
| 439 |
-
st.error("Invalid email format.")
|
|
|
|
|
|
|
| 440 |
if not is_valid_username(username):
|
| 441 |
-
st.error("Username must be 3–32 and letters/digits/_ only.")
|
|
|
|
|
|
|
| 442 |
if not is_alnum(cust):
|
| 443 |
-
st.error("Customer ID must be alphanumeric.")
|
|
|
|
|
|
|
| 444 |
if email_exists(email2):
|
| 445 |
-
st.error("Email already in use.")
|
|
|
|
|
|
|
| 446 |
if username_exists(username):
|
| 447 |
-
st.error("Username already in use.")
|
|
|
|
|
|
|
| 448 |
if pw1 != pw2:
|
| 449 |
-
st.error("Passwords do not match.")
|
|
|
|
|
|
|
| 450 |
errs = password_policy_errors(pw1)
|
| 451 |
if errs:
|
| 452 |
-
st.error("Password policy: " + ", ".join(errs))
|
| 453 |
-
|
| 454 |
-
|
|
|
|
|
|
|
| 455 |
st.error("CAPTCHA incorrect. Please try again.")
|
| 456 |
rotate_captcha(CAPTCHA_KEY_SIGNUP); st.stop()
|
| 457 |
|
|
|
|
| 458 |
now = datetime.utcnow().isoformat(timespec="seconds")
|
| 459 |
pwh = hash_password(pw1)
|
| 460 |
with get_connection() as conn:
|
|
@@ -470,10 +515,12 @@ def render_signup_panel_inline():
|
|
| 470 |
(uid, pwh, now)
|
| 471 |
)
|
| 472 |
conn.commit()
|
|
|
|
| 473 |
st.success("Account created. Please log in.")
|
| 474 |
st.session_state["_auth_view"] = "login"
|
| 475 |
st.rerun()
|
| 476 |
|
|
|
|
| 477 |
def render_login_signup_gate():
|
| 478 |
"""
|
| 479 |
Enterprise-style login: hero image (left) + login card (right).
|
|
|
|
| 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", help="3–32 chars; letters, digits, underscore only")
|
| 411 |
+
email2 = normalize_email(st.text_input("Email address", help="We’ll verify format before creating the account"))
|
| 412 |
+
cust = st.text_input("Customer ID", help="Alphanumeric only (as per Olist ID format)")
|
| 413 |
+
|
| 414 |
+
# Passwords + live policy checklist
|
| 415 |
+
pcol1, pcol2 = st.columns(2)
|
| 416 |
+
with pcol1:
|
| 417 |
+
pw1 = st.text_input("Password", type="password", help="Min 8, with upper, lower, digit, special")
|
| 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:
|
| 436 |
+
cap_raw = st.text_input(
|
| 437 |
f"CAPTCHA: What is {a2} + {b2}?",
|
| 438 |
+
key=f"cap_signup_{nonce2}",
|
| 439 |
+
placeholder="Type the sum"
|
| 440 |
)
|
| 441 |
with cS2:
|
| 442 |
refresh_signup = st.form_submit_button("↻ New CAPTCHA")
|
| 443 |
|
| 444 |
+
ok2 = st.form_submit_button("Create Account", use_container_width=True)
|
| 445 |
|
| 446 |
+
# Handle refresh before any validation
|
| 447 |
if refresh_signup:
|
| 448 |
log.info("captcha.signup: manual refresh pressed (old a=%d b=%d sum=%d nonce=%d)", a2, b2, _sum2, nonce2)
|
| 449 |
rotate_captcha(CAPTCHA_KEY_SIGNUP)
|
| 450 |
st.stop()
|
| 451 |
|
| 452 |
if ok2:
|
| 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 |
+
# Parse CAPTCHA safely
|
| 457 |
+
if cap_raw is None or cap_raw.strip() == "":
|
| 458 |
+
st.warning("Please answer the CAPTCHA.")
|
| 459 |
+
st.stop()
|
| 460 |
+
try:
|
| 461 |
+
cap_ans = int(cap_raw.strip())
|
| 462 |
+
except Exception:
|
| 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 |
+
# Create user
|
| 503 |
now = datetime.utcnow().isoformat(timespec="seconds")
|
| 504 |
pwh = hash_password(pw1)
|
| 505 |
with get_connection() as conn:
|
|
|
|
| 515 |
(uid, pwh, now)
|
| 516 |
)
|
| 517 |
conn.commit()
|
| 518 |
+
|
| 519 |
st.success("Account created. Please log in.")
|
| 520 |
st.session_state["_auth_view"] = "login"
|
| 521 |
st.rerun()
|
| 522 |
|
| 523 |
+
|
| 524 |
def render_login_signup_gate():
|
| 525 |
"""
|
| 526 |
Enterprise-style login: hero image (left) + login card (right).
|