secure-session 0.4.0

Signed, encrypted session cookies for Iron
Documentation
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
//! Iron specific middleware and handlers.

use crate::error::SessionConfigError;
use crate::session::{Session, SessionManager};
use crate::SESSION_COOKIE_NAME;
use cookie::Cookie;
use data_encoding::BASE64;
use iron::headers::{Cookie as IronCookie, SetCookie};
use iron::middleware::{AroundMiddleware, Handler};
use iron::{IronResult, Request, Response};
use serde::de::DeserializeOwned;
use serde::ser::Serialize;
use std::marker::PhantomData;
use time::{Duration, OffsetDateTime};
use typemap;

/// Uses a `SessionManager` to serialize and deserialize cookies during the request/response cycle.
pub struct SessionHandler<V, K, S>
where
    V: Serialize + DeserializeOwned + 'static,
    K: typemap::Key<Value = V>,
    S: SessionManager<V>,
{
    manager: S,
    config: SessionConfig,
    handler: Box<dyn Handler>,
    _key: PhantomData<K>,
}

impl<
        V: Serialize + DeserializeOwned + 'static,
        K: typemap::Key<Value = V>,
        S: SessionManager<V>,
    > SessionHandler<V, K, S>
{
    fn new(manager: S, config: SessionConfig, handler: Box<dyn Handler>) -> Self {
        SessionHandler {
            manager: manager,
            config: config,
            handler: handler,
            _key: PhantomData,
        }
    }

    fn extract_session_cookie(&self, request: &Request) -> Option<Vec<u8>> {
        request.headers.get::<IronCookie>().and_then(|raw_cookie| {
            raw_cookie
                .0
                .iter()
                .filter_map(|c| {
                    Cookie::parse_encoded(c.clone())
                        .ok()
                        .and_then(|cookie| match cookie.name_value() {
                            (SESSION_COOKIE_NAME, value) => Some(value.to_string()),
                            _ => None,
                        })
                        .and_then(|c| BASE64.decode(c.as_bytes()).ok())
                })
                .collect::<Vec<Vec<u8>>>()
                .first()
                .map(|c| c.clone())
        })
    }
}

impl<
        V: Serialize + DeserializeOwned + 'static,
        K: typemap::Key<Value = V> + Send + Sync,
        S: SessionManager<V> + 'static,
    > Handler for SessionHandler<V, K, S>
{
    fn handle(&self, mut request: &mut Request) -> IronResult<Response> {
        // before
        match self
            .extract_session_cookie(&request)
            // TODO ? error out on deserialization failure and remove cookie since it is invalid
            .and_then(|c| self.manager.deserialize(&c).ok())
            .and_then(|s| match s.expires {
                Some(expires) if expires > OffsetDateTime::now_utc() => s.value,
                None => s.value,
                _ => None,
            })
            .take()
        {
            Some(value) => {
                let _ = request.extensions.insert::<K>(value);
            }
            None => (),
        }

        // main
        let mut response = self.handler.handle(&mut request)?;

        // after
        let expires = self
            .config
            .ttl_seconds
            .map(|ttl| OffsetDateTime::now_utc() + Duration::seconds(ttl));
        let session = Session {
            expires: expires,
            value: request.extensions.remove::<K>(),
        };

        // TODO unwrap
        let session_str = BASE64.encode(&self.manager.serialize(&session).unwrap());

        let cookie = Cookie::build(SESSION_COOKIE_NAME, session_str)
            .path(&self.config.path)
            .http_only(true)
            .secure(self.config.secure_cookie);
        // TODO config flag for SameSite

        let cookie = (match self.config.ttl_seconds {
            Some(ttl) => cookie.max_age(Duration::seconds(ttl)),
            None => cookie,
        })
        .finish();

        let mut cookies = vec![cookie.encoded().to_string()];

        {
            if let Some(set_cookie) = response.headers.get::<SetCookie>() {
                cookies.extend(set_cookie.0.clone())
            }
        }
        response.headers.set(SetCookie(cookies));

        Ok(response)
    }
}

/// Middleware for automatic session management.
pub struct SessionMiddleware<V, K, S>
where
    V: Serialize + DeserializeOwned + 'static,
    K: typemap::Key<Value = V>,
    S: SessionManager<V>,
{
    manager: S,
    config: SessionConfig,
    _key: PhantomData<K>,
    _value: PhantomData<V>,
}

