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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
use std::collections::HashSet;
use coarsetime::{Duration, UnixTimeStamp};
use ct_codecs::{Base64UrlSafeNoPadding, Decoder, Encoder, Hex};
use crate::{claims::DEFAULT_TIME_TOLERANCE_SECS, error::*};
pub const DEFAULT_MAX_TOKEN_LENGTH: usize = 1_000_000;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct VerificationOptions {
pub reject_before: Option<UnixTimeStamp>,
pub accept_future: bool,
pub required_subject: Option<String>,
pub required_key_id: Option<String>,
pub required_public_key: Option<String>,
pub required_nonce: Option<String>,
pub allowed_issuers: Option<HashSet<String>>,
pub allowed_audiences: Option<HashSet<String>>,
pub time_tolerance: Option<Duration>,
pub max_validity: Option<Duration>,
pub max_token_length: Option<usize>,
}
impl Default for VerificationOptions {
fn default() -> Self {
Self {
reject_before: None,
accept_future: false,
required_subject: None,
required_key_id: None,
required_public_key: None,
required_nonce: None,
allowed_issuers: None,
allowed_audiences: None,
time_tolerance: Some(Duration::from_secs(DEFAULT_TIME_TOLERANCE_SECS)),
max_validity: None,
max_token_length: Some(DEFAULT_MAX_TOKEN_LENGTH),
}
}
}
#[derive(Debug, Clone, Default)]
pub struct KeyMetadata {
pub(crate) key_set_url: Option<String>,
pub(crate) public_key: Option<String>,
pub(crate) certificate_url: Option<String>,
pub(crate) certificate_sha1_thumbprint: Option<String>,
pub(crate) certificate_sha256_thumbprint: Option<String>,
}
impl KeyMetadata {
pub fn with_key_set_url(mut self, key_set_url: impl ToString) -> Self {
self.key_set_url = Some(key_set_url.to_string());
self
}
pub fn with_public_key(mut self, public_key: impl ToString) -> Self {
self.public_key = Some(public_key.to_string());
self
}
pub fn with_certificate_url(mut self, certificate_url: impl ToString) -> Self {
self.certificate_url = Some(certificate_url.to_string());
self
}
pub fn with_certificate_sha1_thumbprint(
mut self,
certificate_sha1_thumbprint: impl ToString,
) -> Result<Self, Error> {
let thumbprint = certificate_sha1_thumbprint.to_string();
let mut bin = [0u8; 20];
if thumbprint.len() == 40 {
ensure!(
Hex::decode(&mut bin, &thumbprint, None)?.len() == bin.len(),
JWTError::InvalidCertThumprint
);
let thumbprint = Base64UrlSafeNoPadding::encode_to_string(bin)?;
self.certificate_sha1_thumbprint = Some(thumbprint);
return Ok(self);
}
ensure!(
Base64UrlSafeNoPadding::decode(&mut bin, &thumbprint, None)?.len() == bin.len(),
JWTError::InvalidCertThumprint
);
self.certificate_sha1_thumbprint = Some(thumbprint);
Ok(self)
}
pub fn with_certificate_sha256_thumbprint(
mut self,
certificate_sha256_thumbprint: impl ToString,
) -> Result<Self, Error> {
let thumbprint = certificate_sha256_thumbprint.to_string();
let mut bin = [0u8; 32];
if thumbprint.len() == 64 {
ensure!(
Hex::decode(&mut bin, &thumbprint, None)?.len() == bin.len(),
JWTError::InvalidCertThumprint
);
let thumbprint = Base64UrlSafeNoPadding::encode_to_string(bin)?;
self.certificate_sha256_thumbprint = Some(thumbprint);
return Ok(self);
}
ensure!(
Base64UrlSafeNoPadding::decode(&mut bin, &thumbprint, None)?.len() == bin.len(),
JWTError::InvalidCertThumprint
);
self.certificate_sha256_thumbprint = Some(thumbprint);
Ok(self)
}
}
#[inline(never)]
pub(crate) fn timingsafe_eq(a: &[u8], b: &[u8]) -> bool {
if a.len() != b.len() {
return false;
}
a.iter().zip(b.iter()).fold(0, |c, (x, y)| c | (x ^ y)) == 0
}