|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import threading |
| 4 | +import time |
| 5 | +import uuid |
| 6 | +from dataclasses import dataclass, field |
| 7 | +from urllib.parse import parse_qsl, urlencode, urlsplit, urlunsplit |
| 8 | + |
| 9 | +from src.errors import InputValidationError |
| 10 | + |
| 11 | +from backend.services.auth_session_service import restore_authenticated_session |
| 12 | + |
| 13 | + |
| 14 | +HANDOFF_TTL_SECONDS = 60 |
| 15 | + |
| 16 | + |
| 17 | +@dataclass |
| 18 | +class WorkspaceHandoffRecord: |
| 19 | + access_token: str |
| 20 | + refresh_token: str |
| 21 | + created_at: float = field(default_factory=time.time) |
| 22 | + |
| 23 | + |
| 24 | +_HANDOFFS: dict[str, WorkspaceHandoffRecord] = {} |
| 25 | +_LOCK = threading.Lock() |
| 26 | + |
| 27 | + |
| 28 | +def _prune_handoffs() -> None: |
| 29 | + cutoff = time.time() - HANDOFF_TTL_SECONDS |
| 30 | + stale_tokens = [ |
| 31 | + token |
| 32 | + for token, record in _HANDOFFS.items() |
| 33 | + if record.created_at < cutoff |
| 34 | + ] |
| 35 | + for token in stale_tokens: |
| 36 | + _HANDOFFS.pop(token, None) |
| 37 | + |
| 38 | + |
| 39 | +def _append_handoff_query(target_url: str, handoff_token: str) -> str: |
| 40 | + url = str(target_url or "").strip() |
| 41 | + parsed = urlsplit(url) |
| 42 | + query_items = [ |
| 43 | + (key, value) |
| 44 | + for key, value in parse_qsl(parsed.query, keep_blank_values=True) |
| 45 | + if key != "handoff" |
| 46 | + ] |
| 47 | + query_items.append(("handoff", handoff_token)) |
| 48 | + return urlunsplit( |
| 49 | + ( |
| 50 | + parsed.scheme, |
| 51 | + parsed.netloc, |
| 52 | + parsed.path, |
| 53 | + urlencode(query_items), |
| 54 | + parsed.fragment, |
| 55 | + ) |
| 56 | + ) |
| 57 | + |
| 58 | + |
| 59 | +def start_workspace_handoff( |
| 60 | + *, |
| 61 | + access_token: str, |
| 62 | + refresh_token: str, |
| 63 | + target_url: str, |
| 64 | +) -> dict: |
| 65 | + normalized_access = str(access_token or "").strip() |
| 66 | + normalized_refresh = str(refresh_token or "").strip() |
| 67 | + normalized_target = str(target_url or "").strip() |
| 68 | + |
| 69 | + if not normalized_access or not normalized_refresh: |
| 70 | + raise InputValidationError("Sign in with Google before entering the workspace.") |
| 71 | + if not normalized_target: |
| 72 | + raise InputValidationError("A workspace target URL is required.") |
| 73 | + |
| 74 | + with _LOCK: |
| 75 | + _prune_handoffs() |
| 76 | + handoff_token = uuid.uuid4().hex |
| 77 | + _HANDOFFS[handoff_token] = WorkspaceHandoffRecord( |
| 78 | + access_token=normalized_access, |
| 79 | + refresh_token=normalized_refresh, |
| 80 | + ) |
| 81 | + |
| 82 | + return { |
| 83 | + "status": "ready", |
| 84 | + "redirect_url": _append_handoff_query(normalized_target, handoff_token), |
| 85 | + } |
| 86 | + |
| 87 | + |
| 88 | +def exchange_workspace_handoff(*, handoff_token: str) -> dict: |
| 89 | + normalized_token = str(handoff_token or "").strip() |
| 90 | + if not normalized_token: |
| 91 | + raise InputValidationError("A workspace handoff token is required.") |
| 92 | + |
| 93 | + with _LOCK: |
| 94 | + _prune_handoffs() |
| 95 | + record = _HANDOFFS.pop(normalized_token, None) |
| 96 | + |
| 97 | + if record is None: |
| 98 | + raise InputValidationError( |
| 99 | + "That workspace handoff expired. Open the workspace from the landing page again." |
| 100 | + ) |
| 101 | + |
| 102 | + return restore_authenticated_session( |
| 103 | + access_token=record.access_token, |
| 104 | + refresh_token=record.refresh_token, |
| 105 | + ) |
0 commit comments