qualia_cooperative_core/authoring/package_review/
policy.rs1use super::{
2 super::{
3 policy::{text, valid_document, valid_window},
4 *,
5 },
6 *,
7};
8
9#[derive(Debug)]
10pub struct PackagePermit<'a> {
11 pub(super) source: AuthoringPermit<'a>,
12 pub(super) target: &'a PackageRevision,
13 pub(super) grant: Option<&'a PackageReviewGrant>,
14 pub(super) action: PackageAction,
15 pub(super) known_destination_lineage_complete: bool,
16}
17
18pub fn validate_package(
19 target: &PackageRevision,
20 limits: AuthoringLimits,
21) -> Result<(), AuthoringError> {
22 valid_document(&target.source, limits)?;
23 for value in [
24 &target.package_id,
25 &target.filename,
26 &target.producer_subject,
27 ] {
28 text(value, limits)?;
29 if value.trim().is_empty() {
30 return Err(AuthoringError::InvalidBinding);
31 }
32 }
33 if target.package_digest == [0; 32]
34 || target.package_bytes == 0
35 || target.predecessor_digest == Some([0; 32])
36 {
37 return Err(AuthoringError::InvalidBinding);
38 }
39 Ok(())
40}
41
42pub fn authorize_package<'a>(
45 view: &'a PackageAuthorityView<'a>,
46 target: &PackageRevision,
47 expected_generation: u64,
48 action: PackageAction,
49) -> Result<PackagePermit<'a>, AuthoringError> {
50 let source = authorize(
51 view.source,
52 &AuthoringRequest {
53 action: if action == PackageAction::Decide {
54 AuthoringAction::ReadSource
55 } else {
56 AuthoringAction::ContributionRecord
57 },
58 target: &target.source,
59 expected_authority_generation: expected_generation,
60 },
61 )?;
62 let limits = view.source.limits;
63 validate_package(view.current_package, limits)?;
64 validate_package(target, limits)?;
65 if target != view.current_package {
66 return Err(AuthoringError::StaleDocument);
67 }
68 if view.grants.len() > limits.max_review_grants || view.contributor_subjects.len() > 1024 {
69 return Err(AuthoringError::BudgetExceeded);
70 }
71 for subject in view.contributor_subjects {
72 text(subject, limits)?;
73 }
74 let subject = source.principal_subject();
75 let mut selected = None;
76 if action == PackageAction::Decide {
77 if target.producer_subject == subject
78 || target.source.author_subject.as_deref() == Some(subject)
79 || view.contributor_subjects.contains(&subject)
80 {
81 return Err(AuthoringError::SelfReview);
82 }
83 for grant in view.grants {
84 validate_package(&grant.target, limits)?;
85 for value in [&grant.id, &grant.reviewer_subject, &grant.admission_ref] {
86 text(value, limits)?;
87 }
88 if &grant.target != target || grant.reviewer_subject != subject {
89 continue;
90 }
91 if grant.revoked
92 || !valid_window(
93 view.source.now_unix,
94 grant.valid_from_unix,
95 grant.valid_until_unix,
96 )
97 || selected.is_some()
98 {
99 return Err(AuthoringError::ReviewDenied);
100 }
101 selected = Some(grant);
102 }
103 if selected.is_none() {
104 return Err(AuthoringError::ReviewDenied);
105 }
106 } else if target.producer_subject != subject {
107 return Err(AuthoringError::InvalidBinding);
108 }
109 Ok(PackagePermit {
110 source,
111 target: view.current_package,
112 grant: selected,
113 action,
114 known_destination_lineage_complete: view.known_destination_lineage_complete,
115 })
116}