Skip to content

Commit

Permalink
Jws header validation (decentralized-identity#172)
Browse files Browse the repository at this point in the history
* Refactor jws decentralized-identity#143

* update

* lint

---------

Co-authored-by: Kendall Weihe <[email protected]>
  • Loading branch information
2 people authored and enmand committed May 20, 2024
1 parent 9bcbfb3 commit c1e4e90
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ Cargo.lock

# -- developer added

.hermit/
.hermit/
.idea/
2 changes: 2 additions & 0 deletions crates/jws/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ pub enum JwsError {
AlgorithmNotFound(String),
#[error(transparent)]
CryptoError(#[from] CryptoError),
#[error("deserialization error {0}")]
MalformedHeader(String),
}

pub fn splice_parts(compact_jws: &str) -> Result<Vec<String>, JwsError> {
Expand Down
15 changes: 14 additions & 1 deletion crates/jws/src/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ pub enum JwsError {
SerdeJsonError(String),
#[error(transparent)]
DecodeError(#[from] DecodeError),
#[error("Malformed Header: {0}")]
MalformedHeader(String),
}

impl From<SerdeJsonError> for JwsError {
Expand Down Expand Up @@ -93,7 +95,18 @@ impl CompactJws {
pub async fn verify(compact_jws: &str) -> Result<JwsDecoded, JwsError> {
let jws_decoded = CompactJws::decode(compact_jws)?;

// TODO https://github.com/TBD54566975/web5-rs/issues/149
// Validate header fields
if jws_decoded.header.alg.is_empty() {
return Err(JwsError::MalformedHeader(
"alg field is required".to_string(),
));
}

if jws_decoded.header.kid.is_empty() {
return Err(JwsError::MalformedHeader(
"kid field is required for verification processing".to_string(),
));
}

let key_id = jws_decoded.header.kid.clone();
let did_uri = KeyIdFragment(key_id.clone()).splice_uri();
Expand Down

0 comments on commit c1e4e90

Please sign in to comment.