Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jws header validation #172

Merged
merged 4 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading