@@ -54,6 +54,7 @@ pub struct SessionMiddleware<Store> {
54
54
cookie_domain : Option < String > ,
55
55
session_ttl : Option < Duration > ,
56
56
save_unchanged : bool ,
57
+ secure : Option < bool > ,
57
58
same_site_policy : SameSite ,
58
59
key : Key ,
59
60
}
@@ -66,6 +67,7 @@ impl<Store: SessionStore> std::fmt::Debug for SessionMiddleware<Store> {
66
67
. field ( "cookie_name" , & self . cookie_name )
67
68
. field ( "cookie_domain" , & self . cookie_domain )
68
69
. field ( "session_ttl" , & self . session_ttl )
70
+ . field ( "secure" , & self . secure )
69
71
. field ( "same_site_policy" , & self . same_site_policy )
70
72
. field ( "key" , & ".." )
71
73
. field ( "save_unchanged" , & self . save_unchanged )
91
93
session. expire_in ( ttl) ;
92
94
}
93
95
94
- let secure_cookie = request. url ( ) . scheme ( ) == "https" ;
96
+ let mut secure_cookie = request. url ( ) . scheme ( ) == "https" ;
97
+ if let Some ( secure) = self . secure {
98
+ secure_cookie = secure;
99
+ }
95
100
request. set_ext ( session. clone ( ) ) ;
96
101
97
102
let mut response = next. run ( request) . await ;
@@ -140,6 +145,7 @@ impl<Store: SessionStore> SessionMiddleware<Store> {
140
145
/// * cookie path: "/"
141
146
/// * cookie name: "tide.sid"
142
147
/// * session ttl: one day
148
+ /// * secure: request.scheme == 'https'
143
149
/// * same site: strict
144
150
/// * save unchanged: enabled
145
151
///
@@ -160,6 +166,7 @@ impl<Store: SessionStore> SessionMiddleware<Store> {
160
166
/// .with_cookie_name("custom.cookie.name")
161
167
/// .with_cookie_path("/some/path")
162
168
/// .with_cookie_domain("www.rust-lang.org")
169
+ /// .with_secure(true)
163
170
/// .with_same_site_policy(SameSite::Lax)
164
171
/// .with_session_ttl(Some(Duration::from_secs(1)))
165
172
/// .without_save_unchanged(),
@@ -172,6 +179,7 @@ impl<Store: SessionStore> SessionMiddleware<Store> {
172
179
cookie_path : "/" . into ( ) ,
173
180
cookie_name : "tide.sid" . into ( ) ,
174
181
cookie_domain : None ,
182
+ secure : None ,
175
183
same_site_policy : SameSite :: Lax ,
176
184
session_ttl : Some ( Duration :: from_secs ( 24 * 60 * 60 ) ) ,
177
185
key : Key :: derive_from ( secret) ,
@@ -217,6 +225,14 @@ impl<Store: SessionStore> SessionMiddleware<Store> {
217
225
self
218
226
}
219
227
228
+ /// Sets the secure attribute of the cookie.
229
+ /// Defaults to true if the incoming request scheme is 'https'
230
+ /// Can optionally be set to true or false to override
231
+ pub fn with_secure ( mut self , secure : bool ) -> Self {
232
+ self . secure = Some ( secure) ;
233
+ self
234
+ }
235
+
220
236
/// Sets the same site policy for the session cookie. Defaults to
221
237
/// SameSite::Lax. See [incrementally better
222
238
/// cookies](https://tools.ietf.org/html/draft-west-cookie-incrementalism-01)
0 commit comments