diff --git a/src/lib.rs b/src/lib.rs index 20c8dd6..540a2f2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -157,7 +157,7 @@ where Ok(decoded) } -fn serialize_base64_option( +fn serialize_base64_vec( sub: &Option>, serializer: S, ) -> core::result::Result @@ -166,25 +166,23 @@ where { match sub { Some(value) => { - let encoded = BASE64_URL_SAFE_NO_PAD.encode(value); + let encoded = String::from_utf8(value.clone()).map_err(serde::ser::Error::custom)?; serializer.serialize_str(&encoded) } None => serializer.serialize_none(), } } -fn deserialize_base64_option<'de, D>( +fn deserialize_base64_vec<'de, D>( deserializer: D, ) -> core::result::Result>, D::Error> where D: serde::Deserializer<'de>, { - let encoded = String::deserialize(deserializer)?; - let decoded = BASE64_URL_SAFE_NO_PAD - .decode(encoded) - .map_err(serde::de::Error::custom)?; + let string = String::deserialize(deserializer)?; + let bytes = string.into_bytes(); - Ok(Some(decoded)) + Ok(Some(bytes)) } #[derive(Clone, Serialize, Deserialize, Debug)] @@ -202,10 +200,10 @@ pub struct Response { pub encrypted_key: Vec, #[serde( - deserialize_with = "deserialize_base64_option", skip_serializing_if = "Option::is_none", - serialize_with = "serialize_base64_option", - default = "Option::default" + default = "Option::default", + serialize_with = "serialize_base64_vec", + deserialize_with = "deserialize_base64_vec" )] pub aad: Option>, @@ -320,7 +318,7 @@ mod tests { "protected": "eyJhbGciOiJmYWtlYWxnIiwiZW5jIjoiZmFrZWVuYyJ9Cg", "encrypted_key": "ZmFrZWtleQ", "iv": "cmFuZG9tZGF0YQ", - "aad": "ZmFrZWFhZA", + "aad": "fakeaad", "ciphertext": "ZmFrZWVuY291dHB1dA", "tag": "ZmFrZXRhZw" }"#; @@ -344,7 +342,7 @@ mod tests { "protected": "eyJhbGciOiJmYWtlYWxnIiwiZW5jIjoiZmFrZWVuYyIsImZha2VmaWVsZCI6ImZha2V2YWx1ZSJ9", "encrypted_key": "ZmFrZWtleQ", "iv": "cmFuZG9tZGF0YQ", - "aad": "ZmFrZWFhZA", + "aad": "fakeaad", "ciphertext": "ZmFrZWVuY291dHB1dA", "tag": "ZmFrZXRhZw" }"#; @@ -382,7 +380,7 @@ mod tests { "protected": "eyJhbGciOiJmYWtlYWxnIiwiZW5jIjoiZmFrZWVuYyIsImZha2VmaWVsZCI6ImZha2V2YWx1ZSJ9", "encrypted_key": "ZmFrZWtleQ", "iv": "cmFuZG9tZGF0YQ", - "aad": "ZmFrZWFhZA", + "aad": "fakeaad", "ciphertext": "ZmFrZWVuY291dHB1dA", "tag": "ZmFrZXRhZw" });