xdid_method_web/lib.rs
1//! [xdid](https://github.com/unavi-xyz/xdid) implementation of [did:web](https://w3c-ccg.github.io/did-method-web/).
2
3#[cfg(all(not(feature = "tls-ring"), not(feature = "tls-aws-lc-rs")))]
4compile_error!("xdid-method-web: enable `tls-ring` or `tls-aws-lc-rs`");
5
6use std::time::Duration;
7
8use reqwest::Client;
9use thiserror::Error;
10use xdid_core::{
11 Method,
12 MethodFuture,
13 ResolutionError,
14 did::Did,
15 document::Document,
16};
17
18use crate::target::TargetPolicy;
19
20mod client;
21mod parse;
22mod resolve;
23pub mod target;
24
25const NAME: &str = "web";
26
27/// Limits applied while resolving. The defaults assume DIDs arrive from
28/// untrusted input.
29///
30/// A browser build gets the transport the browser gives it, which exposes no
31/// controls for redirects, timeouts or address selection. Each field below says
32/// what survives there.
33#[derive(Debug, Clone)]
34pub struct Config {
35 /// Rejects documents larger than this. A native build streams the body, so
36 /// an oversized or dishonestly-framed response is abandoned rather than
37 /// buffered; a browser build has already buffered it and can only check
38 /// afterwards.
39 pub max_document_bytes: u64,
40 /// Ignored by a browser build.
41 pub connect_timeout: Duration,
42 /// Ignored by a browser build.
43 pub request_timeout: Duration,
44 /// Which hosts a resolution may reach.
45 ///
46 /// [`TargetPolicy::PublicOnly`] also disables the system proxy, since a
47 /// proxy resolves the target host itself and would bypass the address
48 /// checks.
49 ///
50 /// A browser build applies the scheme rule and refuses a literal address,
51 /// but cannot judge the address a hostname resolves to.
52 pub target: TargetPolicy,
53}
54
55impl Default for Config {
56 fn default() -> Self {
57 Self {
58 max_document_bytes: 64 * 1024,
59 connect_timeout: Duration::from_secs(5),
60 request_timeout: Duration::from_secs(10),
61 target: TargetPolicy::PublicOnly,
62 }
63 }
64}
65
66/// Keeps `reqwest` out of the public API, where its version would otherwise be
67/// part of this crate's semver contract.
68#[derive(Debug, Error)]
69#[error("failed to build the HTTP client: {0}")]
70pub struct ClientError(String);
71
72pub struct MethodDidWeb {
73 client: Client,
74 config: Config,
75}
76
77impl MethodDidWeb {
78 /// Create a new did:web resolver.
79 ///
80 /// # Errors
81 ///
82 /// Returns an error if the HTTP client cannot be constructed.
83 pub fn new() -> Result<Self, ClientError> {
84 Self::with_config(Config::default())
85 }
86
87 /// Create a new did:web resolver with the given [`Config`].
88 ///
89 /// # Errors
90 ///
91 /// Returns an error if the HTTP client cannot be constructed.
92 pub fn with_config(config: Config) -> Result<Self, ClientError> {
93 let client = client::build(&config)?;
94 Ok(Self { client, config })
95 }
96}
97
98impl Method for MethodDidWeb {
99 fn method_name(&self) -> &'static str {
100 NAME
101 }
102
103 fn resolve<'a>(&'a self, did: &'a Did) -> MethodFuture<'a, Result<Document, ResolutionError>> {
104 cfg_select! {
105 target_family = "wasm" => Box::pin(resolve::bridged(
106 self.client.clone(),
107 self.config.clone(),
108 did.clone(),
109 )),
110 _ => Box::pin(resolve::document(&self.client, &self.config, did)),
111 }
112 }
113}