Spaces:
Paused
Paused
| # app/core/auth.py | |
| """ | |
| Oddiy autentifikatsiya tizimi (MVP uchun) | |
| Session-based authentication | |
| """ | |
| import secrets | |
| from typing import Optional, Dict | |
| from datetime import datetime, timedelta | |
| import logging | |
| logger = logging.getLogger(__name__) | |
| # Session storage (xotirada saqlash, MVP uchun yetarli) | |
| active_sessions: Dict[str, Dict] = {} | |
| # Session amal qilish muddati | |
| SESSION_LIFETIME = timedelta(hours=12) | |
| def create_session(dispatcher: Dict) -> str: | |
| """ | |
| Dispetcher uchun sessiya yaratish | |
| Returns: session_token | |
| """ | |
| # Tasodifiy session token yaratish | |
| session_token = secrets.token_urlsafe(32) | |
| # Session ma'lumotlarini saqlash | |
| active_sessions[session_token] = { | |
| "dispatcher_id": dispatcher['id'], | |
| "username": dispatcher['username'], | |
| "full_name": dispatcher['full_name'], | |
| "district": dispatcher['district'], | |
| "created_at": datetime.now(), | |
| "expires_at": datetime.now() + SESSION_LIFETIME | |
| } | |
| logger.info(f"β Yangi sessiya yaratildi: {dispatcher['username']}") | |
| return session_token | |
| def verify_session(session_token: str) -> Optional[Dict]: | |
| """ | |
| Session tokenni tekshirish | |
| Returns: dispatcher ma'lumotlari yoki None | |
| """ | |
| session = active_sessions.get(session_token) | |
| if not session: | |
| return None | |
| # Session muddati tugaganligini tekshirish | |
| if datetime.now() > session['expires_at']: | |
| logger.warning(f"β οΈ Session muddati tugagan: {session['username']}") | |
| del active_sessions[session_token] | |
| return None | |
| return session | |
| def delete_session(session_token: str) -> bool: | |
| """Session ni o'chirish (logout)""" | |
| if session_token in active_sessions: | |
| username = active_sessions[session_token]['username'] | |
| del active_sessions[session_token] | |
| logger.info(f"β Session o'chirildi: {username}") | |
| return True | |
| return False | |
| def get_active_dispatchers() -> list: | |
| """Hozir online bo'lgan dispetcherlar""" | |
| current_time = datetime.now() | |
| online_dispatchers = [] | |
| for token, session in active_sessions.items(): | |
| if session['expires_at'] > current_time: | |
| online_dispatchers.append({ | |
| "username": session['username'], | |
| "full_name": session['full_name'], | |
| "district": session['district'] | |
| }) | |
| return online_dispatchers | |
| # Vaqti-vaqti bilan eski sessionlarni tozalash | |
| def cleanup_expired_sessions(): | |
| """Muddati o'tgan sessionlarni o'chirish""" | |
| current_time = datetime.now() | |
| expired_tokens = [ | |
| token for token, session in active_sessions.items() | |
| if session['expires_at'] <= current_time | |
| ] | |
| for token in expired_tokens: | |
| username = active_sessions[token]['username'] | |
| del active_sessions[token] | |
| logger.info(f"ποΈ Eski session tozalandi: {username}") | |
| return len(expired_tokens) |