Skip to main content

openauth_core/options/
advanced.rs

1use crate::utils::ip::Ipv6Subnet;
2
3use super::cookies::CookieConfig;
4
5/// Advanced configuration.
6#[derive(Debug, Clone, Default, PartialEq, Eq)]
7pub struct AdvancedOptions {
8    pub use_secure_cookies: Option<bool>,
9    pub cookie_prefix: Option<String>,
10    pub cross_subdomain_cookies: Option<CookieConfig>,
11    pub default_cookie_attributes: CookieAttributesOverride,
12    pub disable_csrf_check: bool,
13    pub disable_origin_check: bool,
14    pub skip_trailing_slashes: bool,
15    pub ip_address: IpAddressOptions,
16}
17
18impl AdvancedOptions {
19    pub fn new() -> Self {
20        Self::default()
21    }
22
23    pub fn builder() -> Self {
24        Self::new()
25    }
26
27    #[must_use]
28    pub fn use_secure_cookies(mut self, enabled: bool) -> Self {
29        self.use_secure_cookies = Some(enabled);
30        self
31    }
32
33    #[must_use]
34    pub fn cookie_prefix(mut self, prefix: impl Into<String>) -> Self {
35        self.cookie_prefix = Some(prefix.into());
36        self
37    }
38
39    #[must_use]
40    pub fn cross_subdomain_cookies(mut self, config: CookieConfig) -> Self {
41        self.cross_subdomain_cookies = Some(config);
42        self
43    }
44
45    #[must_use]
46    pub fn default_cookie_attributes(mut self, attributes: CookieAttributesOverride) -> Self {
47        self.default_cookie_attributes = attributes;
48        self
49    }
50
51    #[must_use]
52    pub fn disable_csrf_check(mut self, disabled: bool) -> Self {
53        self.disable_csrf_check = disabled;
54        self
55    }
56
57    #[must_use]
58    pub fn disable_origin_check(mut self, disabled: bool) -> Self {
59        self.disable_origin_check = disabled;
60        self
61    }
62
63    #[must_use]
64    pub fn skip_trailing_slashes(mut self, enabled: bool) -> Self {
65        self.skip_trailing_slashes = enabled;
66        self
67    }
68
69    #[must_use]
70    pub fn ip_address(mut self, ip_address: IpAddressOptions) -> Self {
71        self.ip_address = ip_address;
72        self
73    }
74}
75
76#[derive(Debug, Clone, PartialEq, Eq)]
77pub struct IpAddressOptions {
78    pub headers: Vec<String>,
79    pub disable_ip_tracking: bool,
80    pub ipv6_subnet: Ipv6Subnet,
81}
82
83impl Default for IpAddressOptions {
84    fn default() -> Self {
85        Self {
86            headers: Vec::new(),
87            disable_ip_tracking: false,
88            ipv6_subnet: Ipv6Subnet::Prefix64,
89        }
90    }
91}
92
93impl IpAddressOptions {
94    pub fn new() -> Self {
95        Self::default()
96    }
97
98    pub fn builder() -> Self {
99        Self::new()
100    }
101
102    #[must_use]
103    pub fn header(mut self, header: impl Into<String>) -> Self {
104        self.headers.push(header.into());
105        self
106    }
107
108    #[must_use]
109    pub fn headers<I, S>(mut self, headers: I) -> Self
110    where
111        I: IntoIterator<Item = S>,
112        S: Into<String>,
113    {
114        self.headers = headers.into_iter().map(Into::into).collect();
115        self
116    }
117
118    #[must_use]
119    pub fn disable_ip_tracking(mut self, disabled: bool) -> Self {
120        self.disable_ip_tracking = disabled;
121        self
122    }
123
124    #[must_use]
125    pub fn ipv6_subnet(mut self, subnet: Ipv6Subnet) -> Self {
126        self.ipv6_subnet = subnet;
127        self
128    }
129}
130
131/// User-supplied cookie attribute defaults.
132#[derive(Debug, Clone, Default, PartialEq, Eq)]
133pub struct CookieAttributesOverride {
134    pub domain: Option<String>,
135    pub path: Option<String>,
136    pub secure: Option<bool>,
137    pub http_only: Option<bool>,
138    pub same_site: Option<String>,
139    pub max_age: Option<u64>,
140    pub partitioned: Option<bool>,
141}
142
143impl CookieAttributesOverride {
144    pub fn new() -> Self {
145        Self::default()
146    }
147
148    pub fn builder() -> Self {
149        Self::new()
150    }
151
152    #[must_use]
153    pub fn domain(mut self, domain: impl Into<String>) -> Self {
154        self.domain = Some(domain.into());
155        self
156    }
157
158    #[must_use]
159    pub fn path(mut self, path: impl Into<String>) -> Self {
160        self.path = Some(path.into());
161        self
162    }
163
164    #[must_use]
165    pub fn secure(mut self, secure: bool) -> Self {
166        self.secure = Some(secure);
167        self
168    }
169
170    #[must_use]
171    pub fn http_only(mut self, http_only: bool) -> Self {
172        self.http_only = Some(http_only);
173        self
174    }
175
176    #[must_use]
177    pub fn same_site(mut self, same_site: impl Into<String>) -> Self {
178        self.same_site = Some(same_site.into());
179        self
180    }
181
182    #[must_use]
183    pub fn max_age(mut self, max_age: u64) -> Self {
184        self.max_age = Some(max_age);
185        self
186    }
187
188    #[must_use]
189    pub fn partitioned(mut self, partitioned: bool) -> Self {
190        self.partitioned = Some(partitioned);
191        self
192    }
193}