workspace.rs (9122B)
1 //! Authenticated attachment and immutable captured command context. 2 //! 3 //! Descriptors and observed signer keys cannot construct these capabilities. 4 //! Current grant verification remains mandatory at authoritative admission; 5 //! attachment and context construction are not durable command submission. 6 7 use crate::session::host::Shared; 8 use radroots_domain::{ 9 Approval, CommandContextData, CorrelationId, ExpectedOrder, OperationId, UnixSeconds, 10 }; 11 use radroots_storage::workspace::{ 12 AttachmentInput, AttachmentProof, WorkspaceBinding, WorkspaceError, WorkspaceStore, 13 }; 14 use std::{ 15 fmt, 16 sync::{ 17 Mutex, 18 atomic::{AtomicBool, Ordering}, 19 }, 20 }; 21 22 struct Attached { 23 store: Shared<dyn WorkspaceStore>, 24 proof: Mutex<Option<AttachmentProof>>, 25 binding: WorkspaceBinding, 26 closed: AtomicBool, 27 } 28 impl Attached { 29 async fn current(&self) -> Result<WorkspaceBinding, WorkspaceError> { 30 if self.closed.load(Ordering::Acquire) { 31 return Err(WorkspaceError::Closed); 32 } 33 let proof = self 34 .proof 35 .lock() 36 .unwrap_or_else(std::sync::PoisonError::into_inner) 37 .as_ref() 38 .ok_or(WorkspaceError::Closed)? 39 .with_secret(|secret| AttachmentProof::from_protected_input(secret))?; 40 if self.closed.load(Ordering::Acquire) { 41 return Err(WorkspaceError::Closed); 42 } 43 let current = self 44 .store 45 .authenticate(self.binding.input().clone(), proof) 46 .await?; 47 if self.closed.load(Ordering::Acquire) { 48 return Err(WorkspaceError::Closed); 49 } 50 if current.input() != self.binding.input() 51 || current.scope() != self.binding.scope() 52 || current.actor() != self.binding.actor() 53 || current.principal_id() != self.binding.principal_id() 54 || current.grant_revision() != self.binding.grant_revision() 55 || current.expires_at() != self.binding.expires_at() 56 { 57 return Err(WorkspaceError::GrantRevoked); 58 } 59 Ok(current) 60 } 61 } 62 63 /// An owned authenticated attachment to the explicitly configured workspace. 64 /// Clones retain one captured grant and permanently shared close gate. 65 #[derive(Clone)] 66 pub struct WorkspaceSession { 67 attached: Shared<Attached>, 68 } 69 impl WorkspaceSession { 70 pub(crate) fn owns_context(&self, context: &CommandContext) -> bool { 71 Shared::ptr_eq(&self.attached, &context.attached) 72 } 73 pub(crate) fn same_scope(&self, other: &Self) -> bool { 74 Shared::ptr_eq(&self.attached, &other.attached) 75 } 76 pub(crate) fn captured_input( 77 &self, 78 ) -> Result<(WorkspaceBinding, AttachmentProof), WorkspaceError> { 79 if self.is_closed() { 80 return Err(WorkspaceError::Closed); 81 } 82 let proof = self 83 .attached 84 .proof 85 .lock() 86 .unwrap_or_else(std::sync::PoisonError::into_inner) 87 .as_ref() 88 .ok_or(WorkspaceError::Closed)? 89 .with_secret(|secret| AttachmentProof::from_protected_input(secret))?; 90 if self.is_closed() { 91 return Err(WorkspaceError::Closed); 92 } 93 Ok((self.attached.binding.clone(), proof)) 94 } 95 /// Authenticates the protected credential through a qualified host adapter. 96 /// Supplying a custom adapter is an explicit host trust boundary; metadata 97 /// returned by an unqualified adapter cannot qualify native SQL authority. 98 pub async fn attach( 99 store: Shared<dyn WorkspaceStore>, 100 input: AttachmentInput, 101 proof: AttachmentProof, 102 ) -> crate::Result<Self> { 103 let request_proof = proof 104 .with_secret(|secret| AttachmentProof::from_protected_input(secret)) 105 .map_err(|error| crate::Error::workspace(error, None))?; 106 let binding = store 107 .authenticate(input.clone(), request_proof) 108 .await 109 .map_err(|error| crate::Error::workspace(error, None))?; 110 if binding.input() != &input { 111 return Err(crate::Error::workspace(WorkspaceError::Unauthorized, None)); 112 } 113 Ok(Self { 114 attached: Shared::new(Attached { 115 store, 116 proof: Mutex::new(Some(proof)), 117 binding, 118 closed: AtomicBool::new(false), 119 }), 120 }) 121 } 122 /// Borrows the captured grant observation, without claiming it is current. 123 pub fn binding(&self) -> &WorkspaceBinding { 124 &self.attached.binding 125 } 126 /// Purely captures the complete context from the authenticated attachment. 127 /// This performs no clock, storage, signer or network operation. Current 128 /// rights and deadline must be revalidated at authoritative admission. 129 /// Approval remains a descriptor; authoritative commands must additionally 130 /// verify its protected consent origin and exact command intent. 131 pub fn command_context( 132 &self, 133 operation_id: OperationId, 134 expected: Option<ExpectedOrder>, 135 approval: Option<Approval>, 136 not_after: UnixSeconds, 137 ) -> crate::Result<CommandContext> { 138 self.command_context_with_correlation(operation_id, expected, approval, not_after, None) 139 } 140 /// Captures an optional bounded diagnostic correlation value. Correlation 141 /// supplies neither authorization nor semantic idempotency. 142 pub fn command_context_with_correlation( 143 &self, 144 operation_id: OperationId, 145 expected: Option<ExpectedOrder>, 146 approval: Option<Approval>, 147 not_after: UnixSeconds, 148 correlation: Option<CorrelationId>, 149 ) -> crate::Result<CommandContext> { 150 let failure = |error| crate::Error::workspace(error, Some(operation_id)); 151 if self.attached.closed.load(Ordering::Acquire) { 152 return Err(failure(WorkspaceError::Closed)); 153 } 154 let current = &self.attached.binding; 155 let input = current.input(); 156 let data = CommandContextData::new( 157 operation_id, 158 current.actor(), 159 input.workspace_id(), 160 input.authority(), 161 input.route_digest(), 162 approval, 163 expected, 164 not_after, 165 correlation, 166 ) 167 .map_err(|_| failure(WorkspaceError::InvalidInput))?; 168 if not_after <= current.observed_at() || not_after > current.expires_at() { 169 return Err(failure(WorkspaceError::GrantExpired)); 170 } 171 Ok(CommandContext { 172 data, 173 attached: self.attached.clone(), 174 }) 175 } 176 /// Permanently closes this attachment and every context it has issued. 177 /// Releases its credential root; owned in-flight requests retain only their 178 /// own zeroizing input until the adapter's safe completion boundary. 179 /// It does not revoke the persisted grant or close other workspace sessions. 180 pub fn close(&self) { 181 self.attached.closed.store(true, Ordering::Release); 182 self.attached 183 .proof 184 .lock() 185 .unwrap_or_else(std::sync::PoisonError::into_inner) 186 .take(); 187 } 188 pub(crate) fn is_closed(&self) -> bool { 189 self.attached.closed.load(Ordering::Acquire) 190 } 191 } 192 impl fmt::Debug for WorkspaceSession { 193 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 194 f.write_str("WorkspaceSession(<redacted>)") 195 } 196 } 197 198 /// Authenticated captured context. There is no public field constructor or 199 /// deserializer from the corresponding untrusted Domain DTO. 200 /// 201 /// ```compile_fail 202 /// use radroots_domain::CommandContextData; 203 /// use radroots_execution::workspace::CommandContext; 204 /// fn trust_unchecked(data: CommandContextData) -> CommandContext { data } 205 /// ``` 206 pub struct CommandContext { 207 data: CommandContextData, 208 attached: Shared<Attached>, 209 } 210 impl CommandContext { 211 pub(crate) fn captured_input( 212 &self, 213 ) -> Result<(WorkspaceBinding, AttachmentProof), WorkspaceError> { 214 WorkspaceSession { 215 attached: self.attached.clone(), 216 } 217 .captured_input() 218 } 219 /// Borrows the exact captured descriptor without transferring authority. 220 pub const fn data(&self) -> &CommandContextData { 221 &self.data 222 } 223 /// Revalidates current grant and deadline before an executor prepares its 224 /// authoritative transaction. The transaction itself must check them again; 225 /// this observation is not T0, a retained operation or stock admission. 226 pub async fn revalidate(&self) -> crate::Result<WorkspaceBinding> { 227 let failure = |error| crate::Error::workspace(error, Some(self.data.operation_id())); 228 let current = self.attached.current().await.map_err(failure)?; 229 if self.data.not_after() <= current.observed_at() { 230 return Err(failure(WorkspaceError::GrantExpired)); 231 } 232 Ok(current) 233 } 234 } 235 impl fmt::Debug for CommandContext { 236 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 237 f.write_str("CommandContext(<redacted>)") 238 } 239 }