File size: 1,337 Bytes
d7efa84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"""Authentication logic for the Audio Labeling Tool."""

import logging

import streamlit as st

from config import load_config

logger = logging.getLogger(__name__)


def authenticate(username: str, password: str) -> str | None:
    """Validate credentials against config.

    Returns:
        "admin" if admin credentials, "labeler" if labeler credentials, None if invalid.
    """
    config = load_config()

    # Check admin
    admin_cfg = config["admin"]
    if username == admin_cfg["username"] and password == admin_cfg["password"]:
        return "admin"

    # Check labelers
    labeler = config["labelers"].get(username)
    if labeler is not None and labeler["password"] == password:
        return "labeler"

    return None


def login(username: str, role: str) -> None:
    """Set session state on successful login."""
    st.session_state["authenticated"] = True
    st.session_state["username"] = username
    st.session_state["role"] = role
    logger.info(f"User '{username}' logged in as '{role}'.")


def logout() -> None:
    """Clear all session state and return to login."""
    username = st.session_state.get("username", "unknown")
    for key in list(st.session_state.keys()):
        del st.session_state[key]
    st.session_state["authenticated"] = False
    logger.info(f"User '{username}' logged out.")