| Crates.io | gatehouse |
| lib.rs | gatehouse |
| version | 0.1.3 |
| created_at | 2025-03-24 04:51:52.221813+00 |
| updated_at | 2025-03-25 03:48:48.539484+00 |
| description | A flexible authorization library that combines role-based (RBAC), attribute-based (ABAC), and relationship-based (ReBAC) access control policies. |
| homepage | |
| repository | https://github.com/thepartly/gatehouse |
| max_upload_size | |
| id | 1603384 |
| size | 147,205 |
A flexible authorization library that combines role-based (RBAC), attribute-based (ABAC), and relationship-based (ReBAC) access control policies.
AND, OR, NOT)Policy TraitThe foundation of the authorization system:
#[async_trait]
trait Policy<Subject, Resource, Action, Context> {
async fn evaluate_access(
&self,
subject: &Subject,
action: &Action,
resource: &Resource,
context: &Context,
) -> PolicyEvalResult;
}
PermissionCheckerAggregates multiple policies (e.g. RBAC, ABAC) with OR logic by default: if any policy grants access, permission is granted.
let mut checker = PermissionChecker::new();
checker.add_policy(rbac_policy);
checker.add_policy(owner_policy);
// Check if access is granted
let result = checker.evaluate_access(&user, &action, &resource, &context).await;
if result.is_granted() {
// Access allowed
} else {
// Access denied
}
The PolicyBuilder provides a fluent API to construct custom policies by chaining predicate functions for
subjects, actions, resources, and context. Once built, the policy can be added to a [PermissionChecker].
let custom_policy = PolicyBuilder::<MySubject, MyResource, MyAction, MyContext>::new("CustomPolicy")
.subjects(|s| /* ... */)
.actions(|a| /* ... */)
.resources(|r| /* ... */)
.context(|c| /* ... */)
.when(|s, a, r, c| /* ... */)
.build();
AndPolicy: Grants access only if all inner policies allow access OrPolicy: Grants access if any inner policy allows access NotPolicy: Inverts the decision of an inner policy
See the examples directory for complete demonstration of:
rbac_policy)rebac_policy)combinator_policy)Run with:
cargo run --example rbac_policy