acme-proxy-admin 0.6.0

The operation layer and web admin panel of acme-proxy (internal crate, no semver promise)
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
//! The second-factor operations, which both front ends dispatch to and neither
//! owns.
//!
//! The same split [`crate::admin::users`] makes: no printing, no HTTP, no
//! terminal. `src/cli/webadmin.rs` (`admin user totp …`) and
//! `crates/admin/src/webadmin/handlers/mfa.rs` both come here, which is what keeps the replay
//! guard, the session revocation and the "shown once" rule identical between
//! them.
//!
//! The pure halves live next door -- [`crate::admin::totp`] for RFC 6238 and
//! [`crate::admin::recovery`] for the codes. This file is where they meet a
//! database.

use std::sync::Arc;
use uuid::Uuid;

use tracing::{info, warn};

use crate::admin::{password, recovery, totp};
use acme_proxy_store::admin_recovery_code::AdminRecoveryCode;
use acme_proxy_store::admin_session::AdminSession;
use acme_proxy_store::admin_user::AdminUser;
use acme_proxy_store::db::Database;
use acme_proxy_store::nonce::now_secs;

/// Which of the two things an accepted submission was.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MfaMethod {
    Totp,
    RecoveryCode,
}

impl MfaMethod {
    /// The `method` field of `admin_mfa_verified`. A literal, so it stays
    /// greppable.
    #[must_use]
    pub fn as_str(self) -> &'static str {
        match self {
            MfaMethod::Totp => "totp",
            MfaMethod::RecoveryCode => "recovery_code",
        }
    }
}

/// The result of checking a second-factor submission.
///
/// Every variant but [`MfaOutcome::Accepted`] must reach the client as one
/// `invalid_credentials`, exactly as [`crate::admin::users::AuthOutcome`]'s
/// three failures do -- they are kept apart here so the *log* can say which.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum MfaOutcome {
    Accepted {
        via: MfaMethod,
        /// Unspent recovery codes after this submission. The panel shows it;
        /// a low number is the nudge to regenerate.
        recovery_codes_left: i64,
    },
    /// Neither a valid code for this moment nor an unspent recovery code.
    Rejected,
    /// A *correct* TOTP code, resubmitted inside its own 30-second window
    /// (RFC 6238 §5.2). Refused, and worth its own name: it is what somebody
    /// replaying an observed code looks like.
    Replayed,
    /// No confirmed factor to verify against. Reachable only if a factor was
    /// removed between the password step and this one.
    NotEnrolled,
}

impl MfaOutcome {
    /// The `reason` field of `admin_mfa_failed`, or `""` when it succeeded.
    #[must_use]
    pub fn reason(&self) -> &'static str {
        match self {
            MfaOutcome::Accepted { .. } => "",
            MfaOutcome::Rejected => "wrong_code",
            MfaOutcome::Replayed => "replayed",
            MfaOutcome::NotEnrolled => "no_factor",
        }
    }
}

