identity_document/utils/
did_url_query.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// Copyright 2020-2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use std::borrow::Cow;

use identity_did::CoreDID;
use identity_did::DIDUrl;
use identity_did::RelativeDIDUrl;
use identity_did::DID;

/// Specifies a DIDUrl or fragment to query a service or method in a DID Document.
#[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[repr(transparent)]
pub struct DIDUrlQuery<'query>(Cow<'query, str>);

impl DIDUrlQuery<'_> {
  /// Returns whether this query matches the given DIDUrl.
  pub(crate) fn matches(&self, did_url: &DIDUrl) -> bool {
    // Ensure the DID matches if included in the query.
    if let Some(did_str) = self.did_str() {
      if did_str != did_url.did().as_str() {
        return false;
      }
    }

    // Compare fragments.
    match self.fragment().zip(did_url.fragment()) {
      Some((a, b)) => a == b,
      None => false,
    }
  }

  /// Extract the DID portion of the query if it exists.
  fn did_str(&self) -> Option<&str> {
    let query: &str = self.0.as_ref();
    if !query.starts_with(CoreDID::SCHEME) {
      return None;
    }

    // Find end of DID section.
    // did:example:123/path?service=agent&relativeRef=/credentials#degree
    let mut end_pos: usize = query.len();
    end_pos = end_pos.min(query.find('?').unwrap_or(end_pos));
    end_pos = end_pos.min(query.find('/').unwrap_or(end_pos));
    end_pos = end_pos.min(query.find('#').unwrap_or(end_pos));
    query.get(0..end_pos)
  }

  /// Extract the query fragment if it exists.
  fn fragment(&self) -> Option<&str> {
    let query: &str = self.0.as_ref();
    let fragment_maybe: Option<&str> = if query.starts_with(CoreDID::SCHEME) {
      // Extract the fragment from a full DID-Url-like string.
      query.rfind('#').and_then(|index| query.get(index + 1..))
    } else if let Some(fragment_delimiter_index) = query.rfind('#') {
      // Extract the fragment from a relative DID-Url.
      query.get(fragment_delimiter_index + 1..)
    } else {
      // Assume the entire string is a fragment.
      Some(query)
    };
    fragment_maybe.filter(|fragment| !fragment.is_empty())
  }
}

impl<'query> From<&'query str> for DIDUrlQuery<'query> {
  fn from(other: &'query str) -> Self {
    Self(Cow::Borrowed(other))
  }
}

impl<'query> From<&'query String> for DIDUrlQuery<'query> {
  fn from(other: &'query String) -> Self {
    Self(Cow::Borrowed(&**other))
  }
}

impl<'query> From<&'query DIDUrl> for DIDUrlQuery<'query> {
  fn from(other: &'query DIDUrl) -> Self {
    Self(Cow::Owned(other.to_string()))
  }
}

impl From<DIDUrl> for DIDUrlQuery<'_> {
  fn from(other: DIDUrl) -> Self {
    Self(Cow::Owned(other.to_string()))
  }
}

impl<'query> From<&'query RelativeDIDUrl> for DIDUrlQuery<'query> {
  fn from(other: &'query RelativeDIDUrl) -> Self {
    Self(Cow::Owned(other.to_string()))
  }
}

#[cfg(test)]
mod tests {
  use identity_did::DIDUrl;
  use std::ops::Not;

  use super::*;

