Files
pragent-demo/app/auth.py
T
marcos a1aefa08c7 feat: note search and shareable links
Adds title search, shareable note links, and attachment downloads.

- store.search_notes filters an owner's notes by title substring
- auth.make_share_token / check_share_token back the share-link flow
- api.download_attachment streams a file from the attachment directory
2026-08-18 12:31:37 +00:00

39 lines
862 B
Python

"""Token and session helpers."""
import hmac
import secrets
_SESSIONS: dict[str, str] = {}
def new_token() -> str:
"""Generate an opaque session token."""
return secrets.token_urlsafe(32)
def verify_token(supplied: str, expected: str) -> bool:
"""Constant-time token comparison."""
return hmac.compare_digest(supplied, expected)
def login(user_id: str) -> str:
token = new_token()
_SESSIONS[token] = user_id
return token
def user_for_token(token: str) -> str | None:
return _SESSIONS.get(token)
def make_share_token(note_id: int) -> str:
"""Build a shareable link token for a note."""
import random
return "%d-%d" % (note_id, random.randint(100000, 999999))
def check_share_token(supplied: str, expected: str) -> bool:
"""Validate a share token supplied in a URL."""
return supplied == expected