(agent.py): Improve user ID validation by checking if it is a Google numeric ID or a Langflow UUID format

♻️ (langflow_history_service.py, user_binding_service.py): Refactor UUID format check to use a more descriptive and clear logic based on user ID type
This commit is contained in:
cristhianzl 2025-09-04 16:26:31 -03:00
parent 3b26a6b600
commit c87877bb80
3 changed files with 10 additions and 22 deletions

View file

@ -418,12 +418,8 @@ async def async_langflow_chat(
from services.session_ownership_service import session_ownership_service
from services.user_binding_service import user_binding_service
# Check if this is a Google user (has binding but not UUID format)
import re
uuid_pattern = r'^[0-9a-f]{8}-?[0-9a-f]{4}-?[0-9a-f]{4}-?[0-9a-f]{4}-?[0-9a-f]{12}$'
is_uuid = bool(re.match(uuid_pattern, user_id.lower().replace('-', '')))
if not is_uuid and user_binding_service.has_binding(user_id):
# Check if this is a Google user (Google IDs are numeric, Langflow IDs are UUID)
if user_id.isdigit() and user_binding_service.has_binding(user_id):
langflow_user_id = user_binding_service.get_langflow_user_id(user_id)
if langflow_user_id:
session_ownership_service.claim_session(user_id, response_id, langflow_user_id)
@ -511,12 +507,8 @@ async def async_langflow_chat_stream(
from services.session_ownership_service import session_ownership_service
from services.user_binding_service import user_binding_service
# Check if this is a Google user (has binding but not UUID format)
import re
uuid_pattern = r'^[0-9a-f]{8}-?[0-9a-f]{4}-?[0-9a-f]{4}-?[0-9a-f]{4}-?[0-9a-f]{12}$'
is_uuid = bool(re.match(uuid_pattern, user_id.lower().replace('-', '')))
if not is_uuid and user_binding_service.has_binding(user_id):
# Check if this is a Google user (Google IDs are numeric, Langflow IDs are UUID)
if user_id.isdigit() and user_binding_service.has_binding(user_id):
langflow_user_id = user_binding_service.get_langflow_user_id(user_id)
if langflow_user_id:
session_ownership_service.claim_session(user_id, response_id, langflow_user_id)

View file

@ -44,11 +44,9 @@ class LangflowHistoryService:
return None
def _is_uuid_format(self, user_id: str) -> bool:
"""Check if string looks like a UUID (Langflow user ID format)"""
import re
# Basic UUID pattern check (with or without dashes)
uuid_pattern = r'^[0-9a-f]{8}-?[0-9a-f]{4}-?[0-9a-f]{4}-?[0-9a-f]{4}-?[0-9a-f]{12}$'
return bool(re.match(uuid_pattern, user_id.lower().replace('-', '')))
"""Check if string looks like a UUID (Langflow user ID format vs Google numeric ID)"""
# Langflow IDs are UUID v4, Google IDs are purely numeric
return not user_id.isdigit()
def _filter_sessions_by_ownership(self, session_ids: List[str], user_id: str, langflow_user_id: str) -> List[str]:
"""Filter sessions based on user type and ownership"""

View file

@ -237,11 +237,9 @@ class UserBindingService:
return self.bindings.copy()
def is_langflow_user_id(self, user_id: str) -> bool:
"""Check if user_id appears to be a Langflow UUID"""
import re
# Basic UUID pattern check (with or without dashes)
uuid_pattern = r'^[0-9a-f]{8}-?[0-9a-f]{4}-?[0-9a-f]{4}-?[0-9a-f]{4}-?[0-9a-f]{12}$'
return bool(re.match(uuid_pattern, user_id.lower().replace('-', '')))
"""Check if user_id appears to be a Langflow UUID (vs Google numeric ID)"""
# Langflow IDs are UUID v4, Google IDs are purely numeric
return not user_id.isdigit()
def get_user_type(self, user_id: str) -> str:
"""Determine user type: 'google_oauth', 'langflow_direct', or 'unknown'"""