"""The replay seam: `StoreBackedWebAuth` is the core's `FederatedWebAuth`
with its replay state (single-use challenges, used auth event ids) kept in
a `ReplayStore` instead of its own sqlite tables.
Why a subclass, and why these methods. `fleetmesh_core` is a separate repo
and is not modified. In `fleetmesh_core/identity/web_auth.py` every touch of
`auth_challenges` / `used_auth_events` goes through four public methods on
`FederatedWebAuth`: `issue_challenge`, `consume_challenge`,
`consume_auth_event_id` and `reset` (plus `ensure_tables`, which creates
them). The verifiers (`verify_login_payload`, `verify_nip98_request`) only
ever call `self.consume_challenge(...)` / `self.consume_auth_event_id(...)`,
and the key/DID provider calls `web_auth.consume_challenge(...)` for
passkeys; overriding those four is therefore the whole seam. Everything
else (signature checks, portable tickets, NIP-98) is inherited unchanged.
The base class still gets a private in-memory sqlite connection so its
constructor and `ensure_tables()` are satisfied; nothing is ever written to it.
"""
from __future__ import annotations
import hashlib
import sqlite3
from typing import Any, Callable, Dict, Optional
from fleetmesh_core.identity.web_auth import FederatedWebAuth, TokenSource, challenge_message
from fleetmesh_auth.stores.base import ReplayStore
[docs]class StoreBackedWebAuth(FederatedWebAuth):
[docs] def __init__(
self,
replay: ReplayStore,
*,
origin_url: str = "",
node_did: str = "",
challenge_ttl_sec: int = 600,
clock: Optional[Callable[[], int]] = None,
token_source: Optional[TokenSource] = None,
**kwargs: Any,
) -> None:
self.replay = replay
super().__init__(
conn=sqlite3.connect(":memory:", check_same_thread=False),
origin_url=origin_url,
node_did=node_did,
challenge_ttl_sec=challenge_ttl_sec,
clock=clock,
token_source=token_source,
**kwargs,
)
[docs] def reset(self) -> None:
"""Forget every outstanding challenge and used event id (tests)."""
clear = getattr(self.replay, "clear_replay", None)
if clear is not None:
clear()
clear = reset
[docs] def issue_challenge(self, *, now: Optional[int] = None, challenge: Optional[str] = None) -> Dict[str, Any]:
now_ts = int(now if now is not None else self.clock())
challenge = challenge if challenge is not None else self.token_source.challenge_hex()
expires_at = now_ts + self.challenge_ttl_sec
self.replay.issue_challenge(challenge, expires_at)
auth_msg = challenge_message(challenge, self.origin_url, scheme=self.scheme)
return {
"challenge": challenge,
"auth_message": auth_msg,
"auth_message_sha256": hashlib.sha256(auth_msg.encode("utf-8")).hexdigest(),
"instance_url": self.origin_url,
"node_did": self.node_did,
"expires_at": expires_at,
}
[docs] def consume_challenge(self, challenge: str, *, now: Optional[int] = None) -> bool:
if not challenge:
return False
now_ts = int(now if now is not None else self.clock())
return bool(self.replay.consume_challenge(challenge, now_ts))
[docs] def consume_auth_event_id(self, event_id: str, pubkey: str, expires_at: int, *, now: Optional[int] = None) -> bool:
if not event_id:
return False
now_ts = int(now if now is not None else self.clock())
return bool(self.replay.consume_event_id(event_id, pubkey, int(expires_at), now_ts))