identity_credential/credential/
revocation_bitmap_status.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
// Copyright 2020-2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use std::str::FromStr;

use identity_core::common::Object;
use identity_core::common::Url;
use identity_core::common::Value;
use identity_did::DIDUrl;

use crate::credential::Status;
use crate::error::Error;
use crate::error::Result;

/// Information used to determine the current status of a [`Credential`][crate::credential::Credential]
/// using the `RevocationBitmap2022` specification.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct RevocationBitmapStatus(Status);

impl RevocationBitmapStatus {
  const INDEX_PROPERTY: &'static str = "revocationBitmapIndex";
  /// Type name of the revocation bitmap.
  pub const TYPE: &'static str = "RevocationBitmap2022";

  /// Creates a new `RevocationBitmapStatus`.
  ///
  /// The query of the `id` url is overwritten where "index" is set to `index`.
  ///
  /// # Example
  ///
  /// ```
  /// # use identity_credential::credential::RevocationBitmapStatus;
  /// # use identity_did::DIDUrl;
  /// # use identity_did::CoreDID;
  /// let did_url: DIDUrl = DIDUrl::parse("did:method:0xffff#revocation-1").unwrap();
  /// let status: RevocationBitmapStatus = RevocationBitmapStatus::new(did_url, 5);
  /// assert_eq!(
  ///   status.id().unwrap().to_string(),
  ///   "did:method:0xffff?index=5#revocation-1"
  /// );
  /// assert_eq!(status.index().unwrap(), 5);
  /// ```
  pub fn new(mut id: DIDUrl, index: u32) -> Self {
    id.set_query(Some(&format!("index={index}")))
      .expect("the string should be non-empty and a valid URL query");

    let mut object = Object::new();
    object.insert(Self::INDEX_PROPERTY.to_owned(), Value::String(index.to_string()));
    RevocationBitmapStatus(Status::new_with_properties(
      Url::from(id),
      Self::TYPE.to_owned(),
      object,
    ))
  }

  /// Returns the [`DIDUrl`] of the `RevocationBitmapStatus`, which should resolve
  /// to a `RevocationBitmap2022` service in a DID Document.
  pub fn id(&self) -> Result<DIDUrl> {
    DIDUrl::parse(self.0.id.as_str())
      .map_err(|err| Error::InvalidStatus(format!("invalid DID Url '{}': {:?}", self.0.id, err)))
  }

  /// Returns the index of the credential in the issuer's revocation bitmap if it can be decoded.
  pub fn index(&self) -> Result<u32> {
    if let Some(Value::String(index)) = self.0.properties.get(Self::INDEX_PROPERTY) {
      try_index_to_u32(index, Self::INDEX_PROPERTY)
    } else {
      Err(Error::InvalidStatus(format!(
        "expected {} to be an unsigned 32-bit integer expressed as a string",
        Self::INDEX_PROPERTY
      )))
    }
  }
}

impl TryFrom<Status> for RevocationBitmapStatus {
  type Error = Error;

  fn try_from(status: Status) -> Result<Self> {
    if status.type_ != Self::TYPE {
      return Err(Error::InvalidStatus(format!(
        "expected type '{}', got '{}'",
        Self::TYPE,
        status.type_
      )));
    }

    let revocation_bitmap_index: &Value =
      if let Some(revocation_bitmap_index) = status.properties.get(Self::INDEX_PROPERTY) {
        revocation_bitmap_index
      } else {
        return Err(Error::InvalidStatus(format!(
          "missing required property '{}'",
          Self::INDEX_PROPERTY
        )));
      };

    let revocation_bitmap_index: u32 = if let Value::String(index) = revocation_bitmap_index {
      try_index_to_u32(index, Self::INDEX_PROPERTY)?
    } else {
      return Err(Error::InvalidStatus(format!(
        "property '{}' is not a string",
        Self::INDEX_PROPERTY
      )));
    };

    // If the index query is present it must match the revocationBitmapIndex.
    // It is allowed not to be present to maintain backwards-compatibility
    // with an earlier version of the RevocationBitmap spec.
    for pair in status.id.query_pairs() {
      if pair.0 == "index" {
        let index: u32 = try_index_to_u32(pair.1.as_ref(), "value of index query")?;
        if index != revocation_bitmap_index {
          return Err(Error::InvalidStatus(format!(
            "value of index query `{index}` does not match revocationBitmapIndex `{revocation_bitmap_index}`"
          )));
        }
      }
    }

    Ok(Self(status))
  }
}

