actix_identity/
config.rs

1//! Configuration options to tune the behavior of [`IdentityMiddleware`].
2
3use std::time::Duration;
4
5use crate::IdentityMiddleware;
6
7#[derive(Debug, Clone)]
8pub(crate) struct Configuration {
9    pub(crate) on_logout: LogoutBehavior,
10    pub(crate) login_deadline: Option<Duration>,
11    pub(crate) visit_deadline: Option<Duration>,
12    pub(crate) id_key: &'static str,
13    pub(crate) last_visit_unix_timestamp_key: &'static str,
14    pub(crate) login_unix_timestamp_key: &'static str,
15}
16
17impl Default for Configuration {
18    fn default() -> Self {
19        Self {
20            on_logout: LogoutBehavior::PurgeSession,
21            login_deadline: None,
22            visit_deadline: None,
23            id_key: "actix_identity.user_id",
24            last_visit_unix_timestamp_key: "actix_identity.last_visited_at",
25            login_unix_timestamp_key: "actix_identity.logged_in_at",
26        }
27    }
28}
29
30/// Controls what actions are going to be performed when [`Identity::logout`] is invoked.
31///
32/// [`Identity::logout`]: crate::Identity::logout
33#[derive(Debug, Clone)]
34#[non_exhaustive]
35pub enum LogoutBehavior {
36    /// When [`Identity::logout`](crate::Identity::logout) is called, purge the current session.
37    ///
38    /// This behavior might be desirable when you have stored additional information in the session
39    /// state that are tied to the user's identity and should not be retained after logout.
40    PurgeSession,
41
42    /// When [`Identity::logout`](crate::Identity::logout) is called, remove the identity
43    /// information from the current session state. The session itself is not destroyed.
44    ///
45    /// This behavior might be desirable when you have stored information in the session state that
46    /// is not tied to the user's identity and should be retained after logout.
47    DeleteIdentityKeys,
48}
49
50/// A fluent builder to construct an [`IdentityMiddleware`] instance with custom configuration
51/// parameters.
52///
53/// Use [`IdentityMiddleware::builder`] to get started!
54#[derive(Debug, Clone)]
55pub struct IdentityMiddlewareBuilder {
56    configuration: Configuration,
57}
58
59impl IdentityMiddlewareBuilder {
60    pub(crate) fn new() -> Self {
61        Self {
62            configuration: Configuration::default(),
63        }
64    }
65
66    /// Set a custom key to identify the user in the session.
67    pub fn id_key(mut self, key: &'static str) -> Self {
68        self.configuration.id_key = key;
69        self
70    }
71
72    /// Set a custom key to store the last visited unix timestamp.
73    pub fn last_visit_unix_timestamp_key(mut self, key: &'static str) -> Self {
74        self.configuration.last_visit_unix_timestamp_key = key;
75        self
76    }
77
78    /// Set a custom key to store the login unix timestamp.
79    pub fn login_unix_timestamp_key(mut self, key: &'static str) -> Self {
80        self.configuration.login_unix_timestamp_key = key;
81        self
82    }
83
84    /// Determines how [`Identity::logout`](crate::Identity::logout) affects the current session.
85    ///
86    /// By default, the current session is purged ([`LogoutBehavior::PurgeSession`]).
87    pub fn logout_behavior(mut self, logout_behavior: LogoutBehavior) -> Self {
88        self.configuration.on_logout = logout_behavior;
89        self
90    }
91
92    /// Automatically logs out users after a certain amount of time has passed since they logged in,
93    /// regardless of their activity pattern.
94    ///
95    /// If set to:
96    /// - `None`: login deadline is disabled.
97    /// - `Some(duration)`: login deadline is enabled and users will be logged out after `duration`
98    ///   has passed since their login.
99    ///
100    /// By default, login deadline is disabled.
101    pub fn login_deadline(mut self, deadline: Option<Duration>) -> Self {
102        self.configuration.login_deadline = deadline;
103        self
104    }
105
106    /// Automatically logs out users after a certain amount of time has passed since their last
107    /// visit.
108    ///
109    /// If set to:
110    /// - `None`: visit deadline is disabled.
111    /// - `Some(duration)`: visit deadline is enabled and users will be logged out after `duration`
112    ///   has passed since their last visit.
113    ///
114    /// By default, visit deadline is disabled.
115    pub fn visit_deadline(mut self, deadline: Option<Duration>) -> Self {
116        self.configuration.visit_deadline = deadline;
117        self
118    }
119
120    /// Finalises the builder and returns an [`IdentityMiddleware`] instance.
121    pub fn build(self) -> IdentityMiddleware {
122        IdentityMiddleware::new(self.configuration)
123    }
124}