use crate::{AppError, Result};
use serde::{Deserialize, Serialize};
#[derive(Clone, Copy, Debug, Default, Deserialize, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum SameSite {
#[default]
Lax,
Strict,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(default, deny_unknown_fields)]
pub struct AuthSettings {
pub throttle: AuthThrottleSettings,
pub password: crate::PasswordSettings,
pub session_lifetime_seconds: u64,
pub cookie_name: Option<String>,
pub secure: Option<bool>,
pub same_site: SameSite,
pub password_reset_lifetime_seconds: u64,
}
impl Default for AuthSettings {
fn default() -> Self {
Self {
throttle: AuthThrottleSettings::default(),
password: crate::PasswordSettings::default(),
session_lifetime_seconds: 30 * 86400,
cookie_name: None,
secure: None,
same_site: SameSite::Lax,
password_reset_lifetime_seconds: 1800,
}
}
}
impl AuthSettings {
pub fn cookie_name(&self, production: bool) -> &str {
self.cookie_name.as_deref().unwrap_or(if production {
"__Host-session"
} else {
"session"
})
}
pub fn secure(&self, production: bool) -> bool {
production || self.secure.unwrap_or(false)
}
pub fn validate(&self, production: bool) -> Result<()> {
self.password.concurrency()?;
self.throttle.validate()?;
if production && !self.throttle.enabled {
tracing::warn!(event = "auth.throttle.disabled", "Built-in auth throttling is disabled; enforce equivalent abuse limits at your deployment boundary.");
}
let cookie = self.cookie_name(production);
if cookie.is_empty()
|| cookie.len() > 100
|| !cookie
.bytes()
.all(|c| c.is_ascii_alphanumeric() || b"_-".contains(&c))
|| (cookie.starts_with("__Host-") || cookie.starts_with("__Secure-"))
&& !self.secure(production)
|| production && self.secure == Some(false)
{
return Err(AppError::Configuration(
"Auth requires a valid cookie name and Secure cookies in production.".into(),
));
}
if !(60..=90 * 86400).contains(&self.session_lifetime_seconds)
|| !(60..=86400).contains(&self.password_reset_lifetime_seconds)
{
return Err(AppError::Configuration("Auth session lifetime must be 60..=7776000 seconds; reset lifetime must be 60..=86400 seconds.".into()));
}
Ok(())
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct AuthThrottleLimit {
pub limit: u32,
pub window_seconds: u64,
}
impl AuthThrottleLimit {
fn validate(&self) -> Result<()> {
if !(1..=1_000_000).contains(&self.limit) || !(1..=86400).contains(&self.window_seconds) {
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()));
}
Ok(())
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(default, deny_unknown_fields)]
pub struct AuthThrottleSettings {
pub enabled: bool,
pub ip: AuthThrottleLimit,
pub account: AuthThrottleLimit,
}
impl Default for AuthThrottleSettings {
fn default() -> Self {
Self {
enabled: true,
ip: AuthThrottleLimit {
limit: 60,
window_seconds: 60,
},
account: AuthThrottleLimit {
limit: 10,
window_seconds: 60,
},
}
}
}
impl AuthThrottleSettings {
pub fn validate(&self) -> Result<()> {
self.ip.validate()?;
self.account.validate()
}
}