Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

20240410 serde #30

Merged
merged 2 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading