Handle all errors in fetch_jwt_certificates

This commit is contained in:
Sebastian H. Gabrielli 2023-12-30 22:42:56 +01:00
parent 7fe7d62c52
commit ceaf11c15f

View File

@ -116,12 +116,32 @@ fn validate_jwt(token: &str, jwt_info: &mut JwtInfo) -> Result<MyClaims> {
fn fetch_jwt_certificates(jwt_info: &JwtInfo) -> Option<HashMap<String, String>> { fn fetch_jwt_certificates(jwt_info: &JwtInfo) -> Option<HashMap<String, String>> {
// Fetch the JWKS endpoint // Fetch the JWKS endpoint
let jwks_body = reqwest::blocking::get(&jwt_info.jwks_uri) let jwks_body: String;
.unwrap() match reqwest::blocking::get(&jwt_info.jwks_uri) {
.text() Ok(response) => {
.unwrap(); match response.text() {
Ok(text) => jwks_body = text,
Err(e) => {
eprintln!("Failed to extract text from response body with error:\n{}", e);
return None;
}
}
},
Err(e) => {
eprintln!("Failed to get the jwks_uri with error:\n{}", e);
return None;
}
}
// Parse the data into the struct // Parse the data into the struct
let jwks_data: Jwks = serde_json::from_str(&jwks_body).unwrap(); let jwks_data: Jwks;
match serde_json::from_str(&jwks_body) {
Ok(jwks) => jwks_data = jwks,
Err(e) => {
eprintln!("Failed to parse fetched jwks body to Jwks struct with error:\n{}", e);
return None;
}
}
// Create the output hashmap // Create the output hashmap
let mut output_map: HashMap<String, String> = HashMap::new(); let mut output_map: HashMap<String, String> = HashMap::new();
@ -129,13 +149,10 @@ fn fetch_jwt_certificates(jwt_info: &JwtInfo) -> Option<HashMap<String, String>>
// Go through each pair of keys and add them to the output jwt info // Go through each pair of keys and add them to the output jwt info
for key in jwks_data.keys { for key in jwks_data.keys {
// Extract the x5c key data // Extract the x5c key data
let x5c = key.x5c.get(0).unwrap(); let x5c = key.x5c.get(0)?;
// Add the PEM info in to the x5c // Add the PEM info in to the x5c
let pem_data = format!( let pem_data = format!("-----BEGIN CERTIFICATE-----\n{}\n-----END CERTIFICATE-----", x5c);
"-----BEGIN CERTIFICATE-----\n{}\n-----END CERTIFICATE-----",
x5c
);
// Add the resulting key to the hashmap // Add the resulting key to the hashmap
output_map.insert(key.kid, pem_data); output_map.insert(key.kid, pem_data);