impl<
        V: Serialize + DeserializeOwned + 'static,
        K: typemap::Key<Value = V>,
        S: SessionManager<V>,
    > SessionMiddleware<V, K, S>
{
    /// Create a new `SessionMiddleware` for the given `SessionManager` and `SessionConfig`.
    pub fn new(manager: S, config: SessionConfig) -> Self {
        SessionMiddleware {
            manager: manager,
            config: config,
            _key: PhantomData,
            _value: PhantomData,
        }
    }
}

impl<
        V: Serialize + DeserializeOwned + 'static,
        K: typemap::Key<Value = V>,
        S: SessionManager<V> + 'static,
    > AroundMiddleware for SessionMiddleware<V, K, S>
where
    SessionHandler<V, K, S>: Handler,
{
    fn around(self, handler: Box<dyn Handler>) -> Box<dyn Handler> {
        Box::new(SessionHandler::<V, K, S>::new(
            self.manager,
            self.config,
            handler,
        ))
    }
}

/// Configuration of how sessions and session cookies are created and validated.
#[derive(Clone)]
pub struct SessionConfig {
    ttl_seconds: Option<i64>,
    secure_cookie: bool,
    path: String,
}

impl SessionConfig {
    /// Create a new builder that is initialized with the default configuration.
    pub fn build() -> SessionConfigBuilder {
        SessionConfigBuilder::new()
    }
}

impl Default for SessionConfig {
    fn default() -> Self {
        Self {
            ttl_seconds: None,
            secure_cookie: true,
            path: "/".into(),
        }
    }
}

/// A utility to help build a `SessionConfig` in an API backwards compatible way.
#[derive(Clone)]
pub struct SessionConfigBuilder {
    config: SessionConfig,
}

impl SessionConfigBuilder {
    /// Create a new builder that is initialized with the default configuration.
    pub fn new() -> Self {
        SessionConfigBuilder {
            config: SessionConfig::default(),
        }
    }

    /// Set the session time to live (TTL) in seconds. Default: `None`
    pub fn ttl_seconds(mut self, ttl_seconds: Option<i64>) -> Self {
        self.config.ttl_seconds = ttl_seconds;
        self
    }

    /// Set the the secure flag in the session cookie (HTTPS only).
    pub fn secure_cookie(mut self, secure_cookie: bool) -> Self {
        self.config.secure_cookie = secure_cookie;
        self
    }

    /// Set the cookie's path.
    pub fn path<I: Into<String>>(mut self, path: I) -> Self {
        self.config.path = path.into();
        self
    }

