Commit cd6a760046c6d95b4f7eda1f79f8fd6d7e7e0f86
Use error type for PublicKeyJwk JWK conversion
- Add PublicKeyJwkFromJWKError - Add JWKFromPublicKeyJwkError - Remove anyhow::Error from JWSDecodeVerifyErrorCharles E. Lehner committed on 3/1/2022, 3:47:03 PM
Parent: 992dd341661a34fd318e72d8e2b2bf23a55a3609
Files changed
did-ion/src/sidetree.rs | changed |
did-ion/src/sidetree.rs | ||
---|---|---|
@@ -1248,16 +1248,34 @@ | ||
1248 | 1248 … | |
1249 | 1249 … | jwk: Value, |
1250 | 1250 … | } |
1251 | 1251 … | |
1252 … | +/// Error resulting from [converting JWK to PublicKeyJwk][PublicKeyJwk::try_from] | |
1253 … | + | |
1254 … | +pub enum PublicKeyJwkFromJWKError { | |
1255 … | + /// Unable to convert JWK to [Value] | |
1256 … | + | |
1257 … | + ToValue( | serde_json::Error),|
1258 … | + /// Public Key JWK must not contain private key parameters (e.g. "d") | |
1259 … | + | |
1260 … | + PrivateKeyParameters, | |
1261 … | +} | |
1262 … | + | |
1263 … | +/// Error resulting from attempting to convert [PublicKeyJwk] to JWK | |
1264 … | + | |
1265 … | +pub enum JWKFromPublicKeyJwkError { | |
1266 … | + /// Unable to convert [Value] to JWK | |
1267 … | + | |
1268 … | + FromValue( | serde_json::Error),|
1269 … | +} | |
1270 … | + | |
1252 | 1271 … | impl TryFrom<JWK> for PublicKeyJwk { |
1253 | - type Error = AError; | |
1272 … | + type Error = PublicKeyJwkFromJWKError; | |
1254 | 1273 … | 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 … | + }; | |
1260 | 1278 … | Ok(Self { |
1261 | 1279 … | jwk: jwk_value, |
1262 | 1280 … | nonce: None, |
1263 | 1281 … | }) |
@@ -1267,11 +1285,11 @@ | ||
1267 | 1285 … | /// Convert [PublicKeyJwk] to [JWK]. |
1268 | 1286 … | /// |
1269 | 1287 … | /// Note: `nonce` property is dropped. |
1270 | 1288 … | impl TryFrom<PublicKeyJwk> for JWK { |
1271 | - type Error = AError; | |
1289 … | + type Error = JWKFromPublicKeyJwkError; | |
1272 | 1290 … | 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)?; | |
1274 | 1292 … | Ok(jwk) |
1275 | 1293 … | } |
1276 | 1294 … | } |
1277 | 1295 … | |
@@ -1793,9 +1811,9 @@ | ||
1793 | 1811 … | |
1794 | 1812 … | DeserializeJWSPayload( | serde_json::Error),
1795 | 1813 … | /// Unable to convert PublicKeyJwk to JWK |
1796 | 1814 … | |
1797 | - ConvertPublicKeyJwkToJWK( | anyhow::Error),|
1815 … | + JWKFromPublicKeyJwk( | JWKFromPublicKeyJwkError),|
1798 | 1816 … | /// Unable to verify JWS |
1799 | 1817 … | |
1800 | 1818 … | VerifyJWS( | ssi::error::Error),
1801 | 1819 … | } |
@@ -1830,9 +1848,9 @@ | ||
1830 | 1848 … | .map_err(JWSDecodeVerifyError::DecodeJWSParts)?; |
1831 | 1849 … | let claims: Claims = |
1832 | 1850 … | serde_json::from_slice(&payload).map_err(JWSDecodeVerifyError::DeserializeJWSPayload)?; |
1833 | 1851 … | 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)?; | |
1835 | 1853 … | verify_bytes(header.algorithm, &signing_input, &pk, &signature) |
1836 | 1854 … | .map_err(JWSDecodeVerifyError::VerifyJWS)?; |
1837 | 1855 … | Ok((header, claims)) |
1838 | 1856 … | } |
Built with git-ssb-web