1use crate::{utils, Error, PublicKey, Result, XorName};
11use serde::{Deserialize, Serialize};
12use std::{collections::BTreeMap, fmt::Debug, hash::Hash};
13
14#[derive(Clone, Copy, Eq, PartialEq)]
16pub enum Action {
17 Read,
19 Append,
21 Admin,
23}
24
25pub type Entries = Vec<Entry>;
27
28pub type Entry = Vec<u8>;
30
31#[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize, Debug)]
33pub enum Address {
34 Public {
36 name: XorName,
38 tag: u64,
40 },
41 Private {
43 name: XorName,
45 tag: u64,
47 },
48}
49
50impl Address {
51 pub fn from_kind(kind: Kind, name: XorName, tag: u64) -> Self {
53 match kind {
54 Kind::Public => Address::Public { name, tag },
55 Kind::Private => Address::Private { name, tag },
56 }
57 }
58
59 pub fn kind(&self) -> Kind {
61 match self {
62 Address::Public { .. } => Kind::Public,
63 Address::Private { .. } => Kind::Private,
64 }
65 }
66
67 pub fn name(&self) -> &XorName {
69 match self {
70 Address::Public { ref name, .. } | Address::Private { ref name, .. } => name,
71 }
72 }
73
74 pub fn tag(&self) -> u64 {
76 match self {
77 Address::Public { tag, .. } | Address::Private { tag, .. } => *tag,
78 }
79 }
80
81 pub fn is_pub(&self) -> bool {
83 self.kind().is_pub()
84 }
85
86 pub fn is_private(&self) -> bool {
88 self.kind().is_private()
89 }
90
91 pub fn encode_to_zbase32(&self) -> String {
93 utils::encode(&self)
94 }
95
96 pub fn decode_from_zbase32<I: AsRef<str>>(encoded: I) -> Result<Self> {
98 utils::decode(encoded)
99 }
100}
101
102#[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize, Debug)]
104pub enum Kind {
105 Public,
107 Private,
109}
110
111impl Kind {
112 pub fn is_pub(self) -> bool {
114 self == Kind::Public
115 }
116
117 pub fn is_private(self) -> bool {
119 !self.is_pub()
120 }
121}
122
123#[derive(Copy, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
125pub enum Index {
126 FromStart(u64),
128 FromEnd(u64),
130}
131
132impl From<u64> for Index {
133 fn from(index: u64) -> Self {
134 Index::FromStart(index)
135 }
136}
137
138#[derive(Copy, Clone, Serialize, Deserialize, PartialEq, PartialOrd, Ord, Eq, Hash, Debug)]
140pub struct PublicPermissions {
141 append: Option<bool>,
145 admin: Option<bool>,
149}
150
151impl PublicPermissions {
152 pub fn new(append: impl Into<Option<bool>>, manage_perms: impl Into<Option<bool>>) -> Self {
154 Self {
155 append: append.into(),
156 admin: manage_perms.into(),
157 }
158 }
159
160 pub fn set_perms(&mut self, append: impl Into<Option<bool>>, admin: impl Into<Option<bool>>) {
162 self.append = append.into();
163 self.admin = admin.into();
164 }
165
166 pub fn is_allowed(self, action: Action) -> Option<bool> {
169 match action {
170 Action::Read => Some(true), Action::Append => self.append,
172 Action::Admin => self.admin,
173 }
174 }
175}
176
177#[derive(Copy, Clone, Serialize, Deserialize, PartialEq, PartialOrd, Ord, Eq, Hash, Debug)]
179pub struct PrivatePermissions {
180 read: bool,
182 append: bool,
184 admin: bool,
186}
187
188impl PrivatePermissions {
189 pub fn new(read: bool, append: bool, manage_perms: bool) -> Self {
191 Self {
192 read,
193 append,
194 admin: manage_perms,
195 }
196 }
197
198 pub fn set_perms(&mut self, read: bool, append: bool, manage_perms: bool) {
200 self.read = read;
201 self.append = append;
202 self.admin = manage_perms;
203 }
204
205 pub fn is_allowed(self, action: Action) -> bool {
207 match action {
208 Action::Read => self.read,
209 Action::Append => self.append,
210 Action::Admin => self.admin,
211 }
212 }
213}
214
215#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, Debug)]
217pub enum User {
218 Anyone,
220 Key(PublicKey),
222}
223
224#[derive(Clone, Serialize, Deserialize, PartialEq, PartialOrd, Ord, Eq, Hash, Debug)]
226pub struct PublicPolicy {
227 pub owner: PublicKey,
230 pub permissions: BTreeMap<User, PublicPermissions>,
232}
233
234impl PublicPolicy {
235 fn is_action_allowed_by_user(&self, user: &User, action: Action) -> Option<bool> {
238 self.permissions
239 .get(user)
240 .and_then(|perms| perms.is_allowed(action))
241 }
242}
243
244#[derive(Clone, Serialize, Deserialize, PartialEq, PartialOrd, Ord, Eq, Hash, Debug)]
246pub struct PrivatePolicy {
247 pub owner: PublicKey,
250 pub permissions: BTreeMap<PublicKey, PrivatePermissions>,
252}
253
254pub trait Perm {
255 fn is_action_allowed(&self, requester: PublicKey, action: Action) -> Result<()>;
257 fn permissions(&self, user: User) -> Option<Permissions>;
259 fn owner(&self) -> &PublicKey;
261}
262
263impl Perm for PublicPolicy {
264 fn is_action_allowed(&self, requester: PublicKey, action: Action) -> Result<()> {
267 if action == Action::Read || requester == self.owner {
269 Ok(())
270 } else {
271 match self
272 .is_action_allowed_by_user(&User::Key(requester), action)
273 .or_else(|| self.is_action_allowed_by_user(&User::Anyone, action))
274 {
275 Some(true) => Ok(()),
276 Some(false) => Err(Error::AccessDenied),
277 None => Err(Error::AccessDenied),
278 }
279 }
280 }
281
282 fn permissions(&self, user: User) -> Option<Permissions> {
284 self.permissions.get(&user).map(|p| Permissions::Pub(*p))
285 }
286
287 fn owner(&self) -> &PublicKey {
289 &self.owner
290 }
291}
292
293impl Perm for PrivatePolicy {
294 fn is_action_allowed(&self, requester: PublicKey, action: Action) -> Result<()> {
297 if requester == self.owner {
299 Ok(())
300 } else {
301 match self.permissions.get(&requester) {
302 Some(perms) => {
303 if perms.is_allowed(action) {
304 Ok(())
305 } else {
306 Err(Error::AccessDenied)
307 }
308 }
309 None => Err(Error::AccessDenied),
310 }
311 }
312 }
313
314 fn permissions(&self, user: User) -> Option<Permissions> {
316 match user {
317 User::Anyone => None,
318 User::Key(key) => self.permissions.get(&key).map(|p| Permissions::Priv(*p)),
319 }
320 }
321
322 fn owner(&self) -> &PublicKey {
324 &self.owner
325 }
326}
327
328#[derive(Clone, Serialize, Deserialize, PartialEq, PartialOrd, Ord, Eq, Hash, Debug)]
330pub enum Policy {
331 Pub(PublicPolicy),
333 Priv(PrivatePolicy),
335}
336
337impl From<PrivatePolicy> for Policy {
338 fn from(policy: PrivatePolicy) -> Self {
339 Policy::Priv(policy)
340 }
341}
342
343impl From<PublicPolicy> for Policy {
344 fn from(policy: PublicPolicy) -> Self {
345 Policy::Pub(policy)
346 }
347}
348
349#[derive(Clone, Serialize, Deserialize, PartialEq, PartialOrd, Ord, Eq, Hash, Debug)]
351pub enum Permissions {
352 Pub(PublicPermissions),
354 Priv(PrivatePermissions),
356}
357
358impl From<PrivatePermissions> for Permissions {
359 fn from(permission_set: PrivatePermissions) -> Self {
360 Permissions::Priv(permission_set)
361 }
362}
363
364impl From<PublicPermissions> for Permissions {
365 fn from(permission_set: PublicPermissions) -> Self {
366 Permissions::Pub(permission_set)
367 }
368}