From 29df4f4b6468481e91813d6f3cea47fbca7d1279 Mon Sep 17 00:00:00 2001 From: Alex Wright Date: Sun, 23 Feb 2020 20:08:55 +0100 Subject: [PATCH] Fix to handle bad or unreadable .pem keys --- src/main.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 781bfe8..a6ca2a4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -188,9 +188,9 @@ fn auth_handler(req: Request) -> Response { Response::new(Body::from(format!("BasicAuthentication {:?}", user))) } -fn jwk_from_pem(file_path: &Path) -> Result, io::Error> { +fn jwk_from_pem(file_path: &Path) -> Result, Box> { let key_bytes = fs::read(file_path)?; - let rsa = Rsa::private_key_from_pem(key_bytes.as_slice()).unwrap(); + let rsa = Rsa::private_key_from_pem(key_bytes.as_slice())?; Ok(JWK { common: CommonParameters { algorithm: Some(Algorithm::Signature(SignatureAlgorithm::RS256)), @@ -219,7 +219,10 @@ fn get_keys(_req: Request) -> Response { None => return None, }; match ext.as_ref() { - "pem" => Some(jwk_from_pem(path.as_path()).unwrap()), + "pem" => match jwk_from_pem(path.as_path()) { + Ok(jwk) => Some(jwk), + _ => None, + }, _ => None, } })