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