acme_types/v2/
jose.rs

1#[cfg(feature = "json")]
2use serde::{Deserialize, Serialize};
3
4/// Defines a JSON web key object.
5///
6/// For more information, refer to [RFC 8555 § 6.2](https://datatracker.ietf.org/doc/html/rfc8555#section-6.2)
7#[derive(Clone, Debug)]
8#[cfg_attr(feature = "json", derive(Serialize, Deserialize))]
9pub struct JsonWebKey {
10    #[cfg_attr(feature = "json", serde(rename = "kty"))]
11    pub key_type: String,
12    #[cfg_attr(feature = "json", serde(rename = "e"))]
13    pub exponent: String,
14    #[cfg_attr(feature = "json", serde(rename = "n"))]
15    pub modulus: String,
16}
17
18#[cfg(feature = "json")]
19impl JsonWebKey {
20    /// Deserializes a JsonWebKey object from a JSON str
21    pub fn from_str(s: &str) -> Result<JsonWebKey, serde_json::error::Error> {
22        serde_json::from_str(s)
23    }
24
25    /// Serializes a JsonWebKey object to a JSON String
26    pub fn to_string(&self) -> Result<String, serde_json::error::Error> {
27        serde_json::to_string(self)
28    }
29}
30
31/// Defines a JSON web signature object.
32///
33/// For more information, refer to [RFC 8555 § 6.2](https://datatracker.ietf.org/doc/html/rfc8555#section-6.2)
34#[derive(Clone, Debug)]
35#[cfg_attr(feature = "json", derive(Serialize, Deserialize))]
36pub struct JsonWebSignature {
37    pub protected: String,
38    pub payload: String,
39    pub signature: String,
40}
41
42#[cfg(feature = "json")]
43impl JsonWebSignature {
44    /// Deserializes a JsonWebSignature object from a JSON str
45    pub fn from_str(s: &str) -> Result<JsonWebSignature, serde_json::error::Error> {
46        serde_json::from_str(s)
47    }
48
49    /// Serializes a JsonWebSignature object to a JSON String
50    pub fn to_string(&self) -> Result<String, serde_json::error::Error> {
51        serde_json::to_string(self)
52    }
53}
54
55/// Defines the protected data object in the JWS payload.
56///
57/// For more information, refer to [RFC 8555 § 6.2](https://datatracker.ietf.org/doc/html/rfc8555#section-6.2)
58#[derive(Clone, Debug)]
59#[cfg_attr(feature = "json", derive(Serialize, Deserialize))]
60pub struct JsonWebSignatureProtected {
61    #[cfg_attr(feature = "json", serde(rename = "alg"))]
62    pub algorithm: String,
63    #[cfg_attr(feature = "json", serde(skip_serializing_if = "Option::is_none"))]
64    pub nonce: Option<String>,
65    pub url: String,
66    #[cfg_attr(feature = "json", serde(skip_serializing_if = "Option::is_none"))]
67    #[cfg_attr(feature = "json", serde(rename = "jwk"))]
68    pub json_web_key: Option<JsonWebKey>,
69    #[cfg_attr(feature = "json", serde(skip_serializing_if = "Option::is_none"))]
70    #[cfg_attr(feature = "json", serde(rename = "kid"))]
71    pub key_id: Option<String>,
72}
73
74#[cfg(feature = "json")]
75impl JsonWebSignatureProtected {
76    /// Deserializes a JsonWebSignatureProtected object from a JSON str
77    pub fn from_str(s: &str) -> Result<JsonWebSignatureProtected, serde_json::error::Error> {
78        serde_json::from_str(s)
79    }
80
81    /// Serializes a JsonWebSignatureProtected object to a JSON String
82    pub fn to_string(&self) -> Result<String, serde_json::error::Error> {
83        serde_json::to_string(self)
84    }
85}