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 reqwest;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
@ -44,7 +44,13 @@ struct JwtInfo {
|
|||||||
public_keys: HashMap<String, String>,
|
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
|
// Decode the header to give info about the crypto
|
||||||
let jwt_header = decode_header(token)?;
|
let jwt_header = decode_header(token)?;
|
||||||
|
|
||||||
@ -167,14 +173,34 @@ fn fetch_jwt_certificates(jwt_info: &JwtInfo) -> Option<HashMap<String, String>>
|
|||||||
Some(output_map)
|
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
|
// Fetch the info from the well known endpoint
|
||||||
let well_known_body = reqwest::blocking::get(well_known_uri)
|
let well_known_body;
|
||||||
.unwrap()
|
match reqwest::blocking::get(well_known_uri) {
|
||||||
.text()
|
Ok(response) => {
|
||||||
.unwrap();
|
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
|
// 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
|
// Validate the issuer
|
||||||
if !expected_issuer.contains(&well_known_data.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{} ∉ {:?}",
|
"Expected issuer does not contain fetched issuer.\n{} ∉ {:?}",
|
||||||
well_known_data.issuer, expected_issuer
|
well_known_data.issuer, expected_issuer
|
||||||
);
|
);
|
||||||
// TODO: Return Err properly
|
return Err(MyCustomErrorType::JwtError);
|
||||||
//Err("Invalid issuer");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a JwtInfo variable
|
// 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) {
|
match fetch_jwt_certificates(&jwt_info) {
|
||||||
Some(map) => jwt_info.public_keys = map,
|
Some(map) => jwt_info.public_keys = map,
|
||||||
None => {
|
None => {
|
||||||
// TODO: Return err properly
|
return Err(MyCustomErrorType::JwtError);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user