latchkey 0.3.0

Sans-IO sign-in building blocks and policy: Argon2id passwords, TOTP, recovery codes, passkeys, tokens, sealing, cookies, lockout, rate limits and sign-in flows as pure state machines
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
//! The policy settings the flows apply, built and validated once.
//!
//! ```
//! use latchkey::Config;
//! use time::Duration;
//!
//! let config = Config::builder("owner@example.com", "https://home.example.com")
//!     .session_ttl(Duration::days(14))
//!     .build()?;
//! assert_eq!(config.rp_id(), "home.example.com");
//! assert_eq!(config.origin_string(), "https://home.example.com");
//! assert_eq!(config.setup_url(), "https://home.example.com/setup");
//! # Ok::<(), latchkey::Error>(())
//! ```

use crate::error::ConfigReason;
use time::Duration;
use url::Url;

use crate::Error;
use crate::account::{Email, LockoutPolicy, PasswordOnly};
use crate::flow::step_up::StepUpFailures;
use crate::http::CookieName;
use crate::limits::RateLimits;
use crate::password::PasswordParams;

/// Validated settings: the origin and relying party, cookie names, mailed link targets,
/// lifetimes, the lockout and step-up failure policies and the rate-limit and password
/// parameters. Build it with
/// [`Config::builder`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Config {
    origin: Url,
    rp_id: String,
    rp_name: String,
    owner_email: Email,
    owner_name: String,
    cookie_name: CookieName,
    device_cookie_name: CookieName,
    ceremony_cookie_name: CookieName,
    setup_url: Url,
    sign_in_link_url: Url,
    invite_url: Url,
    sessions_url: Url,
    durations: Durations,
    lockout: LockoutPolicy,
    step_up_failures: StepUpFailures,
    password_only: PasswordOnly,
    rate_limits: RateLimits,
    password: PasswordParams,
    insecure_localhost_cookies: bool,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct Durations {
    device_ttl: Duration,
    session_ttl: Duration,
    pending_ttl: Duration,
    setup_ttl: Duration,
    link_ttl: Duration,
    invite_ttl: Duration,
    resend_cooldown: Duration,
    step_up_window: Duration,
}

impl Default for Durations {
    fn default() -> Self {
        Self {
            device_ttl: Duration::days(400),
            session_ttl: Duration::days(30),
            pending_ttl: Duration::minutes(10),
            setup_ttl: Duration::hours(1),
            link_ttl: Duration::minutes(15),
            invite_ttl: Duration::days(3),
            resend_cooldown: Duration::minutes(1),
            step_up_window: Duration::minutes(5),
        }
    }
}

/// Builds a [`Config`]; every setting has the default latchkey 0.2 used.
#[derive(Debug, Clone)]
#[must_use = "call build() to get a Config"]
pub struct ConfigBuilder {
    owner_email: String,
    origin: String,
    rp_id: Option<String>,
    rp_name: Option<String>,
    owner_name: Option<String>,
    cookie_name: String,
    device_cookie_name: String,
    ceremony_cookie_name: String,
    setup_path: String,
    sign_in_link_path: String,
    invite_path: String,
    sessions_path: String,
    durations: Durations,
    lockout: LockoutPolicy,
    step_up_failures: StepUpFailures,
    password_only: PasswordOnly,
    rate_limits: RateLimits,
    password: PasswordParams,
    insecure_localhost_cookies: bool,
}

