git ssb

0+

Spruce git-ssb bridge / ssi



Commit cd6a760046c6d95b4f7eda1f79f8fd6d7e7e0f86

Use error type for PublicKeyJwk JWK conversion

- Add PublicKeyJwkFromJWKError
- Add JWKFromPublicKeyJwkError
- Remove anyhow::Error from JWSDecodeVerifyError
Charles E. Lehner committed on 3/1/2022, 3:47:03 PM
Parent: 992dd341661a34fd318e72d8e2b2bf23a55a3609

Files changed

did-ion/src/sidetree.rschanged
did-ion/src/sidetree.rsView
@@ -1248,16 +1248,34 @@
12481248 #[serde(flatten)]
12491249 jwk: Value,
12501250 }
12511251
1252 +/// Error resulting from [converting JWK to PublicKeyJwk][PublicKeyJwk::try_from]
1253 +#[derive(ThisError, Debug)]
1254 +pub enum PublicKeyJwkFromJWKError {
1255 + /// Unable to convert JWK to [Value]
1256 + #[error("Unable to convert JWK to Value")]
1257 + ToValue(#[from] serde_json::Error),
1258 + /// Public Key JWK must not contain private key parameters (e.g. "d")
1259 + #[error("Public Key JWK must not contain private key parameters")]
1260 + PrivateKeyParameters,
1261 +}
1262 +
1263 +/// Error resulting from attempting to convert [PublicKeyJwk] to JWK
1264 +#[derive(ThisError, Debug)]
1265 +pub enum JWKFromPublicKeyJwkError {
1266 + /// Unable to convert [Value] to JWK
1267 + #[error("Unable to convert Value to JWK")]
1268 + FromValue(#[from] serde_json::Error),
1269 +}
1270 +
12521271 impl TryFrom<JWK> for PublicKeyJwk {
1253- type Error = AError;
1272 + type Error = PublicKeyJwkFromJWKError;
12541273 fn try_from(jwk: JWK) -> Result<Self, Self::Error> {
1255- let jwk_value = serde_json::to_value(jwk).context("Convert JWK to Value")?;
1256- ensure!(
1257- jwk_value.get("d").is_none(),
1258- "Public Key JWK must not contain private key parameters"
1259- );
1274 + let jwk_value = serde_json::to_value(jwk).map_err(PublicKeyJwkFromJWKError::ToValue)?;
1275 + if jwk_value.get("d").is_some() {
1276 + return Err(PublicKeyJwkFromJWKError::PrivateKeyParameters);
1277 + };
12601278 Ok(Self {
12611279 jwk: jwk_value,
12621280 nonce: None,
12631281 })
@@ -1267,11 +1285,11 @@
12671285 /// Convert [PublicKeyJwk] to [JWK].
12681286 ///
12691287 /// Note: `nonce` property is dropped.
12701288 impl TryFrom<PublicKeyJwk> for JWK {
1271- type Error = AError;
1289 + type Error = JWKFromPublicKeyJwkError;
12721290 fn try_from(pkjwk: PublicKeyJwk) -> Result<Self, Self::Error> {
1273- let jwk = serde_json::from_value(pkjwk.jwk).context("Convert Value to JWK")?;
1291 + let jwk = serde_json::from_value(pkjwk.jwk).map_err(JWKFromPublicKeyJwkError::FromValue)?;
12741292 Ok(jwk)
12751293 }
12761294 }
12771295
@@ -1793,9 +1811,9 @@
17931811 #[error("Deserialize JWS payload")]
17941812 DeserializeJWSPayload(#[source] serde_json::Error),
17951813 /// Unable to convert PublicKeyJwk to JWK
17961814 #[error("Unable to convert PublicKeyJwk to JWK")]
1797- ConvertPublicKeyJwkToJWK(#[source] anyhow::Error),
1815 + JWKFromPublicKeyJwk(#[source] JWKFromPublicKeyJwkError),
17981816 /// Unable to verify JWS
17991817 #[error("Unable to verify JWS")]
18001818 VerifyJWS(#[source] ssi::error::Error),
18011819 }
@@ -1830,9 +1848,9 @@
18301848 .map_err(JWSDecodeVerifyError::DecodeJWSParts)?;
18311849 let claims: Claims =
18321850 serde_json::from_slice(&payload).map_err(JWSDecodeVerifyError::DeserializeJWSPayload)?;
18331851 let pk = get_key(&claims);
1834- let pk = JWK::try_from(pk.clone()).map_err(JWSDecodeVerifyError::ConvertPublicKeyJwkToJWK)?;
1852 + let pk = JWK::try_from(pk.clone()).map_err(JWSDecodeVerifyError::JWKFromPublicKeyJwk)?;
18351853 verify_bytes(header.algorithm, &signing_input, &pk, &signature)
18361854 .map_err(JWSDecodeVerifyError::VerifyJWS)?;
18371855 Ok((header, claims))
18381856 }

Built with git-ssb-web