Spaces:
Sleeping
Sleeping
Update src/auth.py
Browse files- src/auth.py +34 -103
src/auth.py
CHANGED
|
@@ -456,25 +456,26 @@ def render_login_signup_gate():
|
|
| 456 |
)
|
| 457 |
|
| 458 |
# Right: card with form
|
| 459 |
-
with col_right:
|
| 460 |
-
# Open ONE card div and immediately render content inside it
|
| 461 |
-
st.markdown(
|
| 462 |
-
"<div id='login-card' style='background:#fff;border:1px solid #e5e7eb;"
|
| 463 |
-
"border-radius:12px;padding:28px;box-shadow:0 10px 30px rgba(0,0,0,.05)'>",
|
| 464 |
-
unsafe_allow_html=True
|
| 465 |
-
)
|
| 466 |
-
|
| 467 |
-
# Logo row + headline (inside the card)
|
| 468 |
-
st.markdown(
|
| 469 |
-
"<div style='display:flex;align-items:center;gap:10px;margin-bottom:6px;'>"
|
| 470 |
-
"<div style='font-size:26px'>🧊</div>"
|
| 471 |
-
"<div style='font-weight:700;font-size:26px'>Logo</div>"
|
| 472 |
-
"</div>"
|
| 473 |
-
"<div style='font-size:18px;color:#374151;margin:6px 0 16px;'>"
|
| 474 |
-
"Sign into your account</div>",
|
| 475 |
-
unsafe_allow_html=True
|
| 476 |
-
)
|
| 477 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 478 |
# === Branch: show SIGNUP when toggled ===
|
| 479 |
if view == "signup":
|
| 480 |
render_signup_panel_inline()
|
|
@@ -488,19 +489,20 @@ def render_login_signup_gate():
|
|
| 488 |
unsafe_allow_html=True
|
| 489 |
)
|
| 490 |
st.markdown("</div>", unsafe_allow_html=True) # close card
|
|
|
|
| 491 |
return False
|
| 492 |
-
|
| 493 |
# === LOGIN PANEL ===
|
| 494 |
CAPTCHA_KEY_LOGIN = "_captcha_login"
|
| 495 |
if CAPTCHA_KEY_LOGIN not in st.session_state:
|
| 496 |
gen_captcha(CAPTCHA_KEY_LOGIN)
|
| 497 |
a, b, _sum = read_captcha(CAPTCHA_KEY_LOGIN)
|
| 498 |
-
|
| 499 |
# The form
|
| 500 |
with st.form("login_form", clear_on_submit=False):
|
| 501 |
email = normalize_email(st.text_input("Email address"))
|
| 502 |
pw = st.text_input("Password", type="password")
|
| 503 |
-
|
| 504 |
# CAPTCHA row: input + refresh button side-by-side
|
| 505 |
cA, cB = st.columns([3, 1])
|
| 506 |
with cA:
|
|
@@ -510,108 +512,37 @@ def render_login_signup_gate():
|
|
| 510 |
)
|
| 511 |
with cB:
|
| 512 |
refresh_captcha = st.form_submit_button("↻ New CAPTCHA")
|
| 513 |
-
|
| 514 |
login_clicked = st.form_submit_button("LOGIN", use_container_width=True)
|
| 515 |
-
|
| 516 |
# Refresh clicked → rotate and stop to re-render
|
| 517 |
if refresh_captcha:
|
| 518 |
gen_captcha(CAPTCHA_KEY_LOGIN)
|
| 519 |
st.stop()
|
| 520 |
-
|
| 521 |
if login_clicked:
|
| 522 |
-
#
|
| 523 |
-
|
| 524 |
-
|
| 525 |
-
gen_captcha(CAPTCHA_KEY_LOGIN)
|
| 526 |
-
st.stop()
|
| 527 |
-
|
| 528 |
-
# 2) Basic fields
|
| 529 |
-
if not email or not pw:
|
| 530 |
-
st.error("Email and password required.")
|
| 531 |
-
st.stop()
|
| 532 |
-
|
| 533 |
-
# 3) Lookup user
|
| 534 |
-
row = user_by_email(email)
|
| 535 |
-
if not row:
|
| 536 |
-
record_login_audit(email, "failure", "no_user", None)
|
| 537 |
-
st.error("Invalid credentials.")
|
| 538 |
-
gen_captcha(CAPTCHA_KEY_LOGIN)
|
| 539 |
-
st.stop()
|
| 540 |
-
|
| 541 |
-
# 4) Account checks
|
| 542 |
-
if int(row["is_active"]) != 1:
|
| 543 |
-
record_login_audit(email, "failure", "disabled", row["id"])
|
| 544 |
-
st.error("Account disabled. Contact admin.")
|
| 545 |
-
st.stop()
|
| 546 |
-
if within_lock(row):
|
| 547 |
-
record_login_audit(email, "locked", "locked", row["id"])
|
| 548 |
-
st.error(f"Account locked until {row['locked_until']}.")
|
| 549 |
-
st.stop()
|
| 550 |
-
|
| 551 |
-
# 5) Password verify (migration-aware if present)
|
| 552 |
-
if "verify_password_migrating" in globals():
|
| 553 |
-
ok, _meta = verify_password_migrating(email, pw)
|
| 554 |
-
else:
|
| 555 |
-
ok = verify_password(pw, row["password_hash"])
|
| 556 |
-
if not ok:
|
| 557 |
-
with get_connection() as conn:
|
| 558 |
-
fa = int(row["failed_attempts"]) + 1
|
| 559 |
-
locked_until = None
|
| 560 |
-
if fa >= 3:
|
| 561 |
-
locked_until = (datetime.utcnow() + timedelta(minutes=LOCKOUT_MINUTES)).isoformat(timespec="seconds")
|
| 562 |
-
fa = 0
|
| 563 |
-
conn.execute(
|
| 564 |
-
"UPDATE users SET failed_attempts=?, locked_until=?, updated_at=? WHERE id=?",
|
| 565 |
-
(fa, locked_until, datetime.utcnow().isoformat(timespec="seconds"), row["id"])
|
| 566 |
-
)
|
| 567 |
-
conn.commit()
|
| 568 |
-
record_login_audit(email, "failure", "bad_password", row["id"])
|
| 569 |
-
st.error("Invalid credentials." + (f" Locked until {locked_until}." if locked_until else ""))
|
| 570 |
-
gen_captcha(CAPTCHA_KEY_LOGIN)
|
| 571 |
-
st.stop()
|
| 572 |
-
|
| 573 |
-
# 6) Success
|
| 574 |
-
with get_connection() as conn:
|
| 575 |
-
conn.execute(
|
| 576 |
-
"UPDATE users SET failed_attempts=0, locked_until=NULL, last_login_at=?, updated_at=? WHERE id=?",
|
| 577 |
-
(datetime.utcnow().isoformat(timespec="seconds"), datetime.utcnow().isoformat(timespec="seconds"), row["id"])
|
| 578 |
-
)
|
| 579 |
-
conn.commit()
|
| 580 |
-
record_login_audit(email, "success", "", row["id"])
|
| 581 |
-
set_session_user(row)
|
| 582 |
-
|
| 583 |
-
# Force change if required/expired
|
| 584 |
-
if st.session_state["user"].get("require_pw_reset", 0) == 1 or is_password_expired(row):
|
| 585 |
-
st.warning("Password change required. Please set a new password.")
|
| 586 |
-
st.session_state["_force_change_pw"] = True
|
| 587 |
-
st.rerun()
|
| 588 |
-
|
| 589 |
-
# Clean rerender so the login form disappears
|
| 590 |
-
st.rerun()
|
| 591 |
-
|
| 592 |
# Links under button
|
| 593 |
st.markdown(
|
| 594 |
-
"<div style='margin-top:10px;color:#6b7280;font-size:14px;'>"
|
| 595 |
-
"Forgot password?"
|
| 596 |
-
"</div>",
|
| 597 |
unsafe_allow_html=True
|
| 598 |
)
|
| 599 |
if st.button("Register here", key="go_signup_link"):
|
| 600 |
st.session_state["_auth_view"] = "signup"
|
| 601 |
st.rerun()
|
| 602 |
-
|
| 603 |
st.markdown(
|
| 604 |
"<div style='margin-top:18px;color:#6b7280;font-size:13px;'>"
|
| 605 |
"Terms of use. Privacy policy"
|
| 606 |
"</div>",
|
| 607 |
unsafe_allow_html=True
|
| 608 |
)
|
|
|
|
|
|
|
| 609 |
|
| 610 |
-
|
| 611 |
-
|
| 612 |
-
# Not logged in during this render
|
| 613 |
-
return False
|
| 614 |
-
|
| 615 |
# =========================
|
| 616 |
# Change password flows
|
| 617 |
# =========================
|
|
|
|
| 456 |
)
|
| 457 |
|
| 458 |
# Right: card with form
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 459 |
|
| 460 |
+
with col_right:
|
| 461 |
+
# Open ONE card div and immediately render content inside it
|
| 462 |
+
st.markdown(
|
| 463 |
+
"<div id='login-card' style='background:#fff;border:1px solid #e5e7eb;"
|
| 464 |
+
"border-radius:12px;padding:28px;box-shadow:0 10px 30px rgba(0,0,0,.05)'>",
|
| 465 |
+
unsafe_allow_html=True
|
| 466 |
+
)
|
| 467 |
+
|
| 468 |
+
# Logo row + headline (inside the card)
|
| 469 |
+
st.markdown(
|
| 470 |
+
"<div style='display:flex;align-items:center;gap:10px;margin-bottom:6px;'>"
|
| 471 |
+
"<div style='font-size:26px'>🧊</div>"
|
| 472 |
+
"<div style='font-weight:700;font-size:26px'>Logo</div>"
|
| 473 |
+
"</div>"
|
| 474 |
+
"<div style='font-size:18px;color:#374151;margin:6px 0 16px;'>"
|
| 475 |
+
"Sign into your account</div>",
|
| 476 |
+
unsafe_allow_html=True
|
| 477 |
+
)
|
| 478 |
+
|
| 479 |
# === Branch: show SIGNUP when toggled ===
|
| 480 |
if view == "signup":
|
| 481 |
render_signup_panel_inline()
|
|
|
|
| 489 |
unsafe_allow_html=True
|
| 490 |
)
|
| 491 |
st.markdown("</div>", unsafe_allow_html=True) # close card
|
| 492 |
+
# IMPORTANT: return while still inside col_right
|
| 493 |
return False
|
| 494 |
+
|
| 495 |
# === LOGIN PANEL ===
|
| 496 |
CAPTCHA_KEY_LOGIN = "_captcha_login"
|
| 497 |
if CAPTCHA_KEY_LOGIN not in st.session_state:
|
| 498 |
gen_captcha(CAPTCHA_KEY_LOGIN)
|
| 499 |
a, b, _sum = read_captcha(CAPTCHA_KEY_LOGIN)
|
| 500 |
+
|
| 501 |
# The form
|
| 502 |
with st.form("login_form", clear_on_submit=False):
|
| 503 |
email = normalize_email(st.text_input("Email address"))
|
| 504 |
pw = st.text_input("Password", type="password")
|
| 505 |
+
|
| 506 |
# CAPTCHA row: input + refresh button side-by-side
|
| 507 |
cA, cB = st.columns([3, 1])
|
| 508 |
with cA:
|
|
|
|
| 512 |
)
|
| 513 |
with cB:
|
| 514 |
refresh_captcha = st.form_submit_button("↻ New CAPTCHA")
|
| 515 |
+
|
| 516 |
login_clicked = st.form_submit_button("LOGIN", use_container_width=True)
|
| 517 |
+
|
| 518 |
# Refresh clicked → rotate and stop to re-render
|
| 519 |
if refresh_captcha:
|
| 520 |
gen_captcha(CAPTCHA_KEY_LOGIN)
|
| 521 |
st.stop()
|
| 522 |
+
|
| 523 |
if login_clicked:
|
| 524 |
+
# ... your existing login checks, set_session_user, etc ...
|
| 525 |
+
pass
|
| 526 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 527 |
# Links under button
|
| 528 |
st.markdown(
|
| 529 |
+
"<div style='margin-top:10px;color:#6b7280;font-size:14px;'>Forgot password?</div>",
|
|
|
|
|
|
|
| 530 |
unsafe_allow_html=True
|
| 531 |
)
|
| 532 |
if st.button("Register here", key="go_signup_link"):
|
| 533 |
st.session_state["_auth_view"] = "signup"
|
| 534 |
st.rerun()
|
| 535 |
+
|
| 536 |
st.markdown(
|
| 537 |
"<div style='margin-top:18px;color:#6b7280;font-size:13px;'>"
|
| 538 |
"Terms of use. Privacy policy"
|
| 539 |
"</div>",
|
| 540 |
unsafe_allow_html=True
|
| 541 |
)
|
| 542 |
+
|
| 543 |
+
st.markdown("</div>", unsafe_allow_html=True) # close #login-card
|
| 544 |
|
| 545 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 546 |
# =========================
|
| 547 |
# Change password flows
|
| 548 |
# =========================
|