Skip to content

Commit 20929de

Browse files
committed
wip
1 parent 6d933fb commit 20929de

File tree

2 files changed

+103
-142
lines changed

2 files changed

+103
-142
lines changed

rust/signed_doc/src/metadata/extra_fields.rs

Lines changed: 102 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use catalyst_types::{problem_report::ProblemReport, uuid::UuidV4};
44
use coset::{cbor::Value, Label, ProtectedHeader};
55

6-
use super::{cose_protected_header_find, decode_cbor_uuid, encode_cbor_uuid, DocumentRef};
6+
use super::{cose_protected_header_find, decode_cbor_uuid, encode_cbor_uuid, DocumentRef, Section};
77

88
/// `ref` field COSE key value
99
const REF_KEY: &str = "ref";
@@ -40,7 +40,7 @@ pub struct ExtraFields {
4040
reply: Option<DocumentRef>,
4141
/// Reference to the document section.
4242
#[serde(skip_serializing_if = "Option::is_none")]
43-
section: Option<String>,
43+
section: Option<Section>,
4444
/// Reference to the document collaborators. Collaborator type is TBD.
4545
#[serde(default = "Vec::new", skip_serializing_if = "Vec::is_empty")]
4646
collabs: Vec<String>,
@@ -79,7 +79,7 @@ impl ExtraFields {
7979

8080
/// Return `section` field.
8181
#[must_use]
82-
pub fn section(&self) -> Option<&String> {
82+
pub fn section(&self) -> Option<&Section> {
8383
self.section.as_ref()
8484
}
8585

@@ -128,7 +128,7 @@ impl ExtraFields {
128128
}
129129

130130
if let Some(section) = &self.section {
131-
builder = builder.text_value(SECTION_KEY.to_string(), Value::Text(section.clone()));
131+
builder = builder.text_value(SECTION_KEY.to_string(), Value::from(section.clone()));
132132
}
133133

134134
if !self.collabs.is_empty() {
@@ -171,178 +171,151 @@ impl ExtraFields {
171171
if let Some(cbor_doc_ref) =
172172
cose_protected_header_find(protected, |key| key == &Label::Text(REF_KEY.to_string()))
173173
{
174-
match DocumentRef::try_from(cbor_doc_ref) {
175-
Ok(doc_ref) => {
176-
extra.doc_ref = Some(doc_ref);
177-
},
178-
Err(e) => {
179-
error_report.conversion_error(
180-
"CBOR COSE protected header doc ref",
181-
&format!("{cbor_doc_ref:?}"),
182-
&format!("Expected DocumentRef: {e}"),
183-
&format!("{CONTEXT}, DocumentRef"),
184-
);
185-
},
186-
}
174+
if let Ok(doc_ref) = DocumentRef::try_from(cbor_doc_ref) {
175+
extra.doc_ref = Some(doc_ref);
176+
} else {
177+
error_report.conversion_error(
178+
"CBOR COSE protected header doc ref",
179+
&format!("{cbor_doc_ref:?}"),
180+
&format!("Expected a CBOR DocumentRef"),
181+
&format!("{CONTEXT}, DocumentRef"),
182+
);
183+
};
187184
}
188185

189186
if let Some(cbor_doc_template) = cose_protected_header_find(protected, |key| {
190187
key == &Label::Text(TEMPLATE_KEY.to_string())
191188
}) {
192-
match DocumentRef::try_from(cbor_doc_template) {
193-
Ok(doc_template) => {
194-
extra.template = Some(doc_template);
195-
},
196-
Err(e) => {
197-
error_report.conversion_error(
198-
"CBOR COSE protected header document template",
199-
&format!("{cbor_doc_template:?}"),
200-
&format!("Expected DocumentRef: {e}"),
201-
&format!("{CONTEXT}, DocumentRef"),
202-
);
203-
},
189+
if let Ok(doc_template) = DocumentRef::try_from(cbor_doc_template) {
190+
extra.template = Some(doc_template);
191+
} else {
192+
error_report.conversion_error(
193+
"CBOR COSE protected header document template",
194+
&format!("{cbor_doc_template:?}"),
195+
&format!("Expected a CBOR DocumentRef"),
196+
&format!("{CONTEXT}, DocumentRef"),
197+
);
204198
}
205199
}
206200

207201
if let Some(cbor_doc_reply) =
208202
cose_protected_header_find(protected, |key| key == &Label::Text(REPLY_KEY.to_string()))
209203
{
210-
match DocumentRef::try_from(cbor_doc_reply) {
211-
Ok(doc_reply) => {
212-
extra.reply = Some(doc_reply);
213-
},
214-
Err(e) => {
215-
error_report.conversion_error(
216-
"CBOR COSE protected header document reply",
217-
&format!("{cbor_doc_reply:?}"),
218-
&format!("Expected DocumentRef: {e}"),
219-
&format!("{CONTEXT}, DocumentRef"),
220-
);
221-
},
204+
if let Ok(doc_reply) = DocumentRef::try_from(cbor_doc_reply) {
205+
extra.reply = Some(doc_reply);
206+
} else {
207+
error_report.conversion_error(
208+
"CBOR COSE protected header document reply",
209+
&format!("{cbor_doc_reply:?}"),
210+
&format!("Expected a CBOR DocumentRef"),
211+
&format!("{CONTEXT}, DocumentRef"),
212+
);
222213
}
223214
}
224215

225216
if let Some(cbor_doc_section) = cose_protected_header_find(protected, |key| {
226217
key == &Label::Text(SECTION_KEY.to_string())
227218
}) {
228-
match cbor_doc_section.clone().into_text() {
229-
Ok(doc_section) => {
230-
extra.section = Some(doc_section);
231-
},
232-
Err(e) => {
233-
error_report.conversion_error(
234-
"COSE protected header document section",
235-
&format!("{cbor_doc_section:?}"),
236-
&format!("Expected String: {e:?}"),
237-
&format!("{CONTEXT}, converting document section to String"),
238-
);
239-
},
219+
if let Ok(section) = Section::try_from(cbor_doc_section) {
220+
extra.section = Some(section);
221+
} else {
222+
error_report.conversion_error(
223+
"COSE protected header document section",
224+
&format!("{cbor_doc_section:?}"),
225+
&format!("Must be a valid CBOR encoded String JSON Path"),
226+
&format!("{CONTEXT}, converting document section to String JSON Path"),
227+
);
240228
}
241229
}
242230

243231
if let Some(cbor_doc_collabs) = cose_protected_header_find(protected, |key| {
244232
key == &Label::Text(COLLABS_KEY.to_string())
245233
}) {
246-
match cbor_doc_collabs.clone().into_array() {
247-
Ok(collabs) => {
248-
let mut c = Vec::new();
249-
for (ids, collaborator) in collabs.iter().cloned().enumerate() {
250-
match collaborator.clone().into_text() {
251-
Ok(collaborator) => {
252-
c.push(collaborator);
253-
},
254-
Err(e) => {
255-
error_report.conversion_error(
256-
&format!("COSE protected header collaborator index {ids}"),
257-
&format!("{collaborator:?}"),
258-
&format!("Expected String: {e:?}"),
259-
&format!("{CONTEXT}, converting collaborator to String"),
260-
);
261-
},
262-
}
234+
if let Ok(collabs) = cbor_doc_collabs.clone().into_array() {
235+
let mut c = Vec::new();
236+
for (ids, collaborator) in collabs.iter().cloned().enumerate() {
237+
match collaborator.clone().into_text() {
238+
Ok(collaborator) => {
239+
c.push(collaborator);
240+
},
241+
Err(_) => {
242+
error_report.conversion_error(
243+
&format!("COSE protected header collaborator index {ids}"),
244+
&format!("{collaborator:?}"),
245+
&format!("Expected a CBOR String"),
246+
&format!("{CONTEXT}, converting collaborator to String"),
247+
);
248+
},
263249
}
264-
extra.collabs = c;
265-
},
266-
Err(e) => {
267-
error_report.conversion_error(
268-
"CBOR COSE protected header collaborators",
269-
&format!("{cbor_doc_collabs:?}"),
270-
&format!("Expected Array: {e:?}"),
271-
&format!("{CONTEXT}, converting collaborators to Array"),
272-
);
273-
},
274-
}
250+
}
251+
extra.collabs = c;
252+
} else {
253+
error_report.conversion_error(
254+
"CBOR COSE protected header collaborators",
255+
&format!("{cbor_doc_collabs:?}"),
256+
&format!("Expected a CBOR Array"),
257+
&format!("{CONTEXT}, converting collaborators to Array"),
258+
);
259+
};
275260
}
276261

277262
if let Some(cbor_doc_brand_id) = cose_protected_header_find(protected, |key| {
278263
key == &Label::Text(BRAND_ID_KEY.to_string())
279264
}) {
280-
match DocumentRef::try_from(cbor_doc_brand_id) {
281-
Ok(brand_id) => {
282-
extra.brand_id = Some(brand_id);
283-
},
284-
Err(e) => {
285-
error_report.conversion_error(
286-
"CBOR COSE protected header brand ID",
287-
&format!("{cbor_doc_brand_id:?}"),
288-
&format!("Expected UUID: {e:?}"),
289-
&format!("{CONTEXT}, decoding CBOR UUID for brand ID"),
290-
);
291-
},
265+
if let Ok(brand_id) = DocumentRef::try_from(cbor_doc_brand_id) {
266+
extra.brand_id = Some(brand_id);
267+
} else {
268+
error_report.conversion_error(
269+
"CBOR COSE protected header brand ID",
270+
&format!("{cbor_doc_brand_id:?}"),
271+
&format!("Expected a CBOR UUID"),
272+
&format!("{CONTEXT}, decoding CBOR UUID for brand ID"),
273+
);
292274
}
293275
}
294276

295277
if let Some(cbor_doc_campaign_id) = cose_protected_header_find(protected, |key| {
296278
key == &Label::Text(CAMPAIGN_ID_KEY.to_string())
297279
}) {
298-
match DocumentRef::try_from(cbor_doc_campaign_id) {
299-
Ok(campaign_id) => {
300-
extra.campaign_id = Some(campaign_id);
301-
},
302-
Err(e) => {
303-
error_report.conversion_error(
304-
"CBOR COSE protected header campaign ID",
305-
&format!("{cbor_doc_campaign_id:?}"),
306-
&format!("Expected UUID: {e:?}"),
307-
&format!("{CONTEXT}, decoding CBOR UUID for campaign ID"),
308-
);
309-
},
280+
if let Ok(campaign_id) = DocumentRef::try_from(cbor_doc_campaign_id) {
281+
extra.campaign_id = Some(campaign_id);
282+
} else {
283+
error_report.conversion_error(
284+
"CBOR COSE protected header campaign ID",
285+
&format!("{cbor_doc_campaign_id:?}"),
286+
&format!("Expected a CBOR UUID"),
287+
&format!("{CONTEXT}, decoding CBOR UUID for campaign ID"),
288+
);
310289
}
311290
}
312291

313292
if let Some(cbor_doc_election_id) = cose_protected_header_find(protected, |key| {
314293
key == &Label::Text(ELECTION_ID_KEY.to_string())
315294
}) {
316-
match decode_cbor_uuid(cbor_doc_election_id.clone()) {
317-
Ok(election_id) => {
318-
extra.election_id = Some(election_id);
319-
},
320-
Err(e) => {
321-
error_report.conversion_error(
322-
"CBOR COSE protected header election ID",
323-
&format!("{cbor_doc_election_id:?}"),
324-
&format!("Expected UUID: {e:?}"),
325-
&format!("{CONTEXT}, decoding CBOR UUID for election ID"),
326-
);
327-
},
295+
if let Ok(election_id) = decode_cbor_uuid(cbor_doc_election_id.clone()) {
296+
extra.election_id = Some(election_id);
297+
} else {
298+
error_report.conversion_error(
299+
"CBOR COSE protected header election ID",
300+
&format!("{cbor_doc_election_id:?}"),
301+
&format!("Expected a CBOR UUID"),
302+
&format!("{CONTEXT}, decoding CBOR UUID for election ID"),
303+
);
328304
}
329305
}
330306

331307
if let Some(cbor_doc_category_id) = cose_protected_header_find(protected, |key| {
332308
key == &Label::Text(CATEGORY_ID_KEY.to_string())
333309
}) {
334-
match DocumentRef::try_from(cbor_doc_category_id) {
335-
Ok(category_id) => {
336-
extra.category_id = Some(category_id);
337-
},
338-
Err(e) => {
339-
error_report.conversion_error(
340-
"CBOR COSE protected header category ID",
341-
&format!("{cbor_doc_category_id:?}"),
342-
&format!("Expected UUID: {e:?}"),
343-
&format!("{CONTEXT}, decoding CBOR UUID for category ID"),
344-
);
345-
},
310+
if let Ok(category_id) = DocumentRef::try_from(cbor_doc_category_id) {
311+
extra.category_id = Some(category_id);
312+
} else {
313+
error_report.conversion_error(
314+
"CBOR COSE protected header category ID",
315+
&format!("{cbor_doc_category_id:?}"),
316+
&format!("Expected a CBOR UUID"),
317+
&format!("{CONTEXT}, decoding CBOR UUID for category ID"),
318+
);
346319
}
347320
}
348321

rust/signed_doc/src/validator/rules/section.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
//! `section` rule type impl.
22
3-
use std::str::FromStr;
4-
53
use futures::{future::BoxFuture, FutureExt};
64

75
use crate::{
@@ -20,17 +18,7 @@ where Provider: 'static + CatalystSignedDocumentProvider
2018
&'a self, doc: &'a CatalystSignedDocument, _provider: &'a Provider,
2119
) -> BoxFuture<'a, anyhow::Result<bool>> {
2220
async {
23-
if let Some(section) = doc.doc_meta().section() {
24-
if jsonpath_rust::JsonPath::<serde_json::Value>::from_str(section).is_err() {
25-
doc.report().invalid_value(
26-
"template",
27-
section,
28-
"Must be a valid JSON Path",
29-
"Invalid referenced template document type",
30-
);
31-
return Ok(false);
32-
}
33-
} else if !self.optional {
21+
if doc.doc_meta().section().is_none() && !self.optional {
3422
doc.report()
3523
.missing_field("section", "Document must have a section field");
3624
return Ok(false);

0 commit comments

Comments
 (0)