Skip to main content

weirdnet_access/
enforce.rs

1//! Host-side enforcement, per request, in a fixed order:
2//!
3//! 1. **Identity.** Noise has authenticated the peer id; the app resolves it to a user
4//!    through the node cert in the peer's card. Revoked nodes are refused.
5//! 2. **Visibility.** Private: a user who isn't the owner and isn't on the trust list gets
6//!    "not found", which leaks nothing; a membership alone doesn't make a Private resource
7//!    visible. Public: continue.
8//! 3. **Membership.** Gated: a valid, unrevoked membership for this resource is needed
9//!    (Public + Gated non-members may see the listing and request to join). Open: continue.
10//! 4. **Role.** The actor's role must allow the action (implications and rank rules
11//!    included). Guests never get managing permissions.
12//! 5. **Transport.** Private or Gated content goes over sealed streams; Public + Open goes
13//!    plain, its integrity coming from the signed manifest.
14
15use crate::axes::{Access, Visibility};
16use crate::catalog::Catalog;
17use crate::roles::{Actor, RoleSet};
18
19/// A resource's settings.
20#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
21pub struct Settings {
22    /// Who can see it.
23    pub visibility: Visibility,
24    /// Whether membership is needed.
25    pub access: Access,
26}
27
28impl Settings {
29    /// How content must travel.
30    pub fn transport(&self) -> Transport {
31        if self.visibility == Visibility::Public && self.access == Access::Open { Transport::Plain } else { Transport::Sealed }
32    }
33}
34
35/// What the host knows about the requester, from checks done before (weirdnet-net: the
36/// node cert, the trust list, `check_presented` for the membership).
37#[derive(Debug, Clone, Default, PartialEq, Eq)]
38pub struct Facts {
39    /// The user the peer resolves to, if its card checked out.
40    pub user: Option<String>,
41    /// The peer's node cert is revoked.
42    pub node_revoked: bool,
43    /// The user is the resource's owner.
44    pub is_owner: bool,
45    /// The user is on the host's trust list.
46    pub trusted: bool,
47    /// The role id from a valid, unexpired, unrevoked membership of this resource.
48    pub membership_role: Option<String>,
49}
50
51/// How content goes out.
52#[derive(Debug, Clone, Copy, PartialEq, Eq)]
53pub enum Transport {
54    /// Sealed streams bound to the exchange and both peers.
55    Sealed,
56    /// Plain (Public + Open; the signed manifest gives integrity).
57    Plain,
58}
59
60/// The answer.
61#[derive(Debug, Clone, PartialEq, Eq)]
62pub enum Decision {
63    /// Go ahead, as this actor, over this transport.
64    Allow {
65        /// Who.
66        actor: Actor,
67        /// How.
68        transport: Transport,
69    },
70    /// Answer "not found" (Private, not trusted; or a revoked node on a Private resource).
71    NotFound,
72    /// Public + Gated, not a member: show the title, owner and description and offer a join
73    /// request; no content.
74    JoinFirst,
75    /// Known, but not allowed.
76    Denied(&'static str),
77}
78
79/// The step that decided (for tests and logs).
80#[derive(Debug, Clone, Copy, PartialEq, Eq)]
81pub enum Step {
82    /// 1.
83    Identity,
84    /// 2.
85    Visibility,
86    /// 3.
87    Membership,
88    /// 4.
89    Role,
90}
91
92/// Decides one request for `perm` on a resource.
93pub fn decide(catalog: &Catalog, roles: &RoleSet, settings: Settings, facts: &Facts, perm: &str) -> Decision {
94    decide_with_step(catalog, roles, settings, facts, perm).0
95}
96
97/// [`decide`], also saying which step decided.
98pub fn decide_with_step(catalog: &Catalog, roles: &RoleSet, settings: Settings, facts: &Facts, perm: &str) -> (Decision, Step) {
99    let private = settings.visibility == Visibility::Private;
100    // 1. Identity.
101    if facts.node_revoked {
102        return (if private { Decision::NotFound } else { Decision::Denied("revoked node") }, Step::Identity);
103    }
104    let owner = facts.is_owner && facts.user.is_some();
105    let member = facts.user.as_deref().zip(facts.membership_role.as_deref()).and_then(|(u, r)| roles.actor(u, r));
106    // 2. Visibility. Members of a Private resource must be on the trust list too.
107    if private && !owner && !(facts.trusted && facts.user.is_some()) {
108        return (Decision::NotFound, Step::Visibility);
109    }
110    // 3. Membership.
111    let actor = if owner {
112        Actor::Owner
113    } else {
114        match (member, settings.access) {
115            (Some(a), _) => a,
116            (None, Access::Open) => Actor::Guest,
117            (None, Access::Gated) => return (if private { Decision::NotFound } else { Decision::JoinFirst }, Step::Membership),
118        }
119    };
120    // 4. Role.
121    if !roles.can(catalog, &actor, perm) {
122        return (Decision::Denied("the role does not allow this"), Step::Role);
123    }
124    // 5. Transport.
125    (Decision::Allow { actor, transport: settings.transport() }, Step::Role)
126}