ferrohealth-session 0.4.0

The OpenID Connect sign-in and the session of the FerroHEALTH operator consoles: the authorization code flow with PKCE over ferrohealth-smart, a server-side or sealed-cookie session with idle and absolute lifetimes counted from the sign-in time, and the session-watch route
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
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
// SPDX-FileCopyrightText: Cadasto B.V.
// SPDX-License-Identifier: BUSL-1.1

use std::time::Duration;

use base64::Engine as _;
use ferrohealth_secret::secret::Secret;
use ferrohealth_smart::oidc::{PendingSignIn, TokenTypeHint};
use http::header::COOKIE;
use http::{HeaderMap, HeaderValue};

use super::{SessionError, Sessions};
use crate::clock::ManualClock;
use crate::config::{SessionSection, StoreKind};
use crate::cookies::CookieNames;
use crate::record::{Credential, Held, SignedIn};
use crate::return_path::ReturnPath;

const START: i64 = 1_800_000_000;

fn section(store: StoreKind) -> SessionSection {
    let key = (store == StoreKind::Sealed).then(|| {
        let mut raw = [0_u8; 64];
        aws_lc_rs::rand::fill(&mut raw).expect("the random source works");
        Secret::new(base64::engine::general_purpose::STANDARD.encode(raw))
    });
    SessionSection {
        store,
        sign_in_timeout_s: 60,
        max_sign_ins: 4,
        idle_timeout_s: 60,
        absolute_timeout_s: 600,
        max_sessions: 2,
        key,
        ..SessionSection::default()
    }
}

fn store(store: StoreKind) -> (Sessions, ManualClock) {
    store_of(&section(store))
}

fn store_of(section: &SessionSection) -> (Sessions, ManualClock) {
    let now = ManualClock::new(START);
    let settings = section.check_with(false).expect("a valid section");
    (
        Sessions::with_clock(
            settings,
            CookieNames::for_console("synthetic_console"),
            now.clock(),
        ),
        now,
    )
}

fn both() -> [StoreKind; 2] {
    [StoreKind::Memory, StoreKind::Sealed]
}

fn signed_in() -> SignedIn {
    SignedIn {
        operator: Some(String::from("Synthetic Operator")),
        subject: Some(String::from("synthetic-subject")),
        scopes: vec![String::from("openid"), String::from("profile")],
        credential: Credential::Bearer(Secret::new("synthetic-access-token")),
        expires_in: None,
        refresh_token: None,
        id_token: Some(Secret::new("synthetic-id-token")),
    }
}

/// The request `Cookie` header that sends back what `set` set.
fn sent(set: &HeaderValue) -> HeaderMap {
    let pair = set
        .to_str()
        .expect("ASCII")
        .split(';')
        .next()
        .expect("a pair")
        .to_owned();
    let mut headers = HeaderMap::new();
    headers.insert(COOKIE, pair.parse().expect("a header"));
    headers
}

fn is_removal(cookies: &[HeaderValue]) -> bool {
    cookies
        .iter()
        .any(|cookie| cookie.to_str().is_ok_and(|text| text.contains("Max-Age=0")))
}

#[test]
fn a_pending_sign_in_is_taken_once_with_its_return_path() {
    for kind in both() {
        let (sessions, _now) = store(kind);
        let pending = PendingSignIn::mint().expect("minted");
        let state = pending.state().to_owned();
        let back = ReturnPath::parse("/sources?page=2", "/sign-in");
        let cookie = sessions.begin(pending, back).expect("begun");
        assert!(
            cookie.to_str().expect("ASCII").contains("Max-Age=60"),
            "{kind:?}"
        );
        let taken = sessions.take_pending(&sent(&cookie)).expect("usable");
        assert!(is_removal(&taken.cookies), "the sign-in cookie is removed");
        let pending = taken.value.expect("pending");
        assert_eq!(state, pending.sign_in.state(), "{kind:?}");
        assert_eq!(
            Some("/sources?page=2"),
            pending.back.as_ref().map(ReturnPath::as_str)
        );
        if kind == StoreKind::Memory {
            let again = sessions.take_pending(&sent(&cookie)).expect("usable");
            assert!(again.value.is_none(), "a memory sign-in is taken once");
        }
    }
}

