Skip to main content

wae_authentication/csrf/
config.rs

1//! CSRF 配置
2
3/// CSRF 保护配置
4#[derive(Debug, Clone)]
5pub struct CsrfConfig {
6    /// 令牌过期时间(秒),默认 3600 秒(1小时)
7    pub token_ttl: u64,
8    /// 令牌长度(字节),默认 32 字节
9    pub token_length: usize,
10}
11
12impl Default for CsrfConfig {
13    fn default() -> Self {
14        Self { token_ttl: 3600, token_length: 32 }
15    }
16}
17
18impl CsrfConfig {
19    /// 创建新的 CSRF 配置
20    ///
21    /// # Arguments
22    /// * `token_ttl` - 令牌过期时间(秒)
23    /// * `token_length` - 令牌长度(字节)
24    pub fn new(token_ttl: u64, token_length: usize) -> Self {
25        Self { token_ttl, token_length: token_length.clamp(16, 64) }
26    }
27
28    /// 设置令牌过期时间
29    ///
30    /// # Arguments
31    /// * `ttl` - 过期时间(秒)
32    pub fn with_token_ttl(mut self, ttl: u64) -> Self {
33        self.token_ttl = ttl;
34        self
35    }
36
37    /// 设置令牌长度
38    ///
39    /// # Arguments
40    /// * `length` - 令牌长度(字节)
41    pub fn with_token_length(mut self, length: usize) -> Self {
42        self.token_length = length.clamp(16, 64);
43        self
44    }
45}