Skip to content

Commit 9341662

Browse files
authored
Merge pull request #730 from rhuffus/add-cookie-domain-to-sessionmiddleware
Add with_cookie_domain method to SessionMiddleware
2 parents 7565150 + 3fd1770 commit 9341662

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/sessions/middleware.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ pub struct SessionMiddleware<Store> {
5151
store: Store,
5252
cookie_path: String,
5353
cookie_name: String,
54+
cookie_domain: Option<String>,
5455
session_ttl: Option<Duration>,
5556
save_unchanged: bool,
5657
same_site_policy: SameSite,
@@ -63,6 +64,7 @@ impl<Store: SessionStore> std::fmt::Debug for SessionMiddleware<Store> {
6364
.field("store", &self.store)
6465
.field("cookie_path", &self.cookie_path)
6566
.field("cookie_name", &self.cookie_name)
67+
.field("cookie_domain", &self.cookie_domain)
6668
.field("session_ttl", &self.session_ttl)
6769
.field("same_site_policy", &self.same_site_policy)
6870
.field("key", &"..")
@@ -157,6 +159,7 @@ impl<Store: SessionStore> SessionMiddleware<Store> {
157159
/// SessionMiddleware::new(MemoryStore::new(), b"please do not hardcode your secret")
158160
/// .with_cookie_name("custom.cookie.name")
159161
/// .with_cookie_path("/some/path")
162+
/// .with_cookie_domain("www.rust-lang.org")
160163
/// .with_same_site_policy(SameSite::Lax)
161164
/// .with_session_ttl(Some(Duration::from_secs(1)))
162165
/// .without_save_unchanged(),
@@ -168,6 +171,7 @@ impl<Store: SessionStore> SessionMiddleware<Store> {
168171
save_unchanged: true,
169172
cookie_path: "/".into(),
170173
cookie_name: "tide.sid".into(),
174+
cookie_domain: None,
171175
same_site_policy: SameSite::Strict,
172176
session_ttl: Some(Duration::from_secs(24 * 60 * 60)),
173177
key: Key::derive_from(secret),
@@ -222,6 +226,12 @@ impl<Store: SessionStore> SessionMiddleware<Store> {
222226
self
223227
}
224228

229+
/// Sets the domain of the cookie.
230+
pub fn with_cookie_domain(mut self, cookie_domain: impl AsRef<str>) -> Self {
231+
self.cookie_domain = Some(cookie_domain.as_ref().to_owned());
232+
self
233+
}
234+
225235
//--- methods below here are private ---
226236

227237
async fn load_or_create(&self, cookie_value: Option<String>) -> Session {
@@ -247,6 +257,10 @@ impl<Store: SessionStore> SessionMiddleware<Store> {
247257
cookie.set_expires(Some((std::time::SystemTime::now() + ttl).into()));
248258
}
249259

260+
if let Some(cookie_domain) = self.cookie_domain.clone() {
261+
cookie.set_domain(cookie_domain)
262+
}
263+
250264
self.sign_cookie(&mut cookie);
251265

252266
cookie

tests/sessions.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ async fn test_basic_sessions() -> tide::Result<()> {
3737
let cookies = Cookies::from_response(&response);
3838
let cookie = &cookies["tide.sid"];
3939
assert_eq!(cookie.name(), "tide.sid");
40+
assert_eq!(cookie.domain(), None);
4041
assert_eq!(cookie.http_only(), Some(true));
4142
assert_eq!(cookie.same_site(), Some(SameSite::Strict));
4243
assert_eq!(cookie.secure(), None); // this request was http://
@@ -62,6 +63,7 @@ async fn test_customized_sessions() -> tide::Result<()> {
6263
SessionMiddleware::new(MemoryStore::new(), b"12345678901234567890123456789012345")
6364
.with_cookie_name("custom.cookie.name")
6465
.with_cookie_path("/nested")
66+
.with_cookie_domain("www.rust-lang.org")
6567
.with_same_site_policy(SameSite::Lax)
6668
.with_session_ttl(Some(Duration::from_secs(1)))
6769
.without_save_unchanged(),
@@ -99,6 +101,7 @@ async fn test_customized_sessions() -> tide::Result<()> {
99101
assert_eq!(cookie.http_only(), Some(true));
100102
assert_eq!(cookie.same_site(), Some(SameSite::Lax));
101103
assert_eq!(cookie.path(), Some("/nested"));
104+
assert_eq!(cookie.domain(), Some("www.rust-lang.org"));
102105
let cookie_value = cookie.value().to_string();
103106

104107
let mut second_response = app

0 commit comments

Comments
 (0)