Skip to content

Commit

Permalink
Merge pull request #30 from kanidm/20240410-serde
Browse files Browse the repository at this point in the history
20240410 serde
  • Loading branch information
yaleman authored Apr 11, 2024
2 parents 180962b + 78a2340 commit ac1f14e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
39 changes: 39 additions & 0 deletions src/compact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,45 @@ impl fmt::Display for JwsCompact {
}
}

impl Serialize for JwsCompact {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let self_str = self.to_string();
serializer.serialize_str(&self_str)
}
}

struct JwsCompactVisitor;

impl<'de> serde::de::Visitor<'de> for JwsCompactVisitor {
type Value = JwsCompact;

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("a compact JWS which consists of three base64 url safe unpadded strings separated with '.'")
}

fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
JwsCompact::from_str(v)
.map_err(|_|
serde::de::Error::invalid_value(serde::de::Unexpected::Str(v), &self)
)
}
}

impl<'de> Deserialize<'de> for JwsCompact {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
deserializer.deserialize_str(JwsCompactVisitor)
}
}

impl JwsVerifiable for JwsCompact {
type Verified = Jws;

Expand Down
6 changes: 2 additions & 4 deletions src/crypto/es256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,7 @@ impl JwsEs256Signer {
.and_then(|der| hash::hash(digest, &der))
.map(|hashout| {
let mut s = hex::encode(hashout);
// 192 bits
s.truncate(48);
s.truncate(KID_LEN);
s
})
.map_err(|e| {
Expand Down Expand Up @@ -433,8 +432,7 @@ impl TryFrom<&Jwk> for JwsEs256Verifier {
.and_then(|der| hash::hash(digest, &der))
.map(|hashout| {
let mut s = hex::encode(hashout);
// 192 bits
s.truncate(48);
s.truncate(KID_LEN);
s
})
.map_err(|e| {
Expand Down

0 comments on commit ac1f14e

Please sign in to comment.