  #[test]
  fn test_did_str() {
    assert!(DIDUrlQuery::from("").did_str().is_none());
    assert!(DIDUrlQuery::from("fragment").did_str().is_none());
    assert!(DIDUrlQuery::from("#fragment").did_str().is_none());
    assert!(DIDUrlQuery::from("?query").did_str().is_none());
    assert!(DIDUrlQuery::from("/path").did_str().is_none());
    assert!(DIDUrlQuery::from("/path?query#fragment").did_str().is_none());
    assert!(DIDUrlQuery::from("method:missingscheme123").did_str().is_none());
    assert!(DIDUrlQuery::from("iota:example").did_str().is_none());
    assert_eq!(
      DIDUrlQuery::from("did:iota:example").did_str(),
      Some("did:iota:example")
    );
    assert_eq!(
      DIDUrlQuery::from("did:iota:example#fragment").did_str(),
      Some("did:iota:example")
    );
    assert_eq!(
      DIDUrlQuery::from("did:iota:example?query").did_str(),
      Some("did:iota:example")
    );
    assert_eq!(
      DIDUrlQuery::from("did:iota:example/path").did_str(),
      Some("did:iota:example")
    );
    assert_eq!(
      DIDUrlQuery::from("did:iota:example/path?query#fragment").did_str(),
      Some("did:iota:example")
    );
    assert_eq!(
      DIDUrlQuery::from("did:iota:example/path?query&relativeRef=/#fragment").did_str(),
      Some("did:iota:example")
    );
  }

  #[test]
  fn test_fragment() {
    assert!(DIDUrlQuery::from("").fragment().is_none());
    assert_eq!(DIDUrlQuery::from("fragment").fragment(), Some("fragment"));
    assert_eq!(DIDUrlQuery::from("#fragment").fragment(), Some("fragment"));
    assert_eq!(DIDUrlQuery::from("/path?query#fragment").fragment(), Some("fragment"));
    assert!(DIDUrlQuery::from("did:iota:example").fragment().is_none());
    assert_eq!(
      DIDUrlQuery::from("did:iota:example#fragment").fragment(),
      Some("fragment")
    );
    assert!(DIDUrlQuery::from("did:iota:example?query").fragment().is_none());
    assert!(DIDUrlQuery::from("did:iota:example/path").fragment().is_none());
    assert_eq!(
      DIDUrlQuery::from("did:iota:example/path?query#fragment").fragment(),
      Some("fragment")
    );
    assert_eq!(
      DIDUrlQuery::from("did:iota:example/path?query&relativeRef=/#fragment").fragment(),
      Some("fragment")
    );
  }