#[test]
fn an_expired_sign_in_is_refused() {
    for kind in both() {
        let (sessions, now) = store(kind);
        let cookie = sessions
            .begin(PendingSignIn::mint().expect("minted"), None)
            .expect("begun");
        now.advance(60);
        let taken = sessions.take_pending(&sent(&cookie)).expect("usable");
        assert!(taken.value.is_none(), "{kind:?}");
    }
}

/// Reads the session `cookie` names every 50 s, `reads` times, and answers
/// the cookie the last read left.
fn active(sessions: &Sessions, now: &ManualClock, cookie: HeaderValue, reads: u32) -> HeaderValue {
    let mut cookie = cookie;
    for _ in 0..reads {
        now.advance(50);
        let held = sessions.held(&sent(&cookie)).expect("usable");
        if let Some(resealed) = held.cookies.first() {
            cookie = resealed.clone();
        }
    }
    cookie
}

/// The last millisecond of the second `START`, where a clock counted in
/// whole seconds lies the furthest behind the time.
const LATE: i64 = START * 1000 + 999;

/// A sign-in with a 1 s timeout begun at `LATE`, with its cookie.
fn late_sign_in(kind: StoreKind) -> (Sessions, ManualClock, HeaderValue) {
    let (sessions, now) = store_of(&SessionSection {
        sign_in_timeout_s: 1,
        ..section(kind)
    });
    now.set_millis(LATE);
    let cookie = sessions
        .begin(PendingSignIn::mint().expect("minted"), None)
        .expect("begun");
    (sessions, now, cookie)
}

/// A session of `signed_in` established at `LATE`, with its cookie.
fn late_session(kind: StoreKind, signed_in: SignedIn) -> (Sessions, ManualClock, HeaderValue) {
    let (sessions, now) = store(kind);
    now.set_millis(LATE);
    let cookie = sessions
        .establish(&HeaderMap::new(), signed_in)
        .expect("room");
    (sessions, now, cookie)
}

#[test]
fn a_sign_in_begun_late_in_a_second_lasts_its_whole_timeout() {
    for kind in both() {
        let (sessions, now, cookie) = late_sign_in(kind);
        now.advance_millis(999);
        let taken = sessions.take_pending(&sent(&cookie)).expect("usable");
        assert!(
            taken.value.is_some(),
            "{kind:?}: 999 ms into a 1 s sign-in timeout"
        );
    }
}

#[test]
fn a_sign_in_begun_late_in_a_second_ends_within_a_second_of_its_timeout() {
    for kind in both() {
        let (sessions, now, cookie) = late_sign_in(kind);
        now.advance_millis(2000);
        let taken = sessions.take_pending(&sent(&cookie)).expect("usable");
        assert!(taken.value.is_none(), "{kind:?}: 2 s into a 1 s timeout");
    }
}

#[test]
fn an_idle_lifetime_begun_late_in_a_second_lasts_its_whole_length() {
    for kind in both() {
        let (sessions, now, cookie) = late_session(kind, signed_in());
        now.advance_millis(59_999);
        let left = sessions.left(&sent(&cookie), false).expect("usable").value;
        assert_eq!(
            Some(1),
            left.map(|left| left.idle),
            "{kind:?}: 59.999 s into a 60 s idle lifetime, rounded up"
        );
    }
}

#[test]
fn an_idle_lifetime_begun_late_in_a_second_ends_within_a_second_of_its_length() {
    for kind in both() {
        let (sessions, now, cookie) = late_session(kind, signed_in());
        now.advance_millis(61_000);
        let held = sessions.held(&sent(&cookie)).expect("usable").value;
        assert!(matches!(held, Held::Ended), "{kind:?}: {held:?}");
    }
}

#[test]
fn an_absolute_lifetime_begun_late_in_a_second_lasts_its_whole_length() {
    for kind in both() {
        let (sessions, now, cookie) = late_session(kind, signed_in());
        let cookie = active(&sessions, &now, cookie, 11);
        now.advance_millis(49_999);
        let held = sessions.held(&sent(&cookie)).expect("usable").value;
        assert!(
            matches!(held, Held::SignedIn(_)),
            "{kind:?}: 599.999 s into a 600 s absolute lifetime: {held:?}"
        );
    }
}

#[test]
fn a_memory_access_token_begun_late_in_a_second_lasts_its_whole_lifetime() {
    let (sessions, now, cookie) = late_session(
        StoreKind::Memory,
        SignedIn {
            expires_in: Some(Duration::from_secs(30)),
            ..signed_in()
        },
    );
    now.advance_millis(29_999);
    let held = sessions.held(&sent(&cookie)).expect("usable").value;
    assert!(
        matches!(held, Held::SignedIn(_)),
        "29.999 s into a 30 s access token: {held:?}"
    );
}

#[test]
fn a_session_carries_the_sign_in_time_and_what_the_sign_in_left() {
    for kind in both() {
        let (sessions, _now) = store(kind);
        let cookie = sessions
            .establish(&HeaderMap::new(), signed_in())
            .expect("room");
        let held = sessions.held(&sent(&cookie)).expect("usable").value;
        let Held::SignedIn(session) = held else {
            panic!("{kind:?}: {held:?}");
        };
        assert_eq!(START, session.signed_in_at(), "the sign-in time");
        assert_eq!(Some("Synthetic Operator"), session.operator());
        assert_eq!(Some("synthetic-subject"), session.subject());
        assert_eq!(2, session.scopes().len(), "the granted scopes");
        assert_eq!(
            &Credential::Bearer(Secret::new("synthetic-access-token")),
            session.credential(),
            "the access token"
        );
        assert_eq!((60, 600), (session.left().idle, session.left().absolute));
    }
}

#[test]
fn the_idle_lifetime_ends_a_session_and_activity_moves_it() {
    for kind in both() {
        let (sessions, now) = store(kind);
        let mut cookie = sessions
            .establish(&HeaderMap::new(), signed_in())
            .expect("room");
        now.advance(50);
        let held = sessions.held(&sent(&cookie)).expect("usable");
        assert!(matches!(held.value, Held::SignedIn(_)), "{kind:?}");
        if let Some(resealed) = held.cookies.first() {
            cookie = resealed.clone();
        }
        now.advance(50);
        assert!(
            matches!(
                sessions.held(&sent(&cookie)).expect("usable").value,
                Held::SignedIn(_)
            ),
            "{kind:?}: the read at 50 s started the idle lifetime again"
        );
        now.advance(60);
        let held = sessions.held(&sent(&cookie)).expect("usable");
        assert!(
            matches!(held.value, Held::Ended),
            "{kind:?}: sixty idle seconds"
        );
        assert!(
            is_removal(&held.cookies),
            "an ended session's cookie is removed"
        );
    }
}

#[test]
fn the_absolute_lifetime_ends_a_session_whatever_the_activity() {
    for kind in both() {
        let (sessions, now) = store(kind);
        let cookie = sessions
            .establish(&HeaderMap::new(), signed_in())
            .expect("room");
        let cookie = active(&sessions, &now, cookie, 12);
        let held = sessions.held(&sent(&cookie)).expect("usable").value;
        assert!(
            matches!(held, Held::Ended),
            "{kind:?}: 600 s from the sign-in: {held:?}"
        );
    }
}

