From b55f4544c9273cd7603229558a9cae91dc9825d8 Mon Sep 17 00:00:00 2001 From: "Sebastian H. Gabrielli" Date: Sat, 30 Dec 2023 22:57:19 +0100 Subject: [PATCH] Handle errors in fetch_jwt_info --- src/main.rs | 47 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 36 insertions(+), 11 deletions(-) diff --git a/src/main.rs b/src/main.rs index 1bbc4ce..7e5d1b4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ -use jsonwebtoken::{decode, decode_header, errors::Result, Algorithm, DecodingKey, Validation, TokenData}; +use jsonwebtoken::{decode, decode_header, Algorithm, DecodingKey, Validation, TokenData}; use reqwest; use serde::{Deserialize, Serialize}; use std::collections::HashMap; @@ -44,7 +44,13 @@ struct JwtInfo { public_keys: HashMap, } -fn validate_jwt(token: &str, jwt_info: &mut JwtInfo) -> Result { +enum MyCustomErrorType { + NetworkError, + JwtError, + JsonParseError, +} + +fn validate_jwt(token: &str, jwt_info: &mut JwtInfo) -> Result { // Decode the header to give info about the crypto let jwt_header = decode_header(token)?; @@ -167,14 +173,34 @@ fn fetch_jwt_certificates(jwt_info: &JwtInfo) -> Option> Some(output_map) } -fn fetch_jwt_info(well_known_uri: &str, expected_issuer: Vec) -> Result { +fn fetch_jwt_info(well_known_uri: &str, expected_issuer: Vec) -> Result { // Fetch the info from the well known endpoint - let well_known_body = reqwest::blocking::get(well_known_uri) - .unwrap() - .text() - .unwrap(); + let well_known_body; + match reqwest::blocking::get(well_known_uri) { + Ok(response) => { + match response.text() { + Ok(text) => well_known_body = text, + Err(e) => { + eprintln!("Failed to extract text from response body with error:\n{}", e); + return Err(MyCustomErrorType::NetworkError); + } + } + }, + Err(e) => { + eprintln!("Failed to get the well known with error:\n{}", e); + return Err(MyCustomErrorType::NetworkError); + } + } + // Parse the data into the well known struct - let well_known_data: AuthorizationWellKnown = serde_json::from_str(&well_known_body).unwrap(); + let well_known_data: AuthorizationWellKnown; + match serde_json::from_str(&well_known_body) { + Ok(data) => well_known_data = data, + Err(e) => { + eprintln!("Failed to parse well known data into struct with err:\n{}", e); + return Err(MyCustomErrorType::JsonParseError); + } + } // Validate the issuer if !expected_issuer.contains(&well_known_data.issuer) { @@ -182,8 +208,7 @@ fn fetch_jwt_info(well_known_uri: &str, expected_issuer: Vec) -> Result< "Expected issuer does not contain fetched issuer.\n{} ∉ {:?}", well_known_data.issuer, expected_issuer ); - // TODO: Return Err properly - //Err("Invalid issuer"); + return Err(MyCustomErrorType::JwtError); } // Create a JwtInfo variable @@ -198,7 +223,7 @@ fn fetch_jwt_info(well_known_uri: &str, expected_issuer: Vec) -> Result< match fetch_jwt_certificates(&jwt_info) { Some(map) => jwt_info.public_keys = map, None => { - // TODO: Return err properly + return Err(MyCustomErrorType::JwtError); } }