Role System
A flexible and powerful role-based access control (RBAC) library for Rust applications.
🚀 Production-ready RBAC system with hierarchical roles, conditional permissions, and async support.
Features
✨ Core Features
- Hierarchical Roles with inheritance
- Fine-grained Permissions with wildcards
- Dynamic Role Management and elevation
- Context-based Conditional Permissions
- Thread-safe Concurrent Access
🔒 Security
- Fail-safe Default Deny
- Comprehensive Audit Logging
- Input Validation and Sanitization
- Security-first Design
⚡ Performance
- Built-in Permission Caching
- Efficient Lock-free Data Structures
- Optimized Pattern Matching
- Detailed Performance Analysis
🔧 Integration
- Comprehensive Middleware Examples - NEW!
- Web Framework Examples
- Multiple Storage Backends
- Async/Await Support
- Custom Storage Adapters
📚 Documentation
-
Comprehensive API Docs
-
Hierarchical Roles: Support for role inheritance and hierarchy
-
Fine-grained Permissions: Detailed permission control with action and resource type specifications
-
Dynamic Role Management: Runtime role assignment, removal, and temporary elevation
-
Conditional Permissions: Context-based permission validation
-
Multiple Subject Types: Support for users, groups, services, and devices
-
Thread-safe: Built with concurrent access in mind using
DashMap -
Async Support: Optional async/await support with Tokio
-
Persistence: Optional serialization support with Serde
-
Audit Logging: Optional audit trail with tracing
-
Caching: Built-in permission caching for performance
-
Flexible Storage: Pluggable storage backends (memory, file, custom)
Quick Start
Add this to your Cargo.toml:
[]
= "0.1"
⚡ 5-Minute Integration
For web applications, check out our comprehensive middleware examples that show how to integrate with popular frameworks in just 5-10 lines of code:
- Actix Web - Custom middleware with Transform trait
- Axum - Type-safe extractors and layers
- Rocket - Request guards and fairings
- Warp - Filter-based authorization
Each example includes JWT authentication, role-based authorization, and production-ready error handling.
Basic Example
use ;
Advanced Example with Conditional Permissions
use ;
use HashMap;
Async Example
use ;
async
Features and Configuration
Feature Flags
async(default): Enables async/await support with Tokiopersistence: Enables serialization support with Serdeaudit: Enables audit logging with tracing
[]
= { = "0.1", = ["persistence", "audit"] }
Storage Backends
The role system supports multiple storage backends:
Memory Storage (Default)
use ;
let role_system = new;
File Storage (with persistence feature)
use ;
let storage = new?;
let config = default;
let role_system = with_storage;
Custom Storage
Implement the Storage trait to create your own storage backend:
use Storage;
Architecture
Core Components
- RoleSystem: Main entry point for role-based access control
- Role: Represents a collection of permissions that can be assigned to subjects
- Permission: Represents an action that can be performed on a resource type
- Subject: Represents an entity that can have roles (user, group, service, device)
- Resource: Represents something that can be accessed or acted upon
- Storage: Pluggable storage backend for persisting role data
Permission Model
Permissions follow the format action:resource_type:
read:documents- Read access to documentswrite:users- Write access to user recordsadmin:*- Admin access to all resource types*:documents- All actions on documents
Role Hierarchy
Roles can inherit from other roles, creating a hierarchy:
// admin inherits all permissions from writer
role_system.add_role_inheritance?;
// writer inherits all permissions from reader
role_system.add_role_inheritance?;
Subject Types
The system supports different types of subjects:
- User: Human users
- Group: Collections of users
- Service: Applications or services
- Device: IoT devices or systems
- Custom: User-defined subject types
Temporary Role Elevation
Users can be temporarily granted additional roles:
// Elevate user to admin role for 1 hour
role_system.elevate_role?;
Performance
- Caching: Built-in permission caching to avoid repeated calculations
- Thread-safe: Uses
DashMapfor concurrent access without locks - Efficient hierarchy: Smart hierarchy traversal with cycle detection
- Lazy evaluation: Permissions are only calculated when needed
Security Considerations
- Fail-safe defaults: Permissions are denied by default
- Explicit grants: All permissions must be explicitly granted
- Hierarchy validation: Prevents circular dependencies in role hierarchy
- Audit logging: Optional comprehensive audit trail
- Context validation: Conditional permissions based on runtime context
Contributing
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
License
This project is licensed under either of
- Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Acknowledgments
This library was extracted and evolved from the role management system originally developed for the COAD (Code Organization and Analysis Dashboard) project, providing a standalone, reusable RBAC solution for the Rust ecosystem.