#[test]
fn a_watch_read_never_counts_as_activity_and_an_extension_does() {
    for kind in both() {
        let (sessions, now) = store(kind);
        let cookie = sessions
            .establish(&HeaderMap::new(), signed_in())
            .expect("room");
        now.advance(30);
        let read = sessions.left(&sent(&cookie), false).expect("usable");
        assert!(read.cookies.is_empty(), "{kind:?}: a read sets nothing");
        let read = read.value.expect("live");
        assert_eq!((30, 570), (read.idle, read.absolute), "{kind:?}");
        let again = sessions
            .left(&sent(&cookie), false)
            .expect("usable")
            .value
            .expect("live");
        assert_eq!(read, again, "{kind:?}: the read moved nothing");
        let extended = sessions.left(&sent(&cookie), true).expect("usable");
        assert_eq!((60, 570), {
            let left = extended.value.expect("live");
            (left.idle, left.absolute)
        });
        assert_eq!(
            kind == StoreKind::Sealed,
            !extended.cookies.is_empty(),
            "a sealed extension seals the cookie again"
        );
    }
}

#[test]
fn an_ended_session_hands_back_its_hint_and_its_revocable_token() {
    for kind in both() {
        let (sessions, _now) = store(kind);
        let cookie = sessions
            .establish(&HeaderMap::new(), signed_in())
            .expect("room");
        let ended = sessions.end(&sent(&cookie)).expect("usable");
        assert!(is_removal(&ended.cookies), "the cookie is removed");
        let ended = ended.value.expect("held");
        assert_eq!(
            Some("synthetic-id-token"),
            ended.id_token.as_ref().map(Secret::expose),
            "{kind:?}"
        );
        let (token, hint) = ended.revocable.expect("a bearer session");
        assert_eq!(
            ("synthetic-access-token", TokenTypeHint::AccessToken),
            (token.expose(), hint)
        );
        if kind == StoreKind::Memory {
            assert!(
                matches!(
                    sessions.held(&sent(&cookie)).expect("usable").value,
                    Held::Ended
                ),
                "an ended memory session is gone"
            );
        }
    }
}

#[test]
fn a_basic_sign_in_carries_its_password_and_nothing_to_revoke() {
    for kind in both() {
        let (sessions, _now) = store(kind);
        let signed_in = SignedIn::basic("synthetic-user", Secret::new("synthetic-password"));
        let cookie = sessions
            .establish(&HeaderMap::new(), signed_in)
            .expect("room");
        let Held::SignedIn(session) = sessions.held(&sent(&cookie)).expect("usable").value else {
            panic!("{kind:?}: live");
        };
        assert!(
            matches!(session.credential(), Credential::Basic { username, password }
                if username == "synthetic-user" && password.expose() == "synthetic-password"),
            "{kind:?}"
        );
        let ended = sessions
            .end(&sent(&cookie))
            .expect("usable")
            .value
            .expect("held");
        assert!(ended.revocable.is_none(), "nothing to revoke");
    }
}

#[test]
fn an_access_token_no_refresh_token_replaces_ends_the_session() {
    for kind in both() {
        let (sessions, now) = store(kind);
        let cookie = sessions
            .establish(
                &HeaderMap::new(),
                SignedIn {
                    expires_in: Some(Duration::from_secs(30)),
                    ..signed_in()
                },
            )
            .expect("room");
        let left = sessions
            .left(&sent(&cookie), false)
            .expect("usable")
            .value
            .expect("live");
        assert_eq!((30, 30), (left.idle, left.absolute), "{kind:?}");
        now.advance(30);
        assert!(
            matches!(
                sessions.held(&sent(&cookie)).expect("usable").value,
                Held::Ended
            ),
            "{kind:?}"
        );
    }
}

#[test]
fn a_sealed_store_keeps_no_refresh_token() {
    let (sessions, now) = store(StoreKind::Sealed);
    let cookie = sessions
        .establish(
            &HeaderMap::new(),
            SignedIn {
                expires_in: Some(Duration::from_secs(30)),
                refresh_token: Some(Secret::new("synthetic-refresh-token")),
                ..signed_in()
            },
        )
        .expect("sealed");
    now.advance(30);
    assert!(
        matches!(
            sessions.held(&sent(&cookie)).expect("usable").value,
            Held::Ended
        ),
        "the session ends with its access token"
    );
}

