Crate scopes_rs

Crate scopes_rs 

Source
Expand description

§Scopes-rs

scopes-rs is a strongly typed scoped authorization library.

GitHub Release Crates.io Version docs.rs codecov

§Features

  • Verify complex scope authorization policies
  • Manipulate scopes in a strongly-typed fashion
  • Generate boilerplate implementation with a derive macro
  • Support for hierarchized scopes with the hierarchy feature

§Usage example

use std::str::FromStr;
use scopes_rs::{
    policy::IntoPolicy,
    derive::Scope
};


#[derive(Clone, PartialEq, Scope)]
enum ApiScope {
    Settings,
    SettingsReadonly,
    Profile,
    ProfileReadonly
}


pub fn main() {

    // Parse the scopes from a web request for example
    let scopes: Vec<ApiScope> = parse_scopes("profile settings.readonly");

    // Convert a single scope in a policy that requires this scope
    let policy = ApiScope::SettingsReadonly.into_policy();

    assert_eq!(true, policy.verify(&scopes));

    // Policies can also be combined together with &, | and !
    let other_policy = policy & ApiScope::ProfileReadonly;

    // With the hierarchy feature, "profile.readonly" is included in "profile", so this is accepted
    #[cfg(feature = "hierarchy")]
    assert_eq!(true, other_policy.verify(&scopes));

    // Otherwise, the scopes require an exact match
    #[cfg(not(feature = "hierarchy"))]
    assert_eq!(false, other_policy.verify(&scopes));
}

// You would usually parse the scopes from a request in a middleware with
// proper error handling, but this is sufficient here
fn parse_scopes(data: &str) -> Vec<ApiScope> {

    data.split_whitespace()
        .filter_map(|s| ApiScope::from_str(s).ok())
        .collect()
}

The examples directory contains some other examples of how to use this library.

The docs also provide more code snippets and examples.

§License

Licensed under either of

at your option.

§Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

§Crate features

  • hierarchy: Enable hierarchical scopes support. See hierarchy for more details.

Modules§

derive
Derive macro for the Scope trait
error
Errors used by the crate
hierarchy
Contains the required trait for scope hierarchy
policy
Policy creation and verification
scope
Contains the trait that types representing a scope should implement