Skip to content

Commit

Permalink
chore: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
junkurihara committed Jan 29, 2024
1 parent 73a314e commit 28b5527
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
11 changes: 6 additions & 5 deletions src/message_component/component.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use anyhow::{bail, ensure};
use rustc_hash::FxHashSet as HashSet;

/* ---------------------------------------------------------------- */
Expand All @@ -14,7 +15,7 @@ impl HttpMessageComponent {
/// Create HttpMessageComponent from serialized string, i.e., `"<id>": <value>` in the signature input
pub(crate) fn from_serialized_str(serialized_str: &str) -> std::result::Result<Self, anyhow::Error> {
let Some((id, value)) = serialized_str.split_once(':') else {
return Err(anyhow::anyhow!("Invalid http message component: {}", serialized_str));
bail!("Invalid http message component: {}", serialized_str);
};
let id = id.trim();
ensure_component_id(id)?;
Expand Down Expand Up @@ -51,7 +52,7 @@ impl HttpMessageComponent {

fn ensure_component_id(id: &str) -> anyhow::Result<()> {
if !id.starts_with('"') || !(id.ends_with('"') || id[1..].contains("\";")) {
return Err(anyhow::anyhow!("Invalid http message component id: {}", id));
bail!("Invalid http message component id: {}", id);
}
Ok(())
}
Expand Down Expand Up @@ -108,7 +109,7 @@ impl TryFrom<(&str, &str)> for HttpMessageComponentId {
} else if !name.starts_with('"') && !name.ends_with('"') {
name.to_string()
} else {
anyhow::bail!("Invalid http message component name: {}", name);
bail!("Invalid http message component name: {}", name);
};

let res = Self {
Expand All @@ -118,7 +119,7 @@ impl TryFrom<(&str, &str)> for HttpMessageComponentId {

// assert for query param
if res.params.0.iter().any(|v| matches!(v, &HttpMessageComponentParam::Name(_))) {
anyhow::ensure!(
ensure!(
matches!(res.name, HttpMessageComponentName::Derived(DerivedComponentName::QueryParam)),
"Invalid http message component id: {}",
res
Expand All @@ -133,7 +134,7 @@ impl TryFrom<(&str, &str)> for HttpMessageComponentId {
|| matches!(v, &HttpMessageComponentParam::Tr)
|| matches!(v, &HttpMessageComponentParam::Key(_))
}) {
anyhow::ensure!(
ensure!(
matches!(res.name, HttpMessageComponentName::HttpField(_)),
"Invalid http message component id: {}",
res
Expand Down
17 changes: 12 additions & 5 deletions src/signature_params.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use crate::{
crypto::VerifyingKey,
message_component::{HttpMessageComponentId, HttpMessageComponentName},
trace::*,
util::has_unique_elements,
};
use anyhow::{bail, ensure};
use base64::{engine::general_purpose, Engine as _};
use rand::Rng;
use std::time::{SystemTime, UNIX_EPOCH};
Expand Down Expand Up @@ -67,7 +69,7 @@ impl TryFrom<&str> for HttpSignatureParams {
fn try_from(value: &str) -> anyhow::Result<Self> {
// first extract string inside `()`
if !(value.starts_with('(') && value.contains(')')) {
anyhow::bail!("Invalid message components: {}", value);
bail!("Invalid message components: {}", value);
}
let mut iter = value[1..].split(')');
let covered_components = iter
Expand All @@ -77,10 +79,13 @@ impl TryFrom<&str> for HttpSignatureParams {
.filter(|v| !v.is_empty())
.map(HttpMessageComponentId::try_from)
.collect::<Vec<_>>();
anyhow::ensure!(
ensure!(
covered_components.iter().all(|v| v.is_ok()),
"Invalid message components: {}",
value
"Invalid message component ids: {value}"
);
ensure!(
has_unique_elements(covered_components.iter().map(|v| v.as_ref().unwrap())),
"duplicate covered component ids: {value}"
);
let covered_components = covered_components
.into_iter()
Expand Down Expand Up @@ -113,7 +118,9 @@ impl TryFrom<&str> for HttpSignatureParams {
"alg" => params.alg = Some(value[1..value.len() - 1].to_string()),
"keyid" => params.keyid = Some(value[1..value.len() - 1].to_string()),
"tag" => params.tag = Some(value[1..value.len() - 1].to_string()),
_ => panic!("Invalid signature parameter: {}", key),
_ => {
error!("Ignore invalid signature parameter: {}", key)
}
}
});
};
Expand Down

0 comments on commit 28b5527

Please sign in to comment.