|
| 1 | +use std::sync::Arc; |
| 2 | + |
1 | 3 | use hashbrown::HashSet; |
2 | 4 |
|
3 | 5 | use crate::namespace::NamespaceName; |
4 | 6 |
|
5 | | -use super::Permission; |
| 7 | +use super::{AuthError, Authenticated, Permission}; |
6 | 8 |
|
7 | 9 | #[derive(Debug, serde::Deserialize, serde::Serialize, Default)] |
8 | 10 | pub struct Authorized { |
@@ -30,17 +32,52 @@ impl Authorized { |
30 | 32 | } |
31 | 33 | } |
32 | 34 |
|
33 | | - pub fn merge_legacy(&mut self, namespace: NamespaceName, perm: Permission) { |
34 | | - let scope = match perm { |
35 | | - Permission::Read => self.read_only.get_or_insert_with(Default::default), |
36 | | - Permission::Write => self.read_write.get_or_insert_with(Default::default), |
37 | | - Permission::AttachRead => self.read_only_attach.get_or_insert_with(Default::default), |
38 | | - }; |
39 | | - |
40 | | - scope |
41 | | - .namespaces |
42 | | - .get_or_insert_with(Default::default) |
43 | | - .insert(namespace); |
| 35 | + fn is_empty(&self) -> bool { |
| 36 | + self.read_write.is_none() |
| 37 | + && self.read_only.is_none() |
| 38 | + && self.read_only_attach.is_none() |
| 39 | + && self.read_write_attach.is_none() |
| 40 | + } |
| 41 | + |
| 42 | + pub fn merge_legacy( |
| 43 | + mut self, |
| 44 | + namespace: Option<NamespaceName>, |
| 45 | + perm: Option<Permission>, |
| 46 | + ) -> Result<Authenticated, AuthError> { |
| 47 | + match (namespace, perm) { |
| 48 | + (Some(ns), Some(perm)) => { |
| 49 | + let scope = match perm { |
| 50 | + Permission::Read => self.read_only.get_or_insert_with(Default::default), |
| 51 | + Permission::Write => self.read_write.get_or_insert_with(Default::default), |
| 52 | + Permission::AttachRead => { |
| 53 | + self.read_only_attach.get_or_insert_with(Default::default) |
| 54 | + } |
| 55 | + }; |
| 56 | + scope |
| 57 | + .namespaces |
| 58 | + .get_or_insert_with(Default::default) |
| 59 | + .insert(ns); |
| 60 | + Ok(Authenticated::Authorized(Arc::new(self))) |
| 61 | + } |
| 62 | + // legacy shit: interpret that as full access to ns |
| 63 | + (Some(ns), None) => { |
| 64 | + self.read_write |
| 65 | + .get_or_insert_with(Default::default) |
| 66 | + .namespaces |
| 67 | + .get_or_insert_with(Default::default) |
| 68 | + .insert(ns); |
| 69 | + Ok(Authenticated::Authorized(Arc::new(self))) |
| 70 | + } |
| 71 | + (None, None) => { |
| 72 | + // if there are no other claims, no claims is interpreted as full access. |
| 73 | + if self.is_empty() { |
| 74 | + Ok(Authenticated::FullAccess) |
| 75 | + } else { |
| 76 | + Ok(Authenticated::Authorized(Arc::new(self))) |
| 77 | + } |
| 78 | + } |
| 79 | + _ => Err(AuthError::JwtInvalid), |
| 80 | + } |
44 | 81 | } |
45 | 82 |
|
46 | 83 | fn can_write_ns(&self, name: &NamespaceName) -> bool { |
|
0 commit comments