actix_extended_session/storage/
interface.rs

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