Fix KeyError in auth.py when password field is empty
Browse files- demo/auth.py +14 -5
demo/auth.py
CHANGED
|
@@ -74,31 +74,40 @@ def check_password() -> bool:
|
|
| 74 |
|
| 75 |
def password_entered():
|
| 76 |
"""Check if entered password is correct."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
# Get password(s) from secrets
|
| 78 |
if "auth" in st.secrets:
|
| 79 |
# Check single password
|
| 80 |
if "password" in st.secrets["auth"]:
|
| 81 |
if hmac.compare_digest(
|
| 82 |
-
|
| 83 |
st.secrets["auth"]["password"]
|
| 84 |
):
|
| 85 |
st.session_state["authenticated"] = True
|
| 86 |
st.session_state["username"] = "user"
|
| 87 |
-
|
|
|
|
| 88 |
return
|
| 89 |
|
| 90 |
# Check multiple users
|
| 91 |
if "passwords" in st.secrets["auth"]:
|
| 92 |
username = st.session_state.get("username_input", "")
|
| 93 |
-
password = st.session_state.get("password", "")
|
| 94 |
|
| 95 |
passwords = st.secrets["auth"]["passwords"]
|
| 96 |
if username in passwords and hmac.compare_digest(
|
| 97 |
-
|
| 98 |
):
|
| 99 |
st.session_state["authenticated"] = True
|
| 100 |
st.session_state["username"] = username
|
| 101 |
-
|
|
|
|
| 102 |
return
|
| 103 |
|
| 104 |
st.session_state["authenticated"] = False
|
|
|
|
| 74 |
|
| 75 |
def password_entered():
|
| 76 |
"""Check if entered password is correct."""
|
| 77 |
+
# Safely get password from session state
|
| 78 |
+
entered_password = st.session_state.get("password", "")
|
| 79 |
+
|
| 80 |
+
if not entered_password:
|
| 81 |
+
st.session_state["authenticated"] = False
|
| 82 |
+
st.session_state["password_incorrect"] = True
|
| 83 |
+
return
|
| 84 |
+
|
| 85 |
# Get password(s) from secrets
|
| 86 |
if "auth" in st.secrets:
|
| 87 |
# Check single password
|
| 88 |
if "password" in st.secrets["auth"]:
|
| 89 |
if hmac.compare_digest(
|
| 90 |
+
entered_password,
|
| 91 |
st.secrets["auth"]["password"]
|
| 92 |
):
|
| 93 |
st.session_state["authenticated"] = True
|
| 94 |
st.session_state["username"] = "user"
|
| 95 |
+
if "password" in st.session_state:
|
| 96 |
+
del st.session_state["password"]
|
| 97 |
return
|
| 98 |
|
| 99 |
# Check multiple users
|
| 100 |
if "passwords" in st.secrets["auth"]:
|
| 101 |
username = st.session_state.get("username_input", "")
|
|
|
|
| 102 |
|
| 103 |
passwords = st.secrets["auth"]["passwords"]
|
| 104 |
if username in passwords and hmac.compare_digest(
|
| 105 |
+
entered_password, passwords[username]
|
| 106 |
):
|
| 107 |
st.session_state["authenticated"] = True
|
| 108 |
st.session_state["username"] = username
|
| 109 |
+
if "password" in st.session_state:
|
| 110 |
+
del st.session_state["password"]
|
| 111 |
return
|
| 112 |
|
| 113 |
st.session_state["authenticated"] = False
|