Skip to main content

subc_protocol/
consent.rs

1//! Shared consent/v1 plan card bodies for the daemon requester and responders.
2//!
3//! A reply can approve only the pending request on the daemon-owned connection,
4//! after that connection's trust and active registration are rechecked. Matching
5//! these body fields is necessary, never sufficient authority. Rendering a card
6//! or asserting that a terminal showed a diff is not approval.
7
8use serde::{Deserialize, Serialize};
9pub use subc_config::{CanonicalError, Digest, InputVector, Plan};
10
11pub const CONSENT_V1: &str = "consent/v1";
12
13#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
14enum ConsentProtocol {
15    #[serde(rename = "consent/v1")]
16    V1,
17}
18
19crate::config::open_string_enum! {
20    /// Only an explicit approved answer can approve; future decisions fail closed.
21    ConsentDecision { Approved => "approved", Declined => "declined" }
22}
23impl ConsentDecision {
24    pub fn is_approved(&self) -> bool {
25        matches!(self, Self::Approved)
26    }
27}
28
29/// Canonical plan view and exact approval binding, not a caller-composed summary.
30#[non_exhaustive]
31#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
32pub struct ConsentRequest {
33    protocol: ConsentProtocol,
34    pub plan_digest: Digest,
35    pub base_revision: u64,
36    pub input_vector: InputVector,
37    pub plan: Plan,
38}
39impl ConsentRequest {
40    /// Builds the request from the plan alone, so its digest, revision and
41    /// input vector always describe the plan the card shows.
42    pub fn new(plan: Plan) -> Result<Self, CanonicalError> {
43        Ok(Self {
44            protocol: ConsentProtocol::V1,
45            plan_digest: plan.digest()?,
46            base_revision: plan.base_revision,
47            input_vector: plan.input_vector.clone(),
48            plan,
49        })
50    }
51
52    /// Whether the binding fields describe the carried plan. A responder shows
53    /// and answers only a consistent request, so a card can never display one
54    /// plan while its approval binds another.
55    pub fn is_consistent(&self) -> bool {
56        self.plan
57            .digest()
58            .is_ok_and(|digest| digest == self.plan_digest)
59            && self.base_revision == self.plan.base_revision
60            && self.input_vector == self.plan.input_vector
61    }
62}
63
64/// A response echoes every approval binding. Unknown decisions decode for
65/// diagnostics but never approve. Decline, timeout or disconnect applies nothing.
66#[non_exhaustive]
67#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
68pub struct ConsentResponse {
69    protocol: ConsentProtocol,
70    pub plan_digest: Digest,
71    pub base_revision: u64,
72    pub input_vector: InputVector,
73    pub decision: ConsentDecision,
74}
75impl ConsentResponse {
76    pub fn new(request: &ConsentRequest, decision: ConsentDecision) -> Self {
77        Self {
78            protocol: ConsentProtocol::V1,
79            plan_digest: request.plan_digest.clone(),
80            base_revision: request.base_revision,
81            input_vector: request.input_vector.clone(),
82            decision,
83        }
84    }
85
86    /// Check body binding only. The caller must additionally authenticate the
87    /// response connection and the still-pending request before consuming it.
88    pub fn approves(&self, request: &ConsentRequest) -> bool {
89        self.decision.is_approved()
90            && self.plan_digest == request.plan_digest
91            && self.base_revision == request.base_revision
92            && self.input_vector == request.input_vector
93    }
94}