Skip to content

Commit

Permalink
Refactor the JWS protected header generation
Browse files Browse the repository at this point in the history
  • Loading branch information
breard-r committed Oct 8, 2020
1 parent d1901be commit 48179d1
Showing 1 changed file with 23 additions and 35 deletions.
58 changes: 23 additions & 35 deletions acmed/src/jws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,14 @@ struct JwsData {
}

#[derive(Serialize)]
struct JwsProtectedHeaderJwkNoNonce {
struct JwsProtectedHeader {
alg: String,
jwk: Value,
url: String,
}

#[derive(Serialize)]
struct JwsProtectedHeaderJwk {
alg: String,
jwk: Value,
nonce: String,
url: String,
}

#[derive(Serialize)]
struct JwsProtectedHeaderKidNoNonce {
alg: String,
kid: String,
url: String,
}

#[derive(Serialize)]
struct JwsProtectedHeaderKid {
alg: String,
kid: String,
nonce: String,
#[serde(skip_serializing_if = "Option::is_none")]
jwk: Option<Value>,
#[serde(skip_serializing_if = "Option::is_none")]
kid: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
nonce: Option<String>,
url: String,
}

Expand Down Expand Up @@ -68,10 +50,11 @@ pub fn encode_jwk(
url: &str,
nonce: &str,
) -> Result<String, Error> {
let protected = JwsProtectedHeaderJwk {
let protected = JwsProtectedHeader {
alg: sign_alg.to_string(),
jwk: key_pair.jwk_public_key()?,
nonce: nonce.into(),
jwk: Some(key_pair.jwk_public_key()?),
kid: None,
nonce: Some(nonce.into()),
url: url.into(),
};
let protected = serde_json::to_string(&protected)?;
Expand All @@ -84,9 +67,11 @@ pub fn encode_jwk_no_nonce(
payload: &[u8],
url: &str,
) -> Result<String, Error> {
let protected = JwsProtectedHeaderJwkNoNonce {
let protected = JwsProtectedHeader {
alg: sign_alg.to_string(),
jwk: key_pair.jwk_public_key()?,
jwk: Some(key_pair.jwk_public_key()?),
kid: None,
nonce: None,
url: url.into(),
};
let protected = serde_json::to_string(&protected)?;
Expand All @@ -101,10 +86,11 @@ pub fn encode_kid(
url: &str,
nonce: &str,
) -> Result<String, Error> {
let protected = JwsProtectedHeaderKid {
let protected = JwsProtectedHeader {
alg: sign_alg.to_string(),
kid: key_id.to_string(),
nonce: nonce.into(),
jwk: None,
kid: Some(key_id.to_string()),
nonce: Some(nonce.into()),
url: url.into(),
};
let protected = serde_json::to_string(&protected)?;
Expand All @@ -118,9 +104,11 @@ pub fn encode_kid_mac(
payload: &[u8],
url: &str,
) -> Result<String, Error> {
let protected = JwsProtectedHeaderKidNoNonce {
let protected = JwsProtectedHeader {
alg: sign_alg.to_string(),
kid: key_id.to_string(),
jwk: None,
kid: Some(key_id.to_string()),
nonce: None,
url: url.into(),
};
let protected = serde_json::to_string(&protected)?;
Expand Down

0 comments on commit 48179d1

Please sign in to comment.