fastauth-plugin-two-factor 0.1.0

Two-factor authentication for fastauth: TOTP, email codes, backup codes and trusted devices.
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
mod common;

use std::time::{SystemTime, UNIX_EPOCH};

use common::{Client, Res, secret};
use fastauth_core::crypto::Argon2id;
use fastauth_core::mail::AuthEmail;
use fastauth_core::model::Model;
use fastauth_core::prelude::*;
use fastauth_plugin_two_factor::{
    EnableInput, PasswordInput, TwoFactor, TwoFactorExt, TwoFactorRow, VerifyInput, totp,
};
use http::{HeaderMap, StatusCode, header};
use serde_json::{Value, json};

type AppAuth = Auth<MemoryAdapter, (), plugins![TwoFactor]>;

const EMAIL: &str = "ada@acme.dk";
const PASSWORD: &str = "correct horse";

fn build(plugin: TwoFactor, rate_limit: RateLimit) -> (AppAuth, MemoryMailer) {
    let mailer = MemoryMailer::new();
    let auth = FastAuth::builder()
        .secret(secret())
        .base_url("http://localhost:3000")
        .database(MemoryAdapter::new())
        .email_password(EmailPassword::default().hasher(Argon2id::with_params(1024, 1, 1)))
        .mailer(mailer.clone())
        .rate_limit(rate_limit)
        .plugin(plugin)
        .build()
        .expect("valid config");
    (auth, mailer)
}

fn app() -> Client<MemoryAdapter, (), plugins![TwoFactor]> {
    Client::new(build(TwoFactor::new().issuer("Acme"), RateLimit::disabled()).0)
}

/// An authenticator app: reads the secret from the URI and never reuses a time step.
struct Authenticator {
    secret: Vec<u8>,
    last_step: Option<u64>,
}

impl Authenticator {
    fn from_uri(uri: &str) -> Self {
        let secret = uri
            .split(['?', '&'])
            .find_map(|p| p.strip_prefix("secret="))
            .expect("secret in the TOTP URI");
        Self {
            secret: base32_decode(secret),
            last_step: None,
        }
    }

    /// A valid code for a step no earlier code used: up to three per 30 seconds.
    fn code(&mut self) -> String {
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_secs()
            / 30;
        let step = self
            .last_step
            .map_or(now - 1, |last| (last + 1).max(now - 1));
        assert!(step <= now + 1, "test used more than three codes in a step");
        self.last_step = Some(step);
        totp::hotp(&self.secret, step, 6)
    }
}

fn base32_decode(input: &str) -> Vec<u8> {
    let mut bits = 0u64;
    let mut count = 0;
    let mut out = Vec::new();
    for c in input.bytes() {
        let value = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"
            .iter()
            .position(|&a| a == c)
            .expect("base32 character");
        bits = (bits << 5) | value as u64;
        count += 5;
        if count >= 8 {
            count -= 8;
            out.push((bits >> count) as u8);
        }
    }
    out
}

fn wrong(code: &str) -> String {
    code.chars()
        .map(|c| char::from(b'0' + (c as u8 - b'0' + 1) % 10))
        .collect()
}

trait TwoFactorClient {
    async fn sign_in_as(&mut self, email: &str) -> Res;
    async fn sign_out(&mut self) -> Res;
    async fn enable_as(&mut self, email: &str) -> (Authenticator, Vec<String>);

    async fn sign_in(&mut self) -> Res {
        self.sign_in_as(EMAIL).await
    }

    async fn enable(&mut self) -> (Authenticator, Vec<String>) {
        self.enable_as(EMAIL).await
    }

    async fn verify(&mut self, path: &str, code: &str) -> Res;
    fn has_cookie(&self, name: &str) -> bool;
    async fn two_factor_enabled(&mut self) -> Value;
}

impl TwoFactorClient for Client<MemoryAdapter, (), plugins![TwoFactor]> {
    async fn sign_in_as(&mut self, email: &str) -> Res {
        self.post(
            "/sign-in/email",
            json!({ "email": email, "password": PASSWORD }),
        )
        .await
    }

    async fn sign_out(&mut self) -> Res {
        self.post("/sign-out", json!({})).await
    }

