|
| 1 | +// Package rbac provides a flexible, upgradeable Role-Based Access Control (RBAC) |
| 2 | +// system for Gno smart contracts and related applications. It decouples authorization |
| 3 | +// logic from fixed addresses, enabling dynamic registration, update, and removal of roles |
| 4 | +// and permissions. |
| 5 | +// |
| 6 | +// ## Overview |
| 7 | +// |
| 8 | +// The RBAC package encapsulates a manager that maintains an internal registry of roles. |
| 9 | +// Each role is defined by a unique name and a set of permissions. A permission is |
| 10 | +// represented by a `PermissionChecker` function that validates whether a given caller |
| 11 | +// (`std.Address`) satisfies the required access conditions. |
| 12 | +// |
| 13 | +// Key components of this package include: |
| 14 | +// |
| 15 | +// 1. **Role**: Represents a role with a name and a collection of permission-checking functions. |
| 16 | +// 2. **PermissionChecker**: A function type defined as `func(caller std.Address) error`, |
| 17 | +// used to verify access for a given permission. |
| 18 | +// 3. **RBAC Manager**: The core type (RBAC) that encapsulates role registration, permission |
| 19 | +// assignment, verification, updating, and removal. |
| 20 | +// |
| 21 | +// ## Key Features |
| 22 | +// |
| 23 | +// - **Dynamic Role Management**: Roles can be registered, and permissions can be assigned |
| 24 | +// or updated at runtime without requiring contract redeployment. |
| 25 | +// - **Multiple Permissions per Role**: A single role can have multiple permissions, |
| 26 | +// each with its own validation logic. |
| 27 | +// - **Declarative Role Definition**: The package supports a Functional Option pattern, |
| 28 | +// allowing roles and their permissions to be defined declaratively via functions like |
| 29 | +// `DeclareRole` and `WithPermission`. |
| 30 | +// - **Encapsulation**: Internal state (roles registry) is encapsulated within the RBAC |
| 31 | +// manager, preventing unintended external modifications. |
| 32 | +// - **Flexible Validation**: Permission checkers can implement custom logic, supporting |
| 33 | +// arbitrary access control policies. |
| 34 | +// |
| 35 | +// ## Workflow |
| 36 | +// |
| 37 | +// Typical usage of the RBAC package includes the following steps: |
| 38 | +// |
| 39 | +// 1. **Initialization**: Create a new RBAC manager using `NewRBAC()`. |
| 40 | +// 2. **Role Registration**: Register roles using `RegisterRole` or declaratively with |
| 41 | +// `DeclareRole`. |
| 42 | +// 3. **Permission Assignment**: Add permissions to roles using `RegisterPermission` or the |
| 43 | +// `WithPermission` option during role declaration. |
| 44 | +// 4. **Permission Verification**: Validate access by invoking `CheckPermission` with the |
| 45 | +// role name, permission name, and the caller's address (std.Address). |
| 46 | +// |
| 47 | +// ## Example Usage |
| 48 | +// |
| 49 | +// The following example demonstrates how to use the RBAC package in both traditional and |
| 50 | +// declarative styles: |
| 51 | +// |
| 52 | +// ```gno |
| 53 | +// package main |
| 54 | +// |
| 55 | +// import ( |
| 56 | +// "std" |
| 57 | +// |
| 58 | +// "gno.land/p/gnoswap/rbac" |
| 59 | +// "gno.land/p/demo/ufmt" |
| 60 | +// ) |
| 61 | +// |
| 62 | +// func main() { |
| 63 | +// // Create a new RBAC manager |
| 64 | +// manager := rbac.NewRBAC() |
| 65 | +// |
| 66 | +// // Define example addresses |
| 67 | +// adminAddr := std.Address("admin") |
| 68 | +// userAddr := std.Address("user") |
| 69 | +// |
| 70 | +// // --- Traditional Role Registration --- |
| 71 | +// // Register an "admin" role |
| 72 | +// if err := manager.RegisterRole("admin"); err != nil { |
| 73 | +// panic(err) |
| 74 | +// } |
| 75 | +// |
| 76 | +// // Register an "access" permission for the "admin" role. |
| 77 | +// // The checker verifies that the caller matches adminAddr. |
| 78 | +// adminChecker := func(caller std.Address) error { |
| 79 | +// if caller != adminAddr { |
| 80 | +// return ufmt.Errorf("caller %s is not admin", caller) |
| 81 | +// } |
| 82 | +// return nil |
| 83 | +// } |
| 84 | +// if err := manager.RegisterPermission("admin", "access", adminChecker); err != nil { |
| 85 | +// panic(err) |
| 86 | +// } |
| 87 | +// |
| 88 | +// // --- Declarative Role Registration --- |
| 89 | +// // Register an "editor" role with a "modify" permission using the Functional Option pattern. |
| 90 | +// editorChecker := func(caller std.Address) error { |
| 91 | +// if caller != userAddr { |
| 92 | +// return ufmt.Errorf("caller %s is not editor", caller) |
| 93 | +// } |
| 94 | +// return nil |
| 95 | +// } |
| 96 | +// if err := manager.DeclareRole("editor", rbac.WithPermission("modify", editorChecker)); err != nil { |
| 97 | +// panic(err) |
| 98 | +// } |
| 99 | +// |
| 100 | +// // --- Permission Check --- |
| 101 | +// // Check if adminAddr has the "access" permission on the "admin" role. |
| 102 | +// if err := manager.CheckPermission("admin", "access", adminAddr); err != nil { |
| 103 | +// println("Access denied for admin:", err) |
| 104 | +// } else { |
| 105 | +// println("Admin access granted") |
| 106 | +// } |
| 107 | +// } |
| 108 | +// |
| 109 | +// ``` |
| 110 | +// |
| 111 | +// ## Error Handling |
| 112 | +// |
| 113 | +// The package reports errors using the ufmt.Errorf function. Typical errors include: |
| 114 | +// |
| 115 | +// - Registering a role that already exists. |
| 116 | +// - Attempting to register a permission for a non-existent role. |
| 117 | +// - Verifying a permission that does not exist on a role. |
| 118 | +// - Failing a permission check due to a caller not meeting the required conditions. |
| 119 | +// |
| 120 | +// ## Limitations and Considerations |
| 121 | +// |
| 122 | +// - This RBAC implementation does not directly map addresses to roles; instead, it verifies |
| 123 | +// the caller against permission-checking functions registered for a role. |
| 124 | +// - Address validation relies on the logic provided within each PermissionChecker. Ensure that |
| 125 | +// your checkers properly validate `std.Address` values (which follow the Bech32 format). |
| 126 | +// - The encapsulated RBAC manager is designed to minimize external mutation, but integrating it |
| 127 | +// with other modules may require additional mapping between addresses and roles. |
| 128 | +// |
| 129 | +// # Notes |
| 130 | +// |
| 131 | +// - The RBAC system is designed to be upgradeable, enabling contracts to modify permission |
| 132 | +// logic without redeploying the entire contract. |
| 133 | +// - Both imperative and declarative styles are supported, providing flexibility to developers. |
| 134 | +// |
| 135 | +// Package rbac is intended for use in Gno smart contracts and other systems requiring dynamic, |
| 136 | +// upgradeable access control mechanisms. |
| 137 | +package rbac |
0 commit comments