/// Checks a submission against this operator's factor, then against their
/// recovery codes.
///
/// **Order matters.** A TOTP check is three HMACs; a recovery check is up to ten
/// PBKDF2 runs and a write. The cheap and overwhelmingly common path goes
/// first, and a submission that cannot be a recovery code by shape never starts
/// the scan at all.
///
/// On success the TOTP path claims its time step
/// ([`AdminUser::claim_totp_step`]) and the recovery path spends its code
/// ([`AdminRecoveryCode::consume`]) -- in both cases the database, not this
/// function, is what makes it single-use.
pub async fn verify_second_factor(
    user: &mut AdminUser,
    submitted: &str,
    database: Arc<Database>,
) -> Result<MfaOutcome, sqlx::Error> {
    let Some(secret) = user.totp_secret.clone() else {
        return Ok(MfaOutcome::NotEnrolled);
    };

    let trimmed = submitted.trim();
    if let Some(step) = totp::verify(&secret, trimmed, now_secs()) {
        if !user.claim_totp_step(step, &database).await? {
            return Ok(MfaOutcome::Replayed);
        }
        let left = AdminRecoveryCode::count_unused(user.id, &database).await?;
        return Ok(MfaOutcome::Accepted {
            via: MfaMethod::Totp,
            recovery_codes_left: left,
        });
    }

    let candidate = recovery::normalize(trimmed);
    if !recovery::is_well_formed(&candidate) {
        return Ok(MfaOutcome::Rejected);
    }

    for code in AdminRecoveryCode::list_unused(user.id, &database).await? {
        match password::verify_password_off_runtime(&code.code_hash, &candidate).await {
            Ok(true) => {
                if !AdminRecoveryCode::consume(code.id, &database).await? {
                    // Lost the race to a concurrent submission of this very
                    // code. Refusing is the correct answer for the loser.
                    return Ok(MfaOutcome::Rejected);
                }
                let left = AdminRecoveryCode::count_unused(user.id, &database).await?;
                warn!(event = "admin_mfa_recovery_code_used",
                      outcome = "success",
                      user_id = %user.id,
                      username = %user.username,
                      remaining = left);
                return Ok(MfaOutcome::Accepted {
                    via: MfaMethod::RecoveryCode,
                    recovery_codes_left: left,
                });
            }
            Ok(false) => {}
            Err(error) => {
                // A corrupt row is not a wrong code -- the same distinction
                // `users::authenticate` draws. Skip it and say so; the operator
                // has nine others and no way to guess this from a 401.
                warn!(event = "admin_recovery_code_hash_unreadable",
                      outcome = "failure",
                      user_id = %user.id,
                      code_id = %code.id,
                      error = %error);
            }
        }
    }

    Ok(MfaOutcome::Rejected)
}

/// Mints a secret and stores it as *pending*, returning the two representations
/// the enrolment page shows exactly once.
///
/// Nothing about the operator's current factor changes here: an enrolment that
/// is started and abandoned leaves a live factor live, which is what makes
/// "set up a new phone" safe to begin from an authenticated session.
pub async fn begin_totp_enrolment(
    user: &mut AdminUser,
    base_url: &str,
    database: Arc<Database>,
) -> Result<totp::Enrolment, sqlx::Error> {
    let account = totp::account_label(&user.username, base_url);
    let enrolment = totp::begin_enrolment(totp::ISSUER, &account);
    user.set_totp_pending(&enrolment.secret, &database).await?;
    Ok(enrolment)
}

/// Re-renders an enrolment already begun, or begins one.
///
/// A page reload must not invalidate the secret the operator has just scanned
/// into an app, so the pending bytes are shown again rather than replaced. The
/// two other representations are derived, not stored -- the database keeps only
/// the secret.
pub async fn resume_or_begin_totp_enrolment(
    user: &mut AdminUser,
    base_url: &str,
    database: Arc<Database>,
) -> Result<totp::Enrolment, sqlx::Error> {
    let Some(secret) = user.totp_pending_secret.clone() else {
        return begin_totp_enrolment(user, base_url, database).await;
    };

    let account = totp::account_label(&user.username, base_url);
    let secret_base32 = totp::base32_encode(&secret);
    let uri = totp::provisioning_uri(&secret_base32, totp::ISSUER, &account);
    Ok(totp::Enrolment {
        secret,
        secret_base32,
        uri,
    })
}

