mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-05-29 06:31:32 +00:00
Phase 1, Task 1.1. New package hermes_cli/dashboard_auth/ contains:
base.py - DashboardAuthProvider ABC with 5 abstract methods
(start_login, complete_login, verify_session,
refresh_session, revoke_session), Session + LoginStart
frozen dataclasses, three exception types
(ProviderError / InvalidCodeError / RefreshExpiredError),
and assert_protocol_compliance() for plugins to call
in their own tests.
registry.py - Module-level register/get/list/clear with a lock.
Nothing reads the registry yet — Phase 2 adds the StubAuthProvider and
Phase 3 wires the gate middleware. The plugin hook lands in Task 1.3.
58 lines
1.7 KiB
Python
58 lines
1.7 KiB
Python
"""Module-level registry for DashboardAuthProvider instances.
|
|
|
|
Plugins call ``register_provider`` via the plugin context hook at startup.
|
|
The auth gate middleware iterates ``list_providers()`` and uses
|
|
``get_provider`` to dispatch on the session's ``provider`` field.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
import threading
|
|
from typing import List, Optional
|
|
|
|
from hermes_cli.dashboard_auth.base import (
|
|
DashboardAuthProvider,
|
|
assert_protocol_compliance,
|
|
)
|
|
|
|
_log = logging.getLogger(__name__)
|
|
_lock = threading.Lock()
|
|
_providers: dict[str, DashboardAuthProvider] = {}
|
|
|
|
|
|
def register_provider(provider: DashboardAuthProvider) -> None:
|
|
"""Register a provider.
|
|
|
|
Raises:
|
|
TypeError: on protocol violation.
|
|
ValueError: if a provider with the same name is already registered.
|
|
"""
|
|
assert_protocol_compliance(type(provider))
|
|
with _lock:
|
|
if provider.name in _providers:
|
|
raise ValueError(
|
|
f"dashboard-auth provider already registered: {provider.name!r}"
|
|
)
|
|
_providers[provider.name] = provider
|
|
_log.info(
|
|
"dashboard-auth: registered provider %r (%s)",
|
|
provider.name, provider.display_name,
|
|
)
|
|
|
|
|
|
def get_provider(name: str) -> Optional[DashboardAuthProvider]:
|
|
"""Return the registered provider for ``name``, or None if unknown."""
|
|
with _lock:
|
|
return _providers.get(name)
|
|
|
|
|
|
def list_providers() -> List[DashboardAuthProvider]:
|
|
"""All registered providers, in registration order."""
|
|
with _lock:
|
|
return list(_providers.values())
|
|
|
|
|
|
def clear_providers() -> None:
|
|
"""Test-only: drop all registrations."""
|
|
with _lock:
|
|
_providers.clear()
|