rbac-guard 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- rbac_guard-0.1.0/PKG-INFO +32 -0
- rbac_guard-0.1.0/README.md +23 -0
- rbac_guard-0.1.0/pyproject.toml +17 -0
- rbac_guard-0.1.0/rbac/__init__.py +3 -0
- rbac_guard-0.1.0/rbac/engine.py +34 -0
- rbac_guard-0.1.0/rbac_guard.egg-info/PKG-INFO +32 -0
- rbac_guard-0.1.0/rbac_guard.egg-info/SOURCES.txt +8 -0
- rbac_guard-0.1.0/rbac_guard.egg-info/dependency_links.txt +1 -0
- rbac_guard-0.1.0/rbac_guard.egg-info/top_level.txt +1 -0
- rbac_guard-0.1.0/setup.cfg +4 -0
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: rbac-guard
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Simple role-based authorization extension
|
|
5
|
+
Author: Sajan Nepali
|
|
6
|
+
License: MIT
|
|
7
|
+
Requires-Python: >=3.8
|
|
8
|
+
Description-Content-Type: text/markdown
|
|
9
|
+
|
|
10
|
+
# RBAC Engine
|
|
11
|
+
|
|
12
|
+
A lightweight policy-based Role-Based Access Control (RBAC) engine for Python.
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
pip install rbac
|
|
18
|
+
from rbac import PolicyEngine
|
|
19
|
+
|
|
20
|
+
# Initialize the engine
|
|
21
|
+
engine = PolicyEngine()
|
|
22
|
+
|
|
23
|
+
# Add roles and permissions
|
|
24
|
+
engine.add_role("admin", ["*"])
|
|
25
|
+
engine.add_role("editor", ["read", "write"])
|
|
26
|
+
|
|
27
|
+
# Assign roles to users
|
|
28
|
+
engine.assign_role("user_123", "editor")
|
|
29
|
+
|
|
30
|
+
# Evaluate permissions
|
|
31
|
+
print(engine.evaluate("user_123", "write")) # True
|
|
32
|
+
print(engine.evaluate("user_123", "delete")) # False
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# RBAC Engine
|
|
2
|
+
|
|
3
|
+
A lightweight policy-based Role-Based Access Control (RBAC) engine for Python.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pip install rbac
|
|
9
|
+
from rbac import PolicyEngine
|
|
10
|
+
|
|
11
|
+
# Initialize the engine
|
|
12
|
+
engine = PolicyEngine()
|
|
13
|
+
|
|
14
|
+
# Add roles and permissions
|
|
15
|
+
engine.add_role("admin", ["*"])
|
|
16
|
+
engine.add_role("editor", ["read", "write"])
|
|
17
|
+
|
|
18
|
+
# Assign roles to users
|
|
19
|
+
engine.assign_role("user_123", "editor")
|
|
20
|
+
|
|
21
|
+
# Evaluate permissions
|
|
22
|
+
print(engine.evaluate("user_123", "write")) # True
|
|
23
|
+
print(engine.evaluate("user_123", "delete")) # False
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61.0"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "rbac-guard"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Simple role-based authorization extension"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.8"
|
|
11
|
+
license = { text = "MIT" }
|
|
12
|
+
authors = [
|
|
13
|
+
{ name = "Sajan Nepali" }
|
|
14
|
+
]
|
|
15
|
+
dependencies = [
|
|
16
|
+
# e.g. "django>=3.2"
|
|
17
|
+
]
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
from typing import Dict, Set, Optional
|
|
2
|
+
|
|
3
|
+
class PolicyEngine:
|
|
4
|
+
def __init__(self):
|
|
5
|
+
# Maps role_name -> set of permitted actions/permissions
|
|
6
|
+
self.roles: Dict[str, Set[str]] = {}
|
|
7
|
+
|
|
8
|
+
# Maps user_id -> set of assigned roles
|
|
9
|
+
self.user_roles: Dict[str, Set[str]] = {}
|
|
10
|
+
|
|
11
|
+
def add_role(self, role: str, permissions: list[str]) -> None:
|
|
12
|
+
"""Define a role and its associated permissions."""
|
|
13
|
+
self.roles[role] = set(permissions)
|
|
14
|
+
|
|
15
|
+
def assign_role(self, user_id: str, role: str) -> None:
|
|
16
|
+
"""Assign a role to a user."""
|
|
17
|
+
if role not in self.roles:
|
|
18
|
+
raise ValueError(f"Role '{role}' does not exist.")
|
|
19
|
+
if user_id not in self.user_roles:
|
|
20
|
+
self.user_roles[user_id] = set()
|
|
21
|
+
self.user_roles[user_id].add(role)
|
|
22
|
+
|
|
23
|
+
def evaluate(self, user_id: str, action: str) -> bool:
|
|
24
|
+
"""
|
|
25
|
+
Evaluate whether a user has a role containing the required action/permission.
|
|
26
|
+
"""
|
|
27
|
+
user_assigned_roles = self.user_roles.get(user_id, set())
|
|
28
|
+
|
|
29
|
+
for role in user_assigned_roles:
|
|
30
|
+
role_perms = self.roles.get(role, set())
|
|
31
|
+
if action in role_perms or "*" in role_perms:
|
|
32
|
+
return True
|
|
33
|
+
|
|
34
|
+
return False
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: rbac-guard
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Simple role-based authorization extension
|
|
5
|
+
Author: Sajan Nepali
|
|
6
|
+
License: MIT
|
|
7
|
+
Requires-Python: >=3.8
|
|
8
|
+
Description-Content-Type: text/markdown
|
|
9
|
+
|
|
10
|
+
# RBAC Engine
|
|
11
|
+
|
|
12
|
+
A lightweight policy-based Role-Based Access Control (RBAC) engine for Python.
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
pip install rbac
|
|
18
|
+
from rbac import PolicyEngine
|
|
19
|
+
|
|
20
|
+
# Initialize the engine
|
|
21
|
+
engine = PolicyEngine()
|
|
22
|
+
|
|
23
|
+
# Add roles and permissions
|
|
24
|
+
engine.add_role("admin", ["*"])
|
|
25
|
+
engine.add_role("editor", ["read", "write"])
|
|
26
|
+
|
|
27
|
+
# Assign roles to users
|
|
28
|
+
engine.assign_role("user_123", "editor")
|
|
29
|
+
|
|
30
|
+
# Evaluate permissions
|
|
31
|
+
print(engine.evaluate("user_123", "write")) # True
|
|
32
|
+
print(engine.evaluate("user_123", "delete")) # False
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
rbac
|