Skip to main content

rmcp_server_kit/
auth.rs

1//! Authentication middleware for MCP servers.
2//!
3//! Supports multiple authentication methods tried in priority order:
4//! 1. mTLS client certificate (if configured and peer cert present)
5//! 2. Bearer token (API key) with Argon2id hash verification
6//!
7//! Includes per-source-IP rate limiting on authentication attempts.
8
9use std::{
10    collections::HashSet,
11    net::{IpAddr, SocketAddr},
12    num::NonZeroU32,
13    path::PathBuf,
14    sync::{
15        Arc, LazyLock, Mutex,
16        atomic::{AtomicU64, Ordering},
17    },
18    time::Duration,
19};
20
21use arc_swap::ArcSwap;
22use argon2::{Argon2, PasswordHash, PasswordHasher, PasswordVerifier, password_hash::SaltString};
23use axum::{
24    body::Body,
25    extract::ConnectInfo,
26    http::{Request, header},
27    middleware::Next,
28    response::{IntoResponse, Response},
29};
30use base64::{Engine as _, engine::general_purpose::URL_SAFE_NO_PAD};
31use secrecy::SecretString;
32use serde::Deserialize;
33use x509_parser::prelude::*;
34
35use crate::{bounded_limiter::BoundedKeyedLimiter, error::McpxError};
36
37/// Identity of an authenticated caller.
38///
39/// The [`Debug`] impl is **manually written** to redact the raw bearer token
40/// and the JWT `sub` claim. This prevents accidental disclosure if an
41/// `AuthIdentity` is ever logged via `tracing::debug!(?identity, …)` or
42/// `format!("{identity:?}")`. Only `name`, `role`, and `method` are printed
43/// in the clear; `raw_token` and `sub` are rendered as `<redacted>` /
44/// `<present>` / `<none>` markers.
45#[derive(Clone)]
46#[non_exhaustive]
47pub struct AuthIdentity {
48    /// Human-readable identity name (e.g. API key label or cert CN).
49    pub name: String,
50    /// RBAC role associated with this identity.
51    pub role: String,
52    /// Which authentication mechanism produced this identity.
53    pub method: AuthMethod,
54    /// Raw bearer token from the `Authorization` header, wrapped in
55    /// [`SecretString`] so it is never accidentally logged or serialized.
56    /// Present for OAuth JWT; `None` for mTLS and API-key auth.
57    /// Tool handlers use this for downstream token passthrough via
58    /// [`crate::rbac::current_token`].
59    pub raw_token: Option<SecretString>,
60    /// JWT `sub` claim (stable user identifier, e.g. Keycloak UUID).
61    /// Used for token store keying. `None` for non-JWT auth.
62    pub sub: Option<String>,
63}
64
65impl std::fmt::Debug for AuthIdentity {
66    /// Redacts `raw_token` and `sub` to prevent secret leakage via
67    /// `format!("{:?}")` or `tracing::debug!(?identity)`.
68    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
69        f.debug_struct("AuthIdentity")
70            .field("name", &self.name)
71            .field("role", &self.role)
72            .field("method", &self.method)
73            .field(
74                "raw_token",
75                &if self.raw_token.is_some() {
76                    "<redacted>"
77                } else {
78                    "<none>"
79                },
80            )
81            .field(
82                "sub",
83                &if self.sub.is_some() {
84                    "<redacted>"
85                } else {
86                    "<none>"
87                },
88            )
89            .finish()
90    }
91}
92
93/// How the caller authenticated.
94#[derive(Debug, Clone, Copy, PartialEq, Eq)]
95#[non_exhaustive]
96pub enum AuthMethod {
97    /// Bearer API key (Argon2id-hashed, configured statically).
98    BearerToken,
99    /// Mutual TLS client certificate.
100    MtlsCertificate,
101    /// OAuth 2.1 JWT bearer token (validated via JWKS).
102    OAuthJwt,
103}
104
105#[derive(Debug, Clone, Copy, PartialEq, Eq)]
106enum AuthFailureClass {
107    MissingCredential,
108    InvalidCredential,
109    #[cfg_attr(not(feature = "oauth"), allow(dead_code))]
110    ExpiredCredential,
111    /// Source IP exceeded the post-failure backoff limit.
112    RateLimited,
113    /// Source IP exceeded the pre-auth abuse gate (rejected before any
114    /// password-hash work — see [`AuthState::pre_auth_limiter`]).
115    PreAuthGate,
116}
117
118impl AuthFailureClass {
119    fn as_str(self) -> &'static str {
120        match self {
121            Self::MissingCredential => "missing_credential",
122            Self::InvalidCredential => "invalid_credential",
123            Self::ExpiredCredential => "expired_credential",
124            Self::RateLimited => "rate_limited",
125            Self::PreAuthGate => "pre_auth_gate",
126        }
127    }
128
129    fn bearer_error(self) -> (&'static str, &'static str) {
130        match self {
131            Self::MissingCredential => (
132                "invalid_request",
133                "missing bearer token or mTLS client certificate",
134            ),
135            Self::InvalidCredential => ("invalid_token", "token is invalid"),
136            Self::ExpiredCredential => ("invalid_token", "token is expired"),
137            Self::RateLimited => ("invalid_request", "too many failed authentication attempts"),
138            Self::PreAuthGate => (
139                "invalid_request",
140                "too many unauthenticated requests from this source",
141            ),
142        }
143    }
144
145    fn response_body(self) -> &'static str {
146        match self {
147            Self::MissingCredential => "unauthorized: missing credential",
148            Self::InvalidCredential => "unauthorized: invalid credential",
149            Self::ExpiredCredential => "unauthorized: expired credential",
150            Self::RateLimited => "rate limited",
151            Self::PreAuthGate => "rate limited (pre-auth)",
152        }
153    }
154}
155
156/// Snapshot of authentication success/failure counters.
157#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize)]
158#[non_exhaustive]
159pub struct AuthCountersSnapshot {
160    /// Successful mTLS authentications.
161    pub success_mtls: u64,
162    /// Successful bearer-token authentications.
163    pub success_bearer: u64,
164    /// Successful OAuth JWT authentications.
165    pub success_oauth_jwt: u64,
166    /// Failures because no credential was presented.
167    pub failure_missing_credential: u64,
168    /// Failures because the credential was malformed or wrong.
169    pub failure_invalid_credential: u64,
170    /// Failures because the credential had expired.
171    pub failure_expired_credential: u64,
172    /// Failures because the source IP was rate-limited (post-failure backoff).
173    pub failure_rate_limited: u64,
174    /// Failures because the source IP exceeded the pre-auth abuse gate.
175    /// These never reach the password-hash verification path.
176    pub failure_pre_auth_gate: u64,
177}
178
179/// Internal atomic counters backing [`AuthCountersSnapshot`].
180#[derive(Debug, Default)]
181pub(crate) struct AuthCounters {
182    success_mtls: AtomicU64,
183    success_bearer: AtomicU64,
184    success_oauth_jwt: AtomicU64,
185    failure_missing_credential: AtomicU64,
186    failure_invalid_credential: AtomicU64,
187    failure_expired_credential: AtomicU64,
188    failure_rate_limited: AtomicU64,
189    failure_pre_auth_gate: AtomicU64,
190}
191
192impl AuthCounters {
193    fn record_success(&self, method: AuthMethod) {
194        match method {
195            AuthMethod::MtlsCertificate => {
196                self.success_mtls.fetch_add(1, Ordering::Relaxed);
197            }
198            AuthMethod::BearerToken => {
199                self.success_bearer.fetch_add(1, Ordering::Relaxed);
200            }
201            AuthMethod::OAuthJwt => {
202                self.success_oauth_jwt.fetch_add(1, Ordering::Relaxed);
203            }
204        }
205    }
206
207    fn record_failure(&self, class: AuthFailureClass) {
208        match class {
209            AuthFailureClass::MissingCredential => {
210                self.failure_missing_credential
211                    .fetch_add(1, Ordering::Relaxed);
212            }
213            AuthFailureClass::InvalidCredential => {
214                self.failure_invalid_credential
215                    .fetch_add(1, Ordering::Relaxed);
216            }
217            AuthFailureClass::ExpiredCredential => {
218                self.failure_expired_credential
219                    .fetch_add(1, Ordering::Relaxed);
220            }
221            AuthFailureClass::RateLimited => {
222                self.failure_rate_limited.fetch_add(1, Ordering::Relaxed);
223            }
224            AuthFailureClass::PreAuthGate => {
225                self.failure_pre_auth_gate.fetch_add(1, Ordering::Relaxed);
226            }
227        }
228    }
229
230    fn snapshot(&self) -> AuthCountersSnapshot {
231        AuthCountersSnapshot {
232            success_mtls: self.success_mtls.load(Ordering::Relaxed),
233            success_bearer: self.success_bearer.load(Ordering::Relaxed),
234            success_oauth_jwt: self.success_oauth_jwt.load(Ordering::Relaxed),
235            failure_missing_credential: self.failure_missing_credential.load(Ordering::Relaxed),
236            failure_invalid_credential: self.failure_invalid_credential.load(Ordering::Relaxed),
237            failure_expired_credential: self.failure_expired_credential.load(Ordering::Relaxed),
238            failure_rate_limited: self.failure_rate_limited.load(Ordering::Relaxed),
239            failure_pre_auth_gate: self.failure_pre_auth_gate.load(Ordering::Relaxed),
240        }
241    }
242}
243
244/// RFC 3339 timestamp, parsed at deserialization time.
245///
246/// Use this for any public field that needs to carry an RFC 3339 timestamp from
247/// TOML/JSON config or builder APIs. Construction is fallible (`parse`); once
248/// constructed the value is guaranteed to be a real RFC 3339 timestamp with a
249/// known offset, so downstream code does not need to handle parse errors.
250///
251/// Wraps [`chrono::DateTime<chrono::FixedOffset>`]; the underlying value is
252/// available via [`Self::as_datetime`] or [`Self::into_inner`]. `Serialize`
253/// emits the canonical RFC 3339 form via [`chrono::DateTime::to_rfc3339`], so
254/// the on-the-wire format for `ApiKeySummary` (admin endpoints) is unchanged.
255#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
256#[non_exhaustive]
257pub struct RfcTimestamp(chrono::DateTime<chrono::FixedOffset>);
258
259impl RfcTimestamp {
260    /// Parse an RFC 3339 timestamp.
261    ///
262    /// # Errors
263    ///
264    /// Returns the underlying [`chrono::ParseError`] when `s` is not a valid
265    /// RFC 3339 timestamp (e.g. missing the `T` separator, missing the offset
266    /// suffix, or out-of-range fields).
267    pub fn parse(s: &str) -> Result<Self, chrono::ParseError> {
268        chrono::DateTime::parse_from_rfc3339(s).map(Self)
269    }
270
271    /// Borrow the underlying [`chrono::DateTime`].
272    #[must_use]
273    pub fn as_datetime(&self) -> &chrono::DateTime<chrono::FixedOffset> {
274        &self.0
275    }
276
277    /// Consume the wrapper and return the underlying [`chrono::DateTime`].
278    #[must_use]
279    pub fn into_inner(self) -> chrono::DateTime<chrono::FixedOffset> {
280        self.0
281    }
282}
283
284impl std::fmt::Display for RfcTimestamp {
285    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
286        // Canonical RFC 3339 form; matches the deserialization input contract.
287        write!(f, "{}", self.0.to_rfc3339())
288    }
289}
290
291impl std::fmt::Debug for RfcTimestamp {
292    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
293        // Render as the canonical RFC 3339 string (not chrono's internal
294        // debug form) so existing `ApiKeyEntry` Debug-redaction tests --
295        // which look for the literal `"2030-01-01T00:00:00Z"` form in the
296        // formatted output -- continue to hold without bespoke handling.
297        write!(f, "{}", self.0.to_rfc3339())
298    }
299}
300
301impl<'de> Deserialize<'de> for RfcTimestamp {
302    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
303    where
304        D: serde::Deserializer<'de>,
305    {
306        // Validate at deserialization time: a malformed `expires_at` in
307        // TOML or JSON aborts config load with a clear serde error rather
308        // than silently producing a key that fails open at runtime.
309        let s = String::deserialize(deserializer)?;
310        Self::parse(&s).map_err(serde::de::Error::custom)
311    }
312}
313
314impl serde::Serialize for RfcTimestamp {
315    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
316    where
317        S: serde::Serializer,
318    {
319        serializer.serialize_str(&self.0.to_rfc3339())
320    }
321}
322
323impl From<chrono::DateTime<chrono::FixedOffset>> for RfcTimestamp {
324    fn from(value: chrono::DateTime<chrono::FixedOffset>) -> Self {
325        Self(value)
326    }
327}
328
329/// A single API key entry (stored as Argon2id hash in config).
330///
331/// The [`Debug`] impl is **manually written** to redact the Argon2id hash.
332/// Although the hash is not directly reversible, treating it as a secret
333/// prevents offline brute-force attempts from leaked logs and matches the
334/// defense-in-depth posture used for [`AuthIdentity`].
335#[derive(Clone, Deserialize)]
336#[non_exhaustive]
337pub struct ApiKeyEntry {
338    /// Human-readable key label (used in logs and audit records).
339    pub name: String,
340    /// Argon2id hash of the token (PHC string format).
341    pub hash: String,
342    /// RBAC role granted when this key authenticates successfully.
343    pub role: String,
344    /// Optional expiry, parsed from an RFC 3339 string at deserialization
345    /// time. Construction from a raw string is fallible (see
346    /// [`RfcTimestamp::parse`] and [`ApiKeyEntry::try_with_expiry`]),
347    /// which guarantees `verify_bearer_token` never sees a malformed value.
348    pub expires_at: Option<RfcTimestamp>,
349}
350
351impl std::fmt::Debug for ApiKeyEntry {
352    /// Redacts the Argon2id `hash` to keep it out of logs, panic backtraces,
353    /// and admin-endpoint responses that might `format!("{:?}", …)` an entry.
354    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
355        f.debug_struct("ApiKeyEntry")
356            .field("name", &self.name)
357            .field("hash", &"<redacted>")
358            .field("role", &self.role)
359            .field("expires_at", &self.expires_at)
360            .finish()
361    }
362}
363
364impl ApiKeyEntry {
365    /// Create a new API key entry (no expiry).
366    #[must_use]
367    pub fn new(name: impl Into<String>, hash: impl Into<String>, role: impl Into<String>) -> Self {
368        Self {
369            name: name.into(),
370            hash: hash.into(),
371            role: role.into(),
372            expires_at: None,
373        }
374    }
375
376    /// Set an RFC 3339 expiry on this key.
377    ///
378    /// Takes an already-parsed [`RfcTimestamp`]; for ergonomic construction
379    /// from a raw string see [`Self::try_with_expiry`].
380    #[must_use]
381    pub fn with_expiry(mut self, expires_at: RfcTimestamp) -> Self {
382        self.expires_at = Some(expires_at);
383        self
384    }
385
386    /// Set an RFC 3339 expiry on this key from a raw string.
387    ///
388    /// # Errors
389    ///
390    /// Returns the underlying [`chrono::ParseError`] when `expires_at` is
391    /// not a valid RFC 3339 timestamp. This is the fallible counterpart to
392    /// [`Self::with_expiry`].
393    pub fn try_with_expiry(
394        mut self,
395        expires_at: impl AsRef<str>,
396    ) -> Result<Self, chrono::ParseError> {
397        self.expires_at = Some(RfcTimestamp::parse(expires_at.as_ref())?);
398        Ok(self)
399    }
400}
401
402/// mTLS client certificate authentication configuration.
403#[derive(Debug, Clone, Deserialize)]
404#[allow(
405    clippy::struct_excessive_bools,
406    reason = "mTLS CRL behavior is intentionally configured as independent booleans"
407)]
408#[non_exhaustive]
409pub struct MtlsConfig {
410    /// Path to CA certificate(s) for verifying client certs (PEM format).
411    pub ca_cert_path: PathBuf,
412    /// If true, clients MUST present a valid certificate.
413    /// If false, client certs are optional (verified if presented).
414    #[serde(default)]
415    pub required: bool,
416    /// Default RBAC role for mTLS-authenticated clients.
417    /// The client cert CN becomes the identity name.
418    #[serde(default = "default_mtls_role")]
419    pub default_role: String,
420    /// Enable CRL-based certificate revocation checks using CDP URLs from the
421    /// configured CA chain and connecting client certificates.
422    #[serde(default = "default_true")]
423    pub crl_enabled: bool,
424    /// Optional fixed refresh interval for known CRLs. When omitted, refresh
425    /// cadence is derived from `nextUpdate` and clamped internally.
426    #[serde(default, with = "humantime_serde::option")]
427    pub crl_refresh_interval: Option<Duration>,
428    /// Timeout for individual CRL fetches.
429    #[serde(default = "default_crl_fetch_timeout", with = "humantime_serde")]
430    pub crl_fetch_timeout: Duration,
431    /// Grace window during which stale CRLs may still be used when refresh
432    /// attempts fail.
433    #[serde(default = "default_crl_stale_grace", with = "humantime_serde")]
434    pub crl_stale_grace: Duration,
435    /// When true, missing or unavailable CRLs cause revocation checks to fail
436    /// closed.
437    #[serde(default)]
438    pub crl_deny_on_unavailable: bool,
439    /// When true, apply revocation checks only to the end-entity certificate.
440    #[serde(default)]
441    pub crl_end_entity_only: bool,
442    /// Allow HTTP CRL distribution-point URLs in addition to HTTPS.
443    ///
444    /// Defaults to `true` because RFC 5280 §4.2.1.13 designates HTTP (and
445    /// LDAP) as the canonical transport for CRL distribution points.
446    /// SSRF defense for HTTP CDPs is provided by the IP-allowlist guard
447    /// (private/loopback/link-local/multicast/cloud-metadata addresses are
448    /// always rejected), redirect=none, body-size cap, and per-host
449    /// concurrency limit -- not by forcing HTTPS.
450    #[serde(default = "default_true")]
451    pub crl_allow_http: bool,
452    /// Enforce CRL expiration during certificate validation.
453    #[serde(default = "default_true")]
454    pub crl_enforce_expiration: bool,
455    /// Maximum concurrent CRL fetches across all hosts. Defense in depth
456    /// against SSRF amplification: even if many CDPs are discovered, no
457    /// more than this many fetches run in parallel. Per-host concurrency
458    /// is independently capped at 1 regardless of this value.
459    /// Default: `4`.
460    #[serde(default = "default_crl_max_concurrent_fetches")]
461    pub crl_max_concurrent_fetches: usize,
462    /// Hard cap on each CRL response body in bytes. Fetches exceeding this
463    /// are aborted mid-stream to bound memory and prevent gzip-bomb-style
464    /// amplification. Default: 5 MiB (`5 * 1024 * 1024`).
465    #[serde(default = "default_crl_max_response_bytes")]
466    pub crl_max_response_bytes: u64,
467    /// Global CDP discovery rate limit, in URLs per minute. Throttles
468    /// how many *new* CDP URLs the verifier may admit into the fetch
469    /// pipeline across the whole process, bounding asymmetric `DoS`
470    /// amplification when attacker-controlled certificates carry large
471    /// CDP lists. The limit is global (not per-source-IP) in this
472    /// release; per-IP scoping is deferred to a future version because
473    /// it requires plumbing the peer `SocketAddr` through the rustls
474    /// verifier hook (a different subsystem than ordinary request
475    /// middleware). Note: the **bearer pre-auth limiter** that gates
476    /// API-key / OAuth `Authorization` headers is already per-IP — see
477    /// [`RateLimitConfig::pre_auth_max_per_minute`] and the keyed
478    /// governor built by `build_pre_auth_limiter`. URLs that lose the
479    /// rate-limiter race are *not* marked as seen, so subsequent
480    /// handshakes observing the same URL can retry admission.
481    /// Default: `60`.
482    #[serde(default = "default_crl_discovery_rate_per_min")]
483    pub crl_discovery_rate_per_min: u32,
484    /// Maximum number of distinct hosts that may hold a CRL fetch
485    /// semaphore at any time. Requests that would grow the map beyond
486    /// this cap return [`McpxError::Config`] containing the literal
487    /// substring `"crl_host_semaphore_cap_exceeded"`. Bounds memory
488    /// growth from attacker-controlled CDP URLs pointing at unique
489    /// hostnames. Default: 1024.
490    #[serde(default = "default_crl_max_host_semaphores")]
491    pub crl_max_host_semaphores: usize,
492    /// Maximum number of distinct URLs tracked in the "seen" set.
493    /// Beyond this, additional discovered URLs are silently dropped
494    /// with a rate-limited warn! log; no error surfaces. Default: 4096.
495    #[serde(default = "default_crl_max_seen_urls")]
496    pub crl_max_seen_urls: usize,
497    /// Maximum number of cached CRL entries. Beyond this, new
498    /// successful fetches are silently dropped with a rate-limited
499    /// warn! log (newest-rejected, not LRU-evicted). Default: 1024.
500    #[serde(default = "default_crl_max_cache_entries")]
501    pub crl_max_cache_entries: usize,
502}
503
504fn default_mtls_role() -> String {
505    "viewer".into()
506}
507
508const fn default_true() -> bool {
509    true
510}
511
512const fn default_crl_fetch_timeout() -> Duration {
513    Duration::from_secs(30)
514}
515
516const fn default_crl_stale_grace() -> Duration {
517    Duration::from_hours(24)
518}
519
520const fn default_crl_max_concurrent_fetches() -> usize {
521    4
522}
523
524const fn default_crl_max_response_bytes() -> u64 {
525    5 * 1024 * 1024
526}
527
528const fn default_crl_discovery_rate_per_min() -> u32 {
529    60
530}
531
532const fn default_crl_max_host_semaphores() -> usize {
533    1024
534}
535
536const fn default_crl_max_seen_urls() -> usize {
537    4096
538}
539
540const fn default_crl_max_cache_entries() -> usize {
541    1024
542}
543
544/// Rate limiting configuration for authentication attempts.
545///
546/// rmcp-server-kit uses two independent per-IP token-bucket limiters for auth:
547///
548/// 1. **Pre-auth abuse gate** ([`Self::pre_auth_max_per_minute`]): consulted
549///    *before* any password-hash work. Throttles unauthenticated traffic from
550///    a single source IP so an attacker cannot pin the CPU on Argon2id by
551///    spraying invalid bearer tokens. Sized generously (default = 10× the
552///    post-failure quota) so legitimate clients are unaffected. mTLS-
553///    authenticated connections bypass this gate entirely (the TLS handshake
554///    already performed expensive crypto with a verified peer).
555/// 2. **Post-failure backoff** ([`Self::max_attempts_per_minute`]): consulted
556///    *after* an authentication attempt fails. Provides explicit backpressure
557///    on bad credentials.
558#[derive(Debug, Clone, Deserialize)]
559#[non_exhaustive]
560pub struct RateLimitConfig {
561    /// Maximum failed authentication attempts per source IP per minute.
562    /// Successful authentications do not consume this budget.
563    #[serde(default = "default_max_attempts")]
564    pub max_attempts_per_minute: u32,
565    /// Maximum *unauthenticated* requests per source IP per minute admitted
566    /// to the password-hash verification path. When `None`, defaults to
567    /// `max_attempts_per_minute * 10` at limiter-construction time.
568    ///
569    /// Set higher than [`Self::max_attempts_per_minute`] so honest clients
570    /// retrying with the wrong key never trip this gate; its purpose is only
571    /// to bound CPU usage under spray attacks.
572    #[serde(default)]
573    pub pre_auth_max_per_minute: Option<u32>,
574    /// Hard cap on the number of distinct source IPs tracked per limiter.
575    /// When reached, idle entries are pruned first; if still full, the
576    /// oldest (LRU) entry is evicted to make room for the new one. This
577    /// bounds memory under IP-spray attacks. Default: `10_000`.
578    #[serde(default = "default_max_tracked_keys")]
579    pub max_tracked_keys: usize,
580    /// Per-IP entries idle for longer than this are eligible for
581    /// opportunistic pruning. Default: 15 minutes.
582    #[serde(default = "default_idle_eviction", with = "humantime_serde")]
583    pub idle_eviction: Duration,
584}
585
586impl Default for RateLimitConfig {
587    fn default() -> Self {
588        Self {
589            max_attempts_per_minute: default_max_attempts(),
590            pre_auth_max_per_minute: None,
591            max_tracked_keys: default_max_tracked_keys(),
592            idle_eviction: default_idle_eviction(),
593        }
594    }
595}
596
597impl RateLimitConfig {
598    /// Create a rate limit config with the given max failed attempts per minute.
599    /// Pre-auth gate defaults to `10x` this value at limiter-construction time.
600    /// Memory-bound defaults are `10_000` tracked keys with 15-minute idle eviction.
601    #[must_use]
602    pub fn new(max_attempts_per_minute: u32) -> Self {
603        Self {
604            max_attempts_per_minute,
605            ..Self::default()
606        }
607    }
608
609    /// Override the pre-auth abuse-gate quota (per source IP per minute).
610    /// When unset, defaults to `max_attempts_per_minute * 10`.
611    #[must_use]
612    pub fn with_pre_auth_max_per_minute(mut self, quota: u32) -> Self {
613        self.pre_auth_max_per_minute = Some(quota);
614        self
615    }
616
617    /// Override the per-limiter cap on tracked source-IP keys (default `10_000`).
618    #[must_use]
619    pub fn with_max_tracked_keys(mut self, max: usize) -> Self {
620        self.max_tracked_keys = max;
621        self
622    }
623
624    /// Override the idle-eviction window (default 15 minutes).
625    #[must_use]
626    pub fn with_idle_eviction(mut self, idle: Duration) -> Self {
627        self.idle_eviction = idle;
628        self
629    }
630}
631
632fn default_max_attempts() -> u32 {
633    30
634}
635
636fn default_max_tracked_keys() -> usize {
637    10_000
638}
639
640fn default_idle_eviction() -> Duration {
641    Duration::from_mins(15)
642}
643
644/// Authentication configuration.
645#[derive(Debug, Clone, Default, Deserialize)]
646#[non_exhaustive]
647pub struct AuthConfig {
648    /// Master switch - when false, all requests are allowed through.
649    #[serde(default)]
650    pub enabled: bool,
651    /// Bearer token API keys.
652    #[serde(default)]
653    pub api_keys: Vec<ApiKeyEntry>,
654    /// mTLS client certificate authentication.
655    pub mtls: Option<MtlsConfig>,
656    /// Rate limiting for auth attempts.
657    pub rate_limit: Option<RateLimitConfig>,
658    /// OAuth 2.1 JWT bearer token authentication.
659    #[cfg(feature = "oauth")]
660    pub oauth: Option<crate::oauth::OAuthConfig>,
661}
662
663impl AuthConfig {
664    /// Create an enabled auth config with the given API keys.
665    #[must_use]
666    pub fn with_keys(keys: Vec<ApiKeyEntry>) -> Self {
667        Self {
668            enabled: true,
669            api_keys: keys,
670            mtls: None,
671            rate_limit: None,
672            #[cfg(feature = "oauth")]
673            oauth: None,
674        }
675    }
676
677    /// Set rate limiting on this auth config.
678    #[must_use]
679    pub fn with_rate_limit(mut self, rate_limit: RateLimitConfig) -> Self {
680        self.rate_limit = Some(rate_limit);
681        self
682    }
683}
684
685/// Summary of a single API key suitable for admin endpoints.
686///
687/// Intentionally omits the Argon2id hash - only metadata is exposed.
688#[derive(Debug, Clone, serde::Serialize)]
689#[non_exhaustive]
690pub struct ApiKeySummary {
691    /// Human-readable key label.
692    pub name: String,
693    /// RBAC role granted when this key authenticates.
694    pub role: String,
695    /// Optional RFC 3339 expiry timestamp. Serialized as a canonical
696    /// RFC 3339 string so the admin-endpoint wire format is preserved.
697    pub expires_at: Option<RfcTimestamp>,
698}
699
700/// Snapshot of the enabled authentication methods for admin endpoints.
701#[derive(Debug, Clone, serde::Serialize)]
702#[allow(
703    clippy::struct_excessive_bools,
704    reason = "this is a flat summary of independent auth-method booleans"
705)]
706#[non_exhaustive]
707pub struct AuthConfigSummary {
708    /// Master enabled flag from config.
709    pub enabled: bool,
710    /// Whether API-key bearer auth is configured.
711    pub bearer: bool,
712    /// Whether mTLS client auth is configured.
713    pub mtls: bool,
714    /// Whether OAuth JWT validation is configured.
715    pub oauth: bool,
716    /// Current API-key list (no hashes).
717    pub api_keys: Vec<ApiKeySummary>,
718}
719
720impl AuthConfig {
721    /// Produce a hash-free summary of the auth config for admin endpoints.
722    #[must_use]
723    pub fn summary(&self) -> AuthConfigSummary {
724        AuthConfigSummary {
725            enabled: self.enabled,
726            bearer: !self.api_keys.is_empty(),
727            mtls: self.mtls.is_some(),
728            #[cfg(feature = "oauth")]
729            oauth: self.oauth.is_some(),
730            #[cfg(not(feature = "oauth"))]
731            oauth: false,
732            api_keys: self
733                .api_keys
734                .iter()
735                .map(|k| ApiKeySummary {
736                    name: k.name.clone(),
737                    role: k.role.clone(),
738                    expires_at: k.expires_at,
739                })
740                .collect(),
741        }
742    }
743}
744
745/// Keyed rate limiter type (per source IP). Memory-bounded by
746/// [`RateLimitConfig::max_tracked_keys`] to defend against IP-spray `DoS`.
747pub(crate) type KeyedLimiter = BoundedKeyedLimiter<IpAddr>;
748
749/// Connection info for TLS connections, carrying the peer socket address
750/// and (when mTLS is configured) the verified client identity extracted
751/// from the peer certificate during the TLS handshake.
752///
753/// Defined as a local type so we can implement axum's `Connected` trait
754/// for our custom `TlsListener` without orphan rule issues. The `identity`
755/// field travels with the connection itself (via the wrapping IO type),
756/// so there is no shared map to race against, no port-reuse aliasing, and
757/// no eviction policy to maintain.
758#[derive(Clone, Debug)]
759#[non_exhaustive]
760pub(crate) struct TlsConnInfo {
761    /// Remote peer socket address.
762    pub addr: SocketAddr,
763    /// Verified mTLS client identity, if a client certificate was presented
764    /// and successfully extracted during the TLS handshake.
765    pub identity: Option<AuthIdentity>,
766}
767
768impl TlsConnInfo {
769    /// Construct a new [`TlsConnInfo`].
770    #[must_use]
771    pub(crate) const fn new(addr: SocketAddr, identity: Option<AuthIdentity>) -> Self {
772        Self { addr, identity }
773    }
774}
775
776/// Default hard cap on the number of distinct authenticated identities
777/// remembered by [`SeenIdentitySet`].
778///
779/// Sized to comfortably exceed realistic identity churn for an MCP server
780/// while bounding worst-case memory at roughly `4096 * avg_name_len`
781/// (~256 KiB at 64-byte names). Honest clients will never trigger eviction;
782/// hostile churn (rotating mTLS subjects or OAuth `sub` values) is bounded.
783const DEFAULT_SEEN_IDENTITY_CAP: usize = 4096;
784
785/// Bounded set tracking which authenticated identities have already been
786/// logged at INFO level (subsequent auths fall back to DEBUG).
787///
788/// # Why bounded?
789///
790/// `id.name` is attacker-influenced under mTLS (SAN/CN) and OAuth (`sub`).
791/// An unbounded [`std::collections::HashSet`] would grow with churn,
792/// producing both a slow memory leak and unbounded log-cardinality
793/// downstream (Loki/ES). The cap follows the same trade-off documented in
794/// [`crate::bounded_limiter`]: when an evicted identity reappears it
795/// re-fires INFO once. This is acceptable for diagnostic logging.
796///
797/// # Concurrency
798///
799/// Uses [`std::sync::Mutex`] because [`Self::insert_is_first`] is purely
800/// synchronous and the critical section never `.await`s. The mutex is
801/// poison-tolerant: a poisoned set is still logically consistent
802/// (only writer is `insert_is_first`, which performs an atomic insert
803/// + bounded eviction; no torn invariants are possible).
804pub(crate) struct SeenIdentitySet {
805    inner: Mutex<SeenInner>,
806}
807
808struct SeenInner {
809    set: HashSet<String>,
810    /// Insertion-order FIFO used for bounded eviction. Tracking strict LRU
811    /// would require touching the queue on every hit (under the mutex);
812    /// FIFO is sufficient because the contract only promises "bounded
813    /// memory", not "remember the most recently seen identities".
814    order: std::collections::VecDeque<String>,
815    cap: usize,
816}
817
818impl SeenIdentitySet {
819    /// Construct with the default cap of [`DEFAULT_SEEN_IDENTITY_CAP`].
820    #[must_use]
821    pub(crate) fn new() -> Self {
822        Self::with_cap(DEFAULT_SEEN_IDENTITY_CAP)
823    }
824
825    /// Construct with an explicit cap. A `cap` of `0` is silently raised
826    /// to `1` to keep the invariant `set.len() <= cap` non-vacuous.
827    #[must_use]
828    pub(crate) fn with_cap(cap: usize) -> Self {
829        let cap = cap.max(1);
830        Self {
831            inner: Mutex::new(SeenInner {
832                set: HashSet::with_capacity(cap.min(64)),
833                order: std::collections::VecDeque::with_capacity(cap.min(64)),
834                cap,
835            }),
836        }
837    }
838
839    /// Insert `name`. Returns `true` if this is the first time `name` was
840    /// inserted (or it was previously evicted and reinserted), `false`
841    /// if it was already present.
842    ///
843    /// When the cap is reached, the oldest inserted entry is evicted to
844    /// make room. Eviction never blocks the caller.
845    pub(crate) fn insert_is_first(&self, name: &str) -> bool {
846        // SAFETY: the only writer is this method; a poisoned set remains
847        // logically consistent (atomic insert + bounded eviction preserve
848        // the `set.len() <= cap` invariant). Continuing past poison only
849        // affects diagnostic logging granularity, not correctness or
850        // security.
851        let mut guard = self
852            .inner
853            .lock()
854            .unwrap_or_else(std::sync::PoisonError::into_inner);
855
856        if guard.set.contains(name) {
857            return false;
858        }
859        // Cap enforcement: evict-then-insert keeps the invariant
860        // `set.len() <= cap` even when the cap is `1`.
861        if guard.set.len() >= guard.cap
862            && let Some(evicted) = guard.order.pop_front()
863        {
864            guard.set.remove(&evicted);
865        }
866        let owned = name.to_owned();
867        guard.set.insert(owned.clone());
868        guard.order.push_back(owned);
869        true
870    }
871
872    /// Test-only snapshot of the current size.
873    #[cfg(test)]
874    pub(crate) fn len(&self) -> usize {
875        self.inner
876            .lock()
877            .unwrap_or_else(std::sync::PoisonError::into_inner)
878            .set
879            .len()
880    }
881}
882
883impl Default for SeenIdentitySet {
884    fn default() -> Self {
885        Self::new()
886    }
887}
888
889/// Shared state for the auth middleware.
890///
891/// `api_keys` uses [`ArcSwap`] so the SIGHUP handler can atomically
892/// swap in a new key list without blocking in-flight requests.
893#[allow(
894    missing_debug_implementations,
895    reason = "contains governor RateLimiter and JwksCache without Debug impls"
896)]
897#[non_exhaustive]
898pub(crate) struct AuthState {
899    /// Active set of API keys (hot-swappable).
900    pub api_keys: ArcSwap<Vec<ApiKeyEntry>>,
901    /// Optional per-IP post-failure rate limiter (consulted *after* auth fails).
902    pub rate_limiter: Option<Arc<KeyedLimiter>>,
903    /// Optional per-IP pre-auth abuse gate (consulted *before* password-hash work).
904    /// mTLS-authenticated connections bypass this gate.
905    pub pre_auth_limiter: Option<Arc<KeyedLimiter>>,
906    #[cfg(feature = "oauth")]
907    /// Optional JWKS cache for OAuth JWT validation.
908    pub jwks_cache: Option<Arc<crate::oauth::JwksCache>>,
909    /// Tracks identity names that have already been logged at INFO level.
910    /// Subsequent auths for the same identity are logged at DEBUG.
911    /// Bounded to prevent attacker-driven memory growth via churned
912    /// mTLS subjects or OAuth `sub` claims (see [`SeenIdentitySet`]).
913    pub seen_identities: SeenIdentitySet,
914    /// Lightweight in-memory auth success/failure counters for diagnostics.
915    pub counters: AuthCounters,
916}
917
918impl AuthState {
919    /// Atomically replace the API key list (lock-free, wait-free).
920    ///
921    /// New requests immediately see the updated keys.
922    /// In-flight requests that already loaded the old list finish
923    /// using it -- no torn reads.
924    pub(crate) fn reload_keys(&self, keys: Vec<ApiKeyEntry>) {
925        let count = keys.len();
926        self.api_keys.store(Arc::new(keys));
927        tracing::info!(keys = count, "API keys reloaded");
928    }
929
930    /// Snapshot auth counters for diagnostics and tests.
931    #[must_use]
932    pub(crate) fn counters_snapshot(&self) -> AuthCountersSnapshot {
933        self.counters.snapshot()
934    }
935
936    /// Produce the admin-endpoint list of API keys (metadata only, no hashes).
937    #[must_use]
938    pub(crate) fn api_key_summaries(&self) -> Vec<ApiKeySummary> {
939        self.api_keys
940            .load()
941            .iter()
942            .map(|k| ApiKeySummary {
943                name: k.name.clone(),
944                role: k.role.clone(),
945                expires_at: k.expires_at,
946            })
947            .collect()
948    }
949
950    /// Log auth success: INFO on first occurrence per identity, DEBUG after.
951    ///
952    /// Backed by [`SeenIdentitySet`], a bounded FIFO set that caps
953    /// retained identities to prevent attacker-driven memory growth.
954    /// FIFO (not LRU) is intentional: this cache de-duplicates INFO logs,
955    /// not security state, so per-hit eviction-order mutation is not
956    /// justified. See [`SeenIdentitySet`] for the full trade-off rationale.
957    fn log_auth(&self, id: &AuthIdentity, method: &str) {
958        self.counters.record_success(id.method);
959        let first = self.seen_identities.insert_is_first(&id.name);
960        if first {
961            tracing::info!(name = %id.name, role = %id.role, "{method} authenticated");
962        } else {
963            tracing::debug!(name = %id.name, role = %id.role, "{method} authenticated");
964        }
965    }
966}
967
968/// Default auth rate limit: 30 attempts per minute per source IP.
969// SAFETY: unwrap() is safe - literal 30 is provably non-zero (const-evaluated).
970const DEFAULT_AUTH_RATE: NonZeroU32 = NonZeroU32::new(30).unwrap();
971
972/// Create a post-failure rate limiter from config.
973#[must_use]
974pub(crate) fn build_rate_limiter(config: &RateLimitConfig) -> Arc<KeyedLimiter> {
975    let quota = governor::Quota::per_minute(
976        NonZeroU32::new(config.max_attempts_per_minute).unwrap_or(DEFAULT_AUTH_RATE),
977    );
978    Arc::new(BoundedKeyedLimiter::new(
979        quota,
980        config.max_tracked_keys,
981        config.idle_eviction,
982    ))
983}
984
985/// Create a pre-auth abuse-gate rate limiter from config.
986///
987/// Quota: `pre_auth_max_per_minute` if set, otherwise
988/// `max_attempts_per_minute * 10` (capped at `u32::MAX`). The 10× factor
989/// keeps the gate generous enough for honest retries while still bounding
990/// attacker CPU on Argon2 verification.
991#[must_use]
992pub(crate) fn build_pre_auth_limiter(config: &RateLimitConfig) -> Arc<KeyedLimiter> {
993    let resolved = config.pre_auth_max_per_minute.unwrap_or_else(|| {
994        config
995            .max_attempts_per_minute
996            .saturating_mul(PRE_AUTH_DEFAULT_MULTIPLIER)
997    });
998    let quota =
999        governor::Quota::per_minute(NonZeroU32::new(resolved).unwrap_or(DEFAULT_PRE_AUTH_RATE));
1000    Arc::new(BoundedKeyedLimiter::new(
1001        quota,
1002        config.max_tracked_keys,
1003        config.idle_eviction,
1004    ))
1005}
1006
1007/// Default multiplier applied to `max_attempts_per_minute` when the operator
1008/// does not set `pre_auth_max_per_minute` explicitly.
1009const PRE_AUTH_DEFAULT_MULTIPLIER: u32 = 10;
1010
1011/// Default pre-auth abuse-gate rate (used only if both the configured value
1012/// and the multiplied fallback are zero, which `NonZeroU32::new` rejects).
1013// SAFETY: unwrap() is safe - literal 300 is provably non-zero (const-evaluated).
1014const DEFAULT_PRE_AUTH_RATE: NonZeroU32 = NonZeroU32::new(300).unwrap();
1015
1016/// Parse an mTLS client certificate and extract an `AuthIdentity`.
1017///
1018/// Reads the Subject CN as the identity name. Falls back to the first
1019/// DNS SAN if CN is absent. The role is taken from the `MtlsConfig`.
1020#[must_use]
1021pub fn extract_mtls_identity(cert_der: &[u8], default_role: &str) -> Option<AuthIdentity> {
1022    let (_, cert) = X509Certificate::from_der(cert_der).ok()?;
1023
1024    // Try CN from Subject first.
1025    let cn = cert
1026        .subject()
1027        .iter_common_name()
1028        .next()
1029        .and_then(|attr| attr.as_str().ok())
1030        .map(String::from);
1031
1032    // Fall back to first DNS SAN.
1033    let name = cn.or_else(|| {
1034        cert.subject_alternative_name()
1035            .ok()
1036            .flatten()
1037            .and_then(|san| {
1038                #[allow(
1039                    clippy::wildcard_enum_match_arm,
1040                    reason = "x509-parser GeneralName is a large external enum; only DNSName is meaningful here"
1041                )]
1042                san.value.general_names.iter().find_map(|gn| match gn {
1043                    GeneralName::DNSName(dns) => Some((*dns).to_owned()),
1044                    _ => None,
1045                })
1046            })
1047    })?;
1048
1049    // Reject identities with characters unsafe for logging and RBAC matching.
1050    if !name
1051        .chars()
1052        .all(|c| c.is_alphanumeric() || matches!(c, '-' | '.' | '_' | '@'))
1053    {
1054        tracing::warn!(cn = %name, "mTLS identity rejected: invalid characters in CN/SAN");
1055        return None;
1056    }
1057
1058    Some(AuthIdentity {
1059        name,
1060        role: default_role.to_owned(),
1061        method: AuthMethod::MtlsCertificate,
1062        raw_token: None,
1063        sub: None,
1064    })
1065}
1066
1067/// Extract the bearer token from an `Authorization` header value.
1068///
1069/// Implements RFC 7235 §2.1: the auth-scheme token is **case-insensitive**.
1070/// `Bearer`, `bearer`, `BEARER`, and `BeArEr` all parse equivalently. Any
1071/// leading whitespace between the scheme and the token is trimmed (per
1072/// RFC 7235 the separator is one or more SP characters; we accept the
1073/// common single-space form plus tolerate extras).
1074///
1075/// Returns `None` if the header value:
1076/// - does not contain a space (no scheme/credentials boundary), or
1077/// - uses a scheme other than `Bearer` (case-insensitively).
1078///
1079/// The caller is responsible for token-level validation (length, charset,
1080/// signature, etc.); this helper only handles the scheme prefix.
1081fn extract_bearer(value: &str) -> Option<&str> {
1082    let (scheme, rest) = value.split_once(' ')?;
1083    if scheme.eq_ignore_ascii_case("Bearer") {
1084        let token = rest.trim_start_matches(' ');
1085        if token.is_empty() { None } else { Some(token) }
1086    } else {
1087        None
1088    }
1089}
1090
1091/// Verify a bearer token against configured API keys.
1092///
1093/// Argon2id verification is CPU-intensive, so this should be called via
1094/// `spawn_blocking`. Returns the matching identity if the token is valid.
1095///
1096/// # Timing-side-channel resistance
1097///
1098/// Always performs **exactly one Argon2id verification per configured key**,
1099/// regardless of:
1100///
1101/// * which slot (if any) matches the presented token, or
1102/// * whether a key has expired.
1103///
1104/// Expired and post-match slots are verified against an internal dummy PHC hash,
1105/// a fixed Argon2id PHC string with the same cost parameters as the real
1106/// hashes. This bounds the timing observable to "one Argon2 per configured
1107/// key" regardless of which (if any) slot held the matching credential,
1108/// closing the first-match latency oracle (CWE-208) and the expired-slot
1109/// timing leak.
1110///
1111/// `subtle::ConstantTimeEq` is used to fold per-slot match bits into the
1112/// final result so the compiler cannot reintroduce a data-dependent branch.
1113///
1114/// # Panics
1115///
1116/// Panics if the internal dummy PHC hash cannot be parsed as an Argon2id PHC string.
1117/// This is impossible by construction: the static is generated by
1118/// [`argon2::Argon2::hash_password`] which always emits a valid PHC string.
1119#[must_use]
1120pub fn verify_bearer_token(token: &str, keys: &[ApiKeyEntry]) -> Option<AuthIdentity> {
1121    use subtle::ConstantTimeEq as _;
1122
1123    let now = chrono::Utc::now();
1124    #[allow(
1125        clippy::expect_used,
1126        reason = "DUMMY_PHC_HASH is a static LazyLock built from a fixed Argon2id PHC string by construction; PasswordHash::new on it is infallible. See DUMMY_PHC_HASH definition."
1127    )]
1128    let dummy_hash = PasswordHash::new(&DUMMY_PHC_HASH)
1129        .expect("DUMMY_PHC_HASH is a valid Argon2id PHC string by construction");
1130
1131    let mut matched_index: usize = usize::MAX;
1132    let mut any_match: u8 = 0;
1133
1134    for (idx, key) in keys.iter().enumerate() {
1135        let expired = key.expires_at.is_some_and(|exp| exp.as_datetime() < &now);
1136
1137        let real_hash = PasswordHash::new(&key.hash);
1138        let verify_against = match (&real_hash, expired, any_match) {
1139            (Ok(h), false, 0) => h,
1140            _ => &dummy_hash,
1141        };
1142
1143        let slot_ok = u8::from(
1144            Argon2::default()
1145                .verify_password(token.as_bytes(), verify_against)
1146                .is_ok(),
1147        );
1148
1149        let real_match = slot_ok & u8::from(!expired) & u8::from(real_hash.is_ok());
1150        let first_real_match = real_match & (1 - any_match);
1151        if first_real_match.ct_eq(&1).into() {
1152            matched_index = idx;
1153        }
1154        any_match |= real_match;
1155    }
1156
1157    if any_match == 0 {
1158        return None;
1159    }
1160    let key = keys.get(matched_index)?;
1161    Some(AuthIdentity {
1162        name: key.name.clone(),
1163        role: key.role.clone(),
1164        method: AuthMethod::BearerToken,
1165        raw_token: None,
1166        sub: None,
1167    })
1168}
1169
1170/// Fixed Argon2id PHC hash used as a constant-time placeholder when an
1171/// API-key slot is expired, malformed, or follows the matching slot.
1172///
1173/// Generated once on first access using the same default Argon2 cost
1174/// parameters as live verifications, so the dummy verify takes
1175/// indistinguishable wall time from a real one. The plaintext
1176/// (`"rmcp-server-kit-dummy"`) and the fixed salt are unrelated to any
1177/// real credential — randomness is unnecessary because this hash is
1178/// only ever compared against attacker-supplied input on slots that
1179/// will be discarded regardless of match result. Using a fixed salt
1180/// avoids depending on `rand_core`'s `getrandom` feature, which is not
1181/// activated transitively in every feature configuration of this crate.
1182static DUMMY_PHC_HASH: LazyLock<String> = LazyLock::new(|| {
1183    // 16 bytes of base64 (`AAAA...`) — minimum valid Argon2 salt length.
1184    #[allow(
1185        clippy::expect_used,
1186        reason = "fixed 22-char base64 ('AAAA...') decodes to a valid 16-byte salt; SaltString::from_b64 is infallible on this literal"
1187    )]
1188    let salt = SaltString::from_b64("AAAAAAAAAAAAAAAAAAAAAA")
1189        .expect("fixed 16-byte base64 salt is well-formed");
1190    #[allow(
1191        clippy::expect_used,
1192        reason = "Argon2::default() with a fixed plaintext and a well-formed salt is infallible; only fails on bad params/salt"
1193    )]
1194    Argon2::default()
1195        .hash_password(b"rmcp-server-kit-dummy", &salt)
1196        .expect("Argon2 default params hash a fixed plaintext")
1197        .to_string()
1198});
1199
1200/// Generate a new API key: 256-bit random token + Argon2id hash.
1201///
1202/// Returns `(plaintext_token, argon2id_hash_phc_string)`.
1203/// The plaintext is shown once to the user and never stored.
1204///
1205/// # Errors
1206///
1207/// Returns an error if salt encoding or Argon2id hashing fails
1208/// (should not happen with valid inputs, but we avoid panicking).
1209pub fn generate_api_key() -> Result<(String, String), McpxError> {
1210    let mut token_bytes = [0u8; 32];
1211    rand::fill(&mut token_bytes);
1212    let token = URL_SAFE_NO_PAD.encode(token_bytes);
1213
1214    // Generate 16 random bytes for salt, encode as base64 for SaltString.
1215    let mut salt_bytes = [0u8; 16];
1216    rand::fill(&mut salt_bytes);
1217    let salt = SaltString::encode_b64(&salt_bytes)
1218        .map_err(|e| McpxError::Auth(format!("salt encoding failed: {e}")))?;
1219    let hash = Argon2::default()
1220        .hash_password(token.as_bytes(), &salt)
1221        .map_err(|e| McpxError::Auth(format!("argon2id hashing failed: {e}")))?
1222        .to_string();
1223
1224    Ok((token, hash))
1225}
1226
1227fn build_www_authenticate_value(
1228    advertise_resource_metadata: bool,
1229    failure: AuthFailureClass,
1230) -> String {
1231    let (error, error_description) = failure.bearer_error();
1232    if advertise_resource_metadata {
1233        return format!(
1234            "Bearer resource_metadata=\"/.well-known/oauth-protected-resource\", error=\"{error}\", error_description=\"{error_description}\""
1235        );
1236    }
1237    format!("Bearer error=\"{error}\", error_description=\"{error_description}\"")
1238}
1239
1240fn auth_method_label(method: AuthMethod) -> &'static str {
1241    match method {
1242        AuthMethod::MtlsCertificate => "mTLS",
1243        AuthMethod::BearerToken => "bearer token",
1244        AuthMethod::OAuthJwt => "OAuth JWT",
1245    }
1246}
1247
1248#[cfg_attr(not(feature = "oauth"), allow(unused_variables))]
1249fn unauthorized_response(state: &AuthState, failure_class: AuthFailureClass) -> Response {
1250    #[cfg(feature = "oauth")]
1251    let advertise_resource_metadata = state.jwks_cache.is_some();
1252    #[cfg(not(feature = "oauth"))]
1253    let advertise_resource_metadata = false;
1254
1255    let challenge = build_www_authenticate_value(advertise_resource_metadata, failure_class);
1256    (
1257        axum::http::StatusCode::UNAUTHORIZED,
1258        [(header::WWW_AUTHENTICATE, challenge)],
1259        failure_class.response_body(),
1260    )
1261        .into_response()
1262}
1263
1264async fn authenticate_bearer_identity(
1265    state: &AuthState,
1266    token: &str,
1267) -> Result<AuthIdentity, AuthFailureClass> {
1268    let mut failure_class = AuthFailureClass::MissingCredential;
1269
1270    #[cfg(feature = "oauth")]
1271    if let Some(ref cache) = state.jwks_cache
1272        && crate::oauth::looks_like_jwt(token)
1273    {
1274        match cache.validate_token_with_reason(token).await {
1275            Ok(mut id) => {
1276                id.raw_token = Some(SecretString::from(token.to_owned()));
1277                return Ok(id);
1278            }
1279            Err(crate::oauth::JwtValidationFailure::Expired) => {
1280                failure_class = AuthFailureClass::ExpiredCredential;
1281            }
1282            Err(crate::oauth::JwtValidationFailure::Invalid) => {
1283                failure_class = AuthFailureClass::InvalidCredential;
1284            }
1285        }
1286    }
1287
1288    let token = token.to_owned();
1289    let keys = state.api_keys.load_full(); // Arc clone, lock-free
1290
1291    // Argon2id is CPU-bound - offload to blocking thread pool.
1292    let identity = tokio::task::spawn_blocking(move || verify_bearer_token(&token, &keys))
1293        .await
1294        .ok()
1295        .flatten();
1296
1297    if let Some(id) = identity {
1298        return Ok(id);
1299    }
1300
1301    if failure_class == AuthFailureClass::MissingCredential {
1302        failure_class = AuthFailureClass::InvalidCredential;
1303    }
1304
1305    Err(failure_class)
1306}
1307
1308/// Consult the pre-auth abuse gate for the given peer.
1309///
1310/// Returns `Some(response)` if the request should be rejected (limiter
1311/// configured AND quota exhausted for this source IP). Returns `None`
1312/// otherwise (limiter absent, peer address unknown, or quota available),
1313/// in which case the caller should proceed with credential verification.
1314///
1315/// Side effects on rejection: increments the `pre_auth_gate` failure
1316/// counter and emits a warn-level log. mTLS-authenticated requests must
1317/// be admitted by the caller *before* invoking this helper.
1318fn pre_auth_gate(state: &AuthState, peer_addr: Option<SocketAddr>) -> Option<Response> {
1319    let limiter = state.pre_auth_limiter.as_ref()?;
1320    let addr = peer_addr?;
1321    if limiter.check_key(&addr.ip()).is_ok() {
1322        return None;
1323    }
1324    state.counters.record_failure(AuthFailureClass::PreAuthGate);
1325    tracing::warn!(
1326        ip = %addr.ip(),
1327        "auth rate limited by pre-auth gate (request rejected before credential verification)"
1328    );
1329    Some(
1330        McpxError::RateLimited("too many unauthenticated requests from this source".into())
1331            .into_response(),
1332    )
1333}
1334
1335/// Axum middleware that enforces authentication.
1336///
1337/// Tries authentication methods in priority order:
1338/// 1. mTLS client certificate identity (populated by TLS acceptor)
1339/// 2. Bearer token from `Authorization` header
1340///
1341/// Failed authentication attempts are rate-limited per source IP.
1342/// Successful authentications do not consume rate limit budget.
1343pub(crate) async fn auth_middleware(
1344    state: Arc<AuthState>,
1345    req: Request<Body>,
1346    next: Next,
1347) -> Response {
1348    // Extract peer address (and any mTLS identity) from ConnectInfo.
1349    // Plain TCP: ConnectInfo<SocketAddr>. TLS / mTLS: ConnectInfo<TlsConnInfo>,
1350    // which carries the verified identity directly on the connection — no
1351    // shared map, no port-reuse aliasing.
1352    let tls_info = req.extensions().get::<ConnectInfo<TlsConnInfo>>().cloned();
1353    let peer_addr = req
1354        .extensions()
1355        .get::<ConnectInfo<SocketAddr>>()
1356        .map(|ci| ci.0)
1357        .or_else(|| tls_info.as_ref().map(|ci| ci.0.addr));
1358
1359    // 1. Try mTLS identity (extracted by the TLS acceptor during handshake
1360    //    and attached to the connection itself).
1361    //
1362    //    mTLS connections bypass the pre-auth abuse gate below: the TLS
1363    //    handshake already performed expensive crypto with a verified peer,
1364    //    so we trust them not to be a CPU-spray attacker.
1365    if let Some(id) = tls_info.and_then(|ci| ci.0.identity) {
1366        state.log_auth(&id, "mTLS");
1367        let mut req = req;
1368        req.extensions_mut().insert(id);
1369        return next.run(req).await;
1370    }
1371
1372    // 2. Pre-auth abuse gate: rejects CPU-spray attacks BEFORE the Argon2id
1373    //    verification path runs. Keyed by source IP. mTLS connections (above)
1374    //    are exempt; this gate only protects the bearer/JWT verification path.
1375    if let Some(blocked) = pre_auth_gate(&state, peer_addr) {
1376        return blocked;
1377    }
1378
1379    let failure_class = if let Some(value) = req.headers().get(header::AUTHORIZATION) {
1380        match value.to_str().ok().and_then(extract_bearer) {
1381            Some(token) => match authenticate_bearer_identity(&state, token).await {
1382                Ok(id) => {
1383                    state.log_auth(&id, auth_method_label(id.method));
1384                    let mut req = req;
1385                    req.extensions_mut().insert(id);
1386                    return next.run(req).await;
1387                }
1388                Err(class) => class,
1389            },
1390            None => AuthFailureClass::InvalidCredential,
1391        }
1392    } else {
1393        AuthFailureClass::MissingCredential
1394    };
1395
1396    tracing::warn!(failure_class = %failure_class.as_str(), "auth failed");
1397
1398    // Rate limit check (applied after auth failure only).
1399    // Successful authentications do not consume rate limit budget.
1400    if let (Some(limiter), Some(addr)) = (&state.rate_limiter, peer_addr)
1401        && limiter.check_key(&addr.ip()).is_err()
1402    {
1403        state.counters.record_failure(AuthFailureClass::RateLimited);
1404        tracing::warn!(ip = %addr.ip(), "auth rate limited after repeated failures");
1405        return McpxError::RateLimited("too many failed authentication attempts".into())
1406            .into_response();
1407    }
1408
1409    state.counters.record_failure(failure_class);
1410    unauthorized_response(&state, failure_class)
1411}
1412
1413#[cfg(test)]
1414mod tests {
1415    use super::*;
1416
1417    #[test]
1418    fn generate_and_verify_api_key() {
1419        let (token, hash) = generate_api_key().unwrap();
1420
1421        // Token is 43 chars (256-bit base64url, no padding)
1422        assert_eq!(token.len(), 43);
1423
1424        // Hash is a valid PHC string
1425        assert!(hash.starts_with("$argon2id$"));
1426
1427        // Verification succeeds with correct token
1428        let keys = vec![ApiKeyEntry {
1429            name: "test".into(),
1430            hash,
1431            role: "viewer".into(),
1432            expires_at: None,
1433        }];
1434        let id = verify_bearer_token(&token, &keys);
1435        assert!(id.is_some());
1436        let id = id.unwrap();
1437        assert_eq!(id.name, "test");
1438        assert_eq!(id.role, "viewer");
1439        assert_eq!(id.method, AuthMethod::BearerToken);
1440    }
1441
1442    #[test]
1443    fn wrong_token_rejected() {
1444        let (_token, hash) = generate_api_key().unwrap();
1445        let keys = vec![ApiKeyEntry {
1446            name: "test".into(),
1447            hash,
1448            role: "viewer".into(),
1449            expires_at: None,
1450        }];
1451        assert!(verify_bearer_token("wrong-token", &keys).is_none());
1452    }
1453
1454    #[test]
1455    fn expired_key_rejected() {
1456        let (token, hash) = generate_api_key().unwrap();
1457        let keys = vec![ApiKeyEntry {
1458            name: "test".into(),
1459            hash,
1460            role: "viewer".into(),
1461            expires_at: Some(RfcTimestamp::parse("2020-01-01T00:00:00Z").unwrap()),
1462        }];
1463        assert!(verify_bearer_token(&token, &keys).is_none());
1464    }
1465
1466    #[test]
1467    fn match_in_last_slot_still_authenticates() {
1468        let (token, hash) = generate_api_key().unwrap();
1469        let (_other_token, other_hash) = generate_api_key().unwrap();
1470        let keys = vec![
1471            ApiKeyEntry {
1472                name: "first".into(),
1473                hash: other_hash.clone(),
1474                role: "viewer".into(),
1475                expires_at: None,
1476            },
1477            ApiKeyEntry {
1478                name: "second".into(),
1479                hash: other_hash,
1480                role: "viewer".into(),
1481                expires_at: None,
1482            },
1483            ApiKeyEntry {
1484                name: "match".into(),
1485                hash,
1486                role: "ops".into(),
1487                expires_at: None,
1488            },
1489        ];
1490        let id = verify_bearer_token(&token, &keys).expect("last-slot match must authenticate");
1491        assert_eq!(id.name, "match");
1492        assert_eq!(id.role, "ops");
1493    }
1494
1495    #[test]
1496    fn expired_slot_before_valid_match_does_not_short_circuit() {
1497        let (token, hash) = generate_api_key().unwrap();
1498        let (_, other_hash) = generate_api_key().unwrap();
1499        let keys = vec![
1500            ApiKeyEntry {
1501                name: "expired".into(),
1502                hash: other_hash,
1503                role: "viewer".into(),
1504                expires_at: Some(RfcTimestamp::parse("2020-01-01T00:00:00Z").unwrap()),
1505            },
1506            ApiKeyEntry {
1507                name: "valid".into(),
1508                hash,
1509                role: "ops".into(),
1510                expires_at: None,
1511            },
1512        ];
1513        let id = verify_bearer_token(&token, &keys)
1514            .expect("valid slot following an expired slot must authenticate");
1515        assert_eq!(id.name, "valid");
1516    }
1517
1518    #[test]
1519    fn malformed_hash_slot_does_not_short_circuit() {
1520        let (token, hash) = generate_api_key().unwrap();
1521        let keys = vec![
1522            ApiKeyEntry {
1523                name: "broken".into(),
1524                hash: "this-is-not-a-phc-string".into(),
1525                role: "viewer".into(),
1526                expires_at: None,
1527            },
1528            ApiKeyEntry {
1529                name: "valid".into(),
1530                hash,
1531                role: "ops".into(),
1532                expires_at: None,
1533            },
1534        ];
1535        let id = verify_bearer_token(&token, &keys)
1536            .expect("valid slot following a malformed-hash slot must authenticate");
1537        assert_eq!(id.name, "valid");
1538    }
1539
1540    // Regression tests for H3 (api_key_expires_at_fail_open).
1541    //
1542    // Prior to 1.6.0 the runtime expiry check used a chained
1543    // `if let Some(_) && let Ok(exp) = parse(_) && exp < now` which
1544    // silently fell through on parse error, letting a key with
1545    // `expires_at = "not-a-date"` authenticate forever. These tests
1546    // pin the type-system fix: malformed RFC 3339 is rejected at
1547    // deserialization time (no `RfcTimestamp` can ever be malformed),
1548    // and the runtime check is a pure comparison with no parse path.
1549
1550    #[test]
1551    fn rfc_timestamp_parse_rejects_malformed() {
1552        for bad in [
1553            "not-a-date",
1554            "",
1555            "2025-13-01T00:00:00Z", // month 13
1556            "2025-01-32T00:00:00Z", // day 32
1557            "2025-01-01T00:00:00",  // missing offset
1558            "01/01/2025",           // wrong format
1559            "2025-01-01T25:00:00Z", // hour 25
1560        ] {
1561            assert!(
1562                RfcTimestamp::parse(bad).is_err(),
1563                "RfcTimestamp::parse must reject {bad:?}"
1564            );
1565        }
1566    }
1567
1568    #[test]
1569    fn rfc_timestamp_parse_accepts_valid() {
1570        for good in [
1571            "2025-01-01T00:00:00Z",
1572            "2025-01-01T00:00:00+00:00",
1573            "2025-12-31T23:59:59-08:00",
1574            "2099-01-01T00:00:00.123456789Z",
1575        ] {
1576            assert!(
1577                RfcTimestamp::parse(good).is_ok(),
1578                "RfcTimestamp::parse must accept {good:?}"
1579            );
1580        }
1581    }
1582
1583    #[test]
1584    fn api_key_entry_deserialize_rejects_malformed_expires_at() {
1585        // TOML with a malformed expires_at must fail to deserialize.
1586        // This is the load-time defense: a typo in auth.toml aborts
1587        // config load with a clear serde error, instead of producing
1588        // a key that authenticates forever (the H3 fail-open).
1589        let toml = r#"
1590            name = "bad-key"
1591            hash = "$argon2id$v=19$m=19456,t=2,p=1$c2FsdA$h4sh"
1592            role = "viewer"
1593            expires_at = "not-a-date"
1594        "#;
1595        let result: Result<ApiKeyEntry, _> = toml::from_str(toml);
1596        assert!(
1597            result.is_err(),
1598            "deserialization must reject malformed expires_at"
1599        );
1600    }
1601
1602    #[test]
1603    fn api_key_entry_deserialize_accepts_valid_expires_at() {
1604        let toml = r#"
1605            name = "good-key"
1606            hash = "$argon2id$v=19$m=19456,t=2,p=1$c2FsdA$h4sh"
1607            role = "viewer"
1608            expires_at = "2099-01-01T00:00:00Z"
1609        "#;
1610        let entry: ApiKeyEntry = toml::from_str(toml).expect("valid RFC 3339 must deserialize");
1611        assert!(entry.expires_at.is_some());
1612    }
1613
1614    #[test]
1615    fn api_key_entry_deserialize_accepts_missing_expires_at() {
1616        // Omitting expires_at must continue to mean "no expiry"; this
1617        // is the documented contract and must survive the H3 fix.
1618        let toml = r#"
1619            name = "eternal-key"
1620            hash = "$argon2id$v=19$m=19456,t=2,p=1$c2FsdA$h4sh"
1621            role = "viewer"
1622        "#;
1623        let entry: ApiKeyEntry = toml::from_str(toml).expect("missing expires_at must deserialize");
1624        assert!(entry.expires_at.is_none());
1625    }
1626
1627    #[test]
1628    fn try_with_expiry_rejects_malformed() {
1629        let entry = ApiKeyEntry::new("k", "hash", "viewer");
1630        assert!(entry.try_with_expiry("not-a-date").is_err());
1631    }
1632
1633    #[test]
1634    fn try_with_expiry_accepts_valid() {
1635        let entry = ApiKeyEntry::new("k", "hash", "viewer")
1636            .try_with_expiry("2099-01-01T00:00:00Z")
1637            .expect("valid RFC 3339 must be accepted");
1638        assert!(entry.expires_at.is_some());
1639    }
1640
1641    #[test]
1642    fn api_key_summary_serializes_expires_at_as_rfc3339() {
1643        // The admin endpoint wire format is `{"expires_at": "RFC 3339 str"}`.
1644        // Pinning this prevents an accidental serialization-format change
1645        // (e.g. chrono's debug form, a Unix timestamp) that would silently
1646        // break operator tooling that parses these payloads.
1647        let summary = ApiKeySummary {
1648            name: "k".into(),
1649            role: "viewer".into(),
1650            expires_at: Some(RfcTimestamp::parse("2030-01-01T00:00:00Z").unwrap()),
1651        };
1652        let json = serde_json::to_string(&summary).unwrap();
1653        assert!(
1654            json.contains(r#""expires_at":"2030-01-01T00:00:00+00:00""#),
1655            "wire format regressed: {json}"
1656        );
1657    }
1658
1659    #[test]
1660    fn future_expiry_accepted() {
1661        let (token, hash) = generate_api_key().unwrap();
1662        let keys = vec![ApiKeyEntry {
1663            name: "test".into(),
1664            hash,
1665            role: "viewer".into(),
1666            expires_at: Some(RfcTimestamp::parse("2099-01-01T00:00:00Z").unwrap()),
1667        }];
1668        assert!(verify_bearer_token(&token, &keys).is_some());
1669    }
1670
1671    #[test]
1672    fn multiple_keys_first_match_wins() {
1673        let (token, hash) = generate_api_key().unwrap();
1674        let keys = vec![
1675            ApiKeyEntry {
1676                name: "wrong".into(),
1677                hash: "$argon2id$v=19$m=19456,t=2,p=1$invalid$invalid".into(),
1678                role: "ops".into(),
1679                expires_at: None,
1680            },
1681            ApiKeyEntry {
1682                name: "correct".into(),
1683                hash,
1684                role: "deploy".into(),
1685                expires_at: None,
1686            },
1687        ];
1688        let id = verify_bearer_token(&token, &keys).unwrap();
1689        assert_eq!(id.name, "correct");
1690        assert_eq!(id.role, "deploy");
1691    }
1692
1693    #[test]
1694    fn rate_limiter_allows_within_quota() {
1695        let config = RateLimitConfig {
1696            max_attempts_per_minute: 5,
1697            pre_auth_max_per_minute: None,
1698            max_tracked_keys: default_max_tracked_keys(),
1699            idle_eviction: default_idle_eviction(),
1700        };
1701        let limiter = build_rate_limiter(&config);
1702        let ip: IpAddr = "10.0.0.1".parse().unwrap();
1703
1704        // First 5 should succeed.
1705        for _ in 0..5 {
1706            assert!(limiter.check_key(&ip).is_ok());
1707        }
1708        // 6th should fail.
1709        assert!(limiter.check_key(&ip).is_err());
1710    }
1711
1712    #[test]
1713    fn rate_limiter_separate_ips() {
1714        let config = RateLimitConfig {
1715            max_attempts_per_minute: 2,
1716            pre_auth_max_per_minute: None,
1717            max_tracked_keys: default_max_tracked_keys(),
1718            idle_eviction: default_idle_eviction(),
1719        };
1720        let limiter = build_rate_limiter(&config);
1721        let ip1: IpAddr = "10.0.0.1".parse().unwrap();
1722        let ip2: IpAddr = "10.0.0.2".parse().unwrap();
1723
1724        // Exhaust ip1's quota.
1725        assert!(limiter.check_key(&ip1).is_ok());
1726        assert!(limiter.check_key(&ip1).is_ok());
1727        assert!(limiter.check_key(&ip1).is_err());
1728
1729        // ip2 should still have quota.
1730        assert!(limiter.check_key(&ip2).is_ok());
1731    }
1732
1733    #[test]
1734    fn extract_mtls_identity_from_cn() {
1735        // Generate a cert with explicit CN.
1736        let mut params = rcgen::CertificateParams::new(vec!["test-client.local".into()]).unwrap();
1737        params.distinguished_name = rcgen::DistinguishedName::new();
1738        params
1739            .distinguished_name
1740            .push(rcgen::DnType::CommonName, "test-client");
1741        let cert = params
1742            .self_signed(&rcgen::KeyPair::generate().unwrap())
1743            .unwrap();
1744        let der = cert.der();
1745
1746        let id = extract_mtls_identity(der, "ops").unwrap();
1747        assert_eq!(id.name, "test-client");
1748        assert_eq!(id.role, "ops");
1749        assert_eq!(id.method, AuthMethod::MtlsCertificate);
1750    }
1751
1752    #[test]
1753    fn extract_mtls_identity_falls_back_to_san() {
1754        // Cert with no CN but has a DNS SAN.
1755        let mut params =
1756            rcgen::CertificateParams::new(vec!["san-only.example.com".into()]).unwrap();
1757        params.distinguished_name = rcgen::DistinguishedName::new();
1758        // No CN set - should fall back to DNS SAN.
1759        let cert = params
1760            .self_signed(&rcgen::KeyPair::generate().unwrap())
1761            .unwrap();
1762        let der = cert.der();
1763
1764        let id = extract_mtls_identity(der, "viewer").unwrap();
1765        assert_eq!(id.name, "san-only.example.com");
1766        assert_eq!(id.role, "viewer");
1767    }
1768
1769    #[test]
1770    fn extract_mtls_identity_invalid_der() {
1771        assert!(extract_mtls_identity(b"not-a-cert", "viewer").is_none());
1772    }
1773
1774    // -- auth_middleware integration tests --
1775
1776    use axum::{
1777        body::Body,
1778        http::{Request, StatusCode},
1779    };
1780    use tower::ServiceExt as _;
1781
1782    fn auth_router(state: Arc<AuthState>) -> axum::Router {
1783        axum::Router::new()
1784            .route("/mcp", axum::routing::post(|| async { "ok" }))
1785            .layer(axum::middleware::from_fn(move |req, next| {
1786                let s = Arc::clone(&state);
1787                auth_middleware(s, req, next)
1788            }))
1789    }
1790
1791    fn test_auth_state(keys: Vec<ApiKeyEntry>) -> Arc<AuthState> {
1792        Arc::new(AuthState {
1793            api_keys: ArcSwap::new(Arc::new(keys)),
1794            rate_limiter: None,
1795            pre_auth_limiter: None,
1796            #[cfg(feature = "oauth")]
1797            jwks_cache: None,
1798            seen_identities: SeenIdentitySet::new(),
1799            counters: AuthCounters::default(),
1800        })
1801    }
1802
1803    #[tokio::test]
1804    async fn middleware_rejects_no_credentials() {
1805        let state = test_auth_state(vec![]);
1806        let app = auth_router(Arc::clone(&state));
1807        let req = Request::builder()
1808            .method(axum::http::Method::POST)
1809            .uri("/mcp")
1810            .body(Body::empty())
1811            .unwrap();
1812        let resp = app.oneshot(req).await.unwrap();
1813        assert_eq!(resp.status(), StatusCode::UNAUTHORIZED);
1814        let challenge = resp
1815            .headers()
1816            .get(header::WWW_AUTHENTICATE)
1817            .unwrap()
1818            .to_str()
1819            .unwrap();
1820        assert!(challenge.contains("error=\"invalid_request\""));
1821
1822        let counters = state.counters_snapshot();
1823        assert_eq!(counters.failure_missing_credential, 1);
1824    }
1825
1826    #[tokio::test]
1827    async fn middleware_accepts_valid_bearer() {
1828        let (token, hash) = generate_api_key().unwrap();
1829        let keys = vec![ApiKeyEntry {
1830            name: "test-key".into(),
1831            hash,
1832            role: "ops".into(),
1833            expires_at: None,
1834        }];
1835        let state = test_auth_state(keys);
1836        let app = auth_router(Arc::clone(&state));
1837        let req = Request::builder()
1838            .method(axum::http::Method::POST)
1839            .uri("/mcp")
1840            .header("authorization", format!("Bearer {token}"))
1841            .body(Body::empty())
1842            .unwrap();
1843        let resp = app.oneshot(req).await.unwrap();
1844        assert_eq!(resp.status(), StatusCode::OK);
1845
1846        let counters = state.counters_snapshot();
1847        assert_eq!(counters.success_bearer, 1);
1848    }
1849
1850    #[tokio::test]
1851    async fn middleware_rejects_wrong_bearer() {
1852        let (_token, hash) = generate_api_key().unwrap();
1853        let keys = vec![ApiKeyEntry {
1854            name: "test-key".into(),
1855            hash,
1856            role: "ops".into(),
1857            expires_at: None,
1858        }];
1859        let state = test_auth_state(keys);
1860        let app = auth_router(Arc::clone(&state));
1861        let req = Request::builder()
1862            .method(axum::http::Method::POST)
1863            .uri("/mcp")
1864            .header("authorization", "Bearer wrong-token-here")
1865            .body(Body::empty())
1866            .unwrap();
1867        let resp = app.oneshot(req).await.unwrap();
1868        assert_eq!(resp.status(), StatusCode::UNAUTHORIZED);
1869        let challenge = resp
1870            .headers()
1871            .get(header::WWW_AUTHENTICATE)
1872            .unwrap()
1873            .to_str()
1874            .unwrap();
1875        assert!(challenge.contains("error=\"invalid_token\""));
1876
1877        let counters = state.counters_snapshot();
1878        assert_eq!(counters.failure_invalid_credential, 1);
1879    }
1880
1881    #[tokio::test]
1882    async fn middleware_rate_limits() {
1883        let state = Arc::new(AuthState {
1884            api_keys: ArcSwap::new(Arc::new(vec![])),
1885            rate_limiter: Some(build_rate_limiter(&RateLimitConfig {
1886                max_attempts_per_minute: 1,
1887                pre_auth_max_per_minute: None,
1888                max_tracked_keys: default_max_tracked_keys(),
1889                idle_eviction: default_idle_eviction(),
1890            })),
1891            pre_auth_limiter: None,
1892            #[cfg(feature = "oauth")]
1893            jwks_cache: None,
1894            seen_identities: SeenIdentitySet::new(),
1895            counters: AuthCounters::default(),
1896        });
1897        let app = auth_router(state);
1898
1899        // First request: UNAUTHORIZED (no credentials, but not rate limited)
1900        let req = Request::builder()
1901            .method(axum::http::Method::POST)
1902            .uri("/mcp")
1903            .body(Body::empty())
1904            .unwrap();
1905        let resp = app.clone().oneshot(req).await.unwrap();
1906        assert_eq!(resp.status(), StatusCode::UNAUTHORIZED);
1907
1908        // Second request from same "IP" (no ConnectInfo in test, so peer_addr is None
1909        // and rate limiter won't fire). That's expected -- rate limiting requires
1910        // ConnectInfo which isn't available in unit tests without a real server.
1911        // This test verifies the middleware wiring doesn't panic.
1912    }
1913
1914    /// Verify that rate limit semantics: only failed auth attempts consume budget.
1915    ///
1916    /// This is a unit test of the limiter behavior. The middleware integration
1917    /// is that on auth failure, `check_key` is called; on auth success, it is NOT.
1918    /// Full e2e tests verify the middleware routing but require `ConnectInfo`.
1919    #[test]
1920    fn rate_limit_semantics_failed_only() {
1921        let config = RateLimitConfig {
1922            max_attempts_per_minute: 3,
1923            pre_auth_max_per_minute: None,
1924            max_tracked_keys: default_max_tracked_keys(),
1925            idle_eviction: default_idle_eviction(),
1926        };
1927        let limiter = build_rate_limiter(&config);
1928        let ip: IpAddr = "192.168.1.100".parse().unwrap();
1929
1930        // Simulate: 3 failed attempts should exhaust quota.
1931        assert!(
1932            limiter.check_key(&ip).is_ok(),
1933            "failure 1 should be allowed"
1934        );
1935        assert!(
1936            limiter.check_key(&ip).is_ok(),
1937            "failure 2 should be allowed"
1938        );
1939        assert!(
1940            limiter.check_key(&ip).is_ok(),
1941            "failure 3 should be allowed"
1942        );
1943        assert!(
1944            limiter.check_key(&ip).is_err(),
1945            "failure 4 should be blocked"
1946        );
1947
1948        // In the actual middleware flow:
1949        // - Successful auth: verify_bearer_token returns Some, we return early
1950        //   WITHOUT calling check_key, so no budget consumed.
1951        // - Failed auth: verify_bearer_token returns None, we call check_key
1952        //   THEN return 401, so budget is consumed.
1953        //
1954        // This means N successful requests followed by M failed requests
1955        // will only count M toward the rate limit, not N+M.
1956    }
1957
1958    // -- pre-auth abuse gate (H-S1) --
1959
1960    /// The pre-auth gate must default to ~10x the post-failure quota so honest
1961    /// retry storms never trip it but a Argon2-spray attacker is throttled.
1962    #[test]
1963    fn pre_auth_default_multiplier_is_10x() {
1964        let config = RateLimitConfig {
1965            max_attempts_per_minute: 5,
1966            pre_auth_max_per_minute: None,
1967            max_tracked_keys: default_max_tracked_keys(),
1968            idle_eviction: default_idle_eviction(),
1969        };
1970        let limiter = build_pre_auth_limiter(&config);
1971        let ip: IpAddr = "10.0.0.1".parse().unwrap();
1972
1973        // Quota should be 50 (5 * 10), not 5. We expect the first 50 to pass.
1974        for i in 0..50 {
1975            assert!(
1976                limiter.check_key(&ip).is_ok(),
1977                "pre-auth attempt {i} (of expected 50) should be allowed under default 10x multiplier"
1978            );
1979        }
1980        // The 51st attempt must be blocked: confirms quota is bounded, not infinite.
1981        assert!(
1982            limiter.check_key(&ip).is_err(),
1983            "pre-auth attempt 51 should be blocked (quota is 50, not unbounded)"
1984        );
1985    }
1986
1987    /// An explicit `pre_auth_max_per_minute` override must win over the
1988    /// 10x-multiplier default.
1989    #[test]
1990    fn pre_auth_explicit_override_wins() {
1991        let config = RateLimitConfig {
1992            max_attempts_per_minute: 100,     // would default to 1000 pre-auth quota
1993            pre_auth_max_per_minute: Some(2), // but operator caps at 2
1994            max_tracked_keys: default_max_tracked_keys(),
1995            idle_eviction: default_idle_eviction(),
1996        };
1997        let limiter = build_pre_auth_limiter(&config);
1998        let ip: IpAddr = "10.0.0.2".parse().unwrap();
1999
2000        assert!(limiter.check_key(&ip).is_ok(), "attempt 1 allowed");
2001        assert!(limiter.check_key(&ip).is_ok(), "attempt 2 allowed");
2002        assert!(
2003            limiter.check_key(&ip).is_err(),
2004            "attempt 3 must be blocked (explicit override of 2 wins over 10x default of 1000)"
2005        );
2006    }
2007
2008    /// End-to-end: the pre-auth gate must reject before the bearer-verification
2009    /// path runs. We exhaust the gate's quota (Some(1)) with one bad-bearer
2010    /// request, then the second request must be rejected with 429 + the
2011    /// `pre_auth_gate` failure counter incremented (NOT
2012    /// `failure_invalid_credential`, which would prove Argon2 ran).
2013    #[tokio::test]
2014    async fn pre_auth_gate_blocks_before_argon2_verification() {
2015        let (_token, hash) = generate_api_key().unwrap();
2016        let keys = vec![ApiKeyEntry {
2017            name: "test-key".into(),
2018            hash,
2019            role: "ops".into(),
2020            expires_at: None,
2021        }];
2022        let config = RateLimitConfig {
2023            max_attempts_per_minute: 100,
2024            pre_auth_max_per_minute: Some(1),
2025            max_tracked_keys: default_max_tracked_keys(),
2026            idle_eviction: default_idle_eviction(),
2027        };
2028        let state = Arc::new(AuthState {
2029            api_keys: ArcSwap::new(Arc::new(keys)),
2030            rate_limiter: None,
2031            pre_auth_limiter: Some(build_pre_auth_limiter(&config)),
2032            #[cfg(feature = "oauth")]
2033            jwks_cache: None,
2034            seen_identities: SeenIdentitySet::new(),
2035            counters: AuthCounters::default(),
2036        });
2037        let app = auth_router(Arc::clone(&state));
2038        let peer: SocketAddr = "10.0.0.10:54321".parse().unwrap();
2039
2040        // First bad-bearer request: gate has quota, bearer verification runs,
2041        // returns 401 (invalid credential).
2042        let mut req1 = Request::builder()
2043            .method(axum::http::Method::POST)
2044            .uri("/mcp")
2045            .header("authorization", "Bearer obviously-not-a-real-token")
2046            .body(Body::empty())
2047            .unwrap();
2048        req1.extensions_mut().insert(ConnectInfo(peer));
2049        let resp1 = app.clone().oneshot(req1).await.unwrap();
2050        assert_eq!(
2051            resp1.status(),
2052            StatusCode::UNAUTHORIZED,
2053            "first attempt: gate has quota, falls through to bearer auth which fails with 401"
2054        );
2055
2056        // Second bad-bearer request from same IP: gate quota exhausted, must
2057        // reject with 429 BEFORE the Argon2 verification path runs.
2058        let mut req2 = Request::builder()
2059            .method(axum::http::Method::POST)
2060            .uri("/mcp")
2061            .header("authorization", "Bearer also-not-a-real-token")
2062            .body(Body::empty())
2063            .unwrap();
2064        req2.extensions_mut().insert(ConnectInfo(peer));
2065        let resp2 = app.oneshot(req2).await.unwrap();
2066        assert_eq!(
2067            resp2.status(),
2068            StatusCode::TOO_MANY_REQUESTS,
2069            "second attempt from same IP: pre-auth gate must reject with 429"
2070        );
2071
2072        let counters = state.counters_snapshot();
2073        assert_eq!(
2074            counters.failure_pre_auth_gate, 1,
2075            "exactly one request must have been rejected by the pre-auth gate"
2076        );
2077        // Critical: Argon2 verification must NOT have run on the gated request.
2078        // The first request's 401 increments `failure_invalid_credential` to 1;
2079        // the second (gated) request must NOT increment it further.
2080        assert_eq!(
2081            counters.failure_invalid_credential, 1,
2082            "bearer verification must run exactly once (only the un-gated first request)"
2083        );
2084    }
2085
2086    /// mTLS-authenticated requests must bypass the pre-auth gate entirely.
2087    /// The TLS handshake already performed expensive crypto with a verified
2088    /// peer, so mTLS callers should never be throttled by this gate.
2089    ///
2090    /// Setup: a pre-auth gate with quota 1 (very tight). Submit two mTLS
2091    /// requests in quick succession from the same IP. Both must succeed.
2092    #[tokio::test]
2093    async fn pre_auth_gate_does_not_throttle_mtls() {
2094        let config = RateLimitConfig {
2095            max_attempts_per_minute: 100,
2096            pre_auth_max_per_minute: Some(1), // tight: would block 2nd plain request
2097            max_tracked_keys: default_max_tracked_keys(),
2098            idle_eviction: default_idle_eviction(),
2099        };
2100        let state = Arc::new(AuthState {
2101            api_keys: ArcSwap::new(Arc::new(vec![])),
2102            rate_limiter: None,
2103            pre_auth_limiter: Some(build_pre_auth_limiter(&config)),
2104            #[cfg(feature = "oauth")]
2105            jwks_cache: None,
2106            seen_identities: SeenIdentitySet::new(),
2107            counters: AuthCounters::default(),
2108        });
2109        let app = auth_router(Arc::clone(&state));
2110        let peer: SocketAddr = "10.0.0.20:54321".parse().unwrap();
2111        let identity = AuthIdentity {
2112            name: "cn=test-client".into(),
2113            role: "viewer".into(),
2114            method: AuthMethod::MtlsCertificate,
2115            raw_token: None,
2116            sub: None,
2117        };
2118        let tls_info = TlsConnInfo::new(peer, Some(identity));
2119
2120        for i in 0..3 {
2121            let mut req = Request::builder()
2122                .method(axum::http::Method::POST)
2123                .uri("/mcp")
2124                .body(Body::empty())
2125                .unwrap();
2126            req.extensions_mut().insert(ConnectInfo(tls_info.clone()));
2127            let resp = app.clone().oneshot(req).await.unwrap();
2128            assert_eq!(
2129                resp.status(),
2130                StatusCode::OK,
2131                "mTLS request {i} must succeed: pre-auth gate must not apply to mTLS callers"
2132            );
2133        }
2134
2135        let counters = state.counters_snapshot();
2136        assert_eq!(
2137            counters.failure_pre_auth_gate, 0,
2138            "pre-auth gate counter must remain at zero: mTLS bypasses the gate"
2139        );
2140        assert_eq!(
2141            counters.success_mtls, 3,
2142            "all three mTLS requests must have been counted as successful"
2143        );
2144    }
2145
2146    // -------------------------------------------------------------------
2147    // RFC 7235 §2.1 case-insensitive scheme parsing for `extract_bearer`.
2148    // -------------------------------------------------------------------
2149
2150    #[test]
2151    fn extract_bearer_accepts_canonical_case() {
2152        assert_eq!(extract_bearer("Bearer abc123"), Some("abc123"));
2153    }
2154
2155    #[test]
2156    fn extract_bearer_is_case_insensitive_per_rfc7235() {
2157        // RFC 7235 §2.1: "auth-scheme is case-insensitive".
2158        // Real-world clients (curl, browsers, custom HTTP libs) emit varied
2159        // casings; rejecting any of them is a spec violation.
2160        for header in &[
2161            "bearer abc123",
2162            "BEARER abc123",
2163            "BeArEr abc123",
2164            "bEaReR abc123",
2165        ] {
2166            assert_eq!(
2167                extract_bearer(header),
2168                Some("abc123"),
2169                "header {header:?} must parse as a Bearer token (RFC 7235 §2.1)"
2170            );
2171        }
2172    }
2173
2174    #[test]
2175    fn extract_bearer_rejects_other_schemes() {
2176        assert_eq!(extract_bearer("Basic dXNlcjpwYXNz"), None);
2177        assert_eq!(extract_bearer("Digest username=\"x\""), None);
2178        assert_eq!(extract_bearer("Token abc123"), None);
2179    }
2180
2181    #[test]
2182    fn extract_bearer_rejects_malformed() {
2183        // Empty string, no separator, scheme-only, scheme + only whitespace.
2184        assert_eq!(extract_bearer(""), None);
2185        assert_eq!(extract_bearer("Bearer"), None);
2186        assert_eq!(extract_bearer("Bearer "), None);
2187        assert_eq!(extract_bearer("Bearer    "), None);
2188    }
2189
2190    #[test]
2191    fn extract_bearer_tolerates_extra_separator_whitespace() {
2192        // Some non-conformant clients emit two spaces; we should still parse.
2193        assert_eq!(extract_bearer("Bearer  abc123"), Some("abc123"));
2194        assert_eq!(extract_bearer("Bearer   abc123"), Some("abc123"));
2195    }
2196
2197    // -------------------------------------------------------------------
2198    // Debug redaction: ensure `AuthIdentity` and `ApiKeyEntry` never leak
2199    // secret material via `format!("{:?}", …)` or `tracing::debug!(?…)`.
2200    // -------------------------------------------------------------------
2201
2202    #[test]
2203    fn auth_identity_debug_redacts_raw_token() {
2204        let id = AuthIdentity {
2205            name: "alice".into(),
2206            role: "admin".into(),
2207            method: AuthMethod::OAuthJwt,
2208            raw_token: Some(SecretString::from("super-secret-jwt-payload-xyz")),
2209            sub: Some("keycloak-uuid-2f3c8b".into()),
2210        };
2211        let dbg = format!("{id:?}");
2212
2213        // Plaintext fields must be visible (they are not secrets).
2214        assert!(dbg.contains("alice"), "name should be visible: {dbg}");
2215        assert!(dbg.contains("admin"), "role should be visible: {dbg}");
2216        assert!(dbg.contains("OAuthJwt"), "method should be visible: {dbg}");
2217
2218        // Secret fields must NOT leak.
2219        assert!(
2220            !dbg.contains("super-secret-jwt-payload-xyz"),
2221            "raw_token must be redacted in Debug output: {dbg}"
2222        );
2223        assert!(
2224            !dbg.contains("keycloak-uuid-2f3c8b"),
2225            "sub must be redacted in Debug output: {dbg}"
2226        );
2227        assert!(
2228            dbg.contains("<redacted>"),
2229            "redaction marker missing: {dbg}"
2230        );
2231    }
2232
2233    #[test]
2234    fn auth_identity_debug_marks_absent_secrets() {
2235        // For non-OAuth identities (mTLS / API key) the secret fields are
2236        // None; redacted Debug output should distinguish that from "present".
2237        let id = AuthIdentity {
2238            name: "viewer-key".into(),
2239            role: "viewer".into(),
2240            method: AuthMethod::BearerToken,
2241            raw_token: None,
2242            sub: None,
2243        };
2244        let dbg = format!("{id:?}");
2245        assert!(
2246            dbg.contains("<none>"),
2247            "absent secrets should be marked: {dbg}"
2248        );
2249        assert!(
2250            !dbg.contains("<redacted>"),
2251            "no <redacted> marker when secrets are absent: {dbg}"
2252        );
2253    }
2254
2255    #[test]
2256    fn api_key_entry_debug_redacts_hash() {
2257        let entry = ApiKeyEntry {
2258            name: "viewer-key".into(),
2259            // Realistic Argon2id PHC string (must NOT leak).
2260            hash: "$argon2id$v=19$m=19456,t=2,p=1$c2FsdHNhbHQ$h4sh3dPa55w0rd".into(),
2261            role: "viewer".into(),
2262            expires_at: Some(RfcTimestamp::parse("2030-01-01T00:00:00Z").unwrap()),
2263        };
2264        let dbg = format!("{entry:?}");
2265
2266        // Non-secret fields visible.
2267        assert!(dbg.contains("viewer-key"));
2268        assert!(dbg.contains("viewer"));
2269        assert!(dbg.contains("2030-01-01T00:00:00+00:00"));
2270
2271        // Hash material must NOT leak.
2272        assert!(
2273            !dbg.contains("$argon2id$"),
2274            "argon2 hash leaked into Debug output: {dbg}"
2275        );
2276        assert!(
2277            !dbg.contains("h4sh3dPa55w0rd"),
2278            "hash digest leaked into Debug output: {dbg}"
2279        );
2280        assert!(
2281            dbg.contains("<redacted>"),
2282            "redaction marker missing: {dbg}"
2283        );
2284    }
2285
2286    // -- AuthFailureClass exact-string contract tests --
2287    //
2288    // These tests pin the exact wire strings emitted for each failure
2289    // class. They exist to kill mutation-test mutants that replace the
2290    // match-arm string literals (e.g. with `""` or with the value from
2291    // another arm). Operators and dashboards rely on these literals
2292    // for metric labels and audit-log filters; any change is a
2293    // breaking observability change and must be reflected in
2294    // CHANGELOG.md.
2295
2296    #[test]
2297    fn auth_failure_class_as_str_exact_strings() {
2298        assert_eq!(
2299            AuthFailureClass::MissingCredential.as_str(),
2300            "missing_credential"
2301        );
2302        assert_eq!(
2303            AuthFailureClass::InvalidCredential.as_str(),
2304            "invalid_credential"
2305        );
2306        assert_eq!(
2307            AuthFailureClass::ExpiredCredential.as_str(),
2308            "expired_credential"
2309        );
2310        assert_eq!(AuthFailureClass::RateLimited.as_str(), "rate_limited");
2311        assert_eq!(AuthFailureClass::PreAuthGate.as_str(), "pre_auth_gate");
2312    }
2313
2314    #[test]
2315    fn auth_failure_class_response_body_exact_strings() {
2316        assert_eq!(
2317            AuthFailureClass::MissingCredential.response_body(),
2318            "unauthorized: missing credential"
2319        );
2320        assert_eq!(
2321            AuthFailureClass::InvalidCredential.response_body(),
2322            "unauthorized: invalid credential"
2323        );
2324        assert_eq!(
2325            AuthFailureClass::ExpiredCredential.response_body(),
2326            "unauthorized: expired credential"
2327        );
2328        assert_eq!(
2329            AuthFailureClass::RateLimited.response_body(),
2330            "rate limited"
2331        );
2332        assert_eq!(
2333            AuthFailureClass::PreAuthGate.response_body(),
2334            "rate limited (pre-auth)"
2335        );
2336    }
2337
2338    #[test]
2339    fn auth_failure_class_bearer_error_exact_strings() {
2340        assert_eq!(
2341            AuthFailureClass::MissingCredential.bearer_error(),
2342            (
2343                "invalid_request",
2344                "missing bearer token or mTLS client certificate"
2345            )
2346        );
2347        assert_eq!(
2348            AuthFailureClass::InvalidCredential.bearer_error(),
2349            ("invalid_token", "token is invalid")
2350        );
2351        assert_eq!(
2352            AuthFailureClass::ExpiredCredential.bearer_error(),
2353            ("invalid_token", "token is expired")
2354        );
2355        assert_eq!(
2356            AuthFailureClass::RateLimited.bearer_error(),
2357            ("invalid_request", "too many failed authentication attempts")
2358        );
2359        assert_eq!(
2360            AuthFailureClass::PreAuthGate.bearer_error(),
2361            (
2362                "invalid_request",
2363                "too many unauthenticated requests from this source"
2364            )
2365        );
2366    }
2367
2368    // -- AuthConfig::summary boolean-flag contract tests --
2369    //
2370    // These tests pin the boolean flags emitted by `AuthConfig::summary`
2371    // so that mutations like deleting `!` (which would invert the
2372    // semantics of `bearer`) or replacing `is_some()` with `is_none()`
2373    // are caught immediately. The summary is consumed by `/admin/*`
2374    // diagnostics so any inversion is an operator-visible regression.
2375
2376    #[test]
2377    fn auth_config_summary_bearer_true_when_keys_present() {
2378        let (_token, hash) = generate_api_key().unwrap();
2379        let cfg = AuthConfig::with_keys(vec![ApiKeyEntry::new("k", hash, "viewer")]);
2380        let s = cfg.summary();
2381        assert!(s.enabled, "summary.enabled must reflect AuthConfig.enabled");
2382        assert!(
2383            s.bearer,
2384            "summary.bearer must be true when api_keys is non-empty (kills `!` deletion at L615)"
2385        );
2386        assert!(!s.mtls, "summary.mtls must be false when mtls is None");
2387        assert!(!s.oauth, "summary.oauth must be false when oauth is None");
2388        assert_eq!(s.api_keys.len(), 1);
2389        assert_eq!(s.api_keys[0].name, "k");
2390        assert_eq!(s.api_keys[0].role, "viewer");
2391    }
2392
2393    #[test]
2394    fn auth_config_summary_bearer_false_when_no_keys() {
2395        let cfg = AuthConfig::with_keys(vec![]);
2396        let s = cfg.summary();
2397        assert!(
2398            !s.bearer,
2399            "summary.bearer must be false when api_keys is empty (kills `!` deletion at L615)"
2400        );
2401        assert!(s.api_keys.is_empty());
2402    }
2403
2404    #[test]
2405    fn seen_identity_set_first_then_repeat() {
2406        let set = SeenIdentitySet::new();
2407        assert!(set.insert_is_first("alice"), "first sighting is first");
2408        assert!(
2409            !set.insert_is_first("alice"),
2410            "second sighting is not first"
2411        );
2412        assert!(set.insert_is_first("bob"));
2413        assert_eq!(set.len(), 2);
2414    }
2415
2416    #[test]
2417    fn seen_identity_set_evicts_oldest_at_cap() {
2418        let set = SeenIdentitySet::with_cap(2);
2419        assert!(set.insert_is_first("a"));
2420        assert!(set.insert_is_first("b"));
2421        // Cap reached; inserting "c" evicts "a".
2422        assert!(set.insert_is_first("c"));
2423        assert_eq!(set.len(), 2);
2424        // "a" was evicted, so it re-fires as "first" (matches the documented
2425        // bounded trade-off: re-INFO once on reappearance). Inserting "a"
2426        // here evicts "b" (next oldest), leaving {c, a}.
2427        assert!(set.insert_is_first("a"));
2428        assert_eq!(set.len(), 2);
2429        // "b" has now been evicted in turn, so it re-fires as "first" too.
2430        assert!(set.insert_is_first("b"));
2431        // Sanity: cap is never exceeded regardless of churn pattern.
2432        for i in 0..32 {
2433            set.insert_is_first(&format!("churn-{i}"));
2434            assert!(set.len() <= 2, "cap invariant must hold");
2435        }
2436    }
2437
2438    #[test]
2439    fn seen_identity_set_cap_zero_is_raised_to_one() {
2440        let set = SeenIdentitySet::with_cap(0);
2441        assert!(set.insert_is_first("only"));
2442        assert_eq!(set.len(), 1);
2443        // Next insert evicts "only".
2444        assert!(set.insert_is_first("next"));
2445        assert_eq!(set.len(), 1);
2446    }
2447
2448    #[test]
2449    fn seen_identity_set_fifo_does_not_refresh_on_repeat_hit() {
2450        // Locks in the FIFO contract: repeat hits MUST NOT bump an entry
2451        // to the back of the eviction queue (that would be LRU).
2452        let set = SeenIdentitySet::with_cap(2);
2453        assert!(set.insert_is_first("a")); // order=[a]
2454        assert!(set.insert_is_first("b")); // order=[a,b]
2455        // Repeat hit on "a" - if this were LRU, "a" would move to the back
2456        // and "b" would be the next eviction victim. Under FIFO, "a" stays
2457        // at the front (oldest by insertion).
2458        assert!(!set.insert_is_first("a"));
2459        // Insert "c" forces eviction. Under FIFO, "a" (oldest by insertion)
2460        // is evicted; "b" survives. Under LRU, "b" would have been evicted.
2461        assert!(set.insert_is_first("c"));
2462        // Prove "a" was evicted: re-inserting fires as first again.
2463        assert!(set.insert_is_first("a"));
2464        // Prove "b" was NOT evicted: re-inserting does NOT fire as first.
2465        // (If LRU semantics had snuck in, this assertion would fail.)
2466        // After the previous step, "a" eviction pushed out "b" as the new
2467        // oldest, so we must re-add "b" via a fresh insert path. To keep
2468        // the test deterministic we rebuild a small scenario:
2469        let set = SeenIdentitySet::with_cap(2);
2470        assert!(set.insert_is_first("x")); // order=[x]
2471        assert!(set.insert_is_first("y")); // order=[x,y]
2472        assert!(!set.insert_is_first("x")); // repeat hit (under FIFO: order unchanged)
2473        assert!(set.insert_is_first("z")); // evicts "x" under FIFO
2474        assert!(
2475            !set.insert_is_first("y"),
2476            "y must still be present (FIFO did not evict it)"
2477        );
2478        assert!(
2479            set.insert_is_first("x"),
2480            "x must have been evicted by FIFO (would NOT have been evicted under LRU)"
2481        );
2482    }
2483}