hessra_cap_engine/
types.rs1use hessra_token_core::TokenTimeConfig;
7use serde::{Deserialize, Serialize};
8
9#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
15pub struct ObjectId(pub String);
16
17impl ObjectId {
18 pub fn new(id: impl Into<String>) -> Self {
19 Self(id.into())
20 }
21
22 pub fn as_str(&self) -> &str {
23 &self.0
24 }
25}
26
27impl std::fmt::Display for ObjectId {
28 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
29 write!(f, "{}", self.0)
30 }
31}
32
33impl From<&str> for ObjectId {
34 fn from(s: &str) -> Self {
35 Self(s.to_string())
36 }
37}
38
39impl From<String> for ObjectId {
40 fn from(s: String) -> Self {
41 Self(s)
42 }
43}
44
45#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
47pub struct Operation(pub String);
48
49impl Operation {
50 pub fn new(op: impl Into<String>) -> Self {
51 Self(op.into())
52 }
53
54 pub fn as_str(&self) -> &str {
55 &self.0
56 }
57}
58
59impl std::fmt::Display for Operation {
60 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
61 write!(f, "{}", self.0)
62 }
63}
64
65impl From<&str> for Operation {
66 fn from(s: &str) -> Self {
67 Self(s.to_string())
68 }
69}
70
71impl From<String> for Operation {
72 fn from(s: String) -> Self {
73 Self(s)
74 }
75}
76
77#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
83pub struct ExposureLabel(pub String);
84
85impl ExposureLabel {
86 pub fn new(label: impl Into<String>) -> Self {
87 Self(label.into())
88 }
89
90 pub fn as_str(&self) -> &str {
91 &self.0
92 }
93}
94
95impl std::fmt::Display for ExposureLabel {
96 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
97 write!(f, "{}", self.0)
98 }
99}
100
101impl From<&str> for ExposureLabel {
102 fn from(s: &str) -> Self {
103 Self(s.to_string())
104 }
105}
106
107impl From<String> for ExposureLabel {
108 fn from(s: String) -> Self {
109 Self(s)
110 }
111}
112
113#[derive(Debug, Clone, Serialize, Deserialize)]
115pub struct CapabilityGrant {
116 pub target: ObjectId,
118 pub operations: Vec<Operation>,
120}
121
122pub struct MintResult {
127 pub token: String,
129 pub context: Option<crate::ContextToken>,
132}
133
134#[derive(Debug, Clone, Default)]
139pub struct MintOptions {
140 pub namespace: Option<String>,
142 pub time_config: Option<TokenTimeConfig>,
144}
145
146#[derive(Debug, Clone, Serialize, Deserialize)]
148pub struct Designation {
149 pub label: String,
150 pub value: String,
151}
152
153#[derive(Debug, Clone)]
155pub struct IdentityConfig {
156 pub ttl: i64,
158 pub delegatable: bool,
160 pub namespace: Option<String>,
162}
163
164impl Default for IdentityConfig {
165 fn default() -> Self {
166 Self {
167 ttl: 3600,
168 delegatable: false,
169 namespace: None,
170 }
171 }
172}
173
174#[derive(Debug, Clone)]
176pub struct SessionConfig {
177 pub ttl: i64,
179}
180
181impl Default for SessionConfig {
182 fn default() -> Self {
183 Self { ttl: 3600 }
184 }
185}
186
187#[derive(Debug, Clone)]
189pub enum PolicyDecision {
190 Granted,
192 Denied { reason: String },
194 DeniedByExposure {
196 label: ExposureLabel,
197 blocked_target: ObjectId,
198 },
199}
200
201impl PolicyDecision {
202 pub fn is_granted(&self) -> bool {
203 matches!(self, PolicyDecision::Granted)
204 }
205}
206
207pub trait PolicyBackend: Send + Sync {
212 fn evaluate(
215 &self,
216 subject: &ObjectId,
217 target: &ObjectId,
218 operation: &Operation,
219 exposure_labels: &[ExposureLabel],
220 ) -> PolicyDecision;
221
222 fn classification(&self, target: &ObjectId) -> Vec<ExposureLabel>;
227
228 fn list_grants(&self, subject: &ObjectId) -> Vec<CapabilityGrant>;
230
231 fn can_delegate(&self, subject: &ObjectId) -> bool;
233}