#[test]
fn a_memory_session_with_a_refresh_token_outlives_its_access_token() {
    let (sessions, now) = store(StoreKind::Memory);
    let cookie = sessions
        .establish(
            &HeaderMap::new(),
            SignedIn {
                expires_in: Some(Duration::from_secs(30)),
                refresh_token: Some(Secret::new("synthetic-refresh-token")),
                ..signed_in()
            },
        )
        .expect("room");
    now.advance(30);
    assert!(
        matches!(
            sessions.held(&sent(&cookie)).expect("usable").value,
            Held::TokenExpired
        ),
        "live, waiting for its refresh"
    );
}

#[test]
fn a_tampered_or_foreign_sealed_cookie_is_no_session() {
    let (sessions, _now) = store(StoreKind::Sealed);
    let (other, _other_now) = store(StoreKind::Sealed);
    let cookie = sessions
        .establish(&HeaderMap::new(), signed_in())
        .expect("sealed");
    let held = other.held(&sent(&cookie)).expect("usable");
    assert!(
        matches!(held.value, Held::Ended),
        "another key opens nothing"
    );
    let text = cookie.to_str().expect("ASCII");
    let (pair, _attributes) = text.split_once(';').expect("attributes");
    let (name, value) = pair.split_once('=').expect("a pair");
    let mut bytes = value.as_bytes().to_vec();
    let last = bytes.last_mut().expect("a value");
    *last = if *last == b'A' { b'B' } else { b'A' };
    let tampered = format!("{name}={}", String::from_utf8(bytes).expect("ASCII"));
    let mut headers = HeaderMap::new();
    headers.insert(COOKIE, tampered.parse().expect("a header"));
    assert!(
        matches!(sessions.held(&headers).expect("usable").value, Held::Ended),
        "tampered"
    );
}

#[test]
fn a_sealed_session_too_large_for_a_browser_is_refused() {
    let (sessions, _now) = store(StoreKind::Sealed);
    let refused = sessions.establish(
        &HeaderMap::new(),
        SignedIn {
            scopes: vec!["x".repeat(4096)],
            ..signed_in()
        },
    );
    assert!(
        matches!(refused, Err(SessionError::TooLarge { .. })),
        "{refused:?}"
    );
}

#[test]
fn a_full_sign_in_pool_drops_its_oldest_and_never_touches_the_sessions() {
    let (sessions, _now) = store(StoreKind::Memory);
    let session = sessions
        .establish(&HeaderMap::new(), signed_in())
        .expect("room");
    let first = sessions
        .begin(PendingSignIn::mint().expect("minted"), None)
        .expect("begun");
    let mut last = first.clone();
    for _ in 0..100 {
        last = sessions
            .begin(PendingSignIn::mint().expect("minted"), None)
            .expect("begun");
    }
    let occupancy = sessions.occupancy().expect("usable").expect("memory");
    assert_eq!(
        (4, 1),
        (occupancy.sign_ins, occupancy.sessions),
        "the bound holds"
    );
    assert!(
        sessions
            .take_pending(&sent(&first))
            .expect("usable")
            .value
            .is_none(),
        "dropped"
    );
    assert!(
        sessions
            .take_pending(&sent(&last))
            .expect("usable")
            .value
            .is_some(),
        "kept"
    );
    assert!(matches!(
        sessions.held(&sent(&session)).expect("usable").value,
        Held::SignedIn(_)
    ));
}

#[test]
fn a_full_session_pool_refuses_a_new_session_and_keeps_the_live_ones() {
    let (sessions, _now) = store(StoreKind::Memory);
    let first = sessions
        .establish(&HeaderMap::new(), signed_in())
        .expect("room");
    let second = sessions
        .establish(&HeaderMap::new(), signed_in())
        .expect("room");
    let refused = sessions.establish(&HeaderMap::new(), signed_in());
    assert!(matches!(refused, Err(SessionError::Full)), "{refused:?}");
    for kept in [first, second] {
        assert!(matches!(
            sessions.held(&sent(&kept)).expect("usable").value,
            Held::SignedIn(_)
        ));
    }
}

