# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-distrust-a-valid-aaif-attestation-token-when-the-hardware-root-of-trust-has-silently-re-keyed-mid-session.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-distrust-a-valid-aaif-attestation-token-when-the-hardware-root-of-trust-has-silently-re-keyed-mid-session.warrant.json
# Markdown SHA-256:   38464f4e3799d49fd6daca7f4c69bd15df040e9eeab058b7b8b97c4281d92f4d
# Code AST SHA-256:   768f6934521a72daa9145322427366f75fe5d5c56a82a56771124bda2cc6417e
# Verified Classes:   BindingMismatchError, AAIFToken
# Defensive Guards:   3 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass


class BindingMismatchError(RuntimeError):
    """Raised when a token's hardware binding cannot be re-confirmed live."""
    pass


@dataclass
class AAIFToken:
    subject: str
    exp: float
    attestation_ref_key_fingerprint: str  # pubkey fp bound at issuance


def verify_presentation(
    token: AAIFToken,
    fresh_quote_fingerprint: str | None,
    now: float | None = None,
) -> None:
    """Verify a presented AAIF token against a *fresh* hardware quote.

    fresh_quote_fingerprint is the pubkey fingerprint derived from a
    nonce-challenged quote taken at presentation time. None means the
    presenter could not produce a fresh signature (e.g. key was cleared).
    """
    now = time.time() if now is None else now

    if now >= token.exp:
        # Routine re-auth path, distinct from binding failure.
        raise BindingMismatchError(
            f"token for {token.subject} expired; routine re-auth required"
        )

    if fresh_quote_fingerprint is None:
        raise BindingMismatchError(
            f"presenter {token.subject} produced no fresh hardware signature; "
            "token binding is unverifiable -> treat as unauthenticated claim"
        )

    if fresh_quote_fingerprint != token.attestation_ref_key_fingerprint:
        raise BindingMismatchError(
            f"binding mismatch for {token.subject}: token names key "
            f"{token.attestation_ref_key_fingerprint!r} but live root is "
            f"{fresh_quote_fingerprint!r}; possible silent re-key or "
            "principal substitution -> quarantine accumulated state"
        )
    # Binding confirmed: token is a live attestation, admit.
