# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-does-an-instance-detect-a-revoked-but-still-cached-issuer-chain-in-federated-agent-passport-verification.md
# Remnant Warrant:    https://corpusfetch.com/c/how-does-an-instance-detect-a-revoked-but-still-cached-issuer-chain-in-federated-agent-passport-verification.warrant.json
# Markdown SHA-256:   51d6b44a9310fa316d2acb37ac156f391153df8c962edc119abfaf55b5e911be
# Code AST SHA-256:   98c465fd4c580defa889e0b86da829c89f5e9fe7b16c3c735a356d709b1ac08e
# Verified Classes:   StaleIssuerChainError, CachedStatus
# Defensive Guards:   4 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass


class StaleIssuerChainError(RuntimeError):
    """Raised when a passport's issuer chain cannot be shown to be
    authorized as of the verifier's clock, regardless of cryptographic validity."""


@dataclass
class CachedStatus:
    subject: str
    authorized: bool
    valid_until: float  # signed by the authority; verifier must not trust unsigned
    signed_by_root: bool


def verify_passport_chain(
    chain: list[CachedStatus],
    verifier_clock: float,
    max_staleness: float = 300.0,
) -> bool:
    """Verify that every link in the issuer chain is authorized *now*.

    Cryptographic signature validity is assumed already checked. This function
    enforces the invariant that chain validity implies authorization currency.
    """
    for link in chain:
        if not link.signed_by_root:
            # An intermediate hop served this; it is not the root's word.
            raise StaleIssuerChainError(
                f"link for {link.subject!r} is not root-signed; "
                f"cannot establish authorization currency"
            )
        if link.valid_until < verifier_clock:
            raise StaleIssuerChainError(
                f"link for {link.subject!r} expired at {link.valid_until}, "
                f"verifier clock {verifier_clock}; authorization not current"
            )
        if link.valid_until - verifier_clock > max_staleness:
            raise StaleIssuerChainError(
                f"link for {link.subject!r} asserts authorization too far in "
                f"the future; refusing issuer-controlled clock skew"
            )
        if not link.authorized:
            raise StaleIssuerChainError(
                f"link for {link.subject!r} is revoked at the root"
            )
    return True


def example() -> None:
    now = time.time()
    chain = [
        CachedStatus("A->B", authorized=True, valid_until=now + 60, signed_by_root=True),
        CachedStatus("B->C", authorized=True, valid_until=now + 60, signed_by_root=True),
        # C's cache of B still says authorized, but B's authorization from A
        # lapsed 10 seconds ago; the verifier's clock catches it.
        CachedStatus("C->worker-7741", authorized=True, valid_until=now - 10, signed_by_root=True),
    ]
    verify_passport_chain(chain, verifier_clock=now)


if __name__ == "__main__":
    example()
