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
pub trait Payload {
fn get_iss(&self) -> Option<String> {
None
}
fn get_exp(&self) -> Option<i64> {
None
}
fn is_expired(&self) -> bool {
if let Some(exp) = self.get_exp() {
exp < time::now_utc().to_timespec().sec
} else {
true
}
}
fn get_nbf(&self) -> Option<i64> {
None
}
fn is_not_before(&self) -> bool {
if let Some(nbf) = self.get_nbf() {
nbf >= time::now_utc().to_timespec().sec
} else {
true
}
}
}
#[cfg(test)]
mod tests {
#[test]
fn test_expired_payload() {
use super::Payload;
struct Expired {};
impl super::Payload for Expired {
fn get_exp(&self) -> Option<i64> {
Some(0)
}
}
assert!(Expired {}.is_expired());
}
#[test]
fn test_not_before_payload() {
use super::Payload;
struct NotBefore {};
impl super::Payload for NotBefore {
fn get_nbf(&self) -> Option<i64> {
Some(std::i64::MAX)
}
}
assert!(NotBefore {}.is_not_before());
}
}