impl From<RevocationBitmapStatus> for Status {
  fn from(status: RevocationBitmapStatus) -> Self {
    status.0
  }
}

/// Attempts to convert the given index string to a u32.
pub fn try_index_to_u32(index: &str, name: &str) -> Result<u32> {
  u32::from_str(index).map_err(|err| {
    Error::InvalidStatus(format!(
      "{name} cannot be converted to an unsigned, 32-bit integer: {err}",
    ))
  })
}

#[cfg(test)]
mod tests {
  use identity_core::common::Object;
  use identity_core::common::Url;
  use identity_core::common::Value;
  use identity_core::convert::FromJson;
  use identity_did::DIDUrl;

  use crate::Error;

  use super::RevocationBitmapStatus;
  use super::Status;

  #[test]
  fn test_embedded_status_invariants() {
    let url: Url = Url::parse("did:method:0xabcd?index=0#revocation").unwrap();
    let did_url: DIDUrl = DIDUrl::parse(url.clone().into_string()).unwrap();
    let revocation_list_index: u32 = 0;
    let embedded_revocation_status: RevocationBitmapStatus =
      RevocationBitmapStatus::new(did_url, revocation_list_index);

    let object: Object = Object::from([(
      RevocationBitmapStatus::INDEX_PROPERTY.to_owned(),
      Value::String(revocation_list_index.to_string()),
    )]);
    let status: Status =
      Status::new_with_properties(url.clone(), RevocationBitmapStatus::TYPE.to_owned(), object.clone());
    assert_eq!(embedded_revocation_status, status.try_into().unwrap());

    let status_missing_property: Status =
      Status::new_with_properties(url.clone(), RevocationBitmapStatus::TYPE.to_owned(), Object::new());
    assert!(RevocationBitmapStatus::try_from(status_missing_property).is_err());

    let status_wrong_type: Status = Status::new_with_properties(url, "DifferentType".to_owned(), object);
    assert!(RevocationBitmapStatus::try_from(status_wrong_type).is_err());
  }

  #[test]
  fn test_revocation_bitmap_status_index_query() {
    // index is set.
    let did_url: DIDUrl = DIDUrl::parse("did:method:0xffff#rev-0").unwrap();
    let revocation_status: RevocationBitmapStatus = RevocationBitmapStatus::new(did_url, 250);
    assert_eq!(revocation_status.id().unwrap().query().unwrap(), "index=250");

    // index is overwritten.
    let did_url: DIDUrl = DIDUrl::parse("did:method:0xffff?index=300#rev-0").unwrap();
    let revocation_status: RevocationBitmapStatus = RevocationBitmapStatus::new(did_url, 250);
    assert_eq!(revocation_status.id().unwrap().query().unwrap(), "index=250");
  }

  #[test]
  fn test_revocation_bitmap_status_index_requirements() {
    // INVALID: index mismatch in id and property.
    let status: Status = Status::from_json_value(serde_json::json!({
      "id": "did:method:0xffff?index=10#rev-0",
      "type": RevocationBitmapStatus::TYPE,
      RevocationBitmapStatus::INDEX_PROPERTY: "5",
    }))
    .unwrap();

    assert!(matches!(
      RevocationBitmapStatus::try_from(status).unwrap_err(),
      Error::InvalidStatus(_)
    ));

    // VALID: index matches in id and property.
    let status: Status = Status::from_json_value(serde_json::json!({
      "id": "did:method:0xffff?index=5#rev-0",
      "type": RevocationBitmapStatus::TYPE,
      RevocationBitmapStatus::INDEX_PROPERTY: "5",
    }))
    .unwrap();
    assert!(RevocationBitmapStatus::try_from(status).is_ok());

    // VALID: missing index in id is allowed to be backwards-compatible.
    let status: Status = Status::from_json_value(serde_json::json!({
      "id": "did:method:0xffff#rev-0",
      "type": RevocationBitmapStatus::TYPE,
      RevocationBitmapStatus::INDEX_PROPERTY: "5",
    }))
    .unwrap();
    assert!(RevocationBitmapStatus::try_from(status).is_ok());
  }
}