Skip to main content

zaun_community_protocol/permissions/
gate.rs

1//! The membership state gate (permissions §3.0, specification §6.1 and §6.4).
2//!
3//! Runs before any permission is resolved. Commands are wire names such as `"message.send"`.
4
5use crate::error_code::ErrorCode;
6use crate::model::MembershipState;
7
8/// Commands an unbound (negotiated) connection may send (specification §6.1, §8.1):
9/// `community.rotations` too, naming the community in the envelope's `community_id` (the
10/// chain is public and signed; a client needs it to verify a welcome before it can bind).
11///
12/// SPEC: §6.1 and the §7.1 `community_id` row still list only the other commands; §8.1 and
13/// permissions.md §6 allow unbound `community.rotations`, and are followed here.
14pub const UNBOUND_ALLOWED: &[&str] = &[
15    "session.hello",
16    "community.create",
17    "invite.redeem",
18    "membership.request",
19    "community.rotations",
20    "migration.reauth",
21];
22
23/// Commands that only make sense unbound; a bound connection gets `CONFLICT` (§6.1).
24pub const UNBOUND_ONLY: &[&str] = &[
25    "community.create",
26    "invite.redeem",
27    "membership.request",
28    "migration.reauth",
29];
30
31/// Commands a `Pending` member may issue (specification §6.4, normative). `verification.*`
32/// (P2, reserved) is not listed.
33pub const PENDING_ALLOWED: &[&str] = &[
34    "session.hello",
35    "community.get",
36    "community.icon.get",
37    "community.rotations",
38    "admission.policy.get",
39    "community.leave",
40    "sync.ack",
41];
42
43/// Commands a `Suspended` member may issue: the same as `Pending` (specification §6.4).
44pub const SUSPENDED_ALLOWED: &[&str] = PENDING_ALLOWED;
45
46/// Why the gate refused a command.
47#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
48pub enum GateDenial {
49    /// `NOT_BOUND`: no binding (unbound connection, or a member that left).
50    NotBound,
51    /// `CONFLICT`: an unbound-only command on a bound connection.
52    AlreadyBound,
53    /// `PENDING_APPROVAL`.
54    PendingApproval,
55    /// `SUSPENDED`.
56    Suspended,
57    /// `BANNED`.
58    Banned,
59}
60
61impl GateDenial {
62    /// The `reason` of the `CONFLICT` body, for [`GateDenial::AlreadyBound`].
63    #[must_use]
64    pub const fn conflict_reason(self) -> Option<&'static str> {
65        match self {
66            Self::AlreadyBound => Some("already_bound"),
67            _ => None,
68        }
69    }
70
71    /// The response code.
72    #[must_use]
73    pub const fn error_code(self) -> ErrorCode {
74        match self {
75            Self::NotBound => ErrorCode::NotBound,
76            Self::AlreadyBound => ErrorCode::Conflict,
77            Self::PendingApproval => ErrorCode::PendingApproval,
78            Self::Suspended => ErrorCode::Suspended,
79            Self::Banned => ErrorCode::Banned,
80        }
81    }
82}
83
84/// The gate for an unbound connection.
85pub fn gate_unbound(command: &str) -> Result<(), GateDenial> {
86    if UNBOUND_ALLOWED.contains(&command) {
87        Ok(())
88    } else {
89        Err(GateDenial::NotBound)
90    }
91}
92
93/// The gate for a connection bound to a member in `state`. `Probation` and `Active` pass
94/// every bound command, which is then subject to its permissions.
95pub fn gate(state: MembershipState, command: &str) -> Result<(), GateDenial> {
96    match state {
97        MembershipState::Banned => return Err(GateDenial::Banned),
98        MembershipState::Left => return Err(GateDenial::NotBound),
99        _ => {}
100    }
101    if UNBOUND_ONLY.contains(&command) {
102        return Err(GateDenial::AlreadyBound);
103    }
104    match state {
105        MembershipState::Pending if !PENDING_ALLOWED.contains(&command) => {
106            Err(GateDenial::PendingApproval)
107        }
108        MembershipState::Suspended if !SUSPENDED_ALLOWED.contains(&command) => {
109            Err(GateDenial::Suspended)
110        }
111        _ => Ok(()),
112    }
113}