Spaces:
Sleeping
Sleeping
File size: 1,711 Bytes
1fff71f |
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 |
"""
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())
|