luxid-core 0.3.0

Core runtime for the Luxid web framework
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
//! Server-side sessions.
//!
//! A session is a bag of values keyed by an opaque id, carried in a cookie.
//! The values live in a [`SessionStore`]; the cookie carries only the id, so a
//! client can neither read nor forge the contents.
//!
//! The session handle is shared rather than owned. Middleware needs to persist
//! whatever the action did, but the action *consumes* the context — so the
//! handle wraps shared state, and the middleware keeps a clone to read back
//! afterwards. That is the same problem the owning context created for the
//! database scope, solved the same way.

use std::collections::BTreeMap;
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};

use serde::Serialize;
use serde::de::DeserializeOwned;
use serde_json::Value;

use crate::auth::Identity;
use crate::error::{Error, Result};
use crate::middleware::BoxFuture;

/// The reserved key holding the authenticated subject.
const SUBJECT: &str = "__luxid_subject";

/// Flash values written during this request, readable on the *next* one.
const FLASH_NEXT: &str = "__luxid_flash_next";

/// Flash values written during the previous request, readable now.
const FLASH_NOW: &str = "__luxid_flash_now";

/// Everything stored against one session id.
pub type SessionData = BTreeMap<String, Value>;

#[derive(Debug, Default)]
struct State {
    id: String,
    data: SessionData,
    dirty: bool,
    destroyed: bool,
    /// False when no session middleware ran, so writes can say so rather than
    /// vanishing.
    attached: bool,
}

/// The request's session.
#[derive(Debug, Clone, Default)]
pub struct Session {
    state: Arc<Mutex<State>>,
}

impl Session {
    /// A session that is not backed by anything. Reads are empty and writes
    /// report the missing middleware.
    pub fn detached() -> Self {
        Self::default()
    }

    pub(crate) fn attached(id: String, data: SessionData) -> Self {
        Self {
            state: Arc::new(Mutex::new(State {
                id,
                data,
                dirty: false,
                destroyed: false,
                attached: true,
            })),
        }
    }

    fn with<T>(&self, read: impl FnOnce(&State) -> T) -> T {
        read(&self.state.lock().expect("session state is not poisoned"))
    }

    fn write<T>(&self, change: impl FnOnce(&mut State) -> T) -> Result<T> {
        let mut state = self.state.lock().expect("session state is not poisoned");

        if !state.attached {
            return Err(Error::internal(
                "no session is active on this route. Add `.middleware(Auth::session())`, \
                 and bind a `SessionStore` in `providers()`.",
            ));
        }

        state.dirty = true;
        Ok(change(&mut state))
    }

    pub fn id(&self) -> String {
        self.with(|state| state.id.clone())
    }

    pub fn is_active(&self) -> bool {
        self.with(|state| state.attached)
    }

    pub fn get<T: DeserializeOwned>(&self, key: &str) -> Result<Option<T>> {
        let raw = self.with(|state| state.data.get(key).cloned());

        match raw {
            Some(value) => serde_json::from_value(value)
                .map(Some)
                .map_err(|err| Error::internal(format!("session key `{key}`: {err}"))),
            None => Ok(None),
        }
    }

    pub fn put<T: Serialize>(&self, key: impl Into<String>, value: T) -> Result<()> {
        let key = key.into();
        let value = serde_json::to_value(value)
            .map_err(|err| Error::internal(format!("session key `{key}`: {err}")))?;

        self.write(|state| {
            state.data.insert(key, value);
        })
    }

    pub fn has(&self, key: &str) -> bool {
        self.with(|state| state.data.contains_key(key))
    }

    pub fn forget(&self, key: &str) -> Result<()> {
        self.write(|state| {
            state.data.remove(key);
        })
    }

    /// Store a value readable on the *next* request only, then discarded.
    ///
    /// This is what makes the post-redirect-get pattern work: an action that
    /// fails validation flashes the errors and redirects, and the GET that
    /// follows reads them once. Nothing has to remember to clean up.
    ///
    /// Flash is deliberately separate from [`Session::put`]. A value that
    /// should survive exactly one hop and a value that should persist are
    /// different intentions, and conflating them is how stale error messages
    /// end up rendered on unrelated pages.
    pub fn flash<T: Serialize>(&self, key: impl Into<String>, value: T) -> Result<()> {
        let key = key.into();
        let value = serde_json::to_value(value)
            .map_err(|err| Error::internal(format!("flash key `{key}`: {err}")))?;

        self.write(|state| {
            let bag = state
                .data
                .entry(FLASH_NEXT.to_owned())
                .or_insert_with(|| Value::Object(Default::default()));

            if let Some(map) = bag.as_object_mut() {
                map.insert(key, value);
            }
        })
    }

