Skip to main content

openauth_plugins/one_time_token/
options.rs

1use std::sync::Arc;
2
3use openauth_core::context::AuthContext;
4use openauth_core::db::{Session, User};
5use openauth_core::error::OpenAuthError;
6
7#[derive(Debug, Clone, PartialEq, Eq)]
8pub struct OneTimeTokenSession {
9    pub session: Session,
10    pub user: User,
11}
12
13pub type GenerateToken =
14    Arc<dyn Fn(&OneTimeTokenSession, &AuthContext) -> Result<String, OpenAuthError> + Send + Sync>;
15pub type HashToken = Arc<dyn Fn(&str) -> Result<String, OpenAuthError> + Send + Sync>;
16
17#[derive(Clone)]
18pub enum StoreToken {
19    Plain,
20    Hashed,
21    Custom(HashToken),
22}
23
24impl StoreToken {
25    pub fn custom<F>(hash: F) -> Self
26    where
27        F: Fn(&str) -> Result<String, OpenAuthError> + Send + Sync + 'static,
28    {
29        Self::Custom(Arc::new(hash))
30    }
31}
32
33#[derive(Clone)]
34pub struct OneTimeTokenOptions {
35    pub expires_in: u64,
36    pub disable_client_request: bool,
37    pub generate_token: Option<GenerateToken>,
38    pub disable_set_session_cookie: bool,
39    pub store_token: StoreToken,
40    pub set_ott_header_on_new_session: bool,
41}
42
43impl Default for OneTimeTokenOptions {
44    fn default() -> Self {
45        Self {
46            expires_in: 3,
47            disable_client_request: false,
48            generate_token: None,
49            disable_set_session_cookie: false,
50            store_token: StoreToken::Plain,
51            set_ott_header_on_new_session: false,
52        }
53    }
54}
55
56impl OneTimeTokenOptions {
57    #[must_use]
58    pub fn expires_in_minutes(mut self, minutes: u64) -> Self {
59        self.expires_in = minutes;
60        self
61    }
62
63    #[must_use]
64    pub fn disable_client_request(mut self, disable: bool) -> Self {
65        self.disable_client_request = disable;
66        self
67    }
68
69    #[must_use]
70    pub fn generate_token<F>(mut self, generate: F) -> Self
71    where
72        F: Fn(&OneTimeTokenSession, &AuthContext) -> Result<String, OpenAuthError>
73            + Send
74            + Sync
75            + 'static,
76    {
77        self.generate_token = Some(Arc::new(generate));
78        self
79    }
80
81    #[must_use]
82    pub fn disable_set_session_cookie(mut self, disable: bool) -> Self {
83        self.disable_set_session_cookie = disable;
84        self
85    }
86
87    #[must_use]
88    pub fn store_token(mut self, store_token: StoreToken) -> Self {
89        self.store_token = store_token;
90        self
91    }
92
93    #[must_use]
94    pub fn set_ott_header_on_new_session(mut self, set_header: bool) -> Self {
95        self.set_ott_header_on_new_session = set_header;
96        self
97    }
98}