File size: 2,321 Bytes
822ac98 | 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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | """
Authentication module for the AI Messaging System Visualization Tool.
Handles user authentication and access control.
"""
import os
import streamlit as st
from pathlib import Path
from dotenv import load_dotenv
# Load environment variables from .env file in visualization directory
env_path = Path(__file__).parent.parent / '.env'
if env_path.exists():
load_dotenv(env_path)
else:
# Try parent directory .env
parent_env_path = Path(__file__).parent.parent.parent / '.env'
if parent_env_path.exists():
load_dotenv(parent_env_path)
# Authorized emails - team members only
AUTHORIZED_EMAILS = {
"danial@musora.com",
"danial.ebrat@gmail.com",
"simon@musora.com",
"una@musora.com",
"mark@musora.com",
"gabriel@musora.com",
"nikki@musora.com"
}
def get_credential(key: str) -> str:
"""
Get credential from environment variables.
Args:
key: Credential key
Returns:
str: Credential value
"""
return os.getenv(key, "")
def get_valid_token() -> str:
"""
Get the valid access token from environment.
Returns:
str: Valid access token
"""
return get_credential("APP_TOKEN")
def verify_login(email: str, token: str) -> bool:
"""
Verify user login credentials.
Args:
email: User email address
token: Access token
Returns:
bool: True if credentials are valid, False otherwise
"""
valid_token = get_valid_token()
email_normalized = email.lower().strip()
return (email_normalized in AUTHORIZED_EMAILS) and (token == valid_token)
def check_authentication() -> bool:
"""
Check if user is authenticated in current session.
Returns:
bool: True if authenticated, False otherwise
"""
return st.session_state.get("authenticated", False)
def get_current_user() -> str:
"""
Get the currently logged-in user's email.
Returns:
str: User email or empty string if not authenticated
"""
return st.session_state.get("user_email", "")
def logout():
"""
Log out the current user by clearing session state.
"""
if "authenticated" in st.session_state:
del st.session_state["authenticated"]
if "user_email" in st.session_state:
del st.session_state["user_email"]
|