1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
use std::collections::HashSet;
use jsonwebtoken::Algorithm;
pub struct Validation {
pub leeway: u64,
pub validate_exp: bool,
pub validate_nbf: bool,
pub aud: Option<Vec<String>>,
pub iss: Option<Vec<String>>,
pub validate_signature: bool,
}
impl Validation {
pub fn new() -> Self {
Default::default()
}
pub fn iss<T: ToString>(mut self, items: &[T]) -> Self {
self.iss = Some(items.iter().map(|x| x.to_string()).collect());
self
}
pub fn aud<T: ToString>(mut self, items: &[T]) -> Self {
self.aud = Some(items.iter().map(|x| x.to_string()).collect());
self
}
pub fn exp(mut self, val: bool) -> Self {
self.validate_exp = val;
self
}
pub fn nbf(mut self, val: bool) -> Self {
self.validate_nbf = val;
self
}
pub fn leeway(mut self, value: u64) -> Self {
self.leeway = value;
self
}
pub fn disable_validation(mut self) -> Self {
self.validate_signature = false;
self
}
pub(crate) fn to_jwt_validation(&self, alg: Algorithm) -> jsonwebtoken::Validation {
let required_claims = if self.validate_exp {
let mut claims = HashSet::with_capacity(1);
claims.insert("exp".to_owned());
claims
} else {
HashSet::with_capacity(0)
};
let aud = self.aud.clone().map(|v| HashSet::from_iter(v.into_iter()));
let iss = self.iss.clone().map(|v| HashSet::from_iter(v.into_iter()));
let mut jwt_validation = jsonwebtoken::Validation::default();
jwt_validation.required_spec_claims = required_claims;
jwt_validation.leeway = self.leeway;
jwt_validation.validate_exp = self.validate_exp;
jwt_validation.validate_nbf = self.validate_nbf;
jwt_validation.iss = iss;
jwt_validation.aud = aud;
jwt_validation.sub = None;
jwt_validation.algorithms = vec![alg];
if !self.validate_signature {
jwt_validation.insecure_disable_signature_validation();
}
jwt_validation
}
}
impl Default for Validation {
fn default() -> Self {
Validation {
leeway: 60,
validate_exp: true,
validate_nbf: false,
iss: None,
aud: None,
validate_signature: true,
}
}
}