sa_token_plugin_ntex/
state.rs1use std::sync::Arc;
2use sa_token_adapter::storage::SaStorage;
3use sa_token_core::{SaTokenManager, SaTokenConfig, StpUtil};
4
5#[derive(Clone)]
10pub struct SaTokenState {
11 pub manager: Arc<SaTokenManager>,
12}
13
14impl SaTokenState {
15 pub fn new(storage: Arc<dyn SaStorage>, config: SaTokenConfig) -> Self {
18 Self {
19 manager: Arc::new(SaTokenManager::new(storage, config)),
20 }
21 }
22
23 pub fn from_manager(manager: SaTokenManager) -> Self {
26 StpUtil::init_manager(manager.clone());
28
29 Self {
30 manager: Arc::new(manager),
31 }
32 }
33
34 pub fn builder() -> SaTokenStateBuilder {
37 SaTokenStateBuilder::default()
38 }
39}
40
41#[derive(Default)]
44pub struct SaTokenStateBuilder {
45 config_builder: sa_token_core::config::SaTokenConfigBuilder,
46}
47
48impl SaTokenStateBuilder {
49 pub fn new() -> Self {
52 Self::default()
53 }
54
55 pub fn token_name(mut self, name: impl Into<String>) -> Self {
58 self.config_builder = self.config_builder.token_name(name);
59 self
60 }
61
62 pub fn timeout(mut self, timeout: i64) -> Self {
65 self.config_builder = self.config_builder.timeout(timeout);
66 self
67 }
68
69 pub fn active_timeout(mut self, timeout: i64) -> Self {
72 self.config_builder = self.config_builder.active_timeout(timeout);
73 self
74 }
75
76 pub fn auto_renew(mut self, enabled: bool) -> Self {
79 self.config_builder = self.config_builder.auto_renew(enabled);
80 self
81 }
82
83 pub fn is_concurrent(mut self, concurrent: bool) -> Self {
86 self.config_builder = self.config_builder.is_concurrent(concurrent);
87 self
88 }
89
90 pub fn is_share(mut self, share: bool) -> Self {
93 self.config_builder = self.config_builder.is_share(share);
94 self
95 }
96
97 pub fn token_style(mut self, style: sa_token_core::config::TokenStyle) -> Self {
100 self.config_builder = self.config_builder.token_style(style);
101 self
102 }
103
104 pub fn token_prefix(mut self, prefix: impl Into<String>) -> Self {
107 self.config_builder = self.config_builder.token_prefix(prefix);
108 self
109 }
110
111 pub fn jwt_secret_key(mut self, key: impl Into<String>) -> Self {
114 self.config_builder = self.config_builder.jwt_secret_key(key);
115 self
116 }
117
118 pub fn storage(mut self, storage: Arc<dyn SaStorage>) -> Self {
121 self.config_builder = self.config_builder.storage(storage);
122 self
123 }
124
125 pub fn build(self) -> SaTokenState {
128 let manager = self.config_builder.build();
131 SaTokenState {
134 manager: Arc::new(manager),
135 }
136 }
137}