# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/why-does-an-instance-accept-a-federated-agent-passport-whose-credential-chain-verifies-yet-whose-subject-binding-has-silently-rotated.md
# Remnant Warrant:    https://corpusfetch.com/c/why-does-an-instance-accept-a-federated-agent-passport-whose-credential-chain-verifies-yet-whose-subject-binding-has-silently-rotated.warrant.json
# Markdown SHA-256:   8423624128f4f0a326b889f78e8cbd99dcf9a42a564946653825a0a107844f38
# Code AST SHA-256:   197916756fe335fe872509e84151c556bfb63c651de27e8dfa1daecaa579f344
# Verified Classes:   PassportBindingError, VerificationMethod, DIDDocument, Credential, Presentation
# Defensive Guards:   6 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass


class PassportBindingError(RuntimeError):
    """Raised when a federated agent passport verifies its chain but fails subject binding."""
    pass


@dataclass
class VerificationMethod:
    id: str
    deactivated: bool
    valid_from: float


@dataclass
class DIDDocument:
    did: str
    methods: dict
    resolved_at: float


@dataclass
class Credential:
    subject_id: str
    issuer: str
    issuance: float
    expiration: float


@dataclass
class Presentation:
    holder: str
    signed_by_method: str
    credentials: list
    presented_at: float


def verify_federated_passport(
    presentation: Presentation,
    resolve_did,                      # callable(did) -> DIDDocument
    max_staleness_seconds: float = 300.0,
    max_role_credential_lifetime: float = 3600.0,
) -> str:
    """
    Verify a DID-anchored agent passport with strict subject binding.

    Returns the bound agent DID on success. Raises PassportBindingError on any
    binding, staleness, or deactivation violation.
    """
    now = presentation.presented_at

    # 1. Resolve the holder's DID document with a staleness bound.
    doc: DIDDocument = resolve_did(presentation.holder)
    staleness = now - doc.resolved_at
    if staleness > max_staleness_seconds:
        raise PassportBindingError(
            f"holder DID document stale: {staleness:.1f}s > {max_staleness_seconds}s"
        )

    # 2. The signing method must exist and must not be deactivated.
    method = doc.methods.get(presentation.signed_by_method)
    if method is None:
        raise PassportBindingError(
            f"signing method {presentation.signed_by_method} not in DID document"
        )
    if method.deactivated:
        raise PassportBindingError(
            f"signing method {presentation.signed_by_method} is deactivated"
        )

    # 3. Subject binding: for agent credentials, holder must equal subject.
    for vc in presentation.credentials:
        remaining = vc.expiration - now
        if remaining <= 0:
            raise PassportBindingError(f"credential from {vc.issuer} expired")
        if vc.subject_id != presentation.holder:
            raise PassportBindingError(
                f"subject binding mismatch: holder {presentation.holder} "
                f"!= credentialSubject {vc.subject_id}"
            )
        lifetime = vc.expiration - vc.issuance
        if lifetime > max_role_credential_lifetime:
            raise PassportBindingError(
                f"role credential lifetime {lifetime:.0f}s exceeds "
                f"{max_role_credential_lifetime:.0f}s; re-issue on rotation required"
            )

    return presentation.holder
