From cd6a760046c6d95b4f7eda1f79f8fd6d7e7e0f86 Mon Sep 17 00:00:00 2001 From: "Charles E. Lehner" Date: Fri, 25 Feb 2022 17:13:49 -0500 Subject: [PATCH] Use error type for PublicKeyJwk JWK conversion - Add PublicKeyJwkFromJWKError - Add JWKFromPublicKeyJwkError - Remove anyhow::Error from JWSDecodeVerifyError --- did-ion/src/sidetree.rs | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/did-ion/src/sidetree.rs b/did-ion/src/sidetree.rs index 3a642e9c7..024e37517 100644 --- a/did-ion/src/sidetree.rs +++ b/did-ion/src/sidetree.rs @@ -1249,14 +1249,32 @@ pub struct PublicKeyJwk { jwk: Value, } +/// Error resulting from [converting JWK to PublicKeyJwk][PublicKeyJwk::try_from] +#[derive(ThisError, Debug)] +pub enum PublicKeyJwkFromJWKError { + /// Unable to convert JWK to [Value] + #[error("Unable to convert JWK to Value")] + ToValue(#[from] serde_json::Error), + /// Public Key JWK must not contain private key parameters (e.g. "d") + #[error("Public Key JWK must not contain private key parameters")] + PrivateKeyParameters, +} + +/// Error resulting from attempting to convert [PublicKeyJwk] to JWK +#[derive(ThisError, Debug)] +pub enum JWKFromPublicKeyJwkError { + /// Unable to convert [Value] to JWK + #[error("Unable to convert Value to JWK")] + FromValue(#[from] serde_json::Error), +} + impl TryFrom for PublicKeyJwk { - type Error = AError; + type Error = PublicKeyJwkFromJWKError; fn try_from(jwk: JWK) -> Result { - let jwk_value = serde_json::to_value(jwk).context("Convert JWK to Value")?; - ensure!( - jwk_value.get("d").is_none(), - "Public Key JWK must not contain private key parameters" - ); + let jwk_value = serde_json::to_value(jwk).map_err(PublicKeyJwkFromJWKError::ToValue)?; + if jwk_value.get("d").is_some() { + return Err(PublicKeyJwkFromJWKError::PrivateKeyParameters); + }; Ok(Self { jwk: jwk_value, nonce: None, @@ -1268,9 +1286,9 @@ impl TryFrom for PublicKeyJwk { /// /// Note: `nonce` property is dropped. impl TryFrom for JWK { - type Error = AError; + type Error = JWKFromPublicKeyJwkError; fn try_from(pkjwk: PublicKeyJwk) -> Result { - let jwk = serde_json::from_value(pkjwk.jwk).context("Convert Value to JWK")?; + let jwk = serde_json::from_value(pkjwk.jwk).map_err(JWKFromPublicKeyJwkError::FromValue)?; Ok(jwk) } } @@ -1794,7 +1812,7 @@ pub enum JWSDecodeVerifyError { DeserializeJWSPayload(#[source] serde_json::Error), /// Unable to convert PublicKeyJwk to JWK #[error("Unable to convert PublicKeyJwk to JWK")] - ConvertPublicKeyJwkToJWK(#[source] anyhow::Error), + JWKFromPublicKeyJwk(#[source] JWKFromPublicKeyJwkError), /// Unable to verify JWS #[error("Unable to verify JWS")] VerifyJWS(#[source] ssi::error::Error), @@ -1831,7 +1849,7 @@ pub fn jws_decode_verify_inner( let claims: Claims = serde_json::from_slice(&payload).map_err(JWSDecodeVerifyError::DeserializeJWSPayload)?; let pk = get_key(&claims); - let pk = JWK::try_from(pk.clone()).map_err(JWSDecodeVerifyError::ConvertPublicKeyJwkToJWK)?; + let pk = JWK::try_from(pk.clone()).map_err(JWSDecodeVerifyError::JWKFromPublicKeyJwk)?; verify_bytes(header.algorithm, &signing_input, &pk, &signature) .map_err(JWSDecodeVerifyError::VerifyJWS)?; Ok((header, claims))