1use std::time::Duration;
6use std::sync::Arc;
7use serde::{Deserialize, Serialize};
8use sa_token_adapter::storage::SaStorage;
9use crate::event::SaTokenListener;
10
11#[derive(Debug, Clone, Serialize, Deserialize)]
13pub struct SaTokenConfig {
14 pub token_name: String,
16
17 pub timeout: i64,
19
20 pub active_timeout: i64,
24
25 pub auto_renew: bool,
36
37 pub is_concurrent: bool,
39
40 pub is_share: bool,
42
43 pub token_style: TokenStyle,
45
46 pub is_log: bool,
48
49 pub is_read_cookie: bool,
51
52 pub is_read_header: bool,
54
55 pub is_read_body: bool,
57
58 pub token_prefix: Option<String>,
60
61 pub jwt_secret_key: Option<String>,
63
64 pub jwt_algorithm: Option<String>,
66
67 pub jwt_issuer: Option<String>,
69
70 pub jwt_audience: Option<String>,
72
73 pub enable_nonce: bool,
75
76 pub nonce_timeout: i64,
78
79 pub enable_refresh_token: bool,
81
82 pub refresh_token_timeout: i64,
84}
85
86impl Default for SaTokenConfig {
87 fn default() -> Self {
88 Self {
89 token_name: "sa-token".to_string(),
90 timeout: 2592000, active_timeout: -1,
92 auto_renew: false, is_concurrent: true,
94 is_share: true,
95 token_style: TokenStyle::Uuid,
96 is_log: false,
97 is_read_cookie: true,
98 is_read_header: true,
99 is_read_body: false,
100 token_prefix: None,
101 jwt_secret_key: None,
102 jwt_algorithm: Some("HS256".to_string()),
103 jwt_issuer: None,
104 jwt_audience: None,
105 enable_nonce: false,
106 nonce_timeout: -1,
107 enable_refresh_token: false,
108 refresh_token_timeout: 604800, }
110 }
111}
112
113impl SaTokenConfig {
114 pub fn builder() -> SaTokenConfigBuilder {
115 SaTokenConfigBuilder::default()
116 }
117
118 pub fn timeout_duration(&self) -> Option<Duration> {
119 if self.timeout < 0 {
120 None
121 } else {
122 Some(Duration::from_secs(self.timeout as u64))
123 }
124 }
125}
126
127#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
129pub enum TokenStyle {
130 Uuid,
132 SimpleUuid,
134 Random32,
136 Random64,
138 Random128,
140 Jwt,
142 Hash,
144 Timestamp,
146 Tik,
148}
149
150#[derive(Default)]
152pub struct SaTokenConfigBuilder {
153 config: SaTokenConfig,
154 storage: Option<Arc<dyn SaStorage>>,
155 listeners: Vec<Arc<dyn SaTokenListener>>,
156}
157
158
159impl SaTokenConfigBuilder {
160 pub fn token_name(mut self, name: impl Into<String>) -> Self {
161 self.config.token_name = name.into();
162 self
163 }
164
165 pub fn timeout(mut self, timeout: i64) -> Self {
166 self.config.timeout = timeout;
167 self
168 }
169
170 pub fn active_timeout(mut self, timeout: i64) -> Self {
171 self.config.active_timeout = timeout;
172 self
173 }
174
175 pub fn auto_renew(mut self, enabled: bool) -> Self {
177 self.config.auto_renew = enabled;
178 self
179 }
180
181 pub fn is_concurrent(mut self, concurrent: bool) -> Self {
182 self.config.is_concurrent = concurrent;
183 self
184 }
185
186 pub fn is_share(mut self, share: bool) -> Self {
187 self.config.is_share = share;
188 self
189 }
190
191 pub fn token_style(mut self, style: TokenStyle) -> Self {
192 self.config.token_style = style;
193 self
194 }
195
196 pub fn token_prefix(mut self, prefix: impl Into<String>) -> Self {
197 self.config.token_prefix = Some(prefix.into());
198 self
199 }
200
201 pub fn jwt_secret_key(mut self, key: impl Into<String>) -> Self {
202 self.config.jwt_secret_key = Some(key.into());
203 self
204 }
205
206 pub fn jwt_algorithm(mut self, algorithm: impl Into<String>) -> Self {
208 self.config.jwt_algorithm = Some(algorithm.into());
209 self
210 }
211
212 pub fn jwt_issuer(mut self, issuer: impl Into<String>) -> Self {
214 self.config.jwt_issuer = Some(issuer.into());
215 self
216 }
217
218 pub fn jwt_audience(mut self, audience: impl Into<String>) -> Self {
220 self.config.jwt_audience = Some(audience.into());
221 self
222 }
223
224 pub fn enable_nonce(mut self, enable: bool) -> Self {
226 self.config.enable_nonce = enable;
227 self
228 }
229
230 pub fn nonce_timeout(mut self, timeout: i64) -> Self {
232 self.config.nonce_timeout = timeout;
233 self
234 }
235
236 pub fn enable_refresh_token(mut self, enable: bool) -> Self {
238 self.config.enable_refresh_token = enable;
239 self
240 }
241
242 pub fn refresh_token_timeout(mut self, timeout: i64) -> Self {
244 self.config.refresh_token_timeout = timeout;
245 self
246 }
247
248 pub fn storage(mut self, storage: Arc<dyn SaStorage>) -> Self {
250 self.storage = Some(storage);
251 self
252 }
253
254 pub fn register_listener(mut self, listener: Arc<dyn SaTokenListener>) -> Self {
272 self.listeners.push(listener);
273 self
274 }
275
276 pub fn build(self) -> crate::SaTokenManager {
306 let storage = self.storage.expect("Storage must be set before building SaTokenManager. Use .storage() method.");
307 let manager = crate::SaTokenManager::new(storage, self.config);
308
309 if !self.listeners.is_empty() {
312 let event_bus = manager.event_bus();
313 for listener in self.listeners {
314 event_bus.register(listener);
315 }
316 }
317
318 crate::StpUtil::init_manager(manager.clone());
321
322 manager
323 }
324
325 pub fn build_config(self) -> SaTokenConfig {
327 self.config
328 }
329}