browser_credential.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. """The browser credential, and how every grant made under it ends when it is replaced.
  2. The browser credential is the dashboard session token (``hermes_cli.web_server._SESSION_TOKEN``). A
  3. loopback Webapp tab holds it; behind the sign-in gate each Webapp login is bound to it
  4. (``hermes_cli.webapp_sign_in``). The gateway keeps the Webapp's credential in its home
  5. (``gateway.webapp_listener.browser_session_token``) and sets it when its listener starts.
  6. Every grant a browser gets under the credential records the credential's generation, a SHA-256 digest
  7. of it, when the grant is made, and is honoured only while :func:`is_current` says that generation is
  8. still the one in use: window and launch tickets (``web_routers.webapp``), socket tickets
  9. (``web_session_sockets.ticket_refusal``), Bot Desktop viewer tickets (``web_routers.display``), file
  10. tickets (``web_server_file_tickets``), open sockets (``web_session_sockets.track``) and a cookie
  11. login's binding (``webapp_sign_in.check``). Stores keep the generation, never the credential.
  12. :func:`rotate` (``hermes webapp --sign-out``) replaces the credential, then runs every revoker
  13. registered with :func:`on_rotate`: grants that stay in use without being presented again (open
  14. sockets) end at once, and stores drop what can no longer be redeemed.
  15. """
  16. from __future__ import annotations
  17. import hashlib
  18. import hmac
  19. import threading
  20. from typing import Callable, Optional
  21. _lock = threading.Lock()
  22. #: Revoker name -> callable that ends the grants made under a replaced credential and returns how
  23. #: many it ended.
  24. _revokers: dict[str, Callable[[], int]] = {}
  25. def current() -> str:
  26. """The browser credential in use now."""
  27. from hermes_cli import web_server
  28. return web_server._SESSION_TOKEN
  29. def generation_of(credential: str) -> str:
  30. """The generation a grant made under *credential* records."""
  31. return hashlib.sha256(credential.encode()).hexdigest()
  32. def generation() -> str:
  33. """The generation of the credential in use now."""
  34. return generation_of(current())
  35. def is_current(recorded: Optional[str]) -> bool:
  36. """Whether a grant that recorded generation *recorded* was made under the credential in use now."""
  37. return current_if(recorded) is not None
  38. def current_if(recorded: Optional[str]) -> Optional[str]:
  39. """The credential in use now when *recorded* is its generation, else None. For a grant that
  40. hands the credential over or binds a login to it: one read, so a rotation between the check
  41. and the use cannot pass on the credential that replaced the one the grant was made under."""
  42. credential = current()
  43. if recorded is None or not hmac.compare_digest(recorded, generation_of(credential)):
  44. return None
  45. return credential
  46. def on_rotate(name: str, revoker: Callable[[], int]) -> None:
  47. """Run *revoker* each time the credential is replaced; registering a name again replaces it."""
  48. with _lock:
  49. _revokers[name] = revoker
  50. def rotate(credential: str) -> dict[str, int]:
  51. """Make *credential* the browser credential and end every grant made under the one it replaces.
  52. Returns how many grants each registered revoker ended, by name. Safe from any thread."""
  53. from hermes_cli import web_server
  54. web_server._SESSION_TOKEN = credential
  55. with _lock:
  56. revokers = dict(_revokers)
  57. return {name: revoker() for name, revoker in revokers.items()}