Handle errors in fetch_jwt_info
This commit is contained in:
parent
ceaf11c15f
commit
b55f4544c9
47
src/main.rs
47
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<String, String>,
|
||||
}
|
||||
|
||||
fn validate_jwt(token: &str, jwt_info: &mut JwtInfo) -> Result<MyClaims> {
|
||||
enum MyCustomErrorType {
|
||||
NetworkError,
|
||||
JwtError,
|
||||
JsonParseError,
|
||||
}
|
||||
|
||||
fn validate_jwt(token: &str, jwt_info: &mut JwtInfo) -> Result<MyClaims, jsonwebtoken::errors::Error> {
|
||||
// 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<HashMap<String, String>>
|
||||
Some(output_map)
|
||||
}
|
||||
|
||||
fn fetch_jwt_info(well_known_uri: &str, expected_issuer: Vec<String>) -> Result<JwtInfo> {
|
||||
fn fetch_jwt_info(well_known_uri: &str, expected_issuer: Vec<String>) -> Result<JwtInfo, MyCustomErrorType> {
|
||||
// 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<String>) -> 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<String>) -> Result<
|
||||
match fetch_jwt_certificates(&jwt_info) {
|
||||
Some(map) => jwt_info.public_keys = map,
|
||||
None => {
|
||||
// TODO: Return err properly
|
||||
return Err(MyCustomErrorType::JwtError);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user