    /// Signs up, enables two-factor and confirms it with a first code.
    async fn enable_as(&mut self, email: &str) -> (Authenticator, Vec<String>) {
        self.sign_up(email, PASSWORD).await;
        let res = self
            .post("/two-factor/enable", json!({ "password": PASSWORD }))
            .await;
        assert_eq!(res.status, StatusCode::OK, "{}", res.json);
        let codes = serde_json::from_value(res.json["backupCodes"].clone()).unwrap();
        let mut app = Authenticator::from_uri(res.json["totpURI"].as_str().unwrap());
        let res = self.verify("/two-factor/verify-totp", &app.code()).await;
        assert_eq!(res.status, StatusCode::OK, "{}", res.json);
        (app, codes)
    }

    async fn verify(&mut self, path: &str, code: &str) -> Res {
        self.post(path, json!({ "code": code })).await
    }

    fn has_cookie(&self, name: &str) -> bool {
        self.jar.contains_key(&format!("fastauth.{name}"))
    }

    async fn two_factor_enabled(&mut self) -> Value {
        self.get("/get-session").await.json["user"]["twoFactorEnabled"].clone()
    }
}

#[tokio::test]
async fn enable_verify_then_sign_in_through_the_challenge() {
    let mut client = app();
    client.sign_up(EMAIL, PASSWORD).await;
    let res = client
        .post("/two-factor/enable", json!({ "password": PASSWORD }))
        .await;
    let uri = res.json["totpURI"].as_str().unwrap().to_owned();
    assert!(
        uri.starts_with("otpauth://totp/Acme:ada%40acme.dk?secret="),
        "{uri}"
    );
    assert!(uri.ends_with("&issuer=Acme&digits=6&period=30"), "{uri}");
    assert_eq!(res.json["backupCodes"].as_array().unwrap().len(), 10);
    assert_eq!(
        client.two_factor_enabled().await,
        false,
        "off until the first code"
    );

    let mut app = Authenticator::from_uri(&uri);
    let res = client.verify("/two-factor/verify-totp", &app.code()).await;
    assert_eq!(res.status, StatusCode::OK, "{}", res.json);
    assert_eq!(res.json["user"]["twoFactorEnabled"], true);
    assert_eq!(client.two_factor_enabled().await, true);

    client.sign_out().await;
    let res = client.sign_in().await;
    assert_eq!(
        res.json,
        json!({ "twoFactorRedirect": true, "twoFactorMethods": ["totp", "otp"] })
    );
    assert!(client.has_cookie("two_factor"));
    assert!(!client.has_cookie("session_token"));
    assert_eq!(client.get("/get-session").await.json, Value::Null);

    let res = client.verify("/two-factor/verify-totp", &app.code()).await;
    assert_eq!(res.status, StatusCode::OK, "{}", res.json);
    assert!(!res.json["token"].as_str().unwrap().is_empty());
    assert_eq!(res.json["user"]["email"], EMAIL);
    assert!(client.has_cookie("session_token"));
    assert!(!client.has_cookie("two_factor"), "pending cookie cleared");
    assert_eq!(
        client.get("/get-session").await.json["user"]["email"],
        EMAIL
    );
}

#[tokio::test]
async fn secrets_are_encrypted_at_rest() {
    let mut client = app();
    let (app, codes) = client.enable().await;
    let rows = client.auth.adapter().dump(TwoFactorRow::TABLE);
    assert_eq!(rows.len(), 1);
    let stored = format!("{:?}", rows[0]);
    assert!(!stored.contains(&format!("{:?}", app.secret)));
    assert!(codes.iter().all(|c| !stored.contains(c.as_str())));
}

#[tokio::test]
async fn sign_in_is_not_challenged_before_the_first_code() {
    let mut client = app();
    client.sign_up(EMAIL, PASSWORD).await;
    client
        .post("/two-factor/enable", json!({ "password": PASSWORD }))
        .await;
    client.sign_out().await;
    assert!(client.sign_in().await.json["token"].is_string());
}

#[tokio::test]
async fn skip_verification_on_enable_turns_it_on_at_once() {
    let (auth, _) = build(
        TwoFactor::new().skip_verification_on_enable(true),
        RateLimit::disabled(),
    );
    let mut client = Client::new(auth);
    client.sign_up(EMAIL, PASSWORD).await;
    let res = client
        .post("/two-factor/enable", json!({ "password": PASSWORD }))
        .await;
    assert!(res.json["totpURI"].as_str().unwrap().contains("fastauth:"));
    assert_eq!(client.two_factor_enabled().await, true);
}

