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
use crate::{HashedPassword, SessionFields, SessionId, User};
macro_rules! future {
(Output = Result<$type:ty, Error>) => {
impl Future<Output = Result<$type, Self::Error>> + Send
}
}
/// The interface for a backend.
pub trait Backend: Send + Sized {
/// The user type.
type User: User;
/// The implementation-defined session data type.
type SessionData: Send;
/// The backend error type.
type Error: std::error::Error + Send;
/// Load the session data.
fn load_session_data(
&self,
id: &SessionId,
) -> future!(Output = Result<Option<SessionFields<Self>>, Error>);
/// Create a new instance of session data.
///
/// This is called when a user does not have an existing session,
/// ie. when they visit the site for the first time.
fn create_session_data(
&self,
) -> future!(Output = Result<Self::SessionData, Error>);
/// Update the session data.
///
/// This is called when the data associated with a session has changed.
fn update_session_data(
&self,
id: &SessionId,
user_id: Option<&<Self::User as User>::Id>,
data: &Self::SessionData,
) -> future!(Output = Result<(), Error>);
/// Load a user by their id.
fn load_user(
&self,
id: &<Self::User as User>::Id,
) -> future!(Output = Result<Option<Self::User>, Error>);
/// Load a user by their email address.
fn load_user_by_email(
&self,
email: &str,
) -> future!(Output = Result<Option<Self::User>, Error>);
/// Update the user password.
fn update_user_password(
&self,
id: &<Self::User as User>::Id,
hashed_password: &HashedPassword,
) -> future!(Output = Result<(), Error>);
}
/// The interface for a backend that stores the session id in a cookie.
pub trait CookieSessionBackend: Backend {
/// Get the name of the session cookie.
fn session_cookie_name(&self) -> &str {
"sessionid"
}
}