Skip to content

Commit fe94e10

Browse files
authored
Merge pull request #82 from andrei-21/feature/case-insensitive
Look up header names in lowercase
2 parents 803d8c5 + e0bb4a5 commit fe94e10

3 files changed

Lines changed: 12 additions & 10 deletions

File tree

rust/auth-impls/src/jwt.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl Authorizer for JWTAuthorizer {
4646
&self, headers_map: &HashMap<String, String>,
4747
) -> Result<AuthResponse, VssError> {
4848
let auth_header = headers_map
49-
.get("Authorization")
49+
.get("authorization")
5050
.ok_or(VssError::AuthError("Authorization header not found.".to_string()))?;
5151

5252
let token = auth_header
@@ -143,7 +143,7 @@ mod tests {
143143
encode(&Header::new(Algorithm::RS256), &claims, &valid_encoding_key).unwrap();
144144
let mut headers_map: HashMap<String, String> = HashMap::new();
145145
let header_value = format!("Bearer {}", valid_jwt_token);
146-
headers_map.insert("Authorization".to_string(), header_value.clone());
146+
headers_map.insert("authorization".to_string(), header_value.clone());
147147
println!("headers_map: {:?}", headers_map);
148148

149149
// JWT signed by valid key results in authenticated user.
@@ -184,7 +184,7 @@ mod tests {
184184

185185
let invalid_jwt_token =
186186
encode(&Header::new(Algorithm::RS256), &claims, &invalid_encoding_key).unwrap();
187-
headers_map.insert("Authorization".to_string(), format!("Bearer {}", invalid_jwt_token));
187+
headers_map.insert("authorization".to_string(), format!("Bearer {}", invalid_jwt_token));
188188

189189
// JWT signed by invalid key results in AuthError.
190190
assert!(matches!(

rust/auth-impls/src/signature.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ impl Authorizer for SignatureValidatingAuthorizer {
4343
&self, headers_map: &HashMap<String, String>,
4444
) -> Result<AuthResponse, VssError> {
4545
let auth_header = headers_map
46-
.get("Authorization")
46+
.get("authorization")
4747
.ok_or_else(|| VssError::AuthError("Authorization header not found.".to_string()))?;
4848

4949
if auth_header.len() <= (33 + 64) * 2 {
@@ -122,17 +122,17 @@ mod tests {
122122

123123
// Test a valid signature
124124
let (token, pubkey) = build_token(now);
125-
headers_map.insert("Authorization".to_string(), token);
125+
headers_map.insert("authorization".to_string(), token);
126126
assert_eq!(auth.verify(&headers_map).await.unwrap().user_token, format!("{pubkey:x}"));
127127

128128
// Test a signature too far in the future
129129
let (token, _) = build_token(now + 60 * 60 * 24 + 10);
130-
headers_map.insert("Authorization".to_string(), token);
130+
headers_map.insert("authorization".to_string(), token);
131131
assert!(matches!(auth.verify(&headers_map).await.unwrap_err(), VssError::AuthError(_)));
132132

133133
// Test a signature too far in the past
134134
let (token, _) = build_token(now - 60 * 60 * 24 - 10);
135-
headers_map.insert("Authorization".to_string(), token);
135+
headers_map.insert("authorization".to_string(), token);
136136
assert!(matches!(auth.verify(&headers_map).await.unwrap_err(), VssError::AuthError(_)));
137137

138138
// Test a token with an invalid signature
@@ -142,7 +142,7 @@ mod tests {
142142
.enumerate()
143143
.map(|(idx, c)| if (33 * 2 + 10..33 * 2 + 15).contains(&idx) { '0' } else { c })
144144
.collect();
145-
headers_map.insert("Authorization".to_string(), token);
145+
headers_map.insert("authorization".to_string(), token);
146146
assert!(matches!(auth.verify(&headers_map).await.unwrap_err(), VssError::AuthError(_)));
147147

148148
// Test a token with the wrong public key
@@ -152,7 +152,7 @@ mod tests {
152152
.enumerate()
153153
.map(|(idx, c)| if (10..15).contains(&idx) { '0' } else { c })
154154
.collect();
155-
headers_map.insert("Authorization".to_string(), token);
155+
headers_map.insert("authorization".to_string(), token);
156156
assert!(matches!(auth.verify(&headers_map).await.unwrap_err(), VssError::AuthError(_)));
157157
}
158158
}

rust/server/src/vss_service.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,10 @@ async fn handle_request<
103103
let headers_map = parts
104104
.headers
105105
.iter()
106-
.map(|(k, v)| (k.as_str().to_string(), v.to_str().unwrap_or_default().to_string()))
106+
// HeaderName converted to a string is in lowercase.
107+
.map(|(k, v)| (k.to_string(), v.to_str().unwrap_or_default().to_string()))
107108
.collect::<HashMap<String, String>>();
109+
debug_assert!(headers_map.keys().all(|key| key.chars().all(|c| !c.is_uppercase())));
108110

109111
let user_token = match authorizer.verify(&headers_map).await {
110112
Ok(auth_response) => auth_response.user_token,

0 commit comments

Comments
 (0)