#[test]
fn signing_in_again_ends_the_session_the_browser_held() {
    let (sessions, _now) = store(StoreKind::Memory);
    let first = sessions
        .establish(&HeaderMap::new(), signed_in())
        .expect("room");
    let second = sessions
        .establish(&sent(&first), signed_in())
        .expect("room");
    let third = sessions.establish(&sent(&second), signed_in());
    assert!(third.is_ok(), "the pool of two never fills: {third:?}");
    assert!(matches!(
        sessions.held(&sent(&first)).expect("usable").value,
        Held::Ended
    ));
}

#[test]
fn a_poisoned_memory_store_answers_poisoned_everywhere() {
    let (sessions, _now) = store(StoreKind::Memory);
    let cookie = sessions
        .establish(&HeaderMap::new(), signed_in())
        .expect("room");
    sessions.memory().expect("memory").poison();
    assert!(matches!(
        sessions.held(&sent(&cookie)),
        Err(SessionError::Poisoned)
    ));
    assert!(matches!(
        sessions.begin(PendingSignIn::mint().expect("minted"), None),
        Err(SessionError::Poisoned)
    ));
    assert!(matches!(
        sessions.end(&sent(&cookie)),
        Err(SessionError::Poisoned)
    ));
}

#[test]
fn no_token_password_or_id_reaches_a_debug_rendering() {
    for kind in both() {
        let (sessions, _now) = store(kind);
        let cookie = sessions
            .establish(
                &HeaderMap::new(),
                SignedIn {
                    refresh_token: Some(Secret::new("synthetic-refresh-token")),
                    ..signed_in()
                },
            )
            .expect("room");
        let held = sessions.held(&sent(&cookie)).expect("usable");
        let ended = sessions.end(&sent(&cookie)).expect("usable");
        let id = sent(&cookie)
            .get(COOKIE)
            .and_then(|value| value.to_str().ok())
            .and_then(|pair| pair.split_once('='))
            .map(|(_, id)| id.to_owned())
            .expect("an id");
        let shown = format!(
            "{sessions:?} {:?} {:?} {:?}",
            held.value,
            ended.value,
            signed_in()
        );
        for secret in [
            "synthetic-access-token",
            "synthetic-id-token",
            "synthetic-refresh-token",
            &id,
        ] {
            assert!(!shown.contains(secret), "{kind:?}: {shown}");
        }
        let basic = SignedIn::basic("synthetic-user", Secret::new("synthetic-password"));
        assert!(
            !format!("{basic:?}").contains("synthetic-password"),
            "the password"
        );
    }
}

#[test]
fn a_session_preference_carries_no_max_age_and_reads_back() {
    for kind in both() {
        let (sessions, _now) = store(kind);
        let cookie = sessions
            .session_preference("sidebar", "rail")
            .expect("a cookie");
        let text = cookie.to_str().expect("text");
        assert_eq!(
            "__Host-synthetic_console_sidebar=rail; HttpOnly; SameSite=Lax; Secure; Path=/", text,
            "{kind:?}: no Max-Age, so the browser drops it with its session"
        );
        assert!(!text.contains("Expires"), "{kind:?}");
        let kept = sessions
            .preference("sidebar", "rail", Duration::from_secs(60))
            .expect("a cookie");
        assert!(
            kept.to_str().expect("text").ends_with("; Max-Age=60"),
            "the kept form keeps its Max-Age"
        );
        let mut headers = HeaderMap::new();
        headers.insert(
            COOKIE,
            HeaderValue::from_static("__Host-synthetic_console_sidebar=rail"),
        );
        assert_eq!(
            Some(String::from("rail")),
            sessions.preference_in(&headers, "sidebar"),
            "{kind:?}"
        );
    }
}