  #[test]
  fn test_matches() {
    let did_base: DIDUrl = DIDUrl::parse("did:iota:example").unwrap();
    let did_path: DIDUrl = DIDUrl::parse("did:iota:example/path").unwrap();
    let did_query: DIDUrl = DIDUrl::parse("did:iota:example?query").unwrap();
    let did_fragment: DIDUrl = DIDUrl::parse("did:iota:example#fragment").unwrap();
    let did_different_fragment: DIDUrl = DIDUrl::parse("did:iota:example#differentfragment").unwrap();
    let did_url: DIDUrl = DIDUrl::parse("did:iota:example/path?query#fragment").unwrap();
    let did_url_complex: DIDUrl = DIDUrl::parse("did:iota:example/path?query&relativeRef=/#fragment").unwrap();

    // INVALID: empty query should not match anything.
    {
      let query_empty = DIDUrlQuery::from("");
      assert!(query_empty.matches(&did_base).not());
      assert!(query_empty.matches(&did_path).not());
      assert!(query_empty.matches(&did_query).not());
      assert!(query_empty.matches(&did_fragment).not());
      assert!(query_empty.matches(&did_different_fragment).not());
      assert!(query_empty.matches(&did_url).not());
      assert!(query_empty.matches(&did_url_complex).not());
    }

    // VALID: query with only a fragment should match the same fragment.
    {
      let query_fragment_only = DIDUrlQuery::from("fragment");
      assert!(query_fragment_only.matches(&did_base).not());
      assert!(query_fragment_only.matches(&did_path).not());
      assert!(query_fragment_only.matches(&did_query).not());
      assert!(query_fragment_only.matches(&did_fragment));
      assert!(query_fragment_only.matches(&did_different_fragment).not());
      assert!(query_fragment_only.matches(&did_url));
      assert!(query_fragment_only.matches(&did_url_complex));
    }

    // VALID: query with differentfragment should only match the same fragment.
    {
      let query_different_fragment = DIDUrlQuery::from("differentfragment");
      assert!(query_different_fragment.matches(&did_base).not());
      assert!(query_different_fragment.matches(&did_path).not());
      assert!(query_different_fragment.matches(&did_query).not());
      assert!(query_different_fragment.matches(&did_fragment).not());
      assert!(query_different_fragment.matches(&did_different_fragment));
      assert!(query_different_fragment.matches(&did_url).not());
      assert!(query_different_fragment.matches(&did_url_complex).not());
    }

    // VALID: query with a #fragment should match the same fragment.
    {
      let query_fragment_delimiter = DIDUrlQuery::from("#fragment");
      assert!(query_fragment_delimiter.matches(&did_base).not());
      assert!(query_fragment_delimiter.matches(&did_path).not());
      assert!(query_fragment_delimiter.matches(&did_query).not());
      assert!(query_fragment_delimiter.matches(&did_fragment));
      assert!(query_fragment_delimiter.matches(&did_different_fragment).not());
      assert!(query_fragment_delimiter.matches(&did_url));
      assert!(query_fragment_delimiter.matches(&did_url_complex));
    }

    // VALID: query with a relative DID Url should match the same fragment.
    {
      let query_relative_did_url = DIDUrlQuery::from("/path?query#fragment");
      assert!(query_relative_did_url.matches(&did_base).not());
      assert!(query_relative_did_url.matches(&did_path).not());
      assert!(query_relative_did_url.matches(&did_query).not());
      assert!(query_relative_did_url.matches(&did_fragment));
      assert!(query_relative_did_url.matches(&did_different_fragment).not());
      assert!(query_relative_did_url.matches(&did_url));
      assert!(query_relative_did_url.matches(&did_url_complex));
    }

    // INVALID: query with DID and no fragment should not match anything.
    {
      let query_did = DIDUrlQuery::from("did:iota:example");
      assert!(query_did.matches(&did_base).not());
      assert!(query_did.matches(&did_path).not());
      assert!(query_did.matches(&did_query).not());
      assert!(query_did.matches(&did_fragment).not());
      assert!(query_did.matches(&did_different_fragment).not());
      assert!(query_did.matches(&did_url).not());
      assert!(query_did.matches(&did_url_complex).not());
    }

    // VALID: query with a DID fragment should match the same fragment.
    {
      let query_did_fragment = DIDUrlQuery::from("did:iota:example#fragment");
      assert!(query_did_fragment.matches(&did_base).not());
      assert!(query_did_fragment.matches(&did_path).not());
      assert!(query_did_fragment.matches(&did_query).not());
      assert!(query_did_fragment.matches(&did_fragment));
      assert!(query_did_fragment.matches(&did_different_fragment).not());
      assert!(query_did_fragment.matches(&did_url));
      assert!(query_did_fragment.matches(&did_url_complex));
    }

    // VALID: query with a DID Url with a fragment should match the same fragment.
    {
      let query_did_fragment = DIDUrlQuery::from("did:iota:example/path?query#fragment");
      assert!(query_did_fragment.matches(&did_base).not());
      assert!(query_did_fragment.matches(&did_path).not());
      assert!(query_did_fragment.matches(&did_query).not());
      assert!(query_did_fragment.matches(&did_fragment));
      assert!(query_did_fragment.matches(&did_different_fragment).not());
      assert!(query_did_fragment.matches(&did_url));
      assert!(query_did_fragment.matches(&did_url_complex));
    }

    // VALID: query with a complex DID Url with a fragment should match the same fragment.
    {
      let query_did_fragment = DIDUrlQuery::from("did:iota:example/path?query&relativeRef=/#fragment");
      assert!(query_did_fragment.matches(&did_base).not());
      assert!(query_did_fragment.matches(&did_path).not());
      assert!(query_did_fragment.matches(&did_query).not());
      assert!(query_did_fragment.matches(&did_fragment));
      assert!(query_did_fragment.matches(&did_different_fragment).not());
      assert!(query_did_fragment.matches(&did_url));
      assert!(query_did_fragment.matches(&did_url_complex));
    }
  }
}