use cookie::SameSite;
use std::time::Duration;
#[derive(Clone, Debug)]
pub struct CsrfConfig {
pub cookie_name: String,
pub header_name: String,
pub cookie_path: String,
pub cookie_domain: Option<String>,
pub cookie_secure: bool,
pub cookie_http_only: bool,
pub cookie_same_site: SameSite,
pub cookie_max_age: Duration,
pub token_length: usize,
}
impl Default for CsrfConfig {
fn default() -> Self {
Self {
cookie_name: "XSRF-TOKEN".to_string(),
header_name: "X-XSRF-TOKEN".to_string(),
cookie_path: "/".to_string(),
cookie_domain: None,
cookie_secure: true, cookie_http_only: false,
cookie_same_site: SameSite::Lax,
cookie_max_age: Duration::from_secs(60 * 60 * 24),
token_length: 32,
}
}
}
impl CsrfConfig {
pub fn new() -> Self {
Self::default()
}
pub fn cookie_name(mut self, name: impl Into<String>) -> Self {
self.cookie_name = name.into();
self
}
pub fn header_name(mut self, name: impl Into<String>) -> Self {
self.header_name = name.into();
self
}
pub fn cookie_domain(mut self, domain: impl Into<String>) -> Self {
self.cookie_domain = Some(domain.into());
self
}
pub fn secure(mut self, secure: bool) -> Self {
self.cookie_secure = secure;
self
}
pub fn same_site(mut self, same_site: SameSite) -> Self {
self.cookie_same_site = same_site;
self
}
}