refresh_singleflight.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. """Single-flight + short replay cache for rotating refresh tokens (both refresh paths).
  2. Rotating refresh tokens with reuse detection (Nous Portal, Authelia, most OIDC IdPs) make a
  3. replay of an already-rotated RT fatal: the provider revokes the whole session. The desktop and
  4. the browser both fire bursts of parallel requests on wake or after the access token lapses,
  5. each still carrying the same old RT, so the gateway must let exactly ONE of them reach the
  6. provider and hand the rotated session to the rest. The cookie gate (``middleware``) and the
  7. native bearer route (``routes.auth_native_refresh``) share this one flight table.
  8. A provider hint only orders discovery: it must neither split one rotating credential's lock
  9. nor let an unrelated provider reuse its result. Raw refresh tokens are never keys.
  10. """
  11. from __future__ import annotations
  12. import hashlib
  13. import logging
  14. import threading
  15. import time
  16. from dataclasses import dataclass, field
  17. from typing import Callable, Optional
  18. from hermes_cli.dashboard_auth.base import DashboardAuthProvider, RefreshExpiredError, Session
  19. from hermes_cli.dashboard_auth.request_utils import scan_session_providers
  20. # The success TTL covers the window between the winning response and the siblings' arrival
  21. # (a laptop waking from sleep can deliver its burst over many seconds); the failure TTL only
  22. # absorbs a retry storm against a token the provider has already declared dead.
  23. _SUCCESS_TTL = 30.0
  24. _FAILURE_TTL = 5.0
  25. _MAX_ENTRIES = 256
  26. _guard = threading.Lock()
  27. @dataclass
  28. class _Flight:
  29. lock: threading.Lock = field(default_factory=threading.Lock)
  30. users: int = 0
  31. # Hold the provider itself while caching: replacement (including same-name scoped
  32. # registrations) invalidates identity, and Python cannot recycle its id under a live entry.
  33. _cache: dict[tuple[int, bytes], tuple[float, DashboardAuthProvider, Session | None]] = {}
  34. _flights: dict[tuple[int, bytes], _Flight] = {}
  35. def _prune(now: float) -> None:
  36. for key, (expires, _, _) in list(_cache.items()):
  37. if expires <= now:
  38. del _cache[key]
  39. while len(_cache) > _MAX_ENTRIES:
  40. del _cache[min(_cache, key=lambda key: _cache[key][0])]
  41. def _refresh_provider(provider: DashboardAuthProvider, token: str) -> Session | None:
  42. # Keyed on the token alone: whoever presents this RT already owns the session, and a client
  43. # that changed network between two requests of one burst must still hit the cache.
  44. digest = hashlib.sha256(token.encode()).digest()
  45. key = (id(provider), digest)
  46. with _guard:
  47. _prune(time.monotonic())
  48. flight = _flights.setdefault(key, _Flight())
  49. flight.users += 1
  50. try:
  51. with flight.lock:
  52. with _guard:
  53. cached = _cache.get(key)
  54. if cached is not None and cached[0] > time.monotonic():
  55. return cached[2]
  56. try:
  57. session = provider.refresh_session(refresh_token=token)
  58. except RefreshExpiredError:
  59. session = None
  60. # ProviderError and unexpected execution failures are deliberately not cached.
  61. with _guard:
  62. now = time.monotonic()
  63. _cache[key] = (now + (_SUCCESS_TTL if session is not None else _FAILURE_TTL), provider, session)
  64. _prune(now)
  65. return session
  66. finally:
  67. with _guard:
  68. flight.users -= 1
  69. if flight.users == 0:
  70. _flights.pop(key, None)
  71. def refresh_session_coalesced(
  72. token: str, provider_hint: str, *, phase: str, log: logging.Logger,
  73. on_rejected: Optional[Callable[[DashboardAuthProvider], None]] = None,
  74. on_unreachable: Optional[Callable[[DashboardAuthProvider], None]] = None,
  75. ) -> Optional[tuple[Session, str]]:
  76. """Rotate ``token`` through the provider stack with per-provider single-flight.
  77. ``(Session, provider_name)`` or ``None`` when every provider rejects the token; a
  78. ``ProviderError`` propagates when nothing rotated and one provider was unreachable
  79. (``scan_session_providers`` semantics, so callers keep their 503-not-relogin handling).
  80. Synchronous and network-bound: async callers run it in a threadpool.
  81. """
  82. def _call(provider: DashboardAuthProvider):
  83. session = _refresh_provider(provider, token)
  84. if session is None:
  85. if on_rejected is not None:
  86. on_rejected(provider)
  87. return None
  88. return session, provider.name
  89. return scan_session_providers(
  90. provider_hint, _call, phase=phase, log=log, on_unreachable=on_unreachable)