authorization.rs (4827B)
1 //! Current signing authorization, separate from historical plan integrity. 2 3 use radroots_event::contract::{ 4 EventAuthoringPolicy, EventContract, EventStability, registry_v8::event_contract_registry_v8, 5 }; 6 use radroots_event_codec::authoring::AuthoredEventPlan; 7 8 use crate::{Actor, actor::ActorSource}; 9 10 /// Current policy decision for one historically valid authored plan. 11 #[non_exhaustive] 12 #[derive(Clone, Copy, Debug, PartialEq, Eq)] 13 pub enum CurrentAuthoringDecision { 14 Allowed, 15 AllowedDeprecated { warning_code: &'static str }, 16 Blocked { code: &'static str }, 17 Revoked { code: &'static str }, 18 } 19 20 /// Host or registry authority for current signing policy. 21 pub trait CurrentAuthoringAuthority: crate::host::HostBound { 22 fn evaluate(&self, plan: &AuthoredEventPlan) -> CurrentAuthoringDecision; 23 } 24 25 /// Current immutable registry policy. 26 #[derive(Clone, Copy, Debug, Default)] 27 pub struct CurrentRegistryAuthority; 28 29 /// Current authoring includes explicit v8 capabilities; ordinary v7 defaults 30 /// remain unchanged. Never authorize from an arbitrary embedded old registry. 31 pub(crate) fn current_authoring_contract( 32 plan: &AuthoredEventPlan, 33 ) -> Option<&'static EventContract> { 34 event_contract_registry_v8(plan.body().contract().contract_id().as_str()) 35 } 36 37 impl CurrentAuthoringAuthority for CurrentRegistryAuthority { 38 fn evaluate(&self, plan: &AuthoredEventPlan) -> CurrentAuthoringDecision { 39 let Some(contract) = current_authoring_contract(plan) else { 40 return CurrentAuthoringDecision::Blocked { 41 code: "contract_not_current", 42 }; 43 }; 44 if contract.authoring_policy() == EventAuthoringPolicy::ReadOnly { 45 return CurrentAuthoringDecision::Revoked { 46 code: "contract_read_only", 47 }; 48 } 49 match contract.stability { 50 EventStability::Stable => CurrentAuthoringDecision::Allowed, 51 EventStability::Experimental => CurrentAuthoringDecision::AllowedDeprecated { 52 warning_code: "contract_experimental", 53 }, 54 } 55 } 56 } 57 58 /// Whether a request explicitly accepts a currently deprecated contract. 59 #[non_exhaustive] 60 #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] 61 #[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))] 62 #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] 63 pub enum DeprecatedPlanPolicy { 64 #[default] 65 Deny, 66 Allow, 67 } 68 69 /// Host-owned policy limiting which validated actor provenance may sign. 70 #[non_exhaustive] 71 #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] 72 #[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))] 73 #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] 74 pub enum ManagedSigningPolicy { 75 #[default] 76 AnyValidatedSource, 77 AccountBackedOnly, 78 LocalAccountOnly, 79 } 80 81 impl ManagedSigningPolicy { 82 #[must_use] 83 pub const fn permits(self, actor: &Actor) -> bool { 84 match self { 85 Self::AnyValidatedSource => true, 86 Self::AccountBackedOnly => actor.source().account_id().is_some(), 87 Self::LocalAccountOnly => matches!(actor.source(), ActorSource::LocalAccount(_)), 88 } 89 } 90 } 91 92 #[cfg(test)] 93 mod tests { 94 use radroots_event::contract::AuthorRole; 95 use radroots_identity::{AccountId, PublicKey}; 96 97 use super::*; 98 99 const KEY: &str = "585591529da0bab31b3b1b1f986611cf5f435dca84f978c89ee8a40cca7103df"; 100 101 #[test] 102 fn managed_signing_policy_covers_every_provenance_class() { 103 let public_key = PublicKey::from_hex(KEY).expect("public key"); 104 let account_id = AccountId::from_hex(KEY).expect("account ID"); 105 let explicit = Actor::new( 106 public_key, 107 ActorSource::ExplicitPublicKey, 108 [AuthorRole::Any], 109 ) 110 .expect("explicit actor"); 111 let local = Actor::new( 112 public_key, 113 ActorSource::LocalAccount(account_id), 114 [AuthorRole::Any], 115 ) 116 .expect("local actor"); 117 let remote = Actor::new( 118 public_key, 119 ActorSource::RemoteSigner(account_id), 120 [AuthorRole::Any], 121 ) 122 .expect("remote actor"); 123 124 for actor in [&explicit, &local, &remote] { 125 assert!(ManagedSigningPolicy::AnyValidatedSource.permits(actor)); 126 } 127 assert!(!ManagedSigningPolicy::AccountBackedOnly.permits(&explicit)); 128 assert!(ManagedSigningPolicy::AccountBackedOnly.permits(&local)); 129 assert!(ManagedSigningPolicy::AccountBackedOnly.permits(&remote)); 130 assert!(!ManagedSigningPolicy::LocalAccountOnly.permits(&explicit)); 131 assert!(ManagedSigningPolicy::LocalAccountOnly.permits(&local)); 132 assert!(!ManagedSigningPolicy::LocalAccountOnly.permits(&remote)); 133 } 134 }