#[tokio::test]
async fn enable_needs_the_password() {
    let mut client = app();
    client.sign_up(EMAIL, PASSWORD).await;
    let res = client
        .post("/two-factor/enable", json!({ "password": "wrong" }))
        .await;
    assert_eq!(res.status, StatusCode::BAD_REQUEST);
    assert_eq!(res.json["code"], "INVALID_PASSWORD");
    client.jar.clear();
    let res = client
        .post("/two-factor/enable", json!({ "password": PASSWORD }))
        .await;
    assert_eq!(res.status, StatusCode::UNAUTHORIZED);
}

#[tokio::test]
async fn wrong_code_is_rejected() {
    let mut client = app();
    let (mut app, _) = client.enable().await;
    client.sign_out().await;
    client.sign_in().await;
    let code = app.code();
    let res = client
        .verify("/two-factor/verify-totp", &wrong(&code))
        .await;
    assert_eq!(res.status, StatusCode::UNAUTHORIZED);
    assert_eq!(res.json["code"], "INVALID_CODE");
    assert!(!client.has_cookie("session_token"));
    let res = client.verify("/two-factor/verify-totp", &code).await;
    assert_eq!(res.status, StatusCode::OK, "{}", res.json);
}

#[tokio::test]
async fn a_code_is_accepted_once() {
    let mut client = app();
    client.sign_up(EMAIL, PASSWORD).await;
    let res = client
        .post("/two-factor/enable", json!({ "password": PASSWORD }))
        .await;
    let mut app = Authenticator::from_uri(res.json["totpURI"].as_str().unwrap());
    let code = app.code();
    let first = client.verify("/two-factor/verify-totp", &code).await;
    assert_eq!(first.status, StatusCode::OK);

    client.sign_out().await;
    client.sign_in().await;
    let replay = client.verify("/two-factor/verify-totp", &code).await;
    assert_eq!(replay.status, StatusCode::UNAUTHORIZED);
    assert_eq!(replay.json["code"], "INVALID_CODE");
    let next = client.verify("/two-factor/verify-totp", &app.code()).await;
    assert_eq!(next.status, StatusCode::OK, "{}", next.json);
}

#[tokio::test]
async fn a_backup_code_works_once() {
    let mut client = app();
    let (_, codes) = client.enable().await;

    client.sign_out().await;
    client.sign_in().await;
    let res = client
        .verify("/two-factor/verify-backup-code", &codes[0])
        .await;
    assert_eq!(res.status, StatusCode::OK, "{}", res.json);
    assert!(client.has_cookie("session_token"));

    client.sign_out().await;
    client.sign_in().await;
    let res = client
        .verify("/two-factor/verify-backup-code", &codes[0])
        .await;
    assert_eq!(res.status, StatusCode::UNAUTHORIZED);
    assert_eq!(res.json["code"], "INVALID_BACKUP_CODE");
    let res = client
        .verify("/two-factor/verify-backup-code", &codes[1])
        .await;
    assert_eq!(res.status, StatusCode::OK, "{}", res.json);
}

#[tokio::test]
async fn regenerating_backup_codes_replaces_the_old_ones() {
    let mut client = app();
    let (_, old) = client.enable().await;
    let res = client
        .post(
            "/two-factor/generate-backup-codes",
            json!({ "password": PASSWORD }),
        )
        .await;
    assert_eq!(res.json["status"], true);
    let new: Vec<String> = serde_json::from_value(res.json["backupCodes"].clone()).unwrap();
    assert_eq!(new.len(), 10);
    assert!(new.iter().all(|c| c.len() == 11 && c.as_bytes()[5] == b'-'));

    client.sign_out().await;
    client.sign_in().await;
    let res = client
        .verify("/two-factor/verify-backup-code", &old[0])
        .await;
    assert_eq!(res.json["code"], "INVALID_BACKUP_CODE");
    let res = client
        .verify("/two-factor/verify-backup-code", &new[0])
        .await;
    assert_eq!(res.status, StatusCode::OK, "{}", res.json);
}

