1use std::time::Duration;
6use std::sync::Arc;
7use serde::{Deserialize, Serialize};
8use sa_token_adapter::storage::SaStorage;
9
10#[derive(Debug, Clone, Serialize, Deserialize)]
12pub struct SaTokenConfig {
13 pub token_name: String,
15
16 pub timeout: i64,
18
19 pub active_timeout: i64,
23
24 pub auto_renew: bool,
35
36 pub is_concurrent: bool,
38
39 pub is_share: bool,
41
42 pub token_style: TokenStyle,
44
45 pub is_log: bool,
47
48 pub is_read_cookie: bool,
50
51 pub is_read_header: bool,
53
54 pub is_read_body: bool,
56
57 pub token_prefix: Option<String>,
59
60 pub jwt_secret_key: Option<String>,
62
63 pub jwt_algorithm: Option<String>,
65
66 pub jwt_issuer: Option<String>,
68
69 pub jwt_audience: Option<String>,
71
72 pub enable_nonce: bool,
74
75 pub nonce_timeout: i64,
77
78 pub enable_refresh_token: bool,
80
81 pub refresh_token_timeout: i64,
83}
84
85impl Default for SaTokenConfig {
86 fn default() -> Self {
87 Self {
88 token_name: "sa-token".to_string(),
89 timeout: 2592000, active_timeout: -1,
91 auto_renew: false, is_concurrent: true,
93 is_share: true,
94 token_style: TokenStyle::Uuid,
95 is_log: false,
96 is_read_cookie: true,
97 is_read_header: true,
98 is_read_body: false,
99 token_prefix: None,
100 jwt_secret_key: None,
101 jwt_algorithm: Some("HS256".to_string()),
102 jwt_issuer: None,
103 jwt_audience: None,
104 enable_nonce: false,
105 nonce_timeout: -1,
106 enable_refresh_token: false,
107 refresh_token_timeout: 604800, }
109 }
110}
111
112impl SaTokenConfig {
113 pub fn builder() -> SaTokenConfigBuilder {
114 SaTokenConfigBuilder::default()
115 }
116
117 pub fn timeout_duration(&self) -> Option<Duration> {
118 if self.timeout < 0 {
119 None
120 } else {
121 Some(Duration::from_secs(self.timeout as u64))
122 }
123 }
124}
125
126#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
128pub enum TokenStyle {
129 Uuid,
131 SimpleUuid,
133 Random32,
135 Random64,
137 Random128,
139 Jwt,
141 Hash,
143 Timestamp,
145 Tik,
147}
148
149pub struct SaTokenConfigBuilder {
151 config: SaTokenConfig,
152 storage: Option<Arc<dyn SaStorage>>,
153}
154
155impl Default for SaTokenConfigBuilder {
156 fn default() -> Self {
157 Self {
158 config: SaTokenConfig::default(),
159 storage: None,
160 }
161 }
162}
163
164impl SaTokenConfigBuilder {
165 pub fn token_name(mut self, name: impl Into<String>) -> Self {
166 self.config.token_name = name.into();
167 self
168 }
169
170 pub fn timeout(mut self, timeout: i64) -> Self {
171 self.config.timeout = timeout;
172 self
173 }
174
175 pub fn active_timeout(mut self, timeout: i64) -> Self {
176 self.config.active_timeout = timeout;
177 self
178 }
179
180 pub fn auto_renew(mut self, enabled: bool) -> Self {
182 self.config.auto_renew = enabled;
183 self
184 }
185
186 pub fn is_concurrent(mut self, concurrent: bool) -> Self {
187 self.config.is_concurrent = concurrent;
188 self
189 }
190
191 pub fn is_share(mut self, share: bool) -> Self {
192 self.config.is_share = share;
193 self
194 }
195
196 pub fn token_style(mut self, style: TokenStyle) -> Self {
197 self.config.token_style = style;
198 self
199 }
200
201 pub fn token_prefix(mut self, prefix: impl Into<String>) -> Self {
202 self.config.token_prefix = Some(prefix.into());
203 self
204 }
205
206 pub fn jwt_secret_key(mut self, key: impl Into<String>) -> Self {
207 self.config.jwt_secret_key = Some(key.into());
208 self
209 }
210
211 pub fn jwt_algorithm(mut self, algorithm: impl Into<String>) -> Self {
213 self.config.jwt_algorithm = Some(algorithm.into());
214 self
215 }
216
217 pub fn jwt_issuer(mut self, issuer: impl Into<String>) -> Self {
219 self.config.jwt_issuer = Some(issuer.into());
220 self
221 }
222
223 pub fn jwt_audience(mut self, audience: impl Into<String>) -> Self {
225 self.config.jwt_audience = Some(audience.into());
226 self
227 }
228
229 pub fn enable_nonce(mut self, enable: bool) -> Self {
231 self.config.enable_nonce = enable;
232 self
233 }
234
235 pub fn nonce_timeout(mut self, timeout: i64) -> Self {
237 self.config.nonce_timeout = timeout;
238 self
239 }
240
241 pub fn enable_refresh_token(mut self, enable: bool) -> Self {
243 self.config.enable_refresh_token = enable;
244 self
245 }
246
247 pub fn refresh_token_timeout(mut self, timeout: i64) -> Self {
249 self.config.refresh_token_timeout = timeout;
250 self
251 }
252
253 pub fn storage(mut self, storage: Arc<dyn SaStorage>) -> Self {
255 self.storage = Some(storage);
256 self
257 }
258
259 pub fn build(self) -> crate::SaTokenManager {
276 let storage = self.storage.expect("Storage must be set before building SaTokenManager. Use .storage() method.");
277 crate::SaTokenManager::new(storage, self.config)
278 }
279
280 pub fn build_config(self) -> SaTokenConfig {
282 self.config
283 }
284}