Skip to content

Commit

Permalink
Fix did:web media type.
Browse files Browse the repository at this point in the history
  • Loading branch information
timothee-haudebourg committed Oct 31, 2024
1 parent 164ac45 commit 229d7be
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
8 changes: 8 additions & 0 deletions crates/dids/core/src/document/representation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ impl MediaType {
pub fn into_name(self) -> &'static str {
self.name()
}

pub fn from_bytes(s: &[u8]) -> Result<Self, Unknown> {
match s {
b"application/did+json" => Ok(Self::Json),
b"application/did+ld+json" => Ok(Self::JsonLd),
unknown => Err(Unknown(String::from_utf8_lossy(unknown).into_owned())),
}
}
}

impl From<MediaType> for String {
Expand Down
14 changes: 12 additions & 2 deletions crates/dids/methods/web/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,16 @@ impl DIDMethodResolver for DIDWeb {
}
})?;

let media_type = resp
.headers()
.get(header::CONTENT_TYPE)
.map(|value| match value.as_bytes() {
b"application/json" => Ok(MediaType::Json),
other => MediaType::from_bytes(other),
})
.transpose()?
.unwrap_or(MediaType::Json);

let document = resp
.bytes()
.await
Expand All @@ -129,7 +139,7 @@ impl DIDMethodResolver for DIDWeb {
Ok(Output {
document: document.into(),
document_metadata: ssi_dids_core::document::Metadata::default(),
metadata: resolution::Metadata::from_content_type(Some(MediaType::JsonLd.to_string())),
metadata: resolution::Metadata::from_content_type(Some(media_type.to_string())),
})
}
}
Expand Down Expand Up @@ -232,7 +242,7 @@ mod tests {
proxy.replace(Some(url));
});
let doc = DIDWeb.resolve(did!("did:web:localhost")).await.unwrap();
let doc_expected = Document::from_bytes(MediaType::JsonLd, DID_JSON.as_bytes()).unwrap();
let doc_expected = Document::from_bytes(MediaType::Json, DID_JSON.as_bytes()).unwrap();
assert_eq!(doc.document.document(), doc_expected.document());
PROXY.with(|proxy| {
proxy.replace(None);
Expand Down

0 comments on commit 229d7be

Please sign in to comment.