adk-auth 3.0.0

Access control and authentication for Rust Agent Development Kit (ADK-Rust)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
//! Key directory sources and the verifier's directory cache. Spec 3.5 §7.3.
//!
//! A verifier resolves the origin a `Signature-Agent` member names to its key directory at
//! `/.well-known/http-message-signatures-directory` (draft-ietf-webbotauth-httpsig-protocol-00
//! §5.5). [`HttpKeyDirectorySource`] fetches it under the draft's SSRF guidance (§6.7): https
//! only, public addresses only, no redirects, a timeout and a size limit.
//! [`InMemoryKeyDirectorySource`] serves directories from memory for tests and for keys
//! configured out of band.

use std::collections::HashMap;
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::Duration;

use adk_core::Clock;
use adk_core::egress::{EgressPolicy, GuardedClient, find_denial};
use async_trait::async_trait;
use chrono::{DateTime, Utc};
use tokio::sync::{Mutex, RwLock};

use super::keys::{DirectoryKey, KeyDirectory};
use super::{DIRECTORY_MEDIA_TYPE, DIRECTORY_WELL_KNOWN_PATH, WebBotAuthError};

/// A key directory and how long it may be cached.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FetchedDirectory {
    /// The directory.
    pub directory: KeyDirectory,
    /// The HTTP cache lifetime of the response (RFC 9111 §4.2.1), when it declared one.
    pub max_age: Option<Duration>,
}

/// Resolves a directory origin to its key directory (draft §5.5).
///
/// `origin` is an ASCII-serialised origin such as `https://bot.example`. Implementations
/// return [`WebBotAuthError::DirectoryUnavailable`] or [`WebBotAuthError::MalformedDirectory`]
/// when no directory can be read, and [`WebBotAuthError::EgressRefused`] when policy forbids the
/// fetch. Spec 3.5 §7.3.
///
/// # Example
///
/// ```rust
/// use std::time::Duration;
/// use adk_auth::web_bot_auth::{Ed25519SigningKey, InMemoryKeyDirectorySource, KeyDirectory, KeyDirectorySource};
///
/// # #[tokio::main(flavor = "current_thread")]
/// # async fn main() {
/// let key = Ed25519SigningKey::generate().unwrap();
/// let source = InMemoryKeyDirectorySource::new();
/// source.insert("https://bot.example", KeyDirectory::from_keys([&key]), Some(Duration::from_secs(300))).await;
/// let fetched = source.fetch("https://bot.example").await.unwrap();
/// assert!(fetched.directory.find(key.thumbprint()).is_some());
/// # }
/// ```
#[async_trait]
pub trait KeyDirectorySource: Send + Sync {
    /// Fetches the key directory published at `origin`.
    async fn fetch(&self, origin: &str) -> Result<FetchedDirectory, WebBotAuthError>;
}

/// Directories held in memory, keyed by origin.
#[derive(Debug, Default)]
pub struct InMemoryKeyDirectorySource {
    directories: RwLock<HashMap<String, FetchedDirectory>>,
    fetches: AtomicUsize,
}

impl InMemoryKeyDirectorySource {
    /// Creates an empty source.
    pub fn new() -> Self {
        Self::default()
    }

    /// Publishes `directory` at `origin` with an optional cache lifetime.
    pub async fn insert(&self, origin: &str, directory: KeyDirectory, max_age: Option<Duration>) {
        self.directories
            .write()
            .await
            .insert(origin.to_string(), FetchedDirectory { directory, max_age });
    }

    /// Removes the directory at `origin`, so later fetches fail as unavailable.
    pub async fn remove(&self, origin: &str) {
        self.directories.write().await.remove(origin);
    }

    /// How many times [`fetch`](KeyDirectorySource::fetch) has been called.
    pub fn fetch_count(&self) -> usize {
        self.fetches.load(Ordering::SeqCst)
    }
}

#[async_trait]
impl KeyDirectorySource for InMemoryKeyDirectorySource {
    async fn fetch(&self, origin: &str) -> Result<FetchedDirectory, WebBotAuthError> {
        self.fetches.fetch_add(1, Ordering::SeqCst);
        self.directories.read().await.get(origin).cloned().ok_or_else(|| {
            WebBotAuthError::DirectoryUnavailable {
                origin: origin.to_string(),
                reason: "no directory is published at this origin".into(),
            }
        })
    }
}

/// Fetches key directories over HTTPS under the draft's SSRF guidance (§6.7).
///
/// Defaults: https only and public addresses only ([`EgressPolicy::default`]), checked by a
/// [`GuardedClient`] on the URL and again on every resolved address; no proxy; no redirects
/// (§5.5); a 5 s timeout;
/// 64 KiB and 64 keys at most; the response must be `200 OK` with media type
/// `application/http-message-signatures-directory+json`.
///
/// # Example
///
/// ```rust
/// use std::time::Duration;
/// use adk_auth::web_bot_auth::HttpKeyDirectorySource;
///
/// let source = HttpKeyDirectorySource::builder()
///     .timeout(Duration::from_secs(3))
///     .max_bytes(32 * 1024)
///     .build()
///     .unwrap();
/// # let _ = source;
/// ```
#[derive(Debug, Clone)]
pub struct HttpKeyDirectorySource {
    client: GuardedClient,
    max_bytes: usize,
    max_keys: usize,
}

impl HttpKeyDirectorySource {
    /// Creates a source with the default limits.
    ///
    /// # Errors
    ///
    /// Returns [`WebBotAuthError::InvalidConfig`] when the HTTP client cannot be built.
    pub fn new() -> Result<Self, WebBotAuthError> {
        Self::builder().build()
    }

    /// Starts a builder with the default limits.
    pub fn builder() -> HttpKeyDirectorySourceBuilder {
        HttpKeyDirectorySourceBuilder {
            egress: EgressPolicy::default(),
            timeout: Duration::from_secs(5),
            max_bytes: 64 * 1024,
            max_keys: 64,
        }
    }
}

/// Builds an [`HttpKeyDirectorySource`].
#[derive(Debug, Clone)]
pub struct HttpKeyDirectorySourceBuilder {
    egress: EgressPolicy,
    timeout: Duration,
    max_bytes: usize,
    max_keys: usize,
}

impl HttpKeyDirectorySourceBuilder {
    /// Sets the egress policy. [`EgressPolicy::allow_private_networks`] admits loopback,
    /// private addresses and plain http (never cloud metadata), for tests and in-cluster
    /// directories only.
    pub fn egress_policy(mut self, egress: EgressPolicy) -> Self {
        self.egress = egress;
        self
    }

    /// Sets the wall-clock limit for one fetch.
    pub fn timeout(mut self, timeout: Duration) -> Self {
        self.timeout = timeout;
        self
    }

    /// Sets the largest directory body accepted.
    pub fn max_bytes(mut self, max_bytes: usize) -> Self {
        self.max_bytes = max_bytes;
        self
    }

    /// Sets the largest number of keys a directory may list.
    pub fn max_keys(mut self, max_keys: usize) -> Self {
        self.max_keys = max_keys;
        self
    }

    /// Builds the source.
    ///
    /// # Errors
    ///
    /// Returns [`WebBotAuthError::InvalidConfig`] when the HTTP client cannot be built.
    pub fn build(self) -> Result<HttpKeyDirectorySource, WebBotAuthError> {
        let client = GuardedClient::builder(self.egress)
            .redirects(0)
            .timeout(self.timeout)
            .connect_timeout(self.timeout.min(Duration::from_secs(5)))
            .user_agent(concat!("adk-rust-web-bot-auth/", env!("CARGO_PKG_VERSION")))
            .build()
            .map_err(|e| WebBotAuthError::InvalidConfig { reason: e.to_string() })?;
        Ok(HttpKeyDirectorySource { client, max_bytes: self.max_bytes, max_keys: self.max_keys })
    }
}

#[async_trait]
impl KeyDirectorySource for HttpKeyDirectorySource {
    async fn fetch(&self, origin: &str) -> Result<FetchedDirectory, WebBotAuthError> {
        let unavailable = |reason: String| WebBotAuthError::DirectoryUnavailable {
            origin: origin.to_string(),
            reason,
        };
        let malformed = |reason: String| WebBotAuthError::MalformedDirectory {
            origin: origin.to_string(),
            reason,
        };
        let refused =
            |reason: String| WebBotAuthError::EgressRefused { origin: origin.to_string(), reason };
        let url = format!("{origin}{DIRECTORY_WELL_KNOWN_PATH}");
        if let Err(e) = reqwest::Url::parse(&url) {
            return Err(WebBotAuthError::InvalidConfig {
                reason: format!("`{origin}` is not an origin: {e}"),
            });
        }
        let mut response = self
            .client
            .get(&url)
            .map_err(|denial| refused(denial.to_string()))?
            .header(reqwest::header::ACCEPT, DIRECTORY_MEDIA_TYPE)
            .send()
            .await
            .map_err(|error| match find_denial(&error) {
                Some(denial) => refused(denial.to_string()),
                None => unavailable(error.without_url().to_string()),
            })?;
        let status = response.status();
        if status != reqwest::StatusCode::OK {
            return Err(unavailable(format!(
                "it answered {status}; a directory answers 200 OK and redirects are not followed"
            )));
        }
        let media_type = response
            .headers()
            .get(reqwest::header::CONTENT_TYPE)
            .and_then(|value| value.to_str().ok())
            .and_then(|value| value.split(';').next())
            .map(|value| value.trim().to_ascii_lowercase());
        if media_type.as_deref() != Some(DIRECTORY_MEDIA_TYPE) {
            return Err(malformed(format!(
                "media type is {}, not {DIRECTORY_MEDIA_TYPE}",
                media_type.as_deref().unwrap_or("absent")
            )));
        }
        if response.content_length().is_some_and(|length| length > self.max_bytes as u64) {
            return Err(malformed(format!("it is larger than {} bytes", self.max_bytes)));
        }
        let max_age = cache_lifetime(response.headers());
        let mut body = Vec::new();
        while let Some(chunk) =
            response.chunk().await.map_err(|e| unavailable(e.without_url().to_string()))?
        {
            if body.len() + chunk.len() > self.max_bytes {
                return Err(malformed(format!("it is larger than {} bytes", self.max_bytes)));
            }
            body.extend_from_slice(&chunk);
        }
        let directory = KeyDirectory::parse(&body, self.max_keys).map_err(malformed)?;
        Ok(FetchedDirectory { directory, max_age })
    }
}

/// The freshness lifetime of a response (RFC 9111 §4.2.1, less its `Age`): `no-store` and
/// `no-cache` give zero, then `max-age`, then `Expires` minus `Date`.
fn cache_lifetime(headers: &reqwest::header::HeaderMap) -> Option<Duration> {
    let header = |name: reqwest::header::HeaderName| {
        headers.get(name).and_then(|value| value.to_str().ok()).map(str::to_string)
    };
    let mut lifetime = None;
    if let Some(cache_control) = header(reqwest::header::CACHE_CONTROL) {
        for directive in cache_control.split(',').map(|d| d.trim().to_ascii_lowercase()) {
            if directive == "no-store" || directive == "no-cache" {
                return Some(Duration::ZERO);
            }
            if lifetime.is_none() {
                lifetime = directive
                    .strip_prefix("max-age=")
                    .and_then(|v| v.trim_matches('"').parse::<u64>().ok())
                    .map(Duration::from_secs);
            }
        }
    }
    if lifetime.is_none()
        && let Some(expires) = header(reqwest::header::EXPIRES)
    {
        let date = header(reqwest::header::DATE).and_then(|d| httpdate::parse_http_date(&d).ok());
        lifetime = Some(match (httpdate::parse_http_date(&expires), date) {
            (Ok(expires), Some(date)) => expires.duration_since(date).unwrap_or_default(),
            (Ok(_), None) | (Err(_), _) => Duration::ZERO,
        });
    }
    let age = header(reqwest::header::AGE).and_then(|a| a.parse::<u64>().ok()).unwrap_or(0);
    lifetime.map(|lifetime| lifetime.saturating_sub(Duration::from_secs(age)))
}

/// How the verifier caches key directories (draft Appendix C.4–C.5).
///
/// A directory is fresh for its HTTP cache lifetime clamped to `min_ttl..=max_ttl`, or
/// `default_ttl` when the response declares none. A failed refresh never evicts a cached
/// directory (draft §6.10): the stale copy keeps verifying for up to `stale_if_error` past its
/// freshness. A failed fetch with nothing cached is remembered for `negative_ttl`, so a burst of
/// requests naming a broken directory fetches it once.
///
/// # Example
///
/// ```rust
/// use std::time::Duration;
/// use adk_auth::web_bot_auth::DirectoryCachePolicy;
///
/// let policy = DirectoryCachePolicy { max_ttl: Duration::from_secs(3600), ..Default::default() };
/// assert_eq!(policy.min_ttl, Duration::from_secs(60));
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DirectoryCachePolicy {
    /// Shortest time a directory is cached, and the least age before an unknown `keyid` may
    /// trigger a refetch. Default 60 s.
    pub min_ttl: Duration,
    /// Longest time a directory is cached. Default 24 h.
    pub max_ttl: Duration,
    /// Lifetime when the response declares none. Default 5 min.
    pub default_ttl: Duration,
    /// How long a stale directory keeps verifying while refreshes fail. Default 1 h.
    pub stale_if_error: Duration,
    /// How long a failed fetch is remembered. Default 30 s (the draft caps it at 5 min).
    pub negative_ttl: Duration,
    /// Most origins held at once; the oldest entry is evicted beyond it. Default 10,000.
    pub max_entries: usize,
}

impl Default for DirectoryCachePolicy {
    fn default() -> Self {
        Self {
            min_ttl: Duration::from_secs(60),
            max_ttl: Duration::from_secs(24 * 60 * 60),
            default_ttl: Duration::from_secs(5 * 60),
            stale_if_error: Duration::from_secs(60 * 60),
            negative_ttl: Duration::from_secs(30),
            max_entries: 10_000,
        }
    }
}

#[derive(Debug, Clone)]
enum CacheEntry {
    Directory {
        directory: Arc<KeyDirectory>,
        fetched_at: DateTime<Utc>,
        fresh_until: DateTime<Utc>,
    },
    Failed {
        error: WebBotAuthError,
        until: DateTime<Utc>,
        stale: Option<Box<CacheEntry>>,
    },
}

/// The verifier's per-origin directory cache: fresh hits, coalesced fetches, stale-on-error and
/// negative caching.
pub(crate) struct DirectoryCache {
    source: Arc<dyn KeyDirectorySource>,
    policy: DirectoryCachePolicy,
    clock: Arc<dyn Clock>,
    entries: RwLock<HashMap<String, CacheEntry>>,
    inflight: Mutex<HashMap<String, Arc<Mutex<()>>>>,
}

impl std::fmt::Debug for DirectoryCache {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("DirectoryCache").field("policy", &self.policy).finish_non_exhaustive()
    }
}

impl DirectoryCache {
    pub(crate) fn new(
        source: Arc<dyn KeyDirectorySource>,
        policy: DirectoryCachePolicy,
        clock: Arc<dyn Clock>,
    ) -> Self {
        Self {
            source,
            policy,
            clock,
            entries: RwLock::new(HashMap::new()),
            inflight: Mutex::new(HashMap::new()),
        }
    }

    /// The key `keyid` from the directory at `origin`.
    pub(crate) async fn key(
        &self,
        origin: &str,
        keyid: &str,
    ) -> Result<DirectoryKey, WebBotAuthError> {
        if let Lookup::Done(result) = self.lookup(origin, keyid).await {
            return result;
        }
        // Concurrent misses for one origin share a single fetch (draft Appendix C.3).
        let lock = {
            let mut inflight = self.inflight.lock().await;
            Arc::clone(inflight.entry(origin.to_string()).or_default())
        };
        let result = {
            let _guard = lock.lock().await;
            // Another task may have refreshed the entry while this one waited.
            match self.lookup(origin, keyid).await {
                Lookup::Done(result) => result,
                Lookup::Fetch => self.refresh(origin, keyid).await,
            }
        };
        let mut inflight = self.inflight.lock().await;
        if inflight.get(origin).is_some_and(|l| Arc::strong_count(l) <= 2) {
            inflight.remove(origin);
        }
        result
    }

