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

Fix did:web media type. #625

Merged
merged 1 commit into from
Oct 31, 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
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