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 pub storage_key_prefix: String,
89}
90
91impl Default for SaTokenConfig {
92 fn default() -> Self {
93 Self {
94 token_name: "sa-token".to_string(),
95 timeout: 2592000, active_timeout: -1,
97 auto_renew: false, is_concurrent: true,
99 is_share: true,
100 token_style: TokenStyle::Uuid,
101 is_log: false,
102 is_read_cookie: true,
103 is_read_header: true,
104 is_read_body: false,
105 token_prefix: None,
106 jwt_secret_key: None,
107 jwt_algorithm: Some("HS256".to_string()),
108 jwt_issuer: None,
109 jwt_audience: None,
110 enable_nonce: false,
111 nonce_timeout: -1,
112 enable_refresh_token: false,
113 refresh_token_timeout: 604800, storage_key_prefix: "sa:".to_string(),
115 }
116 }
117}
118
119impl SaTokenConfig {
120 pub fn builder() -> SaTokenConfigBuilder {
121 SaTokenConfigBuilder::default()
122 }
123
124 pub fn timeout_duration(&self) -> Option<Duration> {
125 if self.timeout < 0 {
126 None
127 } else {
128 Some(Duration::from_secs(self.timeout as u64))
129 }
130 }
131
132 pub fn make_key(&self, suffix: &str, id: &str) -> String {
135 format!("{}{}{}", self.storage_key_prefix, suffix, id)
136 }
137
138 pub fn key_prefix(&self) -> &str {
140 &self.storage_key_prefix
141 }
142}
143
144#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
146pub enum TokenStyle {
147 Uuid,
149 SimpleUuid,
151 Random32,
153 Random64,
155 Random128,
157 Jwt,
159 Hash,
161 Timestamp,
163 Tik,
165}
166
167#[derive(Default)]
169pub struct SaTokenConfigBuilder {
170 config: SaTokenConfig,
171 storage: Option<Arc<dyn SaStorage>>,
172 listeners: Vec<Arc<dyn SaTokenListener>>,
173}
174
175
176impl SaTokenConfigBuilder {
177 pub fn token_name(mut self, name: impl Into<String>) -> Self {
178 self.config.token_name = name.into();
179 self
180 }
181
182 pub fn timeout(mut self, timeout: i64) -> Self {
183 self.config.timeout = timeout;
184 self
185 }
186
187 pub fn active_timeout(mut self, timeout: i64) -> Self {
188 self.config.active_timeout = timeout;
189 self
190 }
191
192 pub fn auto_renew(mut self, enabled: bool) -> Self {
194 self.config.auto_renew = enabled;
195 self
196 }
197
198 pub fn is_concurrent(mut self, concurrent: bool) -> Self {
199 self.config.is_concurrent = concurrent;
200 self
201 }
202
203 pub fn is_share(mut self, share: bool) -> Self {
204 self.config.is_share = share;
205 self
206 }
207
208 pub fn token_style(mut self, style: TokenStyle) -> Self {
209 self.config.token_style = style;
210 self
211 }
212
213 pub fn token_prefix(mut self, prefix: impl Into<String>) -> Self {
214 self.config.token_prefix = Some(prefix.into());
215 self
216 }
217
218 pub fn storage_key_prefix(mut self, prefix: impl Into<String>) -> Self {
223 self.config.storage_key_prefix = prefix.into();
224 self
225 }
226
227 pub fn jwt_secret_key(mut self, key: impl Into<String>) -> Self {
228 self.config.jwt_secret_key = Some(key.into());
229 self
230 }
231
232 pub fn jwt_algorithm(mut self, algorithm: impl Into<String>) -> Self {
234 self.config.jwt_algorithm = Some(algorithm.into());
235 self
236 }
237
238 pub fn jwt_issuer(mut self, issuer: impl Into<String>) -> Self {
240 self.config.jwt_issuer = Some(issuer.into());
241 self
242 }
243
244 pub fn jwt_audience(mut self, audience: impl Into<String>) -> Self {
246 self.config.jwt_audience = Some(audience.into());
247 self
248 }
249
250 pub fn enable_nonce(mut self, enable: bool) -> Self {
252 self.config.enable_nonce = enable;
253 self
254 }
255
256 pub fn nonce_timeout(mut self, timeout: i64) -> Self {
258 self.config.nonce_timeout = timeout;
259 self
260 }
261
262 pub fn enable_refresh_token(mut self, enable: bool) -> Self {
264 self.config.enable_refresh_token = enable;
265 self
266 }
267
268 pub fn refresh_token_timeout(mut self, timeout: i64) -> Self {
270 self.config.refresh_token_timeout = timeout;
271 self
272 }
273
274 pub fn storage(mut self, storage: Arc<dyn SaStorage>) -> Self {
276 self.storage = Some(storage);
277 self
278 }
279
280 pub fn register_listener(mut self, listener: Arc<dyn SaTokenListener>) -> Self {
298 self.listeners.push(listener);
299 self
300 }
301
302 pub fn build(self) -> crate::SaTokenManager {
332 let storage = self.storage.expect("Storage must be set before building SaTokenManager. Use .storage() method.");
333 let manager = crate::SaTokenManager::new(storage, self.config);
334
335 if !self.listeners.is_empty() {
338 let event_bus = manager.event_bus();
339 for listener in self.listeners {
340 event_bus.register(listener);
341 }
342 }
343
344 crate::StpUtil::init_manager(manager.clone());
347
348 manager
349 }
350
351 pub fn build_config(self) -> SaTokenConfig {
353 self.config
354 }
355}