2026-05-12 23:50:49 +00:00
|
|
|
from typing import Dict, List, Set
|
2026-10-06 02:36:54 +00:00
|
|
|
import hashlib
|
|
|
|
|
import secrets
|
2026-05-12 23:50:49 +00:00
|
|
|
|
|
|
|
|
# In-Memory Data Stores
|
2026-10-06 02:36:54 +00:00
|
|
|
# Ephemeral in-memory state: zero persistent database, zero disk logging.
|
2026-05-12 23:50:49 +00:00
|
|
|
user_sessions: Dict[str, dict] = {} # {username: {'key': str, 'last_active': str}}
|
2026-10-06 02:36:54 +00:00
|
|
|
messages: Dict[str, List[dict]] = {} # {room_name: [{'username': str, 'ciphertext': str, 'iv': str, 'timestamp': str}]}
|
2026-05-12 23:50:49 +00:00
|
|
|
room_users: Dict[str, Dict[str, str]] = {} # {room_name: {username: last_active_timestamp}}
|
2026-10-06 02:36:54 +00:00
|
|
|
room_passwords: Dict[str, str] = {} # {room_name: "salt:hash"} - secure room password hashes
|
|
|
|
|
authorized_room_users: Dict[str, Set[str]] = {} # {room_name: {username, ...}} - validated sessions for room
|
|
|
|
|
room_owners: Dict[str, str] = {} # {room_name: owner_username}
|
|
|
|
|
kicked_users: Dict[str, Set[str]] = {} # {room_name: {kicked_username}}
|
2026-05-12 23:50:49 +00:00
|
|
|
banned_ips: Set[str] = set()
|
|
|
|
|
banned_usernames: Set[str] = set()
|
|
|
|
|
user_ips: Dict[str, str] = {} # {username: ip_address}
|
2026-10-06 02:36:54 +00:00
|
|
|
video_state: Dict[str, dict] = {} # {room_name: {'url': str, 'started_at': str, 'is_paused': bool, 'is_active': bool}}
|
2026-05-12 23:50:49 +00:00
|
|
|
nuked_rooms: Dict[str, float] = {} # {room_name: expiry_timestamp_epoch}
|
|
|
|
|
|
|
|
|
|
|
2026-10-06 02:36:54 +00:00
|
|
|
def hash_room_password(password: str) -> str:
|
|
|
|
|
"""Hashes a room password using PBKDF2-HMAC-SHA256 with a cryptographically secure salt."""
|
|
|
|
|
salt = secrets.token_hex(16)
|
|
|
|
|
key_hash = hashlib.pbkdf2_hmac(
|
|
|
|
|
"sha256",
|
|
|
|
|
password.encode("utf-8"),
|
|
|
|
|
salt.encode("utf-8"),
|
|
|
|
|
100000,
|
|
|
|
|
)
|
|
|
|
|
return f"{salt}:{key_hash.hex()}"
|
|
|
|
|
|
2026-06-23 16:43:52 +00:00
|
|
|
|
2026-10-06 02:36:54 +00:00
|
|
|
def verify_room_password(password: str, stored_hash: str) -> bool:
|
|
|
|
|
"""Verifies a room password against the stored salt:hash in constant time."""
|
|
|
|
|
try:
|
|
|
|
|
salt, expected_hash = stored_hash.split(":", 1)
|
|
|
|
|
test_hash = hashlib.pbkdf2_hmac(
|
|
|
|
|
"sha256",
|
|
|
|
|
password.encode("utf-8"),
|
|
|
|
|
salt.encode("utf-8"),
|
|
|
|
|
100000,
|
|
|
|
|
)
|
|
|
|
|
return secrets.compare_digest(test_hash.hex(), expected_hash)
|
|
|
|
|
except Exception:
|
|
|
|
|
return False
|