#[tokio::test]
async fn email_otp_finishes_a_sign_in() {
    let (auth, mailer) = build(TwoFactor::new(), RateLimit::disabled());
    let mut client = Client::new(auth);
    client.enable().await;
    client.sign_out().await;
    client.sign_in().await;

    let res = client.post("/two-factor/send-otp", json!({})).await;
    assert_eq!(res.json, json!({ "status": true }));
    let Some(AuthEmail::Plugin(email)) = mailer.last() else {
        panic!("no OTP email");
    };
    assert_eq!(email.kind, "two-factor-otp");
    assert_eq!(email.to, EMAIL);
    let code = email.code.unwrap();
    assert_eq!(code.len(), 6);
    assert!(email.text.contains(&code));

    let res = client.verify("/two-factor/verify-otp", &wrong(&code)).await;
    assert_eq!(res.json["code"], "INVALID_CODE");
    let res = client.verify("/two-factor/verify-otp", &code).await;
    assert_eq!(res.status, StatusCode::OK, "{}", res.json);
    assert!(client.has_cookie("session_token"));

    client.sign_out().await;
    client.sign_in().await;
    let res = client.verify("/two-factor/verify-otp", &code).await;
    assert_eq!(res.json["code"], "INVALID_CODE", "single use");
}

#[tokio::test]
async fn five_wrong_email_codes_burn_the_code() {
    let (auth, mailer) = build(TwoFactor::new(), RateLimit::disabled());
    let mut client = Client::new(auth);
    client.enable().await;
    client.sign_out().await;
    client.sign_in().await;
    client.post("/two-factor/send-otp", json!({})).await;
    let Some(AuthEmail::Plugin(email)) = mailer.last() else {
        panic!("no OTP email");
    };
    let code = email.code.unwrap();
    for _ in 0..4 {
        let res = client.verify("/two-factor/verify-otp", &wrong(&code)).await;
        assert_eq!(res.json["code"], "INVALID_CODE");
    }
    let res = client.verify("/two-factor/verify-otp", &wrong(&code)).await;
    assert_eq!(res.json["code"], "TOO_MANY_ATTEMPTS_REQUEST_NEW_CODE");
    let res = client.verify("/two-factor/verify-otp", &code).await;
    assert_eq!(res.json["code"], "INVALID_CODE");
}

#[tokio::test]
async fn a_trusted_device_skips_the_challenge() {
    let mut client = app();
    let (mut app, _) = client.enable().await;
    client.sign_out().await;
    client.sign_in().await;
    let res = client
        .post(
            "/two-factor/verify-totp",
            json!({ "code": app.code(), "trustDevice": true }),
        )
        .await;
    assert_eq!(res.status, StatusCode::OK, "{}", res.json);
    assert!(client.has_cookie("trust_device"));

    client.sign_out().await;
    let res = client.sign_in().await;
    assert!(res.json["token"].is_string(), "{}", res.json);

    client.sign_out().await;
    client.enable_as("bob@acme.dk").await;
    client.sign_out().await;
    assert_eq!(
        client.sign_in_as("bob@acme.dk").await.json,
        json!({ "twoFactorRedirect": true, "twoFactorMethods": ["totp", "otp"] }),
        "the trusted device is Ada's, not Bob's"
    );
}

#[tokio::test]
async fn re_enabling_forgets_trusted_devices() {
    let mut client = app();
    let (mut app, _) = client.enable().await;
    client.sign_out().await;
    client.sign_in().await;
    client
        .post(
            "/two-factor/verify-totp",
            json!({ "code": app.code(), "trustDevice": true }),
        )
        .await;
    let res = client
        .post("/two-factor/enable", json!({ "password": PASSWORD }))
        .await;
    let mut app = Authenticator::from_uri(res.json["totpURI"].as_str().unwrap());
    client.sign_out().await;
    assert_eq!(
        client.sign_in().await.json,
        json!({ "twoFactorRedirect": true, "twoFactorMethods": ["totp", "otp"] })
    );
    let res = client.verify("/two-factor/verify-totp", &app.code()).await;
    assert_eq!(res.status, StatusCode::OK, "{}", res.json);
}

#[tokio::test]
async fn disable_turns_the_challenge_off() {
    let mut client = app();
    client.enable().await;
    let res = client
        .post("/two-factor/disable", json!({ "password": "wrong" }))
        .await;
    assert_eq!(res.json["code"], "INVALID_PASSWORD");
    let res = client
        .post("/two-factor/disable", json!({ "password": PASSWORD }))
        .await;
    assert_eq!(res.json, json!({ "status": true }));
    assert_eq!(client.two_factor_enabled().await, false);
    assert!(client.auth.adapter().dump(TwoFactorRow::TABLE).is_empty());

    client.sign_out().await;
    assert!(client.sign_in().await.json["token"].is_string());
    let res = client
        .post("/two-factor/get-totp-uri", json!({ "password": PASSWORD }))
        .await;
    assert_eq!(res.json["code"], "TOTP_NOT_ENABLED");
}