macro_rules! durations {
    ($($(#[$doc:meta])* $name:ident),* $(,)?) => {
        impl ConfigBuilder {
            $(
                $(#[$doc])*
                pub fn $name(mut self, value: Duration) -> Self {
                    self.durations.$name = value;
                    self
                }
            )*
        }

        impl Config {
            $(
                $(#[$doc])*
                #[must_use]
                pub fn $name(&self) -> Duration {
                    self.durations.$name
                }
            )*
        }
    };
}

durations!(
    /// How long a device stays known after its last sign-in. Default 400 days.
    device_ttl,
    /// Lifetime of a signed-in session. Default 30 days.
    session_ttl,
    /// Lifetime of a session waiting for its second factor. Default 10 minutes.
    pending_ttl,
    /// Lifetime of a setup session. Default 1 hour.
    setup_ttl,
    /// Lifetime of a setup or sign-in link. Default 15 minutes.
    link_ttl,
    /// Lifetime of an invite link. Default 3 days.
    invite_ttl,
    /// Minimum time between two setup or sign-in links to one account. Default 1 minute.
    resend_cooldown,
    /// How long a sign-in or step-up counts as recent. Default 5 minutes.
    step_up_window,
);

impl ConfigBuilder {
    /// The relying party id for passkeys. Default: the origin's host.
    pub fn rp_id(mut self, rp_id: &str) -> Self {
        self.rp_id = Some(rp_id.to_owned());
        self
    }

    /// The name passkey prompts show. Default: the relying party id.
    pub fn rp_name(mut self, rp_name: &str) -> Self {
        self.rp_name = Some(rp_name.to_owned());
        self
    }

    /// The owner's display name. Default: the owner's address.
    pub fn owner_name(mut self, name: &str) -> Self {
        self.owner_name = Some(name.to_owned());
        self
    }

    /// The session cookie's name; must start with `__Host-`. Default `__Host-latchkey`.
    pub fn cookie_name(mut self, name: &str) -> Self {
        name.clone_into(&mut self.cookie_name);
        self
    }

    /// The device cookie's name; must start with `__Host-` and differ from the session
    /// cookie's. Default `__Host-latchkey_device`.
    pub fn device_cookie_name(mut self, name: &str) -> Self {
        name.clone_into(&mut self.device_cookie_name);
        self
    }

    /// The name of the cookie that binds a passkey sign-in to the browser that started it
    /// (the `passkey` module, feature `passkeys`); must start with
    /// `__Host-` and differ from the other two. Default `__Host-latchkey_ceremony`.
    pub fn ceremony_cookie_name(mut self, name: &str) -> Self {
        name.clone_into(&mut self.ceremony_cookie_name);
        self
    }

    /// The page that receives `?token=` from setup links, relative to the origin. Default
    /// `/setup`.
    pub fn setup_path(mut self, path: &str) -> Self {
        path.clone_into(&mut self.setup_path);
        self
    }

    /// The page for sign-in links. Default `/sign-in/link`.
    pub fn sign_in_link_path(mut self, path: &str) -> Self {
        path.clone_into(&mut self.sign_in_link_path);
        self
    }

    /// The page for invite links. Default `/invite`.
    pub fn invite_path(mut self, path: &str) -> Self {
        path.clone_into(&mut self.invite_path);
        self
    }

    /// The sessions page the new-device mail links to. Default `/settings/sessions`.
    pub fn sessions_path(mut self, path: &str) -> Self {
        path.clone_into(&mut self.sessions_path);
        self
    }

    /// The lockout policy. Default: 5 failures lock for 5 minutes, doubling up to an hour.
    pub fn lockout(mut self, lockout: LockoutPolicy) -> Self {
        self.lockout = lockout;
        self
    }

    /// Where wrong step-up guesses count. Default [`StepUpFailures::AccountLockout`], as in
    /// latchkey 0.2.
    pub fn step_up_failures(mut self, policy: StepUpFailures) -> Self {
        self.step_up_failures = policy;
        self
    }

    /// Whether accounts with a password and no second factor may sign in. Default
    /// [`PasswordOnly::Refused`], as in latchkey 0.2.
    pub fn password_only(mut self, policy: PasswordOnly) -> Self {
        self.password_only = policy;
        self
    }

    /// **Development only.** Cookies for a plain-HTTP origin on this machine: the cookie names
    /// lose their `__Host-` prefix (browsers refuse `__Host-` cookies without `Secure`) and
    /// [`SetCookie`](crate::http::SetCookie) leaves out `Secure`, so browsers that do not
    /// treat `http://localhost` as secure (Safari) keep the cookies.
    ///
    /// [`build`](Self::build) refuses this unless the origin is `http://localhost`,
    /// `http://127.0.0.1` or `http://[::1]` (any port), so a production config, which needs
    /// an `https` origin, cannot have it. Never derive the origin from a request.
    ///
    /// ```
    /// use latchkey::Config;
    ///
    /// let dev = Config::builder("owner@example.com", "http://localhost:3000")
    ///     .danger_insecure_localhost_cookies()
    ///     .build()?;
    /// assert_eq!(dev.cookie_name().as_str(), "latchkey");
    /// assert!(!dev.cookie_name().is_secure());
    ///
    /// let prod = Config::builder("owner@example.com", "https://home.example.com")
    ///     .danger_insecure_localhost_cookies()
    ///     .build();
    /// assert!(prod.is_err());
    /// # Ok::<(), latchkey::Error>(())
    /// ```
    pub fn danger_insecure_localhost_cookies(mut self) -> Self {
        self.insecure_localhost_cookies = true;
        self
    }

    /// The rate limits and wrong-password throttle.
    pub fn rate_limits(mut self, rate_limits: RateLimits) -> Self {
        self.rate_limits = rate_limits;
        self
    }

    /// Argon2id parameters. Default 64 MiB, t=3, p=2.
    pub fn password_params(mut self, params: PasswordParams) -> Self {
        self.password = params;
        self
    }

    /// Validates everything.
    ///
    /// # Errors
    ///
    /// [`Error::Config`] when the origin is not an `https` URL (or `http://` on `localhost`,
    /// `127.0.0.1` or `[::1]`), insecure localhost cookies are asked for any other origin, the
    /// owner address or a cookie name is invalid, two cookie names are equal, or a path does
    /// not join onto the origin, or a [`StepUpFailures::PerSession`] limit is 0.
    /// [`Error::InvalidRequest`] with `email` for a bad owner
    /// address.
    pub fn build(self) -> Result<Config, Error> {
        let origin =
            Url::parse(&self.origin).map_err(|_| Error::Config(ConfigReason::OriginNotUrl))?;
        let host = origin
            .host_str()
            .ok_or(Error::Config(ConfigReason::OriginHasNoHost))?
            .to_owned();
        let local = matches!(host.as_str(), "localhost" | "127.0.0.1" | "[::1]");
        let plain_local = origin.scheme() == "http" && local;
        if origin.scheme() != "https" && !plain_local {
            return Err(Error::Config(ConfigReason::OriginNotHttps));
        }
        if self.insecure_localhost_cookies && !plain_local {
            return Err(Error::Config(ConfigReason::InsecureCookiesNotLocalhost));
        }
        let owner_email =
            Email::parse(&self.owner_email).map_err(|_| Error::Config(ConfigReason::OwnerEmail))?;
        let cookie = |name: &str| {
            if self.insecure_localhost_cookies {
                CookieName::insecure_localhost(name)
            } else {
                CookieName::new(name)
            }
        };
        let cookie_name = cookie(&self.cookie_name)?;
        let device_cookie_name = cookie(&self.device_cookie_name)?;
        let ceremony_cookie_name = cookie(&self.ceremony_cookie_name)?;
        if cookie_name == device_cookie_name
            || cookie_name == ceremony_cookie_name
            || device_cookie_name == ceremony_cookie_name
        {
            return Err(Error::Config(ConfigReason::CookieNamesEqual));
        }
        let join = |path: &str| {
            origin
                .join(path)
                .map_err(|_| Error::Config(ConfigReason::PathDoesNotJoin))
        };
        if self.step_up_failures == (StepUpFailures::PerSession { limit: 0 }) {
            return Err(Error::Config(ConfigReason::StepUpLimitZero));
        }
        let rp_id = self.rp_id.unwrap_or(host);
        Ok(Config {
            setup_url: join(&self.setup_path)?,
            sign_in_link_url: join(&self.sign_in_link_path)?,
            invite_url: join(&self.invite_path)?,
            sessions_url: join(&self.sessions_path)?,
            rp_name: self.rp_name.unwrap_or_else(|| rp_id.clone()),
            rp_id,
            owner_name: self
                .owner_name
                .unwrap_or_else(|| owner_email.as_str().to_owned()),
            owner_email,
            cookie_name,
            device_cookie_name,
            ceremony_cookie_name,
            origin,
            durations: self.durations,
            lockout: self.lockout,
            step_up_failures: self.step_up_failures,
            password_only: self.password_only,
            rate_limits: self.rate_limits,
            password: self.password,
            insecure_localhost_cookies: self.insecure_localhost_cookies,
        })
    }
}

impl Config {
    /// Starts a builder for the owner's address and the single origin the app is served
    /// from, such as `https://home.example.com`.
    pub fn builder(owner_email: &str, origin: &str) -> ConfigBuilder {
        ConfigBuilder {
            owner_email: owner_email.to_owned(),
            origin: origin.to_owned(),
            rp_id: None,
            rp_name: None,
            owner_name: None,
            cookie_name: "__Host-latchkey".to_owned(),
            device_cookie_name: "__Host-latchkey_device".to_owned(),
            ceremony_cookie_name: "__Host-latchkey_ceremony".to_owned(),
            setup_path: "/setup".to_owned(),
            sign_in_link_path: "/sign-in/link".to_owned(),
            invite_path: "/invite".to_owned(),
            sessions_path: "/settings/sessions".to_owned(),
            durations: Durations::default(),
            lockout: LockoutPolicy::default(),
            step_up_failures: StepUpFailures::default(),
            password_only: PasswordOnly::default(),
            rate_limits: RateLimits::default(),
            password: PasswordParams::default(),
            insecure_localhost_cookies: false,
        }
    }

    /// The origin.
    #[must_use]
    pub fn origin(&self) -> &str {
        self.origin.as_str()
    }

    /// The origin as browsers send it in `Origin`, e.g. `https://home.example.com`.
    #[must_use]
    pub fn origin_string(&self) -> String {
        self.origin.origin().ascii_serialization()
    }

    /// The passkey relying party id.
    #[must_use]
    pub fn rp_id(&self) -> &str {
        &self.rp_id
    }

    /// The passkey relying party name.
    #[must_use]
    pub fn rp_name(&self) -> &str {
        &self.rp_name
    }

    /// The owner's address: setup links only ever go here.
    #[must_use]
    pub fn owner_email(&self) -> &Email {
        &self.owner_email
    }

    /// The owner's display name.
    #[must_use]
    pub fn owner_name(&self) -> &str {
        &self.owner_name
    }

    /// The session cookie's name.
    #[must_use]
    pub fn cookie_name(&self) -> &CookieName {
        &self.cookie_name
    }

    /// The device cookie's name.
    #[must_use]
    pub fn device_cookie_name(&self) -> &CookieName {
        &self.device_cookie_name
    }

    /// Whether it was built with
    /// [`danger_insecure_localhost_cookies`](ConfigBuilder::danger_insecure_localhost_cookies).
    #[must_use]
    pub fn insecure_localhost_cookies(&self) -> bool {
        self.insecure_localhost_cookies
    }

    /// The passkey sign-in binding cookie's name.
    #[must_use]
    pub fn ceremony_cookie_name(&self) -> &CookieName {
        &self.ceremony_cookie_name
    }

    /// Where setup links point.
    #[must_use]
    pub fn setup_url(&self) -> &str {
        self.setup_url.as_str()
    }

    pub(crate) fn setup_target(&self) -> &Url {
        &self.setup_url
    }

    /// Where sign-in links point.
    #[must_use]
    pub fn sign_in_link_url(&self) -> &str {
        self.sign_in_link_url.as_str()
    }

    pub(crate) fn sign_in_link_target(&self) -> &Url {
        &self.sign_in_link_url
    }

    /// Where invite links point.
    #[must_use]
    pub fn invite_url(&self) -> &str {
        self.invite_url.as_str()
    }

    pub(crate) fn invite_target(&self) -> &Url {
        &self.invite_url
    }

    /// The sessions page the new-device mail links to.
    #[must_use]
    pub fn sessions_url(&self) -> &str {
        self.sessions_url.as_str()
    }

    /// The lockout policy.
    #[must_use]
    pub fn lockout(&self) -> &LockoutPolicy {
        &self.lockout
    }

    /// Where wrong step-up guesses count.
    #[must_use]
    pub fn step_up_failures(&self) -> StepUpFailures {
        self.step_up_failures
    }

    /// Whether accounts with a password and no second factor may sign in.
    #[must_use]
    pub fn password_only(&self) -> PasswordOnly {
        self.password_only
    }

    /// The rate limits.
    #[must_use]
    pub fn rate_limits(&self) -> &RateLimits {
        &self.rate_limits
    }

    /// The Argon2id parameters.
    #[must_use]
    pub fn password_params(&self) -> PasswordParams {
        self.password
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn defaults_match_latchkey_0_2() -> Result<(), Error> {
        let config = Config::builder("o@example.com", "https://home.example.com").build()?;
        assert_eq!(config.cookie_name().as_str(), "__Host-latchkey");
        assert_eq!(
            config.device_cookie_name().as_str(),
            "__Host-latchkey_device"
        );
        assert_eq!(
            config.ceremony_cookie_name().as_str(),
            "__Host-latchkey_ceremony"
        );
        assert_eq!(config.session_ttl(), Duration::days(30));
        assert_eq!(config.step_up_window(), Duration::minutes(5));
        assert_eq!(config.link_ttl(), Duration::minutes(15));
        assert_eq!(config.invite_url(), "https://home.example.com/invite");
        assert_eq!(config.owner_name(), "o@example.com");
        assert_eq!(config.rp_name(), "home.example.com");
        assert_eq!(config.step_up_failures(), StepUpFailures::AccountLockout);
        assert_eq!(config.password_only(), PasswordOnly::Refused);
        Ok(())
    }

    #[test]
    fn rejects_bad_cookie_names_and_plain_http() {
        let builder = || Config::builder("o@example.com", "https://home.example.com");
        assert!(builder().cookie_name("session").build().is_err());
        assert!(
            builder()
                .device_cookie_name("__Host-latchkey")
                .build()
                .is_err()
        );
        assert!(
            builder()
                .ceremony_cookie_name("__Host-latchkey_device")
                .build()
                .is_err()
        );
        assert!(builder().ceremony_cookie_name("ceremony").build().is_err());
        assert!(
            Config::builder("o@example.com", "http://home.example.com")
                .build()
                .is_err()
        );
        assert!(
            Config::builder("o@example.com", "http://localhost:3000")
                .build()
                .is_ok()
        );
        assert!(
            Config::builder("not an email", "https://h.example")
                .build()
                .is_err()
        );
        assert!(
            Config::builder("o@example.com", "not a url")
                .build()
                .is_err()
        );
        assert_eq!(
            builder()
                .step_up_failures(StepUpFailures::PerSession { limit: 0 })
                .build()
                .err(),
            Some(Error::Config(ConfigReason::StepUpLimitZero))
        );
        assert!(
            builder()
                .step_up_failures(StepUpFailures::PerSession { limit: 5 })
                .build()
                .is_ok()
        );
    }
}

#[cfg(test)]
mod insecure_localhost_tests {
    use super::*;

    fn dev(origin: &str) -> Result<Config, Error> {
        Config::builder("o@example.com", origin)
            .danger_insecure_localhost_cookies()
            .build()
    }

    #[test]
    fn insecure_cookies_only_for_plain_http_on_this_machine() -> Result<(), Error> {
        for origin in [
            "http://localhost",
            "http://localhost:3000",
            "http://127.0.0.1:8080",
            "http://[::1]:8080",
        ] {
            let config = dev(origin)?;
            assert!(config.insecure_localhost_cookies(), "{origin}");
            assert_eq!(config.cookie_name().as_str(), "latchkey");
            assert_eq!(config.device_cookie_name().as_str(), "latchkey_device");
            assert_eq!(config.ceremony_cookie_name().as_str(), "latchkey_ceremony");
            assert!(!config.cookie_name().is_secure());
        }
        let refused = Some(Error::Config(ConfigReason::InsecureCookiesNotLocalhost));
        for origin in [
            "https://home.example.com",
            "https://localhost:3000",
            "https://127.0.0.1",
        ] {
            assert_eq!(dev(origin).err(), refused, "{origin}");
        }
        for origin in [
            "http://home.example.com",
            "http://localhost.example.com",
            "http://127.0.0.2",
            "http://10.0.0.1",
        ] {
            assert_eq!(
                dev(origin).err(),
                Some(Error::Config(ConfigReason::OriginNotHttps)),
                "{origin}"
            );
        }
        Ok(())
    }

    #[test]
    fn by_default_cookies_are_host_only_and_secure_even_on_localhost() -> Result<(), Error> {
        let config = Config::builder("o@example.com", "http://127.0.0.1:3000").build()?;
        assert!(!config.insecure_localhost_cookies());
        assert_eq!(config.cookie_name().as_str(), "__Host-latchkey");
        assert!(config.cookie_name().is_secure());
        Ok(())
    }

    #[test]
    fn insecure_names_still_differ_and_stay_valid() {
        let builder = || {
            Config::builder("o@example.com", "http://localhost:3000")
                .danger_insecure_localhost_cookies()
        };
        assert!(builder().device_cookie_name("latchkey").build().is_err());
        assert!(builder().cookie_name("__Host-").build().is_err());
        assert_eq!(
            builder()
                .cookie_name("__Host-app")
                .build()
                .map(|config| config.cookie_name().as_str().to_owned()),
            Ok("app".to_owned())
        );
    }
}