Skip to main content

json_web_key/
rsa.rs

1use serde::{Deserialize, Serialize};
2use std::hash::Hash;
3
4use crate::{cert::Certificate, encoded_bytes_field::EncodedBytesField};
5
6#[derive(Clone, Debug, Deserialize, Serialize)]
7pub struct RsaWebKey {
8    #[serde(flatten)]
9    pub cert: Certificate,
10    #[serde(rename = "kid")]
11    pub key_id: Box<str>,
12    #[serde(default)]
13    pub key_ops: Box<[Box<str>]>,
14    #[serde(default, flatten)]
15    pub key_type: KeyType,
16    #[serde(rename = "use", default)]
17    pub use_case: Box<str>,
18    #[serde(rename = "e", with = "EncodedBytesField")]
19    pub exponent: Box<[u8]>,
20    #[serde(rename = "n", with = "EncodedBytesField")]
21    pub modulus: Box<[u8]>,
22}
23
24#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize)]
25#[serde(tag = "kty")]
26pub enum KeyType {
27    #[default]
28    RSA,
29}
30
31impl Default for RsaWebKey {
32    fn default() -> Self {
33        Self {
34            cert: Certificate::default(),
35            key_id: "".into(),
36            key_ops: [].into(),
37            key_type: KeyType::default(),
38            exponent: [].into(),
39            modulus: [].into(),
40            use_case: "".into(),
41        }
42    }
43}
44
45// Derive equality based off key_id, not accurate but --
46// just like database records, we can save a lot of
47// performance by just assuming the key_id is accurate.
48impl Eq for RsaWebKey {}
49
50impl Hash for RsaWebKey {
51    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
52        self.key_id.hash(state)
53    }
54}
55
56impl PartialEq for RsaWebKey {
57    fn eq(&self, other: &Self) -> bool {
58        self.key_id.eq(&other.key_id)
59    }
60}