Skip to main content

khive_capability/
validate.rs

1//! Library request validation over explicitly classified operations.
2//!
3//! Runtime integrations must opt into this adapter; this crate does not wire a gate.
4
5use lion_core::{CapId, PluginId, Right};
6
7use crate::capability::CapabilityManager;
8use crate::error::KernelError;
9
10pub struct RequestValidator<'a> {
11    caps: &'a CapabilityManager,
12}
13
14impl<'a> RequestValidator<'a> {
15    pub fn new(caps: &'a CapabilityManager) -> Self {
16        Self { caps }
17    }
18
19    pub fn check(
20        &self,
21        plugin_id: PluginId,
22        cap_id: CapId,
23        namespace: &str,
24        operation: Operation,
25    ) -> Result<(), KernelError> {
26        let right = operation.required_right();
27        self.caps.validate(plugin_id, cap_id, namespace, right)
28    }
29}
30
31#[derive(Debug, Clone, Copy)]
32pub enum Operation {
33    Read,
34    Create,
35    Update,
36    Delete,
37    Search,
38    Link,
39    Traverse,
40    Send,
41    Recall,
42    Remember,
43    Assign,
44    Complete,
45    /// Read-only graph export.
46    Export,
47}
48
49impl Operation {
50    pub fn required_right(self) -> Right {
51        match self {
52            Self::Read | Self::Search | Self::Traverse | Self::Recall | Self::Export => Right::Read,
53            Self::Create | Self::Remember | Self::Assign | Self::Link => Right::Create,
54            Self::Update | Self::Complete => Right::Write,
55            Self::Delete => Right::Delete,
56            Self::Send => Right::Send,
57        }
58    }
59
60    /// Collapse the same operation classification to Read/Write binding rights.
61    ///
62    /// Read-only operations require Read; all mutations require Write. This is
63    /// distinct from the finer Create/Delete/Send bits of `required_right`.
64    /// The exhaustive match refuses to silently classify a new operation.
65    pub fn binding_right(self) -> Right {
66        match self {
67            Self::Read | Self::Search | Self::Traverse | Self::Recall | Self::Export => Right::Read,
68            Self::Create
69            | Self::Remember
70            | Self::Assign
71            | Self::Link
72            | Self::Update
73            | Self::Complete
74            | Self::Delete
75            | Self::Send => Right::Write,
76        }
77    }
78}
79
80#[cfg(test)]
81mod tests {
82    use super::*;
83
84    /// Every `Operation` variant must classify to exactly one binding right.
85    /// This is enforced at compile time by `binding_right`'s exhaustive match
86    /// (no wildcard arm); this test additionally pins the read/write split so
87    /// a future edit that silently widens the read set is caught in review.
88    #[test]
89    fn binding_right_splits_read_vs_write() {
90        let reads = [
91            Operation::Read,
92            Operation::Search,
93            Operation::Traverse,
94            Operation::Recall,
95            Operation::Export,
96        ];
97        let writes = [
98            Operation::Create,
99            Operation::Remember,
100            Operation::Assign,
101            Operation::Link,
102            Operation::Update,
103            Operation::Complete,
104            Operation::Delete,
105            Operation::Send,
106        ];
107        for op in reads {
108            assert_eq!(op.binding_right(), Right::Read, "{op:?} must need Read");
109        }
110        for op in writes {
111            assert_eq!(op.binding_right(), Right::Write, "{op:?} must need Write");
112        }
113    }
114}