1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//! `SessionConfig`: cookie name, TTL, and cookie-attribute knobs.
use std::time::Duration;
/// Session configuration
#[non_exhaustive]
#[derive(Debug, Clone)]
pub struct SessionConfig {
/// Cookie name
pub cookie_name: String,
/// Session TTL
pub ttl: Duration,
/// HTTPS-only cookie
pub secure: bool,
/// HttpOnly flag
pub http_only: bool,
/// SameSite attribute
pub same_site: Option<String>,
/// Domain
pub domain: Option<String>,
/// Path
pub path: String,
}
impl SessionConfig {
/// Create a new configuration
///
/// # Examples
///
/// ```
/// use std::time::Duration;
/// use reinhardt_middleware::session::SessionConfig;
///
/// let config = SessionConfig::new("sessionid".to_string(), Duration::from_secs(3600));
/// assert_eq!(config.cookie_name, "sessionid");
/// assert_eq!(config.ttl, Duration::from_secs(3600));
/// ```
pub fn new(cookie_name: String, ttl: Duration) -> Self {
Self {
cookie_name,
ttl,
secure: true,
http_only: true,
same_site: Some("Lax".to_string()),
domain: None,
path: "/".to_string(),
}
}
/// Enable secure cookie
///
/// # Examples
///
/// ```
/// use std::time::Duration;
/// use reinhardt_middleware::session::SessionConfig;
///
/// let config = SessionConfig::new("sessionid".to_string(), Duration::from_secs(3600))
/// .with_secure();
/// assert!(config.secure);
/// ```
pub fn with_secure(mut self) -> Self {
self.secure = true;
self
}
/// Set HttpOnly flag
///
/// # Examples
///
/// ```
/// use std::time::Duration;
/// use reinhardt_middleware::session::SessionConfig;
///
/// let config = SessionConfig::new("sessionid".to_string(), Duration::from_secs(3600))
/// .with_http_only(false);
/// assert!(!config.http_only);
/// ```
pub fn with_http_only(mut self, http_only: bool) -> Self {
self.http_only = http_only;
self
}
/// Set SameSite attribute
///
/// # Examples
///
/// ```
/// use std::time::Duration;
/// use reinhardt_middleware::session::SessionConfig;
///
/// let config = SessionConfig::new("sessionid".to_string(), Duration::from_secs(3600))
/// .with_same_site("Strict".to_string());
/// ```
pub fn with_same_site(mut self, same_site: String) -> Self {
self.same_site = Some(same_site);
self
}
/// Set domain
///
/// # Examples
///
/// ```
/// use std::time::Duration;
/// use reinhardt_middleware::session::SessionConfig;
///
/// let config = SessionConfig::new("sessionid".to_string(), Duration::from_secs(3600))
/// .with_domain("example.com".to_string());
/// ```
pub fn with_domain(mut self, domain: String) -> Self {
self.domain = Some(domain);
self
}
/// Set path
///
/// # Examples
///
/// ```
/// use std::time::Duration;
/// use reinhardt_middleware::session::SessionConfig;
///
/// let config = SessionConfig::new("sessionid".to_string(), Duration::from_secs(3600))
/// .with_path("/app".to_string());
/// assert_eq!(config.path, "/app");
/// ```
pub fn with_path(mut self, path: String) -> Self {
self.path = path;
self
}
}
impl Default for SessionConfig {
fn default() -> Self {
Self::new("sessionid".to_string(), Duration::from_secs(3600))
}
}