71 lines
2.3 KiB
Python
71 lines
2.3 KiB
Python
import streamlit as st
|
|
import yaml
|
|
from yaml.loader import SafeLoader
|
|
import streamlit_authenticator as stauth
|
|
from streamlit_authenticator.utilities.exceptions import LoginError
|
|
|
|
from auth import load_credentials_from_db, needs_password_change, update_password
|
|
|
|
def get_authenticator():
|
|
with open("config/auth.yaml", "r", encoding="utf-8") as f:
|
|
base_config = yaml.load(f, Loader=SafeLoader)
|
|
|
|
db_creds = load_credentials_from_db()
|
|
base_config["credentials"] = db_creds
|
|
|
|
authenticator = stauth.Authenticate(
|
|
base_config["credentials"],
|
|
base_config["cookie"]["name"],
|
|
base_config["cookie"]["key"],
|
|
base_config["cookie"]["expiry_days"],
|
|
)
|
|
return authenticator
|
|
|
|
|
|
def require_login():
|
|
"""Sicherstellen, dass der User eingeloggt ist. Auf JEDER Page verwenden."""
|
|
authenticator = get_authenticator()
|
|
|
|
try:
|
|
authenticator.login(location="main", key="Login")
|
|
except LoginError:
|
|
authenticator.logout("ForceLogout", "sidebar")
|
|
st.error("Sitzung ungültig. Bitte neu einloggen.")
|
|
st.stop()
|
|
|
|
auth_status = st.session_state.get("authentication_status")
|
|
if auth_status is False:
|
|
st.error("Login fehlgeschlagen.")
|
|
st.stop()
|
|
if auth_status is None:
|
|
st.warning("Bitte Benutzername und Passwort eingeben.")
|
|
st.stop()
|
|
|
|
# Passwortwechsel erzwingen
|
|
username = st.session_state.get("username")
|
|
if needs_password_change(username):
|
|
st.warning("Du musst dein Passwort ändern, bevor du die Anwendung nutzen kannst.")
|
|
|
|
with st.form("pw_change_form"):
|
|
pw1 = st.text_input("Neues Passwort", type="password")
|
|
pw2 = st.text_input("Neues Passwort (Wiederholung)", type="password")
|
|
submitted = st.form_submit_button("Passwort ändern")
|
|
|
|
if submitted:
|
|
if not pw1 or not pw2:
|
|
st.error("Bitte beide Passwortfelder ausfüllen.")
|
|
st.stop()
|
|
if pw1 != pw2:
|
|
st.error("Passwörter stimmen nicht überein.")
|
|
st.stop()
|
|
if len(pw1) < 8:
|
|
st.error("Passwort sollte mindestens 8 Zeichen lang sein.")
|
|
st.stop()
|
|
|
|
update_password(username, pw1, reset_flag=True)
|
|
st.success("Passwort wurde geändert.")
|
|
st.rerun()
|
|
|
|
st.stop()
|
|
|
|
return authenticator |