lib

Core libraries for Radroots
git clone https://radroots.dev/git/lib.git
Log | Files | Refs | README

local_outbox.rs (6030B)


      1 #![cfg(feature = "protected_outbox")]
      2 use radroots_domain::{
      3     ActorRef, ActorRole, ExecutorId, GrantId, PublicKey, UnixSeconds, WorkspaceId,
      4 };
      5 use radroots_storage::workspace::{
      6     AttachmentProof, WorkspaceError, WorkspaceScope, grant::WorkspaceGrant,
      7     local_outbox::LocalOutboxConfig,
      8 };
      9 
     10 fn key() -> PublicKey {
     11     PublicKey::from_hex("79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798").unwrap()
     12 }
     13 fn enrolled(
     14     seller: bool,
     15 ) -> (
     16     radroots_storage::workspace::AttachmentInput,
     17     AttachmentProof,
     18     WorkspaceGrant,
     19 ) {
     20     let constructor = if seller {
     21         LocalOutboxConfig::seller
     22     } else {
     23         LocalOutboxConfig::new
     24     };
     25     constructor(
     26         key(),
     27         key(),
     28         "synthetic-device",
     29         UnixSeconds::new(200).unwrap(),
     30     )
     31     .unwrap()
     32     .enroll(
     33         UnixSeconds::new(100).unwrap(),
     34         WorkspaceId::from_bytes([1; 32]),
     35         ExecutorId::from_bytes([2; 32]),
     36         GrantId::from_bytes([3; 32]),
     37         AttachmentProof::from_protected_input(&[7; 32]).unwrap(),
     38     )
     39     .unwrap()
     40 }
     41 
     42 #[test]
     43 fn enrollment_separates_scope_routes_genesis_and_credential_commitments() {
     44     let (buyer, _, buyer_grant) = enrolled(false);
     45     let (seller, _, seller_grant) = enrolled(true);
     46     assert_ne!(buyer.route_digest(), seller.route_digest());
     47     assert_ne!(buyer.checkpoint(), seller.checkpoint());
     48     let buyer: serde_json::Value = serde_json::from_slice(&buyer_grant.encode().unwrap()).unwrap();
     49     let seller: serde_json::Value =
     50         serde_json::from_slice(&seller_grant.encode().unwrap()).unwrap();
     51     assert_eq!(buyer["scope"], "new_buyer_outbox");
     52     assert_eq!(seller["scope"], "new_seller_outbox");
     53     assert_ne!(buyer["verifier"], seller["verifier"]);
     54 }
     55 
     56 #[test]
     57 fn ordinary_outbox_admission_refuses_structurally_valid_stock_owner_metadata() {
     58     let (_, _, grant) = enrolled(true);
     59     let original = grant.encode().unwrap();
     60     let mut wire: serde_json::Value = serde_json::from_slice(&original).unwrap();
     61     wire["scope"] = serde_json::json!("seller_stock_owner");
     62     let encoded = serde_json::to_vec(&wire).unwrap();
     63     assert!(WorkspaceGrant::decode(&encoded).is_ok());
     64     assert!(matches!(
     65         WorkspaceGrant::decode_outbox(&encoded),
     66         Err(WorkspaceError::RecoveryRequired)
     67     ));
     68     assert!(WorkspaceGrant::decode_outbox(&original).is_ok());
     69 }
     70 
     71 #[test]
     72 fn both_scopes_retain_protected_access_expiry_revocation_and_exact_role() {
     73     for (seller, scope, role) in [
     74         (false, WorkspaceScope::NewBuyerOutbox, ActorRole::Buyer),
     75         (true, WorkspaceScope::NewSellerOutbox, ActorRole::Seller),
     76     ] {
     77         let (input, proof, grant) = enrolled(seller);
     78         let mut grant = WorkspaceGrant::decode(&grant.encode().unwrap()).unwrap();
     79         let bound = grant
     80             .authenticate(&input, &proof, UnixSeconds::new(100).unwrap())
     81             .unwrap();
     82         assert_eq!(bound.scope(), scope);
     83         assert_eq!(bound.actor().role(), role);
     84         assert_eq!(bound.actor().organization(), None);
     85         assert_eq!(
     86             bound.input().checkpoint().coverage(),
     87             radroots_domain::Coverage::Partial
     88         );
     89         let wrong = AttachmentProof::from_protected_input(&[8; 32]).unwrap();
     90         assert_eq!(
     91             grant
     92                 .authenticate(&input, &wrong, UnixSeconds::new(100).unwrap())
     93                 .unwrap_err(),
     94             WorkspaceError::Unauthorized
     95         );
     96         assert_eq!(
     97             grant
     98                 .authenticate(&input, &proof, UnixSeconds::new(0).unwrap())
     99                 .unwrap_err(),
    100             WorkspaceError::Unavailable
    101         );
    102         assert_eq!(
    103             grant
    104                 .authenticate(&input, &proof, UnixSeconds::new(200).unwrap())
    105                 .unwrap_err(),
    106             WorkspaceError::GrantExpired
    107         );
    108         grant.revoke().unwrap();
    109         let grant = WorkspaceGrant::decode(&grant.encode().unwrap()).unwrap();
    110         assert_eq!(
    111             grant
    112                 .authenticate(&input, &proof, UnixSeconds::new(100).unwrap())
    113                 .unwrap_err(),
    114             WorkspaceError::GrantRevoked
    115         );
    116         // Capture is limited to the original credential and never renews access.
    117         grant.authenticate_retained_capture(&input, &proof).unwrap();
    118     }
    119 }
    120 
    121 #[test]
    122 fn changing_scope_and_role_cannot_upgrade_an_existing_protected_credential() {
    123     for seller in [false, true] {
    124         let (input, proof, grant) = enrolled(seller);
    125         let original: serde_json::Value = serde_json::from_slice(&grant.encode().unwrap()).unwrap();
    126         let mut changed = original.clone();
    127         changed["actor"]["role"] = serde_json::json!(if seller { "buyer" } else { "seller" });
    128         assert!(matches!(
    129             WorkspaceGrant::decode(&serde_json::to_vec(&changed).unwrap()),
    130             Err(WorkspaceError::RecoveryRequired)
    131         ));
    132         changed["scope"] = serde_json::json!(if seller {
    133             "new_buyer_outbox"
    134         } else {
    135             "new_seller_outbox"
    136         });
    137         let changed = WorkspaceGrant::decode(&serde_json::to_vec(&changed).unwrap()).unwrap();
    138         assert_eq!(
    139             changed
    140                 .authenticate(&input, &proof, UnixSeconds::new(100).unwrap())
    141                 .unwrap_err(),
    142             WorkspaceError::Unauthorized
    143         );
    144         for scope in ["seller_stock", "remote_executor", "organization", "unknown"] {
    145             let mut changed = original.clone();
    146             changed["scope"] = serde_json::json!(scope);
    147             assert!(WorkspaceGrant::decode(&serde_json::to_vec(&changed).unwrap()).is_err());
    148         }
    149         // The original buyer constructor never accepts a seller actor.
    150         assert!(
    151             WorkspaceGrant::new(
    152                 input.clone(),
    153                 ActorRef::new(key(), ActorRole::Seller, None, Some(input.grant_id())),
    154                 "device".into(),
    155                 UnixSeconds::new(200).unwrap(),
    156                 &proof
    157             )
    158             .is_err()
    159         );
    160     }
    161 }