From a41c2b6c7f6c8943f442dbdc4566ea3dbb7fb037 Mon Sep 17 00:00:00 2001 From: Enrico Marconi Date: Mon, 4 Mar 2024 19:46:43 +0100 Subject: [PATCH 1/2] Use encoded/decoded string when serializing / deserializing --- Cargo.toml | 1 + src/sd_jwt.rs | 14 +++++++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 29d249f..40414e0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,6 +21,7 @@ itertools = { version = "0.12", default-features = false, features = ["use_std"] iota-crypto = { version = "0.23", default-features = false, features = ["sha"], optional = true } serde = { version = "1.0", default-features = false, features = ["derive"] } json-pointer = "0.3.4" +serde_with = "3.6.1" [dev-dependencies] josekit = "0.8.4" diff --git a/src/sd_jwt.rs b/src/sd_jwt.rs index a0682ec..bd7a9d6 100644 --- a/src/sd_jwt.rs +++ b/src/sd_jwt.rs @@ -2,16 +2,17 @@ // SPDX-License-Identifier: Apache-2.0 use std::fmt::Display; +use std::str::FromStr; use crate::Error; use crate::Result; use itertools::Itertools; -use serde::Deserialize; -use serde::Serialize; +use serde_with::DeserializeFromStr; +use serde_with::SerializeDisplay; /// Representation of an SD-JWT of the format /// `~~~...~~`. -#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)] +#[derive(Debug, Clone, Eq, PartialEq, SerializeDisplay, DeserializeFromStr)] pub struct SdJwt { /// The JWT part. pub jwt: String, @@ -80,6 +81,13 @@ impl Display for SdJwt { } } +impl FromStr for SdJwt { + type Err = Error; + fn from_str(s: &str) -> std::result::Result { + Self::parse(s) + } +} + #[cfg(test)] mod test { use crate::SdJwt; From 96bd0158f203b23ec60d58f9f157ee4259b3eff1 Mon Sep 17 00:00:00 2001 From: Enrico Marconi Date: Wed, 6 Mar 2024 15:35:11 +0100 Subject: [PATCH 2/2] SdJwt shouldn't be serializable / deserializable with serde --- src/sd_jwt.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/sd_jwt.rs b/src/sd_jwt.rs index bd7a9d6..3f93870 100644 --- a/src/sd_jwt.rs +++ b/src/sd_jwt.rs @@ -7,12 +7,10 @@ use std::str::FromStr; use crate::Error; use crate::Result; use itertools::Itertools; -use serde_with::DeserializeFromStr; -use serde_with::SerializeDisplay; /// Representation of an SD-JWT of the format /// `~~~...~~`. -#[derive(Debug, Clone, Eq, PartialEq, SerializeDisplay, DeserializeFromStr)] +#[derive(Debug, Clone, Eq, PartialEq)] pub struct SdJwt { /// The JWT part. pub jwt: String,