    async fn lookup(&self, origin: &str, keyid: &str) -> Lookup {
        let now = self.clock.now();
        let entries = self.entries.read().await;
        match entries.get(origin) {
            Some(CacheEntry::Directory { directory, fetched_at, fresh_until })
                if now < *fresh_until =>
            {
                match directory.find(keyid) {
                    Some(key) => Lookup::Done(Ok(key.clone())),
                    // An unknown keyid may be a key added since; refetch once the entry is old
                    // enough that a stream of invented keyids cannot force a fetch per request.
                    None if now - *fetched_at >= chrono_duration(self.policy.min_ttl) => {
                        Lookup::Fetch
                    }
                    None => Lookup::Done(Err(unknown_key(origin, keyid))),
                }
            }
            Some(CacheEntry::Failed { error, until, stale }) if now < *until => {
                match stale.as_deref().and_then(|stale| self.usable_stale(stale, now)) {
                    Some(directory) => Lookup::Done(
                        directory.find(keyid).cloned().ok_or_else(|| unknown_key(origin, keyid)),
                    ),
                    None => Lookup::Done(Err(error.clone())),
                }
            }
            Some(CacheEntry::Directory { .. } | CacheEntry::Failed { .. }) | None => Lookup::Fetch,
        }
    }

    fn usable_stale(&self, entry: &CacheEntry, now: DateTime<Utc>) -> Option<Arc<KeyDirectory>> {
        match entry {
            CacheEntry::Directory { directory, fresh_until, .. }
                if now < *fresh_until + chrono_duration(self.policy.stale_if_error) =>
            {
                Some(Arc::clone(directory))
            }
            CacheEntry::Directory { .. } | CacheEntry::Failed { .. } => None,
        }
    }

    async fn refresh(&self, origin: &str, keyid: &str) -> Result<DirectoryKey, WebBotAuthError> {
        let fetched = self.source.fetch(origin).await;
        let now = self.clock.now();
        let mut entries = self.entries.write().await;
        match fetched {
            Ok(fetched) => {
                let ttl = fetched
                    .max_age
                    .unwrap_or(self.policy.default_ttl)
                    .clamp(self.policy.min_ttl, self.policy.max_ttl.max(self.policy.min_ttl));
                let directory = Arc::new(fetched.directory);
                let key = directory.find(keyid).cloned();
                // A directory that resolves is evidence and replaces what was cached (draft §4.4).
                self.insert(
                    &mut entries,
                    origin,
                    CacheEntry::Directory {
                        directory,
                        fetched_at: now,
                        fresh_until: now + chrono_duration(ttl),
                    },
                    now,
                );
                tracing::debug!(
                    directory.origin = origin,
                    cache.ttl_secs = ttl.as_secs(),
                    "key directory fetched"
                );
                key.ok_or_else(|| unknown_key(origin, keyid))
            }
            Err(error) => {
                // A failed fetch is not evidence and never evicts a cached directory (draft §6.10).
                let stale = match entries.remove(origin) {
                    Some(entry @ CacheEntry::Directory { .. }) => Some(Box::new(entry)),
                    Some(CacheEntry::Failed { stale, .. }) => stale,
                    None => None,
                };
                let usable = stale.as_deref().and_then(|entry| self.usable_stale(entry, now));
                tracing::warn!(directory.origin = origin, error = %error, stale = usable.is_some(), "key directory fetch failed");
                let result = match &usable {
                    Some(directory) => {
                        directory.find(keyid).cloned().ok_or_else(|| unknown_key(origin, keyid))
                    }
                    None => Err(error.clone()),
                };
                let until = now + chrono_duration(self.policy.negative_ttl);
                self.insert(&mut entries, origin, CacheEntry::Failed { error, until, stale }, now);
                result
            }
        }
    }

    fn insert(
        &self,
        entries: &mut HashMap<String, CacheEntry>,
        origin: &str,
        entry: CacheEntry,
        now: DateTime<Utc>,
    ) {
        if entries.len() >= self.policy.max_entries && !entries.contains_key(origin) {
            entries.retain(|_, entry| match entry {
                CacheEntry::Directory { fresh_until, .. } => *fresh_until > now,
                CacheEntry::Failed { until, .. } => *until > now,
            });
            if entries.len() >= self.policy.max_entries {
                let oldest = entries
                    .iter()
                    .min_by_key(|(_, entry)| match entry {
                        CacheEntry::Directory { fetched_at, .. } => *fetched_at,
                        CacheEntry::Failed { until, .. } => *until,
                    })
                    .map(|(origin, _)| origin.clone());
                if let Some(oldest) = oldest {
                    entries.remove(&oldest);
                }
            }
        }
        entries.insert(origin.to_string(), entry);
    }
}

enum Lookup {
    Done(Result<DirectoryKey, WebBotAuthError>),
    Fetch,
}

