1use std::time::Duration;
4
5#[derive(Clone, Debug)]
7pub struct SessionConfig {
8 pub lifetime: Duration,
10 pub cookie_name: String,
12 pub cookie_path: String,
14 pub cookie_secure: bool,
16 pub cookie_http_only: bool,
18 pub cookie_same_site: String,
20 pub table_name: String,
22}
23
24impl Default for SessionConfig {
25 fn default() -> Self {
26 Self {
27 lifetime: Duration::from_secs(120 * 60), cookie_name: "kit_session".to_string(),
29 cookie_path: "/".to_string(),
30 cookie_secure: true,
31 cookie_http_only: true,
32 cookie_same_site: "Lax".to_string(),
33 table_name: "sessions".to_string(),
34 }
35 }
36}
37
38impl SessionConfig {
39 pub fn new() -> Self {
41 Self::default()
42 }
43
44 pub fn from_env() -> Self {
53 let lifetime_minutes: u64 = crate::env_optional("SESSION_LIFETIME")
54 .and_then(|s: String| s.parse().ok())
55 .unwrap_or(120);
56
57 let cookie_secure = crate::env_optional("SESSION_SECURE")
58 .map(|s: String| s.to_lowercase() == "true" || s == "1")
59 .unwrap_or(true);
60
61 Self {
62 lifetime: Duration::from_secs(lifetime_minutes * 60),
63 cookie_name: crate::env_optional("SESSION_COOKIE")
64 .unwrap_or_else(|| "kit_session".to_string()),
65 cookie_path: crate::env_optional("SESSION_PATH")
66 .unwrap_or_else(|| "/".to_string()),
67 cookie_secure,
68 cookie_http_only: true, cookie_same_site: crate::env_optional("SESSION_SAME_SITE")
70 .unwrap_or_else(|| "Lax".to_string()),
71 table_name: "sessions".to_string(),
72 }
73 }
74
75 pub fn lifetime(mut self, duration: Duration) -> Self {
77 self.lifetime = duration;
78 self
79 }
80
81 pub fn cookie_name(mut self, name: impl Into<String>) -> Self {
83 self.cookie_name = name.into();
84 self
85 }
86
87 pub fn secure(mut self, secure: bool) -> Self {
89 self.cookie_secure = secure;
90 self
91 }
92}