Files
ds-chat/backend/app/services/user_agent_service.py
T
ksmithandClaude Sonnet 5 b26643527d Add per-device active sessions with revocation (#69)
Replaces the stateless signed-cookie session (bare user_id) with a
real server-side sessions table -- the cookie now just carries an
opaque session id, resolved against the DB on every request. Each
session records IP address (respects X-Forwarded-For), a parsed
device label, and last-seen time (throttled updates, not written on
every request).

New GET/DELETE /api/auth/sessions endpoints and an "Active sessions"
section in Profile settings let a user see every device they're
logged in from and revoke one they don't recognize -- including their
own current session, which just signs them out.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-28 20:22:50 -06:00

47 lines
1.6 KiB
Python

def describe_user_agent(user_agent: str | None) -> str:
"""A short, human-readable "Browser on OS" label for the sessions list
-- no dependency pulled in for this (the app has stayed on a
zero-subdependency-when-possible diet, see MessageContent.tsx's
markdown-to-jsx choice), just substring checks against the handful of
tokens that actually distinguish the browsers/platforms this app's
users run. Order matters: Electron and Edge both also contain
"Chrome/", and Chrome-on-iOS/Safari-on-iOS both contain "Safari/", so
the more specific token has to be checked first.
"""
if not user_agent:
return "Unknown device"
if "Electron/" in user_agent:
browser = "DS Chat Desktop"
elif "Edg/" in user_agent:
browser = "Edge"
elif "OPR/" in user_agent:
browser = "Opera"
elif "Firefox/" in user_agent:
browser = "Firefox"
elif "Chrome/" in user_agent:
browser = "Chrome"
elif "CriOS/" in user_agent:
browser = "Chrome"
elif "Safari/" in user_agent:
browser = "Safari"
else:
browser = "Unknown browser"
if "Windows" in user_agent:
os_name = "Windows"
elif "Mac OS X" in user_agent and ("iPhone" in user_agent or "iPad" in user_agent):
os_name = "iOS"
elif "Mac OS X" in user_agent:
os_name = "macOS"
elif "Android" in user_agent:
os_name = "Android"
elif "Linux" in user_agent:
os_name = "Linux"
else:
os_name = "Unknown OS"
if browser == "DS Chat Desktop":
return f"{browser} ({os_name})"
return f"{browser} on {os_name}"