wae_authentication/jwt/
config.rs1use std::time::Duration;
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub enum JwtAlgorithm {
8 HS256,
10 HS384,
12 HS512,
14 RS256,
16 RS384,
18 RS512,
20 ES256,
22 ES384,
24}
25
26impl Default for JwtAlgorithm {
27 fn default() -> Self {
28 Self::HS256
29 }
30}
31
32#[derive(Debug, Clone)]
34pub struct JwtConfig {
35 pub secret: String,
37
38 pub algorithm: JwtAlgorithm,
40
41 pub issuer: Option<String>,
43
44 pub audience: Option<String>,
46
47 pub access_token_ttl: Duration,
49
50 pub refresh_token_ttl: Duration,
52
53 pub validate_issuer: bool,
55
56 pub validate_audience: bool,
58
59 pub leeway_seconds: i64,
61}
62
63impl JwtConfig {
64 pub fn new(secret: impl Into<String>) -> Self {
69 Self {
70 secret: secret.into(),
71 algorithm: JwtAlgorithm::HS256,
72 issuer: None,
73 audience: None,
74 access_token_ttl: Duration::from_secs(3600),
75 refresh_token_ttl: Duration::from_secs(86400 * 7),
76 validate_issuer: false,
77 validate_audience: false,
78 leeway_seconds: 60,
79 }
80 }
81
82 pub fn with_algorithm(mut self, algorithm: JwtAlgorithm) -> Self {
84 self.algorithm = algorithm;
85 self
86 }
87
88 pub fn with_issuer(mut self, issuer: impl Into<String>) -> Self {
90 self.issuer = Some(issuer.into());
91 self.validate_issuer = true;
92 self
93 }
94
95 pub fn with_audience(mut self, audience: impl Into<String>) -> Self {
97 self.audience = Some(audience.into());
98 self.validate_audience = true;
99 self
100 }
101
102 pub fn with_access_token_ttl(mut self, ttl: Duration) -> Self {
104 self.access_token_ttl = ttl;
105 self
106 }
107
108 pub fn with_refresh_token_ttl(mut self, ttl: Duration) -> Self {
110 self.refresh_token_ttl = ttl;
111 self
112 }
113
114 pub fn with_leeway(mut self, seconds: i64) -> Self {
116 self.leeway_seconds = seconds;
117 self
118 }
119
120 pub fn access_token_expires_in(&self) -> i64 {
122 self.access_token_ttl.as_secs() as i64
123 }
124
125 pub fn refresh_token_expires_in(&self) -> i64 {
127 self.refresh_token_ttl.as_secs() as i64
128 }
129}
130
131impl Default for JwtConfig {
132 fn default() -> Self {
133 Self::new("default-secret-key-please-change-in-production")
134 }
135}
136
137#[derive(Debug, Clone)]
139pub struct JwtValidation {
140 pub validate_signature: bool,
142
143 pub validate_exp: bool,
145
146 pub validate_nbf: bool,
148
149 pub validate_iss: bool,
151
152 pub validate_aud: bool,
154
155 pub leeway: i64,
157}
158
159impl Default for JwtValidation {
160 fn default() -> Self {
161 Self {
162 validate_signature: true,
163 validate_exp: true,
164 validate_nbf: true,
165 validate_iss: false,
166 validate_aud: false,
167 leeway: 60,
168 }
169 }
170}
171
172impl JwtValidation {
173 pub fn new() -> Self {
175 Self::default()
176 }
177
178 pub fn with_issuer_validation(mut self, validate: bool) -> Self {
180 self.validate_iss = validate;
181 self
182 }
183
184 pub fn with_audience_validation(mut self, validate: bool) -> Self {
186 self.validate_aud = validate;
187 self
188 }
189
190 pub fn with_leeway(mut self, seconds: i64) -> Self {
192 self.leeway = seconds;
193 self
194 }
195}