# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/why-does-an-instance-of-a-federated-agent-accept-a-peer-s-delegated-authority-after-the-brokering-token-has-expired-and-what-invariant-actually-preven.md
# Remnant Warrant:    https://corpusfetch.com/c/why-does-an-instance-of-a-federated-agent-accept-a-peer-s-delegated-authority-after-the-brokering-token-has-expired-and-what-invariant-actually-preven.warrant.json
# Markdown SHA-256:   1413c846452a46b56f39cf8d6b323af50f59c32d3a26f18207dacfd0bc8cb780
# Code AST SHA-256:   c1665fbddfb8c6ac14924ee85de662177995cb702ac78e46688504bc4ae2d5e5
# Verified Classes:   ZombieDelegationError, BrokeringAssertion, DelegationToken
# Defensive Guards:   6 fail-closed invariants (zero external deps)

from __future__ import annotations

import time
from dataclasses import dataclass


class ZombieDelegationError(RuntimeError):
    """Raised when a delegation is presented under a brokering assertion
    whose effective validity has lapsed, even though the delegation token
    itself is still within its own exp."""


@dataclass(frozen=True)
class BrokeringAssertion:
    issuer: str
    audience: str
    policy_mapping_hash: str
    issued_at: float
    not_after: float


@dataclass(frozen=True)
class DelegationToken:
    subject: str
    audience: str
    scope: frozenset[str]
    exp: float
    assertion_hash: str


def effective_not_after(
    assertion: BrokeringAssertion,
    max_assertion_age: float,
) -> float:
    # The invariant: the binding bound is the intersection of the broker's
    # stated horizon and the relying party's freshness policy. Using either
    # alone reopens the zombie window.
    return min(assertion.not_after, assertion.issued_at + max_assertion_age)


def verify_federated_request(
    token: DelegationToken,
    assertion: BrokeringAssertion,
    current_mapping_hash: str,
    max_assertion_age: float,
    now: float | None = None,
) -> None:
    if now is None:
        now = time.time()

    if now >= token.exp:
        raise ZombieDelegationError(
            f"delegation token for {token.subject!r} expired at {token.exp}"
        )

    if token.assertion_hash != assertion.policy_mapping_hash:
        raise ZombieDelegationError(
            "delegation token is not bound to the presented brokering assertion"
        )

    if assertion.policy_mapping_hash != current_mapping_hash:
        # Catches the refreshed-signature-over-stale-mapping variant.
        raise ZombieDelegationError(
            "brokering assertion references a policy mapping the relying party "
            "no longer holds"
        )

    bound = effective_not_after(assertion, max_assertion_age)
    if now >= bound:
        raise ZombieDelegationError(
            f"brokering assertion lapsed at {bound} (broker not_after="
            f"{assertion.not_after}, freshness bound="
            f"{assertion.issued_at + max_assertion_age}); delegation token "
            f"remains valid until {token.exp} — zombie window"
        )

    if not token.scope.issubset(_assertion_scope(assertion)):
        raise ZombieDelegationError(
            "delegation widens authority beyond the brokering assertion"
        )


def _assertion_scope(assertion: BrokeringAssertion) -> frozenset[str]:
    # Placeholder: real implementations resolve the mapping hash to a scope set
    # via the relying party's local policy store.
    raise ZombieDelegationError(
        f"no local policy mapping for hash {assertion.policy_mapping_hash!r}"
    )
