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
64impl Default for SaTokenConfig {
65 fn default() -> Self {
66 Self {
67 token_name: "sa-token".to_string(),
68 timeout: 2592000, active_timeout: -1,
70 auto_renew: false, is_concurrent: true,
72 is_share: true,
73 token_style: TokenStyle::Uuid,
74 is_log: false,
75 is_read_cookie: true,
76 is_read_header: true,
77 is_read_body: false,
78 token_prefix: None,
79 jwt_secret_key: None,
80 }
81 }
82}
83
84impl SaTokenConfig {
85 pub fn builder() -> SaTokenConfigBuilder {
86 SaTokenConfigBuilder::default()
87 }
88
89 pub fn timeout_duration(&self) -> Option<Duration> {
90 if self.timeout < 0 {
91 None
92 } else {
93 Some(Duration::from_secs(self.timeout as u64))
94 }
95 }
96}
97
98#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
100pub enum TokenStyle {
101 Uuid,
103 SimpleUuid,
105 Random32,
107 Random64,
109 Random128,
111}
112
113pub struct SaTokenConfigBuilder {
115 config: SaTokenConfig,
116 storage: Option<Arc<dyn SaStorage>>,
117}
118
119impl Default for SaTokenConfigBuilder {
120 fn default() -> Self {
121 Self {
122 config: SaTokenConfig::default(),
123 storage: None,
124 }
125 }
126}
127
128impl SaTokenConfigBuilder {
129 pub fn token_name(mut self, name: impl Into<String>) -> Self {
130 self.config.token_name = name.into();
131 self
132 }
133
134 pub fn timeout(mut self, timeout: i64) -> Self {
135 self.config.timeout = timeout;
136 self
137 }
138
139 pub fn active_timeout(mut self, timeout: i64) -> Self {
140 self.config.active_timeout = timeout;
141 self
142 }
143
144 pub fn auto_renew(mut self, enabled: bool) -> Self {
146 self.config.auto_renew = enabled;
147 self
148 }
149
150 pub fn is_concurrent(mut self, concurrent: bool) -> Self {
151 self.config.is_concurrent = concurrent;
152 self
153 }
154
155 pub fn is_share(mut self, share: bool) -> Self {
156 self.config.is_share = share;
157 self
158 }
159
160 pub fn token_style(mut self, style: TokenStyle) -> Self {
161 self.config.token_style = style;
162 self
163 }
164
165 pub fn token_prefix(mut self, prefix: impl Into<String>) -> Self {
166 self.config.token_prefix = Some(prefix.into());
167 self
168 }
169
170 pub fn jwt_secret_key(mut self, key: impl Into<String>) -> Self {
171 self.config.jwt_secret_key = Some(key.into());
172 self
173 }
174
175 pub fn storage(mut self, storage: Arc<dyn SaStorage>) -> Self {
177 self.storage = Some(storage);
178 self
179 }
180
181 pub fn build(self) -> crate::SaTokenManager {
198 let storage = self.storage.expect("Storage must be set before building SaTokenManager. Use .storage() method.");
199 crate::SaTokenManager::new(storage, self.config)
200 }
201
202 pub fn build_config(self) -> SaTokenConfig {
204 self.config
205 }
206}