Skip to main content

ppoppo_sdk_core/audit/
sink.rs

1//! `AuditSink` port + `AuditEvent` value type + ship-with adapters
2//! (`NoopAuditSink`, `MemoryAuditSink`).
3//!
4//! See module-level docs in `audit/mod.rs` for the deep-module rationale
5//! (β1 + composition + non-blocking failure-mode contract).
6
7use std::collections::BTreeMap;
8#[cfg(any(test, feature = "test-support"))]
9use std::sync::{Arc, Mutex};
10
11use async_trait::async_trait;
12use serde::{Deserialize, Serialize};
13use time::OffsetDateTime;
14
15use super::RateLimitKey;
16
17/// Audit emission port for verify-failure events (M48).
18///
19/// **One method, no `Result`**: M48 is observability, NOT auth-flow
20/// critical. Adapters that fail to persist MUST log internally via
21/// `tracing::error!` and continue — the verify hot path NEVER bubbles
22/// audit failures into the auth contract. The trait surface enforces
23/// this by giving the caller no error to propagate in the first place.
24///
25/// **`&self` not `&mut self`**: a single verifier emits concurrently
26/// across many request handlers; interior mutability lives inside each
27/// adapter (e.g. [`MemoryAuditSink`] uses `Mutex`).
28///
29/// **`Send + Sync` bounds**: required for `Arc<dyn AuditSink>` use in
30/// Axum handlers and tokio tasks.
31#[async_trait]
32pub trait AuditSink: std::fmt::Debug + Send + Sync {
33    async fn record_failure(&self, event: AuditEvent);
34}
35
36/// Single typed event emitted on every `BearerVerifier::verify` rejection.
37///
38/// `kind` drives audit-pivot grouping; `source_id` drives rate-limiting
39/// and per-source dashboards; `metadata` carries free-form context
40/// (engine M-row identifier for `Other`, claim names, etc.).
41///
42/// **Best-effort hint decoding**: `client_id_hint` and `kid_hint` come
43/// from the rejected token's payload/header via defensive base64+JSON
44/// parse. Either may be `None` if the token was malformed; callers MUST
45/// NOT treat absence as a security signal — by definition the token
46/// was rejected, so its claims are untrusted. The hints exist for
47/// grouping, not authentication.
48///
49/// **`source_id` derivation**: per Phase 9 design call (e), source_id
50/// is the compound `client_id_hint ‖ kid_hint` key. Anonymous /
51/// kid-less rejections collapse into a canonical `"anon::nokid"`
52/// bucket so attacker-controlled token mangling can't explode the
53/// bucket count. See [`compose_source_id`] and [`AuditEvent::from_hints`].
54///
55/// All fields are `pub` so adapters can serialize them or pivot on
56/// arbitrary subsets. The canonical construction path is
57/// [`AuditEvent::from_hints`], which guarantees `source_id` matches
58/// the hints. Hand-constructing with mismatched values is technically
59/// possible (and useful for fault-injection in tests) but a code
60/// review concern in production.
61#[derive(Debug, Clone, Serialize, Deserialize)]
62pub struct AuditEvent {
63    /// Failure classification — drives audit-pivot grouping.
64    pub kind: VerifyErrorKind,
65    /// Wall-clock at engine reject (UTC, RFC 3339 wire format).
66    #[serde(with = "time::serde::rfc3339")]
67    pub occurred_at: OffsetDateTime,
68    /// Compound `client_id_hint ‖ kid_hint` key for rate-limiting +
69    /// per-source pivot.
70    pub source_id: String,
71    /// Best-effort `client_id` claim from the rejected token's payload.
72    pub client_id_hint: Option<String>,
73    /// Best-effort `kid` from the rejected token's header.
74    pub kid_hint: Option<String>,
75    /// Free-form structured context — engine M-row identifier for
76    /// `Other`, claim names for telemetry, etc. `BTreeMap` (not
77    /// `HashMap`) for deterministic ordering in snapshot tests.
78    pub metadata: BTreeMap<String, serde_json::Value>,
79}
80
81impl AuditEvent {
82    /// Canonical constructor — composes `source_id` from the hints so
83    /// the two never disagree. Production callers (Phase 9.D
84    /// `PasJwtVerifier::verify`) use this.
85    #[must_use]
86    pub fn from_hints(
87        kind: VerifyErrorKind,
88        occurred_at: OffsetDateTime,
89        client_id_hint: Option<String>,
90        kid_hint: Option<String>,
91        metadata: BTreeMap<String, serde_json::Value>,
92    ) -> Self {
93        let source_id = compose_source_id(client_id_hint.as_deref(), kid_hint.as_deref());
94        Self {
95            kind,
96            occurred_at,
97            source_id,
98            client_id_hint,
99            kid_hint,
100            metadata,
101        }
102    }
103
104    /// id_token-specific canonical constructor (Phase 10.11.D, δ2).
105    ///
106    /// Composes the 3-tuple `azp ‖ aud ‖ kid` source key — strongest
107    /// per-source discrimination for log-flood DoS prevention on the
108    /// RP side. azp (when present) is the canonical "authorized party";
109    /// aud may be array (the engine surfaces only the first element to
110    /// the hint pipeline); kid identifies the signing key.
111    ///
112    /// **Field repurpose**: stores `azp_hint` in
113    /// [`Self::client_id_hint`] (the SDK-shaped "authorized party"
114    /// shares semantic with access-token's `client_id`); pushes
115    /// `aud_hint` into `metadata` under the key `"aud_hint"`. Dashboard
116    /// pivots on the access-token side use `client_id_hint` directly;
117    /// id_token pivots use the same field plus the `aud_hint` metadata
118    /// entry.
119    ///
120    /// Production caller: [`crate::oidc::PasIdTokenVerifier::emit_failure`].
121    #[must_use]
122    pub fn from_id_token_hints(
123        kind: VerifyErrorKind,
124        occurred_at: OffsetDateTime,
125        azp_hint: Option<String>,
126        aud_hint: Option<String>,
127        kid_hint: Option<String>,
128        mut metadata: BTreeMap<String, serde_json::Value>,
129    ) -> Self {
130        let source_id = compose_id_token_source_id(
131            azp_hint.as_deref(),
132            aud_hint.as_deref(),
133            kid_hint.as_deref(),
134        );
135        if let Some(aud) = &aud_hint {
136            metadata.insert(
137                "aud_hint".to_owned(),
138                serde_json::Value::String(aud.clone()),
139            );
140        }
141        Self {
142            kind,
143            occurred_at,
144            source_id,
145            client_id_hint: azp_hint,
146            kid_hint,
147            metadata,
148        }
149    }
150
151    /// Per-bucket rate-limit key. By default 1:1 with `source_id`.
152    /// Composing once at construction keeps this O(1).
153    #[must_use]
154    pub fn rate_limit_key(&self) -> RateLimitKey {
155        RateLimitKey::new(self.source_id.clone())
156    }
157}
158
159/// Compose a Phase 9 (e) compound source key from optional hints.
160///
161/// Free function (not a method) because it is referentially transparent:
162/// same inputs → same output, no Self needed. `PasJwtVerifier::verify`
163/// (Phase 9.D) calls this to derive the `source_id` field from a
164/// best-effort token decode; tests use it to construct expected keys
165/// without building an `AuditEvent` first.
166///
167/// Sentinels `"anon"` (missing client_id) and `"nokid"` (missing kid)
168/// collapse anonymous rejections into a single bucket so attacker-
169/// controlled token mangling can't explode bucket count. The `"::"`
170/// separator is a deliberate choice for grep-friendliness ("all
171/// anon::*" surfaces every anonymous bucket as a glob).
172#[must_use]
173pub fn compose_source_id(client_id_hint: Option<&str>, kid_hint: Option<&str>) -> String {
174    let cid = client_id_hint.unwrap_or("anon");
175    let kid = kid_hint.unwrap_or("nokid");
176    format!("{cid}::{kid}")
177}
178
179/// Phase 10.11.D δ2 — id_token compound source key from `azp ‖ aud ‖ kid`.
180///
181/// Sibling of [`compose_source_id`] for the RP-side id_token verify
182/// pipeline. Three components give strongest discrimination for
183/// log-flood DoS prevention: the canonical authorized party (`azp`),
184/// the audience (`aud`, first element when array), and the signing key
185/// (`kid`). Sentinels `"anon"` / `"noaud"` / `"nokid"` collapse
186/// anonymous rejections into canonical buckets.
187///
188/// Used by [`AuditEvent::from_id_token_hints`] to derive `source_id`;
189/// boundary tests reference it to construct expected keys without
190/// building a full event first.
191#[must_use]
192pub fn compose_id_token_source_id(
193    azp_hint: Option<&str>,
194    aud_hint: Option<&str>,
195    kid_hint: Option<&str>,
196) -> String {
197    let azp = azp_hint.unwrap_or("anon");
198    let aud = aud_hint.unwrap_or("noaud");
199    let kid = kid_hint.unwrap_or("nokid");
200    format!("{azp}::{aud}::{kid}")
201}
202
203/// Failure classification — mirrors the
204/// [`VerifyError`](crate::token::VerifyError) and
205/// [`IdVerifyError`](crate::oidc::IdVerifyError) surfaces but lives at
206/// the audit layer.
207///
208/// Stable, low-cardinality enum suitable for audit-dashboard pivots.
209/// `MissingClaim` carries the claim name as `String` (~6 well-known
210/// claims in production: `"aud"`, `"iat"`, `"jti"`, `"sub"`, `"exp"`,
211/// `"client_id"`) — still low-cardinality at the dashboard layer.
212/// The String allocation (vs `&'static str` in
213/// [`VerifyError::MissingClaim`](crate::token::VerifyError::MissingClaim))
214/// is the cost of full serde round-trip support; per-event overhead
215/// is negligible on the rare-failure emission path.
216///
217/// `Other` is a flat catch-all — engine M-row identifier goes into
218/// [`AuditEvent::metadata`] under the key `"engine_msg"` to keep this
219/// enum bounded.
220///
221/// ── id_token nesting (Phase 10.11.B) ───────────────────────────────────
222///
223/// OIDC-specific failure rows (M66-M73 + M29-mirror) live inside
224/// [`IdTokenFailureKind`] under a single nested variant `IdToken(_)`.
225/// The audit-pivot pattern callers use:
226///
227/// ```ignore
228/// match event.kind {
229///     VerifyErrorKind::IdToken(_) => bucket_for("id_token failures"),
230///     VerifyErrorKind::IdTokenAsBearer => bucket_for("M73 misuse"),
231///     VerifyErrorKind::Expired => bucket_for("token expired"),
232///     // ...
233/// }
234/// ```
235///
236/// A single `IdToken(_)` arm filters the entire OIDC failure family —
237/// dashboards do not need to enumerate 14 names. Profile-agnostic JOSE
238/// failures (`Expired`, `IssuerInvalid`, `AudienceInvalid`,
239/// `MissingClaim`, `SignatureInvalid`, `KeysetUnavailable`,
240/// `InvalidFormat`) reuse the existing flat variants — they are shared
241/// between access_token and id_token verify surfaces, and forcing
242/// consumers to write a 2nd match arm for "id_token expired"
243/// distinct from "access_token expired" would noise the call site
244/// without changing the operator action ("token expired, refresh").
245/// The audit log distinguishes the two via [`AuditEvent::source_id`]
246/// composition (compound `azp ‖ aud ‖ kid` for id_token vs
247/// `client_id ‖ kid` for access).
248#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
249#[serde(tag = "tag", content = "claim")]
250pub enum VerifyErrorKind {
251    InvalidFormat,
252    SignatureInvalid,
253    Expired,
254    IssuerInvalid,
255    AudienceInvalid,
256    MissingClaim(String),
257    KeysetUnavailable,
258    IdTokenAsBearer,
259    /// Phase 10.11.B — nested OIDC-specific failure family. See
260    /// [`IdTokenFailureKind`].
261    IdToken(IdTokenFailureKind),
262    /// Phase 11.Z — token's `sv` claim below authoritative substrate
263    /// (engine `check_epoch` reject). Distinct from `Expired`: stale
264    /// is revocation-driven, expired is `exp`-bound. Audit dashboards
265    /// pivot on this kind to surface break-glass propagation lag.
266    SessionVersionStale,
267    /// Phase 11.Z — engine `check_epoch` could not reach its substrate
268    /// (cache miss + fetcher transient). Fail-closed surface; audit
269    /// dashboards pivot on this kind to surface substrate health
270    /// problems distinct from cryptographic failure.
271    SessionVersionLookupUnavailable,
272    /// Phase 11.Z 0.10.0 (RFC_2026-05-08 §4.2) — L2 session liveness
273    /// reject. Token's `sid` resolved to a row absent OR with
274    /// `revoked_at` set. Distinct from `SessionVersionStale` (L1):
275    /// L2 = consumer-DB row revocation; L1 = cross-service break-glass
276    /// propagation. Pivots help operators distinguish per-session
277    /// logout traffic from cluster-wide break-glass bumps.
278    SessionRevoked,
279    /// Phase 11.Z 0.10.0 — L2 session liveness substrate unreachable.
280    /// Fail-closed surface; audit dashboards pivot on this kind to
281    /// surface consumer-DB substrate health problems distinct from
282    /// `SessionVersionLookupUnavailable` (L1 substrate health).
283    SessionLivenessLookupUnavailable,
284    Other,
285}
286
287/// id_token-specific failure classification (Phase 10.11.B).
288///
289/// Mirrors the OIDC-specific variants of
290/// [`IdVerifyError`](crate::oidc::IdVerifyError). Nests inside
291/// [`VerifyErrorKind::IdToken`] so dashboard pivots can filter "all
292/// id_token failures" via a single match arm.
293///
294/// Variants in this enum cover M66-M73 + M29-mirror — JOSE-layer
295/// rejections (Expired, SignatureInvalid, etc.) are *shared* with
296/// access_token and remain on the outer [`VerifyErrorKind`] flat
297/// variants. The boundary mapping (`From<&IdVerifyError> for
298/// VerifyErrorKind`, lands in Phase 10.11.D) routes JOSE rejections to
299/// the flat variants and id_token-specific rejections to
300/// `IdToken(IdTokenFailureKind::*)`.
301///
302/// Two payload-carrying variants:
303///
304/// - `UnknownClaim(String)` — engine M72 carries the offending claim
305///   name (e.g. `"backdoor"`, `"email"` at Openid scope). Audit logs
306///   distinguish forgery (random key) from issuer drift (legitimate
307///   OIDC claim outside scope) by reading the inner string.
308/// - `CatMismatch(String)` — engine M29-mirror carries the offending
309///   `cat` value (e.g. `"access"` for an attacker presenting a forged
310///   id_token; `""` for a stripped-claims forgery; arbitrary string
311///   for bespoke forgery).
312#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
313pub enum IdTokenFailureKind {
314    /// M66 — `nonce` claim absent.
315    NonceMissing,
316    /// M66 — `nonce` claim present but does not match expected.
317    NonceMismatch,
318    /// M67 — `at_hash` claim absent while access_token binding is configured.
319    AtHashMissing,
320    /// M67 — `at_hash` claim present but does not match.
321    AtHashMismatch,
322    /// M68 — `c_hash` claim absent while authorization_code binding is configured.
323    CHashMissing,
324    /// M68 — `c_hash` claim present but does not match.
325    CHashMismatch,
326    /// M69 — `azp` claim absent on multi-aud id_token.
327    AzpMissing,
328    /// M69 — `azp` claim present but does not equal expected client_id.
329    AzpMismatch,
330    /// M70 — `auth_time` claim absent while max_age is configured.
331    AuthTimeMissing,
332    /// M70 — `now - auth_time > max_age`.
333    AuthTimeStale,
334    /// M71 — `acr` claim absent while acr_values is configured.
335    AcrMissing,
336    /// M71 — `acr` claim present but not in allowlist.
337    AcrNotAllowed,
338    /// M72 — claim name outside per-scope allowlist. Carries name.
339    UnknownClaim(String),
340    /// M29-mirror — `cat` claim value is not `"id"`. Carries the
341    /// offending value.
342    CatMismatch(String),
343}
344
345/// Default sink — explicitly does nothing.
346///
347/// Used when a consumer constructs `PasJwtVerifier::from_jwks_url`
348/// without calling `with_audit`. Named "Noop" (not "Empty" / "Null")
349/// so call sites read as an explicit choice. Cheap to clone; carries
350/// no state.
351#[derive(Debug, Default, Clone)]
352pub struct NoopAuditSink;
353
354#[async_trait]
355impl AuditSink for NoopAuditSink {
356    async fn record_failure(&self, _event: AuditEvent) {}
357}
358
359/// Test-support sink — accumulates events in insertion order behind a
360/// `Mutex`.
361///
362/// Test code calls [`MemoryAuditSink::events`] to assert ordering and
363/// contents. The `Arc<Mutex<Vec<...>>>` shape is deliberate:
364/// [`AuditSink::record_failure`] takes `&self`, so interior mutability
365/// is required; `Mutex` (rather than `RwLock`) is right because every
366/// access is a mutation.
367///
368/// Uses `std::sync::Mutex` rather than `tokio::sync::Mutex` so the
369/// adapter compiles unconditionally — no tokio runtime required when
370/// downstream consumers enable just `test-support` without `oauth` /
371/// `well-known-fetch`. This is safe because no `.await` happens while
372/// the lock is held; the future stays `Send`.
373#[cfg(any(test, feature = "test-support"))]
374#[derive(Debug, Default, Clone)]
375pub struct MemoryAuditSink {
376    events: Arc<Mutex<Vec<AuditEvent>>>,
377}
378
379#[cfg(any(test, feature = "test-support"))]
380impl MemoryAuditSink {
381    #[must_use]
382    pub fn new() -> Self {
383        Self::default()
384    }
385
386    /// Snapshot the recorded events. Returns a clone so the caller
387    /// doesn't hold the lock during assertion logic. `AuditEvent` is
388    /// shallow-cloneable; copying the whole vec is cheap for test
389    /// volumes.
390    pub fn events(&self) -> Vec<AuditEvent> {
391        self.events
392            .lock()
393            .unwrap_or_else(|poisoned| poisoned.into_inner())
394            .clone()
395    }
396
397    /// Convenience: count without cloning the vec.
398    pub fn len(&self) -> usize {
399        self.events
400            .lock()
401            .unwrap_or_else(|poisoned| poisoned.into_inner())
402            .len()
403    }
404
405    pub fn is_empty(&self) -> bool {
406        self.len() == 0
407    }
408}
409
410#[cfg(any(test, feature = "test-support"))]
411#[async_trait]
412impl AuditSink for MemoryAuditSink {
413    async fn record_failure(&self, event: AuditEvent) {
414        let mut events = self
415            .events
416            .lock()
417            .unwrap_or_else(|poisoned| poisoned.into_inner());
418        events.push(event);
419    }
420}
421
422#[cfg(test)]
423mod tests {
424    use super::*;
425
426    fn fixture(kind: VerifyErrorKind, source_id: &str) -> AuditEvent {
427        AuditEvent {
428            kind,
429            occurred_at: OffsetDateTime::UNIX_EPOCH,
430            source_id: source_id.to_owned(),
431            client_id_hint: None,
432            kid_hint: None,
433            metadata: BTreeMap::new(),
434        }
435    }
436
437    #[test]
438    fn compose_source_id_uses_compound_separator() {
439        assert_eq!(
440            compose_source_id(Some("rcw-client"), Some("k1")),
441            "rcw-client::k1"
442        );
443    }
444
445    #[test]
446    fn compose_source_id_collapses_anonymous_into_canonical_bucket() {
447        // Phase 9 (e): missing client_id and missing kid both collapse
448        // to canonical sentinels so attacker-controlled token mangling
449        // can't explode the bucket count.
450        assert_eq!(compose_source_id(None, None), "anon::nokid");
451    }
452
453    #[test]
454    fn compose_id_token_source_id_uses_three_tuple_separator() {
455        // δ2 — `azp ‖ aud ‖ kid`. Three-component key gives strongest
456        // discrimination for RP-side id_token DoS prevention.
457        assert_eq!(
458            compose_id_token_source_id(Some("rp-id"), Some("rp-aud"), Some("k1")),
459            "rp-id::rp-aud::k1"
460        );
461    }
462
463    #[test]
464    fn compose_id_token_source_id_collapses_anonymous_into_canonical() {
465        assert_eq!(
466            compose_id_token_source_id(None, None, None),
467            "anon::noaud::nokid"
468        );
469    }
470
471    #[test]
472    fn compose_id_token_source_id_partial_anonymity() {
473        // azp absent but aud present — common when the IdP omits azp
474        // on a single-aud id_token (OIDC §2 SHOULD, not MUST).
475        assert_eq!(
476            compose_id_token_source_id(None, Some("rp-aud"), Some("k1")),
477            "anon::rp-aud::k1"
478        );
479        // azp present, aud absent (multi-aud token where the engine
480        // failed to surface either element) — degenerate but possible.
481        assert_eq!(
482            compose_id_token_source_id(Some("rp-id"), None, Some("k1")),
483            "rp-id::noaud::k1"
484        );
485    }
486
487    #[test]
488    fn from_id_token_hints_derives_three_tuple_source_id_and_pushes_aud_into_metadata() {
489        let event = AuditEvent::from_id_token_hints(
490            VerifyErrorKind::IdToken(IdTokenFailureKind::NonceMismatch),
491            OffsetDateTime::UNIX_EPOCH,
492            Some("rp-id".to_owned()),
493            Some("rp-aud".to_owned()),
494            Some("k1".to_owned()),
495            BTreeMap::new(),
496        );
497        assert_eq!(event.source_id, "rp-id::rp-aud::k1");
498        assert_eq!(event.client_id_hint.as_deref(), Some("rp-id"));
499        assert_eq!(event.kid_hint.as_deref(), Some("k1"));
500        assert_eq!(
501            event
502                .metadata
503                .get("aud_hint")
504                .and_then(|v| v.as_str()),
505            Some("rp-aud")
506        );
507    }
508
509    #[test]
510    fn compose_source_id_partial_anonymity() {
511        assert_eq!(compose_source_id(None, Some("k1")), "anon::k1");
512        assert_eq!(compose_source_id(Some("rcw"), None), "rcw::nokid");
513    }
514
515    #[test]
516    fn from_hints_derives_source_id_from_hints() {
517        let event = AuditEvent::from_hints(
518            VerifyErrorKind::SignatureInvalid,
519            OffsetDateTime::UNIX_EPOCH,
520            Some("rcw".to_owned()),
521            Some("k1".to_owned()),
522            BTreeMap::new(),
523        );
524        assert_eq!(event.source_id, "rcw::k1");
525        assert_eq!(event.client_id_hint.as_deref(), Some("rcw"));
526        assert_eq!(event.kid_hint.as_deref(), Some("k1"));
527    }
528
529    #[test]
530    fn rate_limit_key_round_trip() {
531        let event = fixture(VerifyErrorKind::SignatureInvalid, "rcw::k1");
532        assert_eq!(event.rate_limit_key().as_str(), "rcw::k1");
533    }
534
535    #[test]
536    #[allow(clippy::expect_used)]
537    fn verify_error_kind_round_trips_through_serde() {
538        let kind = VerifyErrorKind::MissingClaim("aud".to_owned());
539        let json = serde_json::to_string(&kind).expect("serialize");
540        let back: VerifyErrorKind = serde_json::from_str(&json).expect("deserialize");
541        assert_eq!(kind, back);
542    }
543
544    #[test]
545    #[allow(clippy::expect_used)]
546    fn verify_error_kind_id_token_variants_round_trip_through_serde() {
547        // Unit nested variant — wire shape:
548        // {"tag": "IdToken", "claim": "NonceMissing"}.
549        let unit = VerifyErrorKind::IdToken(IdTokenFailureKind::NonceMissing);
550        let json = serde_json::to_string(&unit).expect("serialize unit");
551        let back: VerifyErrorKind = serde_json::from_str(&json).expect("deserialize unit");
552        assert_eq!(unit, back);
553
554        // Payloaded nested variant — wire shape:
555        // {"tag": "IdToken", "claim": {"UnknownClaim": "backdoor"}}.
556        let payload = VerifyErrorKind::IdToken(IdTokenFailureKind::UnknownClaim(
557            "backdoor".to_owned(),
558        ));
559        let json = serde_json::to_string(&payload).expect("serialize payload");
560        let back: VerifyErrorKind = serde_json::from_str(&json).expect("deserialize payload");
561        assert_eq!(payload, back);
562
563        // CatMismatch with empty value — engine signals stripped-claims
564        // forgery this way; round-trip must preserve the empty string.
565        let cat = VerifyErrorKind::IdToken(IdTokenFailureKind::CatMismatch(String::new()));
566        let json = serde_json::to_string(&cat).expect("serialize cat");
567        let back: VerifyErrorKind = serde_json::from_str(&json).expect("deserialize cat");
568        assert_eq!(cat, back);
569    }
570
571    #[tokio::test]
572    async fn noop_sink_is_a_no_op() {
573        let sink = NoopAuditSink;
574        let event = fixture(VerifyErrorKind::Expired, "x");
575        // Contract: returns without panic. Nothing observable to assert.
576        sink.record_failure(event).await;
577    }
578
579    #[tokio::test]
580    async fn memory_sink_records_events_in_insertion_order() {
581        let sink = MemoryAuditSink::new();
582        sink.record_failure(fixture(VerifyErrorKind::Expired, "a"))
583            .await;
584        sink.record_failure(fixture(VerifyErrorKind::SignatureInvalid, "b"))
585            .await;
586        sink.record_failure(fixture(VerifyErrorKind::IdTokenAsBearer, "c"))
587            .await;
588
589        let events = sink.events();
590        assert_eq!(events.len(), 3);
591        assert_eq!(events[0].kind, VerifyErrorKind::Expired);
592        assert_eq!(events[1].kind, VerifyErrorKind::SignatureInvalid);
593        assert_eq!(events[2].kind, VerifyErrorKind::IdTokenAsBearer);
594        assert_eq!(events[0].source_id, "a");
595    }
596
597    #[tokio::test]
598    async fn memory_sink_is_empty_initially() {
599        let sink = MemoryAuditSink::new();
600        assert!(sink.is_empty());
601        sink.record_failure(fixture(VerifyErrorKind::Other, "x"))
602            .await;
603        assert_eq!(sink.len(), 1);
604    }
605
606    /// Compile-time guard: MemoryAuditSink + NoopAuditSink must be
607    /// usable behind `Arc<dyn AuditSink>`. If a future change adds a
608    /// generic method to `AuditSink`, object-safety regresses and this
609    /// fails to compile.
610    #[allow(dead_code)]
611    fn dyn_object_safety() {
612        let _: Arc<dyn AuditSink> = Arc::new(NoopAuditSink);
613        let _: Arc<dyn AuditSink> = Arc::new(MemoryAuditSink::new());
614    }
615}