actix_session/storage/
interface.rs

1use std::future::Future;
2
3use actix_web::cookie::time::Duration;
4use derive_more::derive::Display;
5use serde_json::{Map, Value};
6
7use super::SessionKey;
8
9/// Convenience type for the map structure backing session state.
10pub type SessionState = Map<String, Value>;
11
12/// The interface to retrieve and save the current session data from/to the chosen storage backend.
13///
14/// You can provide your own custom session store backend by implementing this trait.
15pub trait SessionStore {
16    /// Loads the session state associated to a session key.
17    fn load(
18        &self,
19        session_key: &SessionKey,
20    ) -> impl Future<Output = Result<Option<SessionState>, LoadError>>;
21
22    /// Persist the session state for a newly created session.
23    ///
24    /// Returns the corresponding session key.
25    fn save(
26        &self,
27        session_state: SessionState,
28        ttl: &Duration,
29    ) -> impl Future<Output = Result<SessionKey, SaveError>>;
30
31    /// Updates the session state associated to a pre-existing session key.
32    fn update(
33        &self,
34        session_key: SessionKey,
35        session_state: SessionState,
36        ttl: &Duration,
37    ) -> impl Future<Output = Result<SessionKey, UpdateError>>;
38
39    /// Updates the TTL of the session state associated to a pre-existing session key.
40    fn update_ttl(
41        &self,
42        session_key: &SessionKey,
43        ttl: &Duration,
44    ) -> impl Future<Output = Result<(), anyhow::Error>>;
45
46    /// Deletes a session from the store.
47    fn delete(&self, session_key: &SessionKey) -> impl Future<Output = Result<(), anyhow::Error>>;
48}
49
50// We cannot derive the `Error` implementation using `derive_more` for our custom errors:
51// `derive_more`'s `#[error(source)]` attribute requires the source implement the `Error` trait,
52// while it's actually enough for it to be able to produce a reference to a dyn Error.
53
54/// Possible failures modes for [`SessionStore::load`].
55#[derive(Debug, Display)]
56pub enum LoadError {
57    /// Failed to deserialize session state.
58    #[display("Failed to deserialize session state")]
59    Deserialization(anyhow::Error),
60
61    /// Something went wrong when retrieving the session state.
62    #[display("Something went wrong when retrieving the session state")]
63    Other(anyhow::Error),
64}
65
66impl std::error::Error for LoadError {
67    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
68        match self {
69            Self::Deserialization(err) => Some(err.as_ref()),
70            Self::Other(err) => Some(err.as_ref()),
71        }
72    }
73}
74
75/// Possible failures modes for [`SessionStore::save`].
76#[derive(Debug, Display)]
77pub enum SaveError {
78    /// Failed to serialize session state.
79    #[display("Failed to serialize session state")]
80    Serialization(anyhow::Error),
81
82    /// Something went wrong when persisting the session state.
83    #[display("Something went wrong when persisting the session state")]
84    Other(anyhow::Error),
85}
86
87impl std::error::Error for SaveError {
88    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
89        match self {
90            Self::Serialization(err) => Some(err.as_ref()),
91            Self::Other(err) => Some(err.as_ref()),
92        }
93    }
94}
95
96#[derive(Debug, Display)]
97/// Possible failures modes for [`SessionStore::update`].
98pub enum UpdateError {
99    /// Failed to serialize session state.
100    #[display("Failed to serialize session state")]
101    Serialization(anyhow::Error),
102
103    /// Something went wrong when updating the session state.
104    #[display("Something went wrong when updating the session state.")]
105    Other(anyhow::Error),
106}
107
108impl std::error::Error for UpdateError {
109    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
110        match self {
111            Self::Serialization(err) => Some(err.as_ref()),
112            Self::Other(err) => Some(err.as_ref()),
113        }
114    }
115}