Fix to handle bad or unreadable .pem keys

This commit is contained in:
Alex Wright 2020-02-23 20:08:55 +01:00
parent 4bd0de2e8d
commit 29df4f4b64
1 changed files with 6 additions and 3 deletions

View File

@ -188,9 +188,9 @@ fn auth_handler(req: Request<Body>) -> Response<Body> {
Response::new(Body::from(format!("BasicAuthentication {:?}", user))) Response::new(Body::from(format!("BasicAuthentication {:?}", user)))
} }
fn jwk_from_pem(file_path: &Path) -> Result<JWK<Empty>, io::Error> { fn jwk_from_pem(file_path: &Path) -> Result<JWK<Empty>, Box<dyn std::error::Error + 'static>> {
let key_bytes = fs::read(file_path)?; 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 { Ok(JWK {
common: CommonParameters { common: CommonParameters {
algorithm: Some(Algorithm::Signature(SignatureAlgorithm::RS256)), algorithm: Some(Algorithm::Signature(SignatureAlgorithm::RS256)),
@ -219,7 +219,10 @@ fn get_keys(_req: Request<Body>) -> Response<Body> {
None => return None, None => return None,
}; };
match ext.as_ref() { 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, _ => None,
} }
}) })