MemPrepMate / src /services /session_service.py
Christian Kniep
new webapp
1fff71f
"""
Session ID generation service.
Feature: 012-profile-contact-ui
"""
import re
import uuid
def generate_profile_session_id(user_id: str) -> str:
"""
Generate profile session ID: {user_id}_session
Args:
user_id: HuggingFace username
Returns:
Profile session ID string
Raises:
ValueError: If user_id is empty or contains invalid characters
"""
if not user_id:
raise ValueError("user_id cannot be empty")
# Validate user_id format (alphanumeric + hyphens/underscores)
if not re.match(r"^[a-zA-Z0-9_-]+$", user_id):
raise ValueError(
"user_id must contain only alphanumeric characters, hyphens, and underscores"
)
return f"{user_id}_session"
def generate_contact_session_id(user_id: str) -> str:
"""
Generate contact session ID: UUID_v4
Note: Returns pure UUID format (without user_id prefix) to maintain
compatibility with PostgreSQL UUID type in the backend API.
Args:
user_id: HuggingFace username (validated but not included in session_id)
Returns:
Contact session ID string (pure UUID format)
Raises:
ValueError: If user_id is empty or contains invalid characters
"""
if not user_id:
raise ValueError("user_id cannot be empty")
# Validate user_id format (alphanumeric + hyphens/underscores)
if not re.match(r"^[a-zA-Z0-9_-]+$", user_id):
raise ValueError(
"user_id must contain only alphanumeric characters, hyphens, and underscores"
)
# Generate UUID v4 (cryptographically random)
# Return pure UUID without prefix to match PostgreSQL UUID type
return str(uuid.uuid4())