1use crate::{AppError, Result};
2use serde::{Deserialize, Serialize};
3
4#[derive(Clone, Copy, Debug, Default, Deserialize, Serialize)]
6#[serde(rename_all = "lowercase")]
7pub enum SameSite {
8 #[default]
10 Lax,
11 Strict,
13}
14
15#[derive(Clone, Debug, Deserialize, Serialize)]
17#[serde(default, deny_unknown_fields)]
18pub struct AuthSettings {
19 pub throttle: AuthThrottleSettings,
21 pub password: crate::PasswordSettings,
23 pub session_lifetime_seconds: u64,
25 pub cookie_name: Option<String>,
27 pub secure: Option<bool>,
29 pub same_site: SameSite,
31 pub password_reset_lifetime_seconds: u64,
33}
34
35impl Default for AuthSettings {
36 fn default() -> Self {
37 Self {
38 throttle: AuthThrottleSettings::default(),
39 password: crate::PasswordSettings::default(),
40 session_lifetime_seconds: 30 * 86400,
41 cookie_name: None,
42 secure: None,
43 same_site: SameSite::Lax,
44 password_reset_lifetime_seconds: 1800,
45 }
46 }
47}
48
49impl AuthSettings {
50 pub fn cookie_name(&self, production: bool) -> &str {
52 self.cookie_name.as_deref().unwrap_or(if production {
53 "__Host-session"
54 } else {
55 "session"
56 })
57 }
58
59 pub fn secure(&self, production: bool) -> bool {
61 production || self.secure.unwrap_or(false)
62 }
63
64 pub fn validate(&self, production: bool) -> Result<()> {
66 self.password.concurrency()?;
67 self.throttle.validate()?;
68 if production && !self.throttle.enabled {
69 tracing::warn!(event = "auth.throttle.disabled", "Built-in auth throttling is disabled; enforce equivalent abuse limits at your deployment boundary.");
70 }
71 let cookie = self.cookie_name(production);
72 if cookie.is_empty()
73 || cookie.len() > 100
74 || !cookie
75 .bytes()
76 .all(|c| c.is_ascii_alphanumeric() || b"_-".contains(&c))
77 || (cookie.starts_with("__Host-") || cookie.starts_with("__Secure-"))
78 && !self.secure(production)
79 || production && self.secure == Some(false)
80 {
81 return Err(AppError::Configuration(
82 "Auth requires a valid cookie name and Secure cookies in production.".into(),
83 ));
84 }
85 if !(60..=90 * 86400).contains(&self.session_lifetime_seconds)
86 || !(60..=86400).contains(&self.password_reset_lifetime_seconds)
87 {
88 return Err(AppError::Configuration("Auth session lifetime must be 60..=7776000 seconds; reset lifetime must be 60..=86400 seconds.".into()));
89 }
90 Ok(())
91 }
92}
93
94#[derive(Clone, Debug, Deserialize, Serialize)]
96#[serde(deny_unknown_fields)]
97pub struct AuthThrottleLimit {
98 pub limit: u32,
100 pub window_seconds: u64,
102}
103impl AuthThrottleLimit {
104 fn validate(&self) -> Result<()> {
105 if !(1..=1_000_000).contains(&self.limit) || !(1..=86400).contains(&self.window_seconds) {
106 return Err(AppError::Configuration("Invalid auth.throttle budget: limit must be 1..=1000000 and window_seconds 1..=86400. Use enabled=false for an explicit opt-out.".into()));
107 }
108 Ok(())
109 }
110}
111
112#[derive(Clone, Debug, Deserialize, Serialize)]
116#[serde(default, deny_unknown_fields)]
117pub struct AuthThrottleSettings {
118 pub enabled: bool,
121 pub ip: AuthThrottleLimit,
123 pub account: AuthThrottleLimit,
126}
127impl Default for AuthThrottleSettings {
128 fn default() -> Self {
129 Self {
130 enabled: true,
131 ip: AuthThrottleLimit {
132 limit: 60,
133 window_seconds: 60,
134 },
135 account: AuthThrottleLimit {
136 limit: 10,
137 window_seconds: 60,
138 },
139 }
140 }
141}
142impl AuthThrottleSettings {
143 pub fn validate(&self) -> Result<()> {
145 self.ip.validate()?;
146 self.account.validate()
147 }
148}