Skip to main content

rustauth_core/options/
cookies.rs

1use time::Duration;
2
3/// Session cookie cache configuration.
4#[derive(Debug, Clone, PartialEq, Eq)]
5pub struct CookieCacheOptions {
6    pub enabled: bool,
7    pub max_age: Option<Duration>,
8    pub strategy: CookieCacheStrategy,
9    pub refresh_cache: bool,
10    pub version: Option<String>,
11}
12
13impl Default for CookieCacheOptions {
14    fn default() -> Self {
15        Self {
16            enabled: false,
17            max_age: None,
18            strategy: CookieCacheStrategy::Compact,
19            refresh_cache: false,
20            version: None,
21        }
22    }
23}
24
25impl CookieCacheOptions {
26    pub fn new() -> Self {
27        Self::default()
28    }
29
30    pub fn builder() -> Self {
31        Self::new()
32    }
33
34    #[must_use]
35    pub fn enabled(mut self, enabled: bool) -> Self {
36        self.enabled = enabled;
37        self
38    }
39
40    #[must_use]
41    pub fn max_age(mut self, max_age: Duration) -> Self {
42        self.max_age = Some(max_age);
43        self
44    }
45
46    #[must_use]
47    pub fn strategy(mut self, strategy: CookieCacheStrategy) -> Self {
48        self.strategy = strategy;
49        self
50    }
51
52    #[must_use]
53    pub fn refresh_cache(mut self, refresh_cache: bool) -> Self {
54        self.refresh_cache = refresh_cache;
55        self
56    }
57
58    #[must_use]
59    pub fn version(mut self, version: impl Into<String>) -> Self {
60        self.version = Some(version.into());
61        self
62    }
63}
64
65/// Cookie cache encoding strategy.
66#[derive(Debug, Clone, Copy, PartialEq, Eq)]
67pub enum CookieCacheStrategy {
68    Compact,
69    Jwt,
70    Jwe,
71}
72
73/// Cross-subdomain cookie configuration.
74#[derive(Debug, Clone, Default, PartialEq, Eq)]
75pub struct CookieConfig {
76    pub enabled: bool,
77    pub domain: Option<String>,
78}
79
80impl CookieConfig {
81    pub fn new() -> Self {
82        Self::default()
83    }
84
85    pub fn builder() -> Self {
86        Self::new()
87    }
88
89    #[must_use]
90    pub fn enabled(mut self, enabled: bool) -> Self {
91        self.enabled = enabled;
92        self
93    }
94
95    #[must_use]
96    pub fn domain(mut self, domain: impl Into<String>) -> Self {
97        self.domain = Some(domain.into());
98        self
99    }
100}