-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
165 additions
and
43 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
mod scope; | ||
|
||
pub use self::scope::Scopes; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
use compact_str::CompactString; | ||
use serde::Deserialize; | ||
use std::{ | ||
collections::{hash_set, HashSet}, | ||
convert::Infallible, | ||
str::FromStr, | ||
}; | ||
|
||
#[derive(Default, Deserialize)] | ||
#[serde(transparent)] | ||
pub struct Scopes { | ||
inner: HashSet<CompactString>, | ||
} | ||
|
||
impl FromStr for Scopes { | ||
type Err = Infallible; | ||
|
||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
Ok(s.split_whitespace().collect()) | ||
} | ||
} | ||
|
||
impl Scopes { | ||
#[inline] | ||
#[must_use] | ||
pub fn new() -> Self { | ||
Self::default() | ||
} | ||
|
||
#[inline] | ||
pub fn insert<Item>(&mut self, item: Item) | ||
where | ||
Item: Into<CompactString>, | ||
{ | ||
self.inner.insert(item.into()); | ||
} | ||
|
||
/// Determine whether `self` can be accessed by `resource` | ||
/// | ||
/// This implies that `resource` is equal to or a superset of `self` | ||
#[inline] | ||
#[must_use] | ||
pub fn can_be_accessed_by(&self, resource: &Self) -> bool { | ||
resource.inner.is_superset(&self.inner) | ||
} | ||
|
||
/// Determine whether `self` is allowed to perform an action | ||
/// for which you at least need `resource` scope | ||
#[inline] | ||
#[must_use] | ||
pub fn can_perform(&self, resource: &Self) -> bool { | ||
self.inner.is_superset(&resource.inner) | ||
} | ||
|
||
#[inline] | ||
pub fn iter(&self) -> impl Iterator<Item = &str> { | ||
self.inner.iter().map(CompactString::as_str) | ||
} | ||
} | ||
|
||
impl<Item> FromIterator<Item> for Scopes | ||
where | ||
Item: Into<CompactString>, | ||
{ | ||
#[inline] | ||
fn from_iter<T: IntoIterator<Item = Item>>(iter: T) -> Self { | ||
let mut collection = Self::new(); | ||
for item in iter { | ||
collection.insert(item.into()); | ||
} | ||
collection | ||
} | ||
} | ||
|
||
impl IntoIterator for Scopes { | ||
type Item = CompactString; | ||
type IntoIter = hash_set::IntoIter<Self::Item>; | ||
|
||
fn into_iter(self) -> Self::IntoIter { | ||
self.inner.into_iter() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use komainu::primitive::Scopes; | ||
use rstest::rstest; | ||
|
||
#[rstest] | ||
#[case("read", "read write")] | ||
#[case("read write", "read write")] | ||
#[case("read write follow", "read write follow push")] | ||
fn can_perform(#[case] request: &str, #[case] client: &str) { | ||
let request: Scopes = request.parse().unwrap(); | ||
let client: Scopes = client.parse().unwrap(); | ||
|
||
assert!(client.can_perform(&request)); | ||
} | ||
|
||
#[rstest] | ||
#[case("read write", "read")] | ||
#[case("read follow", "write")] | ||
#[case("write push", "read")] | ||
fn cant_perform(#[case] request: &str, #[case] client: &str) { | ||
let request: Scopes = request.parse().unwrap(); | ||
let client: Scopes = client.parse().unwrap(); | ||
|
||
assert!(!client.can_perform(&request)); | ||
} | ||
|
||
#[rstest] | ||
#[case("read", "read write")] | ||
#[case("read", "read")] | ||
#[case("follow", "read follow")] | ||
#[case("write follow", "follow write")] | ||
fn can_access(#[case] endpoint: &str, #[case] client: &str) { | ||
let endpoint: Scopes = endpoint.parse().unwrap(); | ||
let client: Scopes = client.parse().unwrap(); | ||
|
||
assert!(endpoint.can_be_accessed_by(&client)); | ||
} | ||
|
||
#[rstest] | ||
#[case("read write", "write")] | ||
#[case("follow", "read write")] | ||
#[case("write follow", "read follow")] | ||
fn cant_access(#[case] endpoint: &str, #[case] client: &str) { | ||
let endpoint: Scopes = endpoint.parse().unwrap(); | ||
let client: Scopes = client.parse().unwrap(); | ||
|
||
assert!(!endpoint.can_be_accessed_by(&client)); | ||
} |