author_web/session/
mod.rs1use cookie::{Key, SameSite};
2use std::str::FromStr;
3use std::sync::Arc;
4use thiserror::Error;
5
6pub mod store;
7
8#[derive(Debug, Error)]
9pub enum SessionError {
10 #[error("Session with given ID not found")]
11 SessionNotFound,
12 #[error("Unexpected session error: {0}")]
13 UnexpectedError(#[from] anyhow::Error),
14}
15
16#[derive(Clone)]
17pub struct SessionConfig {
18 pub cookie_name: Arc<str>,
19 pub key: Key,
20 pub same_site: SameSite,
21}
22
23impl SessionConfig {
24 pub fn new(cookie_name: impl AsRef<str>, key: Key, same_site: SameSite) -> Self {
25 SessionConfig {
26 cookie_name: cookie_name.as_ref().into(),
27 key,
28 same_site,
29 }
30 }
31}
32
33impl Default for SessionConfig {
34 fn default() -> Self {
35 SessionConfig {
36 cookie_name: "author_session_cookie".into(),
37 key: Key::generate(),
38 same_site: SameSite::Strict,
39 }
40 }
41}
42
43pub trait SessionKey: FromStr {
44 fn generate() -> Self;
45}
46
47pub trait SessionSubject<Subject> {
48 fn subject() -> Subject;
49}