Skip to main content

sa_token_plugin_actix_web_core/
state.rs

1//! Application state without actix-web types.
2
3use std::sync::Arc;
4
5use sa_token_adapter::storage::SaStorage;
6use sa_token_core::{config::TokenStyle, SaTokenManager};
7
8/// Shared application state (wrap with `actix_web::web::Data` in the v4 binding).
9#[derive(Clone)]
10pub struct SaTokenState {
11    pub manager: Arc<SaTokenManager>,
12}
13
14impl SaTokenState {
15    pub fn builder() -> SaTokenStateBuilder {
16        SaTokenStateBuilder::default()
17    }
18}
19
20#[derive(Default)]
21pub struct SaTokenStateBuilder {
22    config_builder: sa_token_core::config::SaTokenConfigBuilder,
23}
24
25impl SaTokenStateBuilder {
26    pub fn new() -> Self {
27        Self::default()
28    }
29
30    pub fn storage(mut self, storage: Arc<dyn SaStorage>) -> Self {
31        self.config_builder = self.config_builder.storage(storage);
32        self
33    }
34
35    pub fn token_name(mut self, name: impl Into<String>) -> Self {
36        self.config_builder = self.config_builder.token_name(name);
37        self
38    }
39
40    pub fn timeout(mut self, timeout: i64) -> Self {
41        self.config_builder = self.config_builder.timeout(timeout);
42        self
43    }
44
45    pub fn active_timeout(mut self, timeout: i64) -> Self {
46        self.config_builder = self.config_builder.active_timeout(timeout);
47        self
48    }
49
50    pub fn auto_renew(mut self, enabled: bool) -> Self {
51        self.config_builder = self.config_builder.auto_renew(enabled);
52        self
53    }
54
55    pub fn is_concurrent(mut self, concurrent: bool) -> Self {
56        self.config_builder = self.config_builder.is_concurrent(concurrent);
57        self
58    }
59
60    pub fn is_share(mut self, share: bool) -> Self {
61        self.config_builder = self.config_builder.is_share(share);
62        self
63    }
64
65    pub fn token_style(mut self, style: TokenStyle) -> Self {
66        self.config_builder = self.config_builder.token_style(style);
67        self
68    }
69
70    pub fn token_prefix(mut self, prefix: impl Into<String>) -> Self {
71        self.config_builder = self.config_builder.token_prefix(prefix);
72        self
73    }
74
75    pub fn jwt_secret_key(mut self, key: impl Into<String>) -> Self {
76        self.config_builder = self.config_builder.jwt_secret_key(key);
77        self
78    }
79
80    pub fn build(self) -> SaTokenState {
81        SaTokenState {
82            manager: Arc::new(self.config_builder.build()),
83        }
84    }
85}