| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 18 | pub enum Actor<'a> { |
| 19 | |
| 20 | Anonymous, |
| 21 | |
| 22 | Member { |
| 23 | |
| 24 | sub: &'a str, |
| 25 | |
| 26 | is_admin: bool, |
| 27 | }, |
| 28 | |
| 29 | Link { |
| 30 | |
| 31 | can_download: bool, |
| 32 | }, |
| 33 | |
| 34 | Service { |
| 35 | |
| 36 | key: &'a str, |
| 37 | }, |
| 38 | } |
| 39 | |
| 40 | |
| 41 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 42 | pub struct Target<'a> { |
| 43 | |
| 44 | pub owner: &'a str, |
| 45 | |
| 46 | pub trashed: bool, |
| 47 | |
| 48 | pub inside_link: bool, |
| 49 | } |
| 50 | |
| 51 | |
| 52 | #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] |
| 53 | pub enum Grant { |
| 54 | |
| 55 | None, |
| 56 | |
| 57 | View, |
| 58 | |
| 59 | Download, |
| 60 | } |
| 61 | |
| 62 | |
| 63 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 64 | pub enum Action { |
| 65 | |
| 66 | List, |
| 67 | |
| 68 | View, |
| 69 | |
| 70 | Download, |
| 71 | |
| 72 | Write, |
| 73 | |
| 74 | Restore, |
| 75 | |
| 76 | Share, |
| 77 | |
| 78 | Revoke, |
| 79 | |
| 80 | Purge, |
| 81 | |
| 82 | Administer, |
| 83 | } |
| 84 | |
| 85 | |
| 86 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 87 | pub enum Denied { |
| 88 | |
| 89 | NotFound, |
| 90 | |
| 91 | Forbidden, |
| 92 | } |
| 93 | |
| 94 | |
| 95 | pub fn authorize( |
| 96 | actor: Actor<'_>, |
| 97 | target: Target<'_>, |
| 98 | grant: Grant, |
| 99 | action: Action, |
| 100 | ) -> Result<(), Denied> { |
| 101 | match actor { |
| 102 | Actor::Anonymous | Actor::Service { .. } => Err(Denied::NotFound), |
| 103 | Actor::Link { can_download } => { |
| 104 | if !target.inside_link || target.trashed { |
| 105 | return Err(Denied::NotFound); |
| 106 | } |
| 107 | match action { |
| 108 | Action::List | Action::View => Ok(()), |
| 109 | Action::Download if can_download => Ok(()), |
| 110 | Action::Download => Err(Denied::Forbidden), |
| 111 | Action::Administer => Err(Denied::NotFound), |
| 112 | _ => Err(Denied::Forbidden), |
| 113 | } |
| 114 | } |
| 115 | Actor::Member { is_admin, .. } if is_admin && action == Action::Administer => Ok(()), |
| 116 | Actor::Member { sub, .. } if sub == target.owner => owner_rule(target.trashed, action), |
| 117 | Actor::Member { .. } => { |
| 118 | if target.trashed || grant == Grant::None || action == Action::Administer { |
| 119 | return Err(Denied::NotFound); |
| 120 | } |
| 121 | match action { |
| 122 | Action::List | Action::View => Ok(()), |
| 123 | Action::Download if grant == Grant::Download => Ok(()), |
| 124 | _ => Err(Denied::Forbidden), |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | |
| 131 | fn owner_rule(trashed: bool, action: Action) -> Result<(), Denied> { |
| 132 | match (trashed, action) { |
| 133 | (_, Action::Administer) => Err(Denied::NotFound), |
| 134 | |
| 135 | (_, Action::Revoke) => Ok(()), |
| 136 | (false, Action::Purge | Action::Restore) => Err(Denied::Forbidden), |
| 137 | (false, _) => Ok(()), |
| 138 | (true, Action::View | Action::Download | Action::Restore | Action::Purge) => Ok(()), |
| 139 | (true, _) => Err(Denied::Forbidden), |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | #[cfg(test)] |
| 144 | mod tests { |
| 145 | use super::*; |
| 146 | |
| 147 | const OWNER: &str = "owner"; |
| 148 | const OTHER: &str = "other"; |
| 149 | |
| 150 | fn live() -> Target<'static> { |
| 151 | Target { |
| 152 | owner: OWNER, |
| 153 | trashed: false, |
| 154 | inside_link: false, |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | fn trashed() -> Target<'static> { |
| 159 | Target { |
| 160 | trashed: true, |
| 161 | ..live() |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | fn owner() -> Actor<'static> { |
| 166 | Actor::Member { |
| 167 | sub: OWNER, |
| 168 | is_admin: false, |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | fn grantee() -> Actor<'static> { |
| 173 | Actor::Member { |
| 174 | sub: OTHER, |
| 175 | is_admin: false, |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | fn admin() -> Actor<'static> { |
| 180 | Actor::Member { |
| 181 | sub: "root", |
| 182 | is_admin: true, |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | |
| 187 | #[test] |
| 188 | fn truth_table() { |
| 189 | use Action::*; |
| 190 | use Denied::*; |
| 191 | let link_dl = Actor::Link { can_download: true }; |
| 192 | let link_view = Actor::Link { |
| 193 | can_download: false, |
| 194 | }; |
| 195 | let inside = Target { |
| 196 | inside_link: true, |
| 197 | ..live() |
| 198 | }; |
| 199 | type Case<'a> = (Actor<'a>, Target<'a>, Grant, Action, Result<(), Denied>); |
| 200 | let cases: Vec<Case> = vec![ |
| 201 | |
| 202 | ( |
| 203 | Actor::Anonymous, |
| 204 | live(), |
| 205 | Grant::Download, |
| 206 | View, |
| 207 | Err(NotFound), |
| 208 | ), |
| 209 | ( |
| 210 | Actor::Service { key: "im" }, |
| 211 | live(), |
| 212 | Grant::Download, |
| 213 | List, |
| 214 | Err(NotFound), |
| 215 | ), |
| 216 | |
| 217 | (owner(), live(), Grant::None, List, Ok(())), |
| 218 | (owner(), live(), Grant::None, View, Ok(())), |
| 219 | (owner(), live(), Grant::None, Download, Ok(())), |
| 220 | (owner(), live(), Grant::None, Write, Ok(())), |
| 221 | (owner(), live(), Grant::None, Share, Ok(())), |
| 222 | (owner(), live(), Grant::None, Revoke, Ok(())), |
| 223 | (owner(), live(), Grant::None, Purge, Err(Forbidden)), |
| 224 | (owner(), live(), Grant::None, Restore, Err(Forbidden)), |
| 225 | (owner(), live(), Grant::None, Administer, Err(NotFound)), |
| 226 | |
| 227 | (owner(), trashed(), Grant::None, View, Ok(())), |
| 228 | (owner(), trashed(), Grant::None, Download, Ok(())), |
| 229 | (owner(), trashed(), Grant::None, Restore, Ok(())), |
| 230 | (owner(), trashed(), Grant::None, Purge, Ok(())), |
| 231 | (owner(), trashed(), Grant::None, Write, Err(Forbidden)), |
| 232 | (owner(), trashed(), Grant::None, Share, Err(Forbidden)), |
| 233 | (owner(), trashed(), Grant::None, Revoke, Ok(())), |
| 234 | (owner(), trashed(), Grant::None, List, Err(Forbidden)), |
| 235 | |
| 236 | (grantee(), live(), Grant::View, List, Ok(())), |
| 237 | (grantee(), live(), Grant::View, View, Ok(())), |
| 238 | (grantee(), live(), Grant::View, Download, Err(Forbidden)), |
| 239 | (grantee(), live(), Grant::View, Write, Err(Forbidden)), |
| 240 | |
| 241 | (grantee(), live(), Grant::Download, Download, Ok(())), |
| 242 | (grantee(), live(), Grant::Download, Share, Err(Forbidden)), |
| 243 | (grantee(), live(), Grant::Download, Revoke, Err(Forbidden)), |
| 244 | |
| 245 | (grantee(), live(), Grant::None, View, Err(NotFound)), |
| 246 | (grantee(), trashed(), Grant::Download, View, Err(NotFound)), |
| 247 | |
| 248 | (admin(), live(), Grant::None, Administer, Ok(())), |
| 249 | (admin(), live(), Grant::None, View, Err(NotFound)), |
| 250 | |
| 251 | (admin(), live(), Grant::None, Download, Err(NotFound)), |
| 252 | |
| 253 | (admin(), live(), Grant::Download, Download, Ok(())), |
| 254 | |
| 255 | ( |
| 256 | Actor::Member { |
| 257 | sub: OWNER, |
| 258 | is_admin: true, |
| 259 | }, |
| 260 | live(), |
| 261 | Grant::None, |
| 262 | Write, |
| 263 | Ok(()), |
| 264 | ), |
| 265 | |
| 266 | (link_dl, inside, Grant::None, Download, Ok(())), |
| 267 | (link_view, inside, Grant::None, Download, Err(Forbidden)), |
| 268 | (link_view, inside, Grant::None, View, Ok(())), |
| 269 | (link_dl, live(), Grant::None, View, Err(NotFound)), |
| 270 | (link_dl, trashed(), Grant::None, View, Err(NotFound)), |
| 271 | ]; |
| 272 | for (actor, target, grant, action, want) in cases { |
| 273 | assert_eq!( |
| 274 | authorize(actor, target, grant, action), |
| 275 | want, |
| 276 | "actor {actor:?}, target {target:?}, grant {grant:?}, action {action:?}" |
| 277 | ); |
| 278 | } |
| 279 | } |
| 280 | } |