fn unknown_key(origin: &str, keyid: &str) -> WebBotAuthError {
    WebBotAuthError::UnknownKey { origin: origin.to_string(), keyid: keyid.to_string() }
}

fn chrono_duration(duration: Duration) -> chrono::Duration {
    chrono::Duration::from_std(duration).unwrap_or(chrono::Duration::MAX)
}

#[cfg(test)]
mod tests {
    use super::*;
    use reqwest::header::{HeaderMap, HeaderValue};

    fn headers(pairs: &[(&'static str, &str)]) -> HeaderMap {
        let mut map = HeaderMap::new();
        for (name, value) in pairs {
            map.append(*name, HeaderValue::from_str(value).unwrap());
        }
        map
    }

    #[test]
    fn cache_lifetime_reads_cache_control_expires_and_age() {
        assert_eq!(
            cache_lifetime(&headers(&[("cache-control", "public, max-age=86400")])),
            Some(Duration::from_secs(86_400))
        );
        assert_eq!(
            cache_lifetime(&headers(&[("cache-control", "max-age=600, no-cache")])),
            Some(Duration::ZERO)
        );
        assert_eq!(
            cache_lifetime(&headers(&[("cache-control", "max-age=600"), ("age", "100")])),
            Some(Duration::from_secs(500))
        );
        assert_eq!(
            cache_lifetime(&headers(&[
                ("date", "Sun, 25 Oct 2026 02:00:00 GMT"),
                ("expires", "Sun, 25 Oct 2026 03:00:00 GMT"),
            ])),
            Some(Duration::from_secs(3600))
        );
        assert_eq!(cache_lifetime(&headers(&[("expires", "0")])), Some(Duration::ZERO));
        assert_eq!(cache_lifetime(&headers(&[])), None);
    }

    #[test]
    fn defaults_follow_the_draft_ssrf_guidance() {
        let builder = HttpKeyDirectorySource::builder();
        assert_eq!(builder.egress, EgressPolicy::public_only());
        assert_eq!(builder.timeout, Duration::from_secs(5));
        assert_eq!(builder.max_bytes, 64 * 1024);
        assert_eq!(builder.max_keys, 64);
        assert_eq!(
            DirectoryCachePolicy::default(),
            DirectoryCachePolicy {
                min_ttl: Duration::from_secs(60),
                max_ttl: Duration::from_secs(86_400),
                default_ttl: Duration::from_secs(300),
                stale_if_error: Duration::from_secs(3600),
                negative_ttl: Duration::from_secs(30),
                max_entries: 10_000,
            }
        );
    }

    #[tokio::test]
    async fn the_cache_evicts_the_oldest_origin_beyond_max_entries() {
        let clock = adk_core::ManualClock::new(chrono::Utc::now());
        let source = Arc::new(InMemoryKeyDirectorySource::new());
        let key = super::super::Ed25519SigningKey::generate().unwrap();
        for origin in ["https://a.example", "https://b.example", "https://c.example"] {
            source.insert(origin, KeyDirectory::from_keys([&key]), None).await;
        }
        let policy = DirectoryCachePolicy { max_entries: 2, ..DirectoryCachePolicy::default() };
        let cache = DirectoryCache::new(source.clone(), policy, Arc::new(clock.clone()));
        for origin in ["https://a.example", "https://b.example", "https://c.example"] {
            cache.key(origin, key.thumbprint()).await.unwrap();
            clock.advance(Duration::from_secs(1));
        }
        assert_eq!(cache.entries.read().await.len(), 2);
        assert!(!cache.entries.read().await.contains_key("https://a.example"));
        cache.key("https://a.example", key.thumbprint()).await.unwrap();
        assert_eq!(source.fetch_count(), 4);
    }

    #[tokio::test]
    async fn egress_refuses_non_public_origins() {
        let source = HttpKeyDirectorySource::new().unwrap();
        for origin in [
            "https://127.0.0.1",
            "https://10.0.0.1",
            "https://169.254.169.254",
            "https://[::1]",
            "https://localhost",
            "https://api.localhost",
            "http://bot.example",
            "https://[::ffff:169.254.169.254]",
            "https://168.63.129.16",
        ] {
            assert!(
                matches!(source.fetch(origin).await, Err(WebBotAuthError::EgressRefused { .. })),
                "{origin} must be refused"
            );
        }
        assert!(source.client.get("https://bot.example/.well-known/x").is_ok());
    }
}