authorization.rs (7200B)
1 //! Explicit protected stock grants and authorization checkpoint metadata. 2 use super::{SellerAuthoritySelection, StockGrantScope}; 3 use crate::workspace::{ 4 AttachmentInput, AttachmentProof, WorkspaceBinding, WorkspaceError, WorkspaceScope, 5 grant::WorkspaceGrant, 6 }; 7 use radroots_domain::{ActorRef, CheckpointDigest, Sequence, UnixSeconds}; 8 use serde::{Deserialize, Serialize}; 9 10 /// A claimed protected grant-set observation. It never authenticates a caller. 11 #[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize)] 12 #[serde(deny_unknown_fields)] 13 pub struct StockAuthorizationRef { 14 generation: Sequence, 15 digest: CheckpointDigest, 16 } 17 impl<'de> Deserialize<'de> for StockAuthorizationRef { 18 fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> { 19 #[derive(Deserialize)] 20 #[serde(deny_unknown_fields)] 21 struct Wire { 22 generation: Sequence, 23 digest: CheckpointDigest, 24 } 25 struct Visitor; 26 impl<'de> serde::de::Visitor<'de> for Visitor { 27 type Value = Wire; 28 fn expecting(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { 29 f.write_str("a closed authorization checkpoint object") 30 } 31 fn visit_map<A: serde::de::MapAccess<'de>>(self, map: A) -> Result<Wire, A::Error> { 32 Wire::deserialize(serde::de::value::MapAccessDeserializer::new(map)) 33 } 34 } 35 let v = deserializer.deserialize_map(Visitor)?; 36 Self::from_host_metadata(v.generation, v.digest).map_err(serde::de::Error::custom) 37 } 38 } 39 impl StockAuthorizationRef { 40 pub fn from_host_metadata( 41 generation: Sequence, 42 digest: CheckpointDigest, 43 ) -> Result<Self, WorkspaceError> { 44 if generation.get() == 0 { 45 return Err(WorkspaceError::InvalidInput); 46 } 47 Ok(Self { generation, digest }) 48 } 49 pub const fn generation(self) -> Sequence { 50 self.generation 51 } 52 pub const fn digest(self) -> CheckpointDigest { 53 self.digest 54 } 55 } 56 57 /// Closed stock-only grant. The envelope and current namespace/grant-set must 58 /// be authenticated by the concrete host before these claims are consumed. 59 pub struct StockGrantRecord { 60 credential: WorkspaceGrant, 61 permissions: StockGrantScope, 62 administrator: bool, 63 } 64 #[derive(Serialize, Deserialize)] 65 #[serde(deny_unknown_fields)] 66 struct Wire<'a> { 67 version: u8, 68 #[serde(borrow)] 69 credential: &'a serde_json::value::RawValue, 70 permissions: StockGrantScope, 71 administrator: bool, 72 } 73 impl StockGrantRecord { 74 pub fn new( 75 input: AttachmentInput, 76 principal: String, 77 expires_at: UnixSeconds, 78 permissions: StockGrantScope, 79 proof: &AttachmentProof, 80 ) -> Result<Self, WorkspaceError> { 81 let actor = ActorRef::new( 82 input.participant(), 83 WorkspaceScope::SellerStockOwner.actor_role(), 84 None, 85 Some(input.grant_id()), 86 ); 87 let credential = WorkspaceGrant::with_scope( 88 input, 89 WorkspaceScope::SellerStockOwner, 90 actor, 91 principal, 92 expires_at, 93 proof, 94 )?; 95 Ok(Self { 96 credential, 97 permissions, 98 administrator: false, 99 }) 100 } 101 /// Only fresh authority enrollment supplies the original administrative grant. 102 /// Hosts must never treat construction of this metadata as authorization. 103 pub fn from_enrollment(credential: WorkspaceGrant) -> Result<Self, WorkspaceError> { 104 if credential.scope() != WorkspaceScope::SellerStockOwner { 105 return Err(WorkspaceError::InvalidInput); 106 } 107 Ok(Self { 108 credential, 109 permissions: StockGrantScope::full_inventory(), 110 administrator: true, 111 }) 112 } 113 pub fn encode(&self) -> Result<zeroize::Zeroizing<Vec<u8>>, WorkspaceError> { 114 let clear = self.credential.encode()?; 115 let credential: &serde_json::value::RawValue = 116 serde_json::from_slice(&clear).map_err(|_| WorkspaceError::StorageFailure)?; 117 let bytes = zeroize::Zeroizing::new( 118 serde_json::to_vec(&Wire { 119 version: 2, 120 credential, 121 permissions: self.permissions.clone(), 122 administrator: self.administrator, 123 }) 124 .map_err(|_| WorkspaceError::StorageFailure)?, 125 ); 126 if bytes.len() > 16 * 1024 { 127 return Err(WorkspaceError::Capacity); 128 } 129 Ok(bytes) 130 } 131 pub fn decode(bytes: &[u8]) -> Result<Self, WorkspaceError> { 132 if bytes.len() > 16 * 1024 133 || bytes.iter().copied().find(|b| !b.is_ascii_whitespace()) != Some(b'{') 134 { 135 return Err(WorkspaceError::RecoveryRequired); 136 } 137 let wire: Wire<'_> = 138 serde_json::from_slice(bytes).map_err(|_| WorkspaceError::RecoveryRequired)?; 139 if wire.version != 2 { 140 return Err(WorkspaceError::RecoveryRequired); 141 } 142 if !wire.credential.get().starts_with('{') { 143 return Err(WorkspaceError::RecoveryRequired); 144 } 145 let credential = WorkspaceGrant::decode(wire.credential.get().as_bytes())?; 146 if credential.scope() != WorkspaceScope::SellerStockOwner 147 || (wire.administrator && wire.permissions != StockGrantScope::full_inventory()) 148 { 149 return Err(WorkspaceError::RecoveryRequired); 150 } 151 Ok(Self { 152 credential, 153 permissions: wire.permissions, 154 administrator: wire.administrator, 155 }) 156 } 157 pub fn input(&self) -> &AttachmentInput { 158 self.credential.input() 159 } 160 pub fn expires_at(&self) -> UnixSeconds { 161 self.credential.expires_at() 162 } 163 /// Returns the decoded revocation claim. Current protected provenance and 164 /// grant-set membership must be authenticated by the concrete owner. 165 pub fn is_revoked(&self) -> bool { 166 self.credential.is_revoked() 167 } 168 pub fn permissions(&self) -> &StockGrantScope { 169 &self.permissions 170 } 171 pub const fn administrator(&self) -> bool { 172 self.administrator 173 } 174 pub fn authenticate( 175 &self, 176 input: &AttachmentInput, 177 proof: &AttachmentProof, 178 now: UnixSeconds, 179 ) -> Result<WorkspaceBinding, WorkspaceError> { 180 self.credential.authenticate(input, proof, now) 181 } 182 pub fn authenticate_retained_capture( 183 &self, 184 input: &AttachmentInput, 185 proof: &AttachmentProof, 186 ) -> Result<(), WorkspaceError> { 187 self.credential.authenticate_retained_capture(input, proof) 188 } 189 pub fn revoke(&mut self) -> Result<(), WorkspaceError> { 190 self.credential.revoke() 191 } 192 pub fn belongs_to(&self, namespace: &SellerAuthoritySelection) -> Result<bool, WorkspaceError> { 193 Ok(namespace.matches_attachment(self.credential.input())) 194 } 195 } 196 impl core::fmt::Debug for StockGrantRecord { 197 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { 198 f.write_str("StockGrantRecord(<redacted>)") 199 } 200 }