pavex_session/
incoming.rs

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
use crate::{config::SessionCookieConfig, wire::WireClientState, SessionId};
use pavex::cookie::RequestCookies;
use pavex_tracing::fields::{error_details, error_message, ERROR_DETAILS, ERROR_MESSAGE};
use serde_json::Value;
use std::collections::HashMap;

/// The session information attached to the incoming request.
///
/// Built using [`IncomingSession::extract`].
pub struct IncomingSession {
    pub(crate) id: SessionId,
    pub(crate) client_state: HashMap<String, Value>,
}

impl IncomingSession {
    /// Extract a session cookie from the incoming request, if it exists.
    ///
    /// If the cookie is not found, or if the cookie is invalid, this method will return `None`.
    pub fn extract(cookies: &RequestCookies<'_>, config: &SessionCookieConfig) -> Option<Self> {
        let cookie = cookies.get(&config.name)?;
        match serde_json::from_str::<WireClientState>(cookie.value()) {
            Ok(s) => Some(Self {
                id: s.session_id,
                client_state: s.user_values.into_owned(),
            }),
            Err(e) => {
                tracing::event!(
                    tracing::Level::WARN,
                    { ERROR_MESSAGE } = error_message(&e),
                    { ERROR_DETAILS } = error_details(&e),
                    "Invalid client state for session, creating a new session."
                );
                None
            }
        }
    }

    /// Build an [`IncomingSession`] instance from its parts.
    pub fn from_parts(id: SessionId, state: HashMap<String, Value>) -> Self {
        Self {
            id,
            client_state: state,
        }
    }
}