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
150pub struct SaTokenConfigBuilder {
152 config: SaTokenConfig,
153 storage: Option<Arc<dyn SaStorage>>,
154 listeners: Vec<Arc<dyn SaTokenListener>>,
155}
156
157impl Default for SaTokenConfigBuilder {
158 fn default() -> Self {
159 Self {
160 config: SaTokenConfig::default(),
161 storage: None,
162 listeners: Vec::new(),
163 }
164 }
165}
166
167impl SaTokenConfigBuilder {
168 pub fn token_name(mut self, name: impl Into<String>) -> Self {
169 self.config.token_name = name.into();
170 self
171 }
172
173 pub fn timeout(mut self, timeout: i64) -> Self {
174 self.config.timeout = timeout;
175 self
176 }
177
178 pub fn active_timeout(mut self, timeout: i64) -> Self {
179 self.config.active_timeout = timeout;
180 self
181 }
182
183 pub fn auto_renew(mut self, enabled: bool) -> Self {
185 self.config.auto_renew = enabled;
186 self
187 }
188
189 pub fn is_concurrent(mut self, concurrent: bool) -> Self {
190 self.config.is_concurrent = concurrent;
191 self
192 }
193
194 pub fn is_share(mut self, share: bool) -> Self {
195 self.config.is_share = share;
196 self
197 }
198
199 pub fn token_style(mut self, style: TokenStyle) -> Self {
200 self.config.token_style = style;
201 self
202 }
203
204 pub fn token_prefix(mut self, prefix: impl Into<String>) -> Self {
205 self.config.token_prefix = Some(prefix.into());
206 self
207 }
208
209 pub fn jwt_secret_key(mut self, key: impl Into<String>) -> Self {
210 self.config.jwt_secret_key = Some(key.into());
211 self
212 }
213
214 pub fn jwt_algorithm(mut self, algorithm: impl Into<String>) -> Self {
216 self.config.jwt_algorithm = Some(algorithm.into());
217 self
218 }
219
220 pub fn jwt_issuer(mut self, issuer: impl Into<String>) -> Self {
222 self.config.jwt_issuer = Some(issuer.into());
223 self
224 }
225
226 pub fn jwt_audience(mut self, audience: impl Into<String>) -> Self {
228 self.config.jwt_audience = Some(audience.into());
229 self
230 }
231
232 pub fn enable_nonce(mut self, enable: bool) -> Self {
234 self.config.enable_nonce = enable;
235 self
236 }
237
238 pub fn nonce_timeout(mut self, timeout: i64) -> Self {
240 self.config.nonce_timeout = timeout;
241 self
242 }
243
244 pub fn enable_refresh_token(mut self, enable: bool) -> Self {
246 self.config.enable_refresh_token = enable;
247 self
248 }
249
250 pub fn refresh_token_timeout(mut self, timeout: i64) -> Self {
252 self.config.refresh_token_timeout = timeout;
253 self
254 }
255
256 pub fn storage(mut self, storage: Arc<dyn SaStorage>) -> Self {
258 self.storage = Some(storage);
259 self
260 }
261
262 pub fn register_listener(mut self, listener: Arc<dyn SaTokenListener>) -> Self {
280 self.listeners.push(listener);
281 self
282 }
283
284 pub fn build(self) -> crate::SaTokenManager {
314 let storage = self.storage.expect("Storage must be set before building SaTokenManager. Use .storage() method.");
315 let manager = crate::SaTokenManager::new(storage, self.config);
316
317 if !self.listeners.is_empty() {
320 let event_bus = manager.event_bus();
321 for listener in self.listeners {
322 event_bus.register(listener);
323 }
324 }
325
326 crate::StpUtil::init_manager(manager.clone());
329
330 manager
331 }
332
333 pub fn build_config(self) -> SaTokenConfig {
335 self.config
336 }
337}