From 229d7be78eb5aeeb1d153730349e0ec583a90299 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Haudebourg?= Date: Thu, 31 Oct 2024 18:30:24 +0100 Subject: [PATCH] Fix `did:web` media type. --- crates/dids/core/src/document/representation.rs | 8 ++++++++ crates/dids/methods/web/src/lib.rs | 14 ++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/crates/dids/core/src/document/representation.rs b/crates/dids/core/src/document/representation.rs index 630bfb82f..8ac7a4d8d 100644 --- a/crates/dids/core/src/document/representation.rs +++ b/crates/dids/core/src/document/representation.rs @@ -108,6 +108,14 @@ impl MediaType { pub fn into_name(self) -> &'static str { self.name() } + + pub fn from_bytes(s: &[u8]) -> Result { + 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 for String { diff --git a/crates/dids/methods/web/src/lib.rs b/crates/dids/methods/web/src/lib.rs index 48afe4f3e..eb32545b5 100644 --- a/crates/dids/methods/web/src/lib.rs +++ b/crates/dids/methods/web/src/lib.rs @@ -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 @@ -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())), }) } } @@ -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);