Skip to content

Commit

Permalink
Preserve enum KeyUse for common cases
Browse files Browse the repository at this point in the history
  • Loading branch information
nhynes committed Apr 21, 2022
1 parent b422bad commit 4b61780
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 13 deletions.
31 changes: 30 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub struct JsonWebKey {
pub key: Box<Key>,

#[serde(default, rename = "use", skip_serializing_if = "Option::is_none")]
pub key_use: Option<String>,
pub key_use: Option<KeyUse>,

#[serde(default, skip_serializing_if = "KeyOps::is_empty")]
pub key_ops: KeyOps,
Expand Down Expand Up @@ -525,6 +525,35 @@ impl fmt::Debug for RsaPrivate {
}
}

#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum KeyUse {
Signing,
Encryption,
Custom(String),
}

impl Serialize for KeyUse {
fn serialize<S: serde::ser::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
match self {
Self::Signing => "sig",
Self::Encryption => "enc",
Self::Custom(u) => u.as_str(),
}
.serialize(s)
}
}

impl<'de> Deserialize<'de> for KeyUse {
fn deserialize<D: serde::de::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
Ok(match <&'de str>::deserialize(d)? {
"sig" => Self::Signing,
"enc" => Self::Encryption,
u => Self::Custom(u.into()),
})
}
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[allow(clippy::upper_case_acronyms)]
pub enum Algorithm {
Expand Down
24 changes: 12 additions & 12 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ fn deserialize_es256() {
algorithm: Some(Algorithm::ES256),
key_id: Some("a key".into()),
key_ops: KeyOps::empty(),
key_use: Some("enc".to_string()),
key_use: Some(KeyUse::Encryption),
x5: Default::default(),
}
);
Expand Down Expand Up @@ -121,7 +121,7 @@ fn deserialize_es256_with_other_use() {
algorithm: Some(Algorithm::ES256),
key_id: Some("a key".into()),
key_ops: KeyOps::empty(),
key_use: Some("jwt-svid".into()),
key_use: Some(KeyUse::Custom("jwt-svid".into())),
x5: Default::default(),
}
);
Expand Down Expand Up @@ -288,7 +288,7 @@ fn deserialize_rs256() {
algorithm: None,
key_id: None,
key_ops: KeyOps::WRAP_KEY,
key_use: Some("enc".into()),
key_use: Some(KeyUse::Encryption),
x5: Default::default(),
}
);
Expand All @@ -309,7 +309,7 @@ fn deserialize_rs256_other_use() {
168, 141, 85, 152, 107, 76, 110, 140, 47, 153, 63, 182, 97, 196, 28, 143,
199, 39, 54, 61, 172, 240, 20, 146, 98, 246, 43, 217, 254, 8, 17, 195
]
.into()
.into()
},
private: Some(RsaPrivate {
d: vec![
Expand All @@ -318,51 +318,51 @@ fn deserialize_rs256_other_use() {
44, 209, 101, 31, 104, 135, 249, 115, 121, 253, 233, 26, 195, 12, 12, 230,
48, 76, 32, 42, 114, 123, 3, 83, 73, 244, 217, 115, 207, 134, 116, 1
]
.into(),
.into(),
p: Some(
vec![
232, 4, 56, 200, 119, 159, 215, 182, 167, 254, 46, 75, 64, 241, 205,
35, 28, 233, 31, 174, 113, 88, 228, 159, 254, 160, 129, 238, 175, 165,
95, 35
]
.into()
.into()
),
q: Some(
vec![
181, 37, 95, 165, 231, 194, 177, 253, 98, 90, 96, 44, 215, 54, 47, 197,
209, 44, 82, 43, 244, 84, 193, 46, 64, 27, 91, 78, 40, 227, 252, 225
]
.into()
.into()
),
dp: Some(
vec![
169, 89, 203, 136, 167, 168, 72, 111, 206, 151, 61, 123, 56, 96, 70,
119, 134, 182, 178, 165, 69, 158, 184, 225, 255, 157, 112, 185, 164, 3,
117, 57
]
.into()
.into()
),
dq: Some(
vec![
24, 191, 196, 115, 172, 88, 131, 108, 245, 21, 23, 242, 200, 108, 148,
214, 88, 31, 208, 18, 69, 77, 151, 31, 52, 143, 8, 72, 131, 121, 178,
193
]
.into()
.into()
),
qi: Some(
vec![
105, 216, 80, 28, 127, 8, 25, 113, 95, 44, 67, 39, 103, 155, 127, 77,
224, 169, 231, 56, 18, 193, 9, 45, 39, 105, 102, 202, 92, 84, 27, 67
]
.into()
.into()
)
})
}),
algorithm: None,
key_id: None,
key_ops: KeyOps::WRAP_KEY,
key_use: Some("x509-svid".to_string()),
key_use: Some(KeyUse::Custom("x509-svid".to_string())),
x5: Default::default(),
}
);
Expand All @@ -388,7 +388,7 @@ fn serialize_rs256() {
key_id: None,
algorithm: None,
key_ops: KeyOps::empty(),
key_use: Some("sig".into()),
key_use: Some(KeyUse::Signing),
x5: Default::default(),
};
assert_eq!(
Expand Down

0 comments on commit 4b61780

Please sign in to comment.