scopes-rs

Crates.ioscopes-rs
lib.rsscopes-rs
version0.2.0
created_at2025-12-30 10:12:19.353773+00
updated_at2026-01-04 00:09:20.687244+00
descriptionStrongly typed scoped authorization library
homepage
repositoryhttps://github.com/aripot007/scopes-rs
max_upload_size
id2012417
size64,645
(aripot007)

documentation

README

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.

Commit count: 0

cargo fmt