/// Confirms a pending enrolment against a code the authenticator produced.
///
/// On success: the pending secret becomes the real one, a fresh recovery set is
/// issued and returned **once**, and every other session this operator holds is
/// revoked. `None` is a wrong code, and leaves `totp_secret` exactly as it was.
///
/// `keep_session` is the token hash of the session doing the enrolling, so the
/// operator is not logged out by their own action; `None` revokes every session,
/// which is what a change made on their behalf should do.
pub async fn confirm_totp_enrolment(
    user: &mut AdminUser,
    code: &str,
    keep_session: Option<&str>,
    database: Arc<Database>,
) -> Result<Option<Vec<String>>, sqlx::Error> {
    let Some(pending) = user.totp_pending_secret.clone() else {
        return Ok(None);
    };

    let Some(step) = totp::verify(&pending, code.trim(), now_secs()) else {
        return Ok(None);
    };

    user.confirm_totp(&database).await?;
    // The code that proved the enrolment must not also work as the first login
    // code. `confirm_totp` cleared the guard, so this is the first claim.
    user.claim_totp_step(step, &database).await?;

    let codes = issue_recovery_codes(user, database.clone()).await?;
    revoke_other_sessions(user, keep_session, database).await?;

    info!(event = "admin_mfa_enabled",
          outcome = "success",
          user_id = %user.id,
          username = %user.username,
          recovery_codes = codes.len());
    Ok(Some(codes))
}

/// Removes the factor, any half-finished enrolment, the replay guard and every
/// recovery code -- a code that recovers access to a factor that no longer
/// exists is just a second password.
///
/// Revokes sessions on the same rule as [`confirm_totp_enrolment`]: a security
/// control removed that left every other browser signed in would be a change in
/// name only, which is `users::set_password`'s argument verbatim.
pub async fn disable_totp(
    user: &mut AdminUser,
    keep_session: Option<&str>,
    database: Arc<Database>,
) -> Result<(), sqlx::Error> {
    user.clear_totp(&database).await?;
    AdminRecoveryCode::delete_for_user(user.id, &database).await?;
    revoke_other_sessions(user, keep_session, database).await?;

    info!(event = "admin_mfa_disabled", outcome = "success", user_id = %user.id, username = %user.username);
    Ok(())
}

/// Mints a fresh recovery set, superseding the previous one, and returns it
/// **once**. Nothing stores or logs the plaintext.
pub async fn regenerate_recovery_codes(
    user: &AdminUser,
    database: Arc<Database>,
) -> Result<Vec<String>, sqlx::Error> {
    let codes = issue_recovery_codes(user, database).await?;
    info!(event = "admin_mfa_recovery_codes_regenerated",
          outcome = "success",
          user_id = %user.id,
          username = %user.username,
          minted = codes.len());
    Ok(codes)
}

/// How many unspent recovery codes this operator holds.
pub async fn recovery_codes_remaining(
    user_id: Uuid,
    database: Arc<Database>,
) -> Result<i64, sqlx::Error> {
    AdminRecoveryCode::count_unused(user_id, &database).await
}

/// How many operators have no confirmed factor -- what the startup warning
/// under `admin.require_mfa` counts.
pub async fn operators_without_a_factor(database: Arc<Database>) -> Result<usize, sqlx::Error> {
    Ok(AdminUser::list_all(&database)
        .await?
        .iter()
        .filter(|user| !user.has_totp())
        .count())
}

async fn issue_recovery_codes(
    user: &AdminUser,
    database: Arc<Database>,
) -> Result<Vec<String>, sqlx::Error> {
    let codes = recovery::generate_codes();
    // Stored the way they are compared: normalised, so the grouping an operator
    // reads off the screen never has to survive into the hash.
    let hashes: Vec<String> = codes
        .iter()
        .map(|code| password::hash_generated_secret(&recovery::normalize(code)))
        .collect();
    AdminRecoveryCode::replace_all(user.id, &hashes, &database).await?;
    Ok(codes)
}