#[tokio::test]
async fn get_totp_uri_shows_the_same_secret() {
    let mut client = app();
    client.sign_up(EMAIL, PASSWORD).await;
    let enabled = client
        .post("/two-factor/enable", json!({ "password": PASSWORD }))
        .await;
    let again = client
        .post("/two-factor/get-totp-uri", json!({ "password": PASSWORD }))
        .await;
    assert_eq!(again.json["totpURI"], enabled.json["totpURI"]);
}

#[tokio::test]
async fn the_pending_sign_in_expires() {
    let (auth, _) = build(
        TwoFactor::new().two_factor_cookie_max_age(Duration::seconds(1)),
        RateLimit::disabled(),
    );
    let mut client = Client::new(auth);
    let (mut app, _) = client.enable().await;
    client.sign_out().await;
    assert_eq!(
        client.sign_in().await.json,
        json!({ "twoFactorRedirect": true, "twoFactorMethods": ["totp", "otp"] })
    );
    tokio::time::sleep(std::time::Duration::from_millis(2100)).await;
    assert!(
        client.has_cookie("two_factor"),
        "the browser still sends it"
    );
    let res = client.verify("/two-factor/verify-totp", &app.code()).await;
    assert_eq!(res.status, StatusCode::UNAUTHORIZED);
    assert_eq!(res.json["code"], "INVALID_TWO_FACTOR_COOKIE");
    assert!(!client.has_cookie("session_token"));
}

#[tokio::test]
async fn verify_without_a_session_or_pending_sign_in_is_refused() {
    let mut client = app();
    let res = client.verify("/two-factor/verify-totp", "123456").await;
    assert_eq!(res.status, StatusCode::UNAUTHORIZED);
    assert_eq!(res.json["code"], "INVALID_TWO_FACTOR_COOKIE");
}

#[tokio::test]
async fn verify_endpoints_allow_five_attempts_a_minute() {
    let (auth, _) = build(TwoFactor::new(), RateLimit::default());
    let mut client = Client::new(auth);
    client.sign_up(EMAIL, PASSWORD).await;
    client
        .post("/two-factor/enable", json!({ "password": PASSWORD }))
        .await;
    for path in ["totp", "otp", "backup-code", "totp", "totp"] {
        let res = client
            .verify(&format!("/two-factor/verify-{path}"), "000000")
            .await;
        assert_ne!(res.status, StatusCode::TOO_MANY_REQUESTS);
    }
    let res = client.verify("/two-factor/verify-totp", "000000").await;
    assert_eq!(res.status, StatusCode::TOO_MANY_REQUESTS);
}

#[tokio::test]
async fn server_api_enables_verifies_and_disables() {
    let (auth, _) = build(TwoFactor::new(), RateLimit::disabled());
    let mut client = Client::new(auth.clone());
    client.sign_up(EMAIL, PASSWORD).await;
    let mut headers = HeaderMap::new();
    headers.insert(
        header::COOKIE,
        client.cookie_header().unwrap().parse().unwrap(),
    );

    let enabled = auth
        .two_factor()
        .enable(
            &headers,
            EnableInput {
                password: PASSWORD.into(),
                issuer: None,
            },
        )
        .await
        .unwrap()
        .data;
    let mut app = Authenticator::from_uri(&enabled.totp_uri);
    let verified = auth
        .two_factor()
        .verify_totp(
            &headers,
            VerifyInput {
                code: app.code(),
                trust_device: None,
            },
        )
        .await
        .unwrap()
        .data;
    assert_eq!(verified.user["twoFactorEnabled"], true);

    let replay = auth
        .two_factor()
        .verify_totp(
            &headers,
            VerifyInput {
                code: totp::hotp(&app.secret, app.last_step.unwrap(), 6),
                trust_device: None,
            },
        )
        .await
        .unwrap_err();
    assert_eq!(replay.code(), "INVALID_CODE");

    auth.two_factor()
        .disable(
            &headers,
            PasswordInput {
                password: PASSWORD.into(),
            },
        )
        .await
        .unwrap();
    assert_eq!(client.two_factor_enabled().await, false);
}