    /// Read a value flashed by the previous request.
    ///
    /// Reading does not consume it: within one request the same flash can be
    /// read by middleware and by an action. It is discarded when the *next*
    /// request ages the bag.
    pub fn flashed<T: DeserializeOwned>(&self, key: &str) -> Result<Option<T>> {
        let raw = self.with(|state| {
            state
                .data
                .get(FLASH_NOW)
                .and_then(|bag| bag.as_object())
                .and_then(|map| map.get(key))
                .cloned()
        });

        match raw {
            Some(value) => serde_json::from_value(value)
                .map(Some)
                .map_err(|err| Error::internal(format!("flash key `{key}`: {err}"))),
            None => Ok(None),
        }
    }

    /// Whether anything was flashed by the previous request.
    pub fn has_flash(&self) -> bool {
        self.with(|state| {
            state
                .data
                .get(FLASH_NOW)
                .and_then(|bag| bag.as_object())
                .is_some_and(|map| !map.is_empty())
        })
    }

    /// Promote last request's flash bag to be readable now, and start an empty
    /// one for this request. Called by the session middleware at load time.
    ///
    /// This does not mark the session dirty on its own: a request that neither
    /// reads nor writes anything should not cause a store write just because
    /// an empty bag was rotated.
    pub(crate) fn age_flash(&self) {
        let mut state = self.state.lock().expect("session state is not poisoned");

        let carried = state.data.remove(FLASH_NEXT);
        let had_now = state.data.remove(FLASH_NOW).is_some();

        match carried {
            Some(bag) => {
                state.data.insert(FLASH_NOW.to_owned(), bag);
                // Something changed that must reach the store, or the flash
                // would be delivered again on the request after this one.
                state.dirty = true;
            }
            None => {
                if had_now {
                    state.dirty = true;
                }
            }
        }
    }

    /// Empty the session, keeping the id.
    pub fn flush(&self) -> Result<()> {
        self.write(|state| state.data.clear())
    }

    /// Invalidate the session entirely: the store entry is removed and the
    /// cookie cleared.
    pub fn destroy(&self) -> Result<()> {
        self.write(|state| {
            state.data.clear();
            state.destroyed = true;
        })
    }

    /// Record who this session belongs to.
    ///
    /// Does **not** rotate the id — call [`Session::regenerate`] on login to
    /// defeat session fixation, which is what `login_as` does for you.
    pub fn set_subject(&self, subject: impl Into<String>) -> Result<()> {
        self.put(SUBJECT, subject.into())
    }

    pub fn subject(&self) -> Option<String> {
        self.with(|state| {
            state
                .data
                .get(SUBJECT)
                .and_then(Value::as_str)
                .map(str::to_owned)
        })
    }

    /// Give the session a fresh id, keeping its contents.
    ///
    /// Do this whenever privilege changes. Without it, an attacker who fixed a
    /// victim's session id before login still holds a valid id afterwards.
    pub fn regenerate(&self) -> Result<()> {
        let id = new_id();
        self.write(|state| state.id = id)
    }

    /// Log an identity in: rotate the id, then record the subject.
    pub fn login(&self, identity: &Identity) -> Result<()> {
        self.regenerate()?;
        self.set_subject(identity.subject())
    }

    /// Log out and invalidate, so the old cookie is worthless.
    pub fn logout(&self) -> Result<()> {
        self.destroy()
    }

    pub(crate) fn snapshot(&self) -> (String, SessionData, bool, bool) {
        self.with(|state| {
            (
                state.id.clone(),
                state.data.clone(),
                state.dirty,
                state.destroyed,
            )
        })
    }
}

/// Where session data lives.
pub trait SessionStore: Send + Sync + 'static {
    fn load(&self, id: &str) -> BoxFuture<'_, Result<Option<SessionData>>>;

    fn save(&self, id: &str, data: SessionData, ttl: Duration) -> BoxFuture<'_, Result<()>>;

    fn destroy(&self, id: &str) -> BoxFuture<'_, Result<()>>;
}

/// An in-process store.
///
/// Fine for a single process and for tests. Sessions are lost on restart and
/// are not shared between instances, so anything running more than one process
/// needs a shared store.
#[derive(Debug, Default)]
pub struct MemoryStore {
    entries: Mutex<BTreeMap<String, (SessionData, Instant)>>,
}

impl MemoryStore {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn len(&self) -> usize {
        self.entries.lock().expect("not poisoned").len()
    }

    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

impl SessionStore for MemoryStore {
    fn load(&self, id: &str) -> BoxFuture<'_, Result<Option<SessionData>>> {
        let id = id.to_owned();

        Box::pin(async move {
            let mut entries = self.entries.lock().expect("not poisoned");

            // Expiry is checked on read rather than swept on a timer: a store
            // this simple should not own a background task.
            match entries.get(&id) {
                Some((_, expires)) if *expires <= Instant::now() => {
                    entries.remove(&id);
                    Ok(None)
                }
                Some((data, _)) => Ok(Some(data.clone())),
                None => Ok(None),
            }
        })
    }