    /// Consume the builder and return a config.
    pub fn finish(self) -> Result<SessionConfig, SessionConfigError> {
        match self.config.ttl_seconds {
            Some(ttl) if ttl < 0 => {
                return Err(SessionConfigError::Undefined);
            }
            _ => (),
        };
        Ok(self.config)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::session::{
        AesGcmSessionManager, ChaCha20Poly1305SessionManager, MultiSessionManager,
    };
    use iron::headers::{Cookie as IronCookie, Headers, SetCookie};
    use iron::status;
    use iron_test::request as mock_request;
    use serde::{Deserialize, Serialize};
    use typemap;

    const KEY_32: [u8; 32] = *b"01234567012345670123456701234567";

    #[derive(Serialize, Deserialize, Eq, PartialEq, Clone, Debug)]
    struct Data {
        string: String,
    }

    struct DataKey {}

    impl typemap::Key for DataKey {
        type Value = Data;
    }

    fn mock_handler(request: &mut Request) -> IronResult<Response> {
        let stat = match request.extensions.get::<DataKey>() {
            Some(_) => status::Ok,
            None => status::NoContent,
        };

        request.extensions.insert::<DataKey>(Data {
            string: "wat".to_string(),
        });

        Ok(Response::with((stat, "")))
    }

    macro_rules! test_cases {
        ($strct: ident, $md: ident) => {
            mod $md {
                use cookie::Cookie;
                use iron::headers::{Cookie as IronCookie, Headers, SetCookie};
                use iron::status;
                use iron_test::request as mock_request;

                use super::{mock_handler, Data, DataKey, KEY_32};
                use $crate::middleware::{SessionConfig, SessionConfigBuilder, SessionHandler};
                use $crate::session::$strct;

                #[test]
                fn no_expiry() {
                    let config = SessionConfig::default();
                    let manager = $strct::<Data>::from_key(KEY_32);
                    let middleware = SessionHandler::<Data, DataKey, $strct<Data>>::new(
                        manager,
                        config,
                        Box::new(mock_handler),
                    );

                    let path = "http://localhost/";

                    let response = mock_request::get(path, Headers::new(), &middleware)
                        .expect("request failed");
                    assert_eq!(response.status, Some(status::NoContent));

                    // get the cookies out
                    let set_cookie = response
                        .headers
                        .get::<SetCookie>()
                        .expect("no SetCookie header");
                    let cookie = Cookie::parse(set_cookie.0[0].clone()).expect("cookie not parsed");
                    let mut headers = Headers::new();
                    headers.set(IronCookie(vec![cookie.to_string()]));

                    // resend the request and get a different code back
                    let response =
                        mock_request::get(path, headers, &middleware).expect("request failed");
                    assert_eq!(response.status, Some(status::Ok));
                }

                #[test]
                fn sessions_expire() {
                    let config = SessionConfigBuilder::new()
                        .ttl_seconds(Some(0))
                        .finish()
                        .unwrap();
                    let manager = $strct::<Data>::from_key(KEY_32);
                    let middleware = SessionHandler::<Data, DataKey, $strct<Data>>::new(
                        manager,
                        config,
                        Box::new(mock_handler),
                    );

                    let path = "http://localhost/";

                    let response = mock_request::get(path, Headers::new(), &middleware)
                        .expect("request failed");
                    assert_eq!(response.status, Some(status::NoContent));

                    // get the cookies out
                    let set_cookie = response
                        .headers
                        .get::<SetCookie>()
                        .expect("no SetCookie header");
                    let cookie = Cookie::parse(set_cookie.0[0].clone()).expect("cookie not parsed");
                    let mut headers = Headers::new();
                    headers.set(IronCookie(vec![cookie.to_string()]));

                    // resend the request and get a different code back
                    let response =
                        mock_request::get(path, headers, &middleware).expect("request failed");
                    assert_eq!(response.status, Some(status::NoContent));
                }
            }
        };
    }

    test_cases!(AesGcmSessionManager, aesgcm);
    test_cases!(ChaCha20Poly1305SessionManager, chacha20poly1305);

    #[test]
    fn multisession_and_rotation() {
        let config = SessionConfig::default();

        let manager_1 = AesGcmSessionManager::<Data>::from_key(KEY_32);
        let manager_1_clone = AesGcmSessionManager::<Data>::from_key(KEY_32);
        let middle_1 = SessionMiddleware::<Data, DataKey, AesGcmSessionManager<Data>>::new(
            manager_1,
            config.clone(),
        );
        let handler_1 = middle_1.around(Box::new(mock_handler));

        let manager_2 = ChaCha20Poly1305SessionManager::<Data>::from_key(KEY_32);
        let manager_2_clone = ChaCha20Poly1305SessionManager::<Data>::from_key(KEY_32);
        let middle_2 =
            SessionMiddleware::<Data, DataKey, ChaCha20Poly1305SessionManager<Data>>::new(
                manager_2,
                config.clone(),
            );
        let handler_2 = middle_2.around(Box::new(mock_handler));

        let multi = MultiSessionManager::<Data>::new(
            Box::new(manager_2_clone),
            vec![Box::new(manager_1_clone)],
        );
        let multi_middle =
            SessionMiddleware::<Data, DataKey, MultiSessionManager<Data>>::new(multi, config);
        let multi_handler = multi_middle.around(Box::new(mock_handler));

        // make a request to the first handler and get the initial session
        let resp = mock_request::get("http://localhost/", Headers::new(), &handler_1).unwrap();
        let set_cookie = resp
            .headers
            .get::<SetCookie>()
            .expect("no SetCookie header");
        let cookie = Cookie::parse(set_cookie.0[0].clone()).expect("cookie not parsed");

        // make a request to the multi handler
        let mut headers = Headers::new();
        headers.set(IronCookie(vec![cookie.to_string()]));
        let resp = mock_request::get("http://localhost/", headers, &multi_handler).unwrap();
        assert_eq!(resp.status, Some(status::Ok));

        // get the cookie back out
        let set_cookie = resp
            .headers
            .get::<SetCookie>()
            .expect("no SetCookie header");
        let cookie = Cookie::parse(set_cookie.0[0].clone()).expect("cookie not parsed");

        // make a request to the second handler
        let mut headers = Headers::new();
        headers.set(IronCookie(vec![cookie.to_string()]));
        let resp = mock_request::get("http://localhost/", headers, &handler_2).unwrap();
        assert_eq!(resp.status, Some(status::Ok));
    }
}