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
32impl From<JwtAlgorithm> for jsonwebtoken::Algorithm {
33 fn from(alg: JwtAlgorithm) -> Self {
34 match alg {
35 JwtAlgorithm::HS256 => jsonwebtoken::Algorithm::HS256,
36 JwtAlgorithm::HS384 => jsonwebtoken::Algorithm::HS384,
37 JwtAlgorithm::HS512 => jsonwebtoken::Algorithm::HS512,
38 JwtAlgorithm::RS256 => jsonwebtoken::Algorithm::RS256,
39 JwtAlgorithm::RS384 => jsonwebtoken::Algorithm::RS384,
40 JwtAlgorithm::RS512 => jsonwebtoken::Algorithm::RS512,
41 JwtAlgorithm::ES256 => jsonwebtoken::Algorithm::ES256,
42 JwtAlgorithm::ES384 => jsonwebtoken::Algorithm::ES384,
43 }
44 }
45}
46
47#[derive(Debug, Clone)]
49pub struct JwtConfig {
50 pub secret: String,
52
53 pub public_key: Option<String>,
55
56 pub algorithm: JwtAlgorithm,
58
59 pub issuer: Option<String>,
61
62 pub audience: Option<String>,
64
65 pub access_token_ttl: Duration,
67
68 pub refresh_token_ttl: Duration,
70
71 pub validate_issuer: bool,
73
74 pub validate_audience: bool,
76
77 pub leeway_seconds: i64,
79}
80
81impl JwtConfig {
82 pub fn new(secret: impl Into<String>) -> Self {
87 Self {
88 secret: secret.into(),
89 public_key: None,
90 algorithm: JwtAlgorithm::HS256,
91 issuer: None,
92 audience: None,
93 access_token_ttl: Duration::from_secs(3600),
94 refresh_token_ttl: Duration::from_secs(86400 * 7),
95 validate_issuer: false,
96 validate_audience: false,
97 leeway_seconds: 60,
98 }
99 }
100
101 pub fn with_algorithm(mut self, algorithm: JwtAlgorithm) -> Self {
103 self.algorithm = algorithm;
104 self
105 }
106
107 pub fn with_issuer(mut self, issuer: impl Into<String>) -> Self {
109 self.issuer = Some(issuer.into());
110 self.validate_issuer = true;
111 self
112 }
113
114 pub fn with_audience(mut self, audience: impl Into<String>) -> Self {
116 self.audience = Some(audience.into());
117 self.validate_audience = true;
118 self
119 }
120
121 pub fn with_access_token_ttl(mut self, ttl: Duration) -> Self {
123 self.access_token_ttl = ttl;
124 self
125 }
126
127 pub fn with_refresh_token_ttl(mut self, ttl: Duration) -> Self {
129 self.refresh_token_ttl = ttl;
130 self
131 }
132
133 pub fn with_public_key(mut self, public_key: impl Into<String>) -> Self {
135 self.public_key = Some(public_key.into());
136 self
137 }
138
139 pub fn with_leeway(mut self, seconds: i64) -> Self {
141 self.leeway_seconds = seconds;
142 self
143 }
144
145 pub fn access_token_expires_in(&self) -> i64 {
147 self.access_token_ttl.as_secs() as i64
148 }
149
150 pub fn refresh_token_expires_in(&self) -> i64 {
152 self.refresh_token_ttl.as_secs() as i64
153 }
154}
155
156impl Default for JwtConfig {
157 fn default() -> Self {
158 Self::new("default-secret-key-please-change-in-production")
159 }
160}
161
162#[derive(Debug, Clone)]
164pub struct JwtValidation {
165 pub validate_signature: bool,
167
168 pub validate_exp: bool,
170
171 pub validate_nbf: bool,
173
174 pub validate_iss: bool,
176
177 pub validate_aud: bool,
179
180 pub leeway: i64,
182}
183
184impl Default for JwtValidation {
185 fn default() -> Self {
186 Self {
187 validate_signature: true,
188 validate_exp: true,
189 validate_nbf: true,
190 validate_iss: false,
191 validate_aud: false,
192 leeway: 60,
193 }
194 }
195}
196
197impl JwtValidation {
198 pub fn new() -> Self {
200 Self::default()
201 }
202
203 pub fn with_issuer_validation(mut self, validate: bool) -> Self {
205 self.validate_iss = validate;
206 self
207 }
208
209 pub fn with_audience_validation(mut self, validate: bool) -> Self {
211 self.validate_aud = validate;
212 self
213 }
214
215 pub fn with_leeway(mut self, seconds: i64) -> Self {
217 self.leeway = seconds;
218 self
219 }
220}