    fn save(&self, id: &str, data: SessionData, ttl: Duration) -> BoxFuture<'_, Result<()>> {
        let id = id.to_owned();

        Box::pin(async move {
            self.entries
                .lock()
                .expect("not poisoned")
                .insert(id, (data, Instant::now() + ttl));
            Ok(())
        })
    }

    fn destroy(&self, id: &str) -> BoxFuture<'_, Result<()>> {
        let id = id.to_owned();

        Box::pin(async move {
            self.entries.lock().expect("not poisoned").remove(&id);
            Ok(())
        })
    }
}

/// A 256-bit opaque identifier.
///
/// Sourced from the OS random generator, not a counter or a timestamp: a
/// guessable session id is an account takeover.
pub fn new_id() -> String {
    use argon2::password_hash::rand_core::RngCore;

    let mut bytes = [0u8; 32];
    argon2::password_hash::rand_core::OsRng.fill_bytes(&mut bytes);

    bytes.iter().map(|byte| format!("{byte:02x}")).collect()
}

#[cfg(test)]
mod tests {
    use super::*;

    fn session() -> Session {
        Session::attached(new_id(), SessionData::new())
    }

    #[test]
    fn ids_are_long_and_unpredictable() {
        let a = new_id();
        let b = new_id();

        assert_eq!(a.len(), 64, "32 bytes as hex");
        assert_ne!(a, b);
    }

    #[test]
    fn values_round_trip() {
        let session = session();

        session.put("cart", vec![1, 2, 3]).expect("attached");
        session.put("name", "Ada").expect("attached");

        assert_eq!(
            session.get::<Vec<i32>>("cart").unwrap(),
            Some(vec![1, 2, 3])
        );
        assert_eq!(session.get::<String>("name").unwrap(), Some("Ada".into()));
        assert_eq!(session.get::<String>("absent").unwrap(), None);
    }

    #[test]
    fn a_detached_session_reports_the_missing_middleware() {
        let session = Session::detached();

        assert!(!session.is_active());
        assert_eq!(session.get::<String>("name").unwrap(), None);

        let err = session.put("name", "Ada").unwrap_err();
        let message = format!("{err}");

        assert!(message.contains("no session is active"), "{message}");
        assert!(message.contains("Auth::session()"), "{message}");
    }

    #[test]
    fn forget_and_flush() {
        let session = session();
        session.put("a", 1).unwrap();
        session.put("b", 2).unwrap();

        session.forget("a").unwrap();
        assert!(!session.has("a"));
        assert!(session.has("b"));

        session.flush().unwrap();
        assert!(!session.has("b"));
    }

    #[test]
    fn login_rotates_the_id_to_defeat_fixation() {
        let session = session();
        let before = session.id();

        session.login(&Identity::new("42")).expect("attached");

        assert_ne!(session.id(), before, "the id must change on login");
        assert_eq!(session.subject(), Some("42".to_owned()));
    }

    #[test]
    fn logout_destroys_the_session() {
        let session = session();
        session.login(&Identity::new("42")).unwrap();

        session.logout().unwrap();

        let (_, data, _, destroyed) = session.snapshot();
        assert!(destroyed);
        assert!(data.is_empty());
        assert_eq!(session.subject(), None);
    }

    #[test]
    fn a_clone_shares_state_with_the_original() {
        // This is what lets middleware read back what the action did.
        let session = session();
        let handle = session.clone();

        session.put("touched", true).unwrap();

        assert_eq!(handle.get::<bool>("touched").unwrap(), Some(true));
        assert!(handle.snapshot().2, "dirty must be visible to the holder");
    }

    #[tokio::test]
    async fn the_memory_store_round_trips() {
        let store = MemoryStore::new();
        let mut data = SessionData::new();
        data.insert("name".into(), Value::String("Ada".into()));

        store
            .save("abc", data.clone(), Duration::from_secs(60))
            .await
            .unwrap();

        assert_eq!(store.load("abc").await.unwrap(), Some(data));
        assert_eq!(store.load("nope").await.unwrap(), None);

        store.destroy("abc").await.unwrap();
        assert_eq!(store.load("abc").await.unwrap(), None);
    }

    #[tokio::test]
    async fn expired_entries_read_as_absent_and_are_dropped() {
        let store = MemoryStore::new();

        store
            .save("abc", SessionData::new(), Duration::from_secs(0))
            .await
            .unwrap();

        assert_eq!(store.load("abc").await.unwrap(), None);
        assert!(
            store.is_empty(),
            "the expired entry was removed, not merely hidden"
        );
    }
}