owner_identity.py 5.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. """The Webapp is the machine owner: one identity for every signed-in surface of this host.
  2. Hermes is single-user. Native Desktop, the TUI and the CLI reach the gateway as the owner's own OS
  3. user (``uid:<n>`` / ``sid:<sid>``, minted only for a same-user peer by the control channel). A Webapp
  4. tab, a standalone dashboard's forwarded login or a phone signed in through the dashboard login is
  5. that same person: sign-in only decides whether a browser may open the Webapp at all, it never names a
  6. second owner. So every authenticated non-native principal acts as :func:`host_owner_subject`, and
  7. the login stamps older builds stored (``auth:v1:[provider, issuer, user]`` subjects, the
  8. ``<provider>:<user>`` labels ``hermes serve`` stamped on Desktop rows) read as the owner's too. A
  9. ``local`` stamp names an OS user, so it matches this machine's account only. Every comparison of a
  10. stored owner with a caller goes through :func:`same_owner`, so the sidebar, the open, adoption and the
  11. room controls cannot disagree about who owns a row.
  12. """
  13. from __future__ import annotations
  14. def host_owner_subject() -> str:
  15. """The owner's own OS identity: the subject native Desktop's ticket carries on this host."""
  16. from gateway.session_worker_reservation import owner_worker_subject
  17. return owner_worker_subject()
  18. def principal_subject(identity: dict) -> str:
  19. """The subject a connection acts as. Whatever the private control channel or a native grant
  20. minted (provider ``local``: Desktop, the TUI, the CLI, a managed worker) keeps its subject; every
  21. login (a Webapp tab, the loopback session token, the server's own PTY child, or a dashboard login
  22. a standalone backend forwards in a local ticket) is the owner. An identity naming nobody stays
  23. unbound, exactly as before."""
  24. from gateway.session_identity import authenticated_subject, parse_authenticated_subject
  25. subject = authenticated_subject(identity)
  26. if not identity.get('user_id'):
  27. return subject
  28. provider = str(identity.get('provider') or '').strip()
  29. if provider == 'local':
  30. login = parse_authenticated_subject(subject)
  31. return host_owner_subject() if login is not None and _login_provider(login[0]) and login[0] != 'local' else subject
  32. return host_owner_subject() if _login_provider(provider) else subject
  33. # The interactive sign-ins Hermes bundles; a plugin-registered dashboard provider counts too
  34. # (:func:`_login_provider`). ``session-token`` is the loopback Webapp's own credential and
  35. # ``server-internal`` the PTY child a signed-in dashboard spawns: both act for whoever opened that page,
  36. # the owner. ``local`` (a native grant) is not a sign-in: its stamp names an OS user (:func:`_owner_login`).
  37. _BUNDLED_LOGINS = frozenset({'basic', 'nous', 'self-hosted', 'session-token', 'server-internal'})
  38. def _login_provider(provider: str) -> bool:
  39. if provider in _BUNDLED_LOGINS:
  40. return True
  41. from hermes_cli.dashboard_auth.registry import list_session_providers
  42. return any(p.name == provider for p in list_session_providers())
  43. def _owner_login(provider: str, user: str) -> bool:
  44. """Whether a stored ``(provider, user)`` stamp names the host owner. A ``local`` stamp (the native
  45. grant a served Desktop presented) names an OS user, so it is the owner's only when that user is this
  46. machine's owner; any other OS user stays foreign. A sign-in stamp is the owner's."""
  47. if provider == 'local':
  48. return user.strip() == host_owner_subject()
  49. return _login_provider(provider)
  50. def _login_label(value: str) -> bool:
  51. """``<provider>:<user>`` for a dashboard sign-in: the label ``hermes serve`` stamped for a login
  52. (session_transports). Any other prefix (``telegram:42``, ``uid:501``) names someone else, and a
  53. ``nous:`` stamp keeps its own rule (``legacy_account_label``: a Desktop row's account label)."""
  54. provider, sep, user = value.partition(':')
  55. return bool(sep and user.strip()) and provider != 'nous' and _owner_login(provider, user)
  56. def _login_subject(value) -> bool:
  57. """An ``auth:v1:[provider, issuer, user]`` subject a sign-in stored (a provider is named)."""
  58. from gateway.session_identity import parse_authenticated_subject
  59. parts = parse_authenticated_subject(value)
  60. return parts is not None and bool(parts[2].strip()) and _owner_login(parts[0], parts[2])
  61. def same_owner(stored, subject) -> bool:
  62. """THE owner rule: whether a stored owner stamp (a room owner, a history binding's subject, an
  63. admission's principal, a routeless row's ``user_id``) belongs to *subject*. True when equal, when it
  64. is the ``<provider>:<user>`` label of *subject*'s own login, or, while *subject* is the host owner,
  65. when it is any sign-in stamp an older build stored (an ``auth:v1`` login subject or a served
  66. dashboard's ``<provider>:<user>`` label). One-way: a non-owner subject never gains owner stamps.
  67. Every identity comparison on a stored owner goes through here so readers cannot disagree."""
  68. if stored == subject:
  69. return True
  70. if not isinstance(stored, str) or not stored or not subject:
  71. return False
  72. from hermes_state_mutation_binding import _login_label as label_of
  73. if stored == label_of(subject):
  74. return True
  75. return subject == host_owner_subject() and (_login_subject(stored) or _login_label(stored))
  76. def owners_agree(first, second) -> bool:
  77. """Whether two stored owner stamps name the same owner (:func:`same_owner` either way round), for
  78. checks that compare one stored stamp with another rather than with a caller."""
  79. return same_owner(first, second) or same_owner(second, first)