async fn revoke_other_sessions(
    user: &AdminUser,
    keep_session: Option<&str>,
    database: Arc<Database>,
) -> Result<u64, sqlx::Error> {
    match keep_session {
        Some(token_hash) => {
            AdminSession::delete_for_user_except(user.id, token_hash, &database).await
        }
        None => AdminSession::delete_for_user(user.id, &database).await,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::admin::totp::{DIGITS, step_at, totp_at};
    use acme_proxy_store::admin_session::NewSession;

    async fn db() -> Arc<Database> {
        Arc::new(Database::connect_in_memory().await.unwrap())
    }

    async fn operator(database: Arc<Database>) -> AdminUser {
        AdminUser::create("alice", "hash", None, &database)
            .await
            .unwrap()
    }

    /// An operator with a confirmed factor, returning the secret so a test can
    /// compute codes against it.
    async fn enrolled(database: Arc<Database>) -> (AdminUser, Vec<u8>) {
        let mut user = operator(database.clone()).await;
        let enrolment = begin_totp_enrolment(&mut user, "http://localhost:3001", database.clone())
            .await
            .unwrap();
        let code = totp_at(&enrolment.secret, step_at(now_secs()), DIGITS);
        let codes = confirm_totp_enrolment(&mut user, &code, None, database)
            .await
            .unwrap()
            .expect("a freshly generated code must confirm its own enrolment");
        assert_eq!(codes.len(), recovery::CODE_COUNT);
        (user, enrolment.secret)
    }

    #[tokio::test]
    async fn an_operator_with_no_factor_is_not_enrolled() {
        let db = db().await;
        let mut user = operator(db.clone()).await;

        assert_eq!(
            verify_second_factor(&mut user, "123456", db).await.unwrap(),
            MfaOutcome::NotEnrolled
        );
    }

    #[tokio::test]
    async fn enrolment_is_two_steps_and_a_wrong_code_finishes_neither() {
        let db = db().await;
        let mut user = operator(db.clone()).await;

        let enrolment = begin_totp_enrolment(&mut user, "http://localhost:3001", db.clone())
            .await
            .unwrap();
        assert!(
            !user.has_totp(),
            "a pending enrolment is not a second factor"
        );
        assert!(user.has_pending_totp());

        // A wrong code leaves the pending secret pending and mints no codes.
        assert!(
            confirm_totp_enrolment(&mut user, "000000", None, db.clone())
                .await
                .unwrap()
                .is_none()
        );
        assert!(!user.has_totp());
        assert!(user.has_pending_totp());
        assert_eq!(
            recovery_codes_remaining(user.id, db.clone()).await.unwrap(),
            0
        );

        let code = totp_at(&enrolment.secret, step_at(now_secs()), DIGITS);
        let codes = confirm_totp_enrolment(&mut user, &code, None, db.clone())
            .await
            .unwrap()
            .unwrap();

        assert!(user.has_totp());
        assert!(
            !user.has_pending_totp(),
            "confirming must clear the pending column, not leave two secrets live"
        );
        assert_eq!(codes.len(), recovery::CODE_COUNT);
        assert_eq!(
            recovery_codes_remaining(user.id, db).await.unwrap(),
            recovery::CODE_COUNT as i64
        );
    }

    /// RFC 6238 §5.2. The code that proved the enrolment must not also be the
    /// first login code, and no code may be spent twice inside its window.
    #[tokio::test]
    async fn a_correct_code_is_accepted_once_and_replayed_thereafter() {
        let db = db().await;
        let (mut user, secret) = enrolled(db.clone()).await;

        // The enrolment already claimed a step, so the code for *that* step is
        // a replay -- which is the point. Read it back rather than recomputing
        // it from the clock: the two would disagree whenever the enrolment and
        // this line straddle a 30-second boundary.
        let claimed = user.totp_last_step.expect("enrolment claims its own step");
        let code = totp_at(&secret, claimed, DIGITS);
        assert_eq!(
            verify_second_factor(&mut user, &code, db.clone())
                .await
                .unwrap(),
            MfaOutcome::Replayed
        );

        // The next step's code is accepted, then it too is spent.
        let next = totp_at(&secret, step_at(now_secs()) + 1, DIGITS);
        assert_eq!(
            verify_second_factor(&mut user, &next, db.clone())
                .await
                .unwrap(),
            MfaOutcome::Accepted {
                via: MfaMethod::Totp,
                recovery_codes_left: recovery::CODE_COUNT as i64,
            }
        );
        assert_eq!(
            verify_second_factor(&mut user, &next, db).await.unwrap(),
            MfaOutcome::Replayed
        );
    }

    #[tokio::test]
    async fn a_wrong_code_is_rejected_without_touching_the_replay_guard() {
        let db = db().await;
        let (mut user, secret) = enrolled(db.clone()).await;
        let claimed = user.totp_last_step;

        for wrong in ["000000", "12345", "abcdef", ""] {
            assert_eq!(
                verify_second_factor(&mut user, wrong, db.clone())
                    .await
                    .unwrap(),
                MfaOutcome::Rejected,
                "submission {wrong:?}"
            );
        }
        assert_eq!(
            user.totp_last_step, claimed,
            "a wrong code must not advance the guard, or it would lock out the right one"
        );

        // And the guard really is where it was: the next step still works.
        let next = totp_at(&secret, step_at(now_secs()) + 1, DIGITS);
        assert!(matches!(
            verify_second_factor(&mut user, &next, db).await.unwrap(),
            MfaOutcome::Accepted { .. }
        ));
    }

    #[tokio::test]
    async fn a_recovery_code_is_accepted_once_and_decrements_the_count() {
        let db = db().await;
        let mut user = operator(db.clone()).await;
        let enrolment = begin_totp_enrolment(&mut user, "http://localhost:3001", db.clone())
            .await
            .unwrap();
        let code = totp_at(&enrolment.secret, step_at(now_secs()), DIGITS);
        let codes = confirm_totp_enrolment(&mut user, &code, None, db.clone())
            .await
            .unwrap()
            .unwrap();

        // Submitted exactly as the page rendered it, separator and all.
        assert_eq!(
            verify_second_factor(&mut user, &codes[0], db.clone())
                .await
                .unwrap(),
            MfaOutcome::Accepted {
                via: MfaMethod::RecoveryCode,
                recovery_codes_left: recovery::CODE_COUNT as i64 - 1,
            }
        );
        assert_eq!(
            verify_second_factor(&mut user, &codes[0], db.clone())
                .await
                .unwrap(),
            MfaOutcome::Rejected,
            "single-use: a spent code is worth nothing"
        );

        // And retyped the ways a human retypes one.
        assert_eq!(
            verify_second_factor(&mut user, &codes[1].to_lowercase(), db.clone())
                .await
                .unwrap(),
            MfaOutcome::Accepted {
                via: MfaMethod::RecoveryCode,
                recovery_codes_left: recovery::CODE_COUNT as i64 - 2,
            }
        );
        assert_eq!(
            verify_second_factor(&mut user, &codes[2].replace('-', " "), db.clone())
                .await
                .unwrap(),
            MfaOutcome::Accepted {
                via: MfaMethod::RecoveryCode,
                recovery_codes_left: recovery::CODE_COUNT as i64 - 3,
            }
        );

        assert_eq!(
            recovery_codes_remaining(user.id, db).await.unwrap(),
            recovery::CODE_COUNT as i64 - 3
        );
    }

    #[tokio::test]
    async fn regenerating_supersedes_the_previous_set() {
        let db = db().await;
        let (mut user, _) = enrolled(db.clone()).await;

        let first = AdminRecoveryCode::list_unused(user.id, &db).await.unwrap();
        let second = regenerate_recovery_codes(&user, db.clone()).await.unwrap();
        assert_eq!(second.len(), recovery::CODE_COUNT);

        // A code from the old set is worth nothing now. Verify through the
        // stored hashes rather than the plaintext, which the first call never
        // returned.
        for code in AdminRecoveryCode::list_unused(user.id, &db).await.unwrap() {
            assert!(first.iter().all(|old| old.id != code.id));
        }
        assert_eq!(
            verify_second_factor(&mut user, &second[0], db.clone())
                .await
                .unwrap(),
            MfaOutcome::Accepted {
                via: MfaMethod::RecoveryCode,
                recovery_codes_left: recovery::CODE_COUNT as i64 - 1,
            }
        );
    }

    #[tokio::test]
    async fn disabling_clears_every_column_and_every_code() {
        let db = db().await;
        let (mut user, _) = enrolled(db.clone()).await;
        assert!(user.has_totp());

        disable_totp(&mut user, None, db.clone()).await.unwrap();

        assert!(!user.has_totp());
        assert!(!user.has_pending_totp());
        assert_eq!(user.totp_last_step, None);
        assert_eq!(
            recovery_codes_remaining(user.id, db.clone()).await.unwrap(),
            0,
            "a recovery code for a factor that no longer exists is a second password"
        );

        // Re-read from the database: the in-memory sync must not be the only
        // place this happened.
        let reloaded = AdminUser::find_by_id(user.id, &db).await.unwrap().unwrap();
        assert!(!reloaded.has_totp());
        assert!(!reloaded.has_pending_totp());
        assert_eq!(reloaded.totp_last_step, None);
    }

    #[tokio::test]
    async fn a_factor_change_revokes_every_other_session() {
        let db = db().await;
        let mut user = operator(db.clone()).await;

        let kept = AdminSession::create(
            NewSession {
                user_id: user.id,
                token_hash: "kept-hash",
                csrf_token: "csrf",
                created_ip: None,
                user_agent: None,
            },
            std::time::Duration::from_secs(3600),
            &db,
        )
        .await
        .unwrap();
        AdminSession::create(
            NewSession {
                user_id: user.id,
                token_hash: "other-hash",
                csrf_token: "csrf",
                created_ip: None,
                user_agent: None,
            },
            std::time::Duration::from_secs(3600),
            &db,
        )
        .await
        .unwrap();

        let enrolment = begin_totp_enrolment(&mut user, "http://localhost:3001", db.clone())
            .await
            .unwrap();
        let code = totp_at(&enrolment.secret, step_at(now_secs()), DIGITS);
        confirm_totp_enrolment(&mut user, &code, Some(&kept.token_hash), db.clone())
            .await
            .unwrap()
            .unwrap();

        let live = AdminSession::list_all(Some(user.id), &db).await.unwrap();
        assert_eq!(live.len(), 1, "every other browser must be signed out");
        assert_eq!(live[0].token_hash, "kept-hash");

        // And disabling takes the last one too, when nothing is kept.
        disable_totp(&mut user, None, db.clone()).await.unwrap();
        assert!(
            AdminSession::list_all(Some(user.id), &db)
                .await
                .unwrap()
                .is_empty()
        );
    }

    #[tokio::test]
    async fn every_outcome_names_itself_for_the_log() {
        assert_eq!(
            MfaOutcome::Accepted {
                via: MfaMethod::Totp,
                recovery_codes_left: 10
            }
            .reason(),
            ""
        );
        assert_eq!(MfaOutcome::Rejected.reason(), "wrong_code");
        assert_eq!(MfaOutcome::Replayed.reason(), "replayed");
        assert_eq!(MfaOutcome::NotEnrolled.reason(), "no_factor");
        assert_eq!(MfaMethod::Totp.as_str(), "totp");
        assert_eq!(MfaMethod::RecoveryCode.as_str(), "recovery_code");
    }

    #[tokio::test]
    async fn the_startup_count_sees_only_confirmed_factors() {
        let db = db().await;
        let mut alice = operator(db.clone()).await;
        AdminUser::create("bob", "hash", None, &db).await.unwrap();
        assert_eq!(operators_without_a_factor(db.clone()).await.unwrap(), 2);

        // A pending enrolment is not a factor, so it does not clear the count.
        let enrolment = begin_totp_enrolment(&mut alice, "http://localhost:3001", db.clone())
            .await
            .unwrap();
        assert_eq!(operators_without_a_factor(db.clone()).await.unwrap(), 2);

        let code = totp_at(&enrolment.secret, step_at(now_secs()), DIGITS);
        confirm_totp_enrolment(&mut alice, &code, None, db.clone())
            .await
            .unwrap()
            .unwrap();
        assert_eq!(operators_without_a_factor(db).await.unwrap(), 1);
    }
}