abs-auth-rbac-core 0.1.0__py3-none-any.whl

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.

Potentially problematic release.


This version of abs-auth-rbac-core might be problematic. Click here for more details.

@@ -0,0 +1,232 @@
1
+ Metadata-Version: 2.3
2
+ Name: abs-auth-rbac-core
3
+ Version: 0.1.0
4
+ Summary: RBAC and Auth core utilities including JWT token management.
5
+ License: MIT
6
+ Author: AutoBridgeSystems
7
+ Author-email: info@autobridgesystems.com
8
+ Requires-Python: >=3.13,<4.0
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Programming Language :: Python :: 3
11
+ Classifier: Programming Language :: Python :: 3.13
12
+ Requires-Dist: abs-exception-core (>=0.1.0,<0.2.0)
13
+ Requires-Dist: casbin (>=1.41.0,<2.0.0)
14
+ Requires-Dist: casbin-sqlalchemy-adapter (>=1.4.0,<2.0.0)
15
+ Requires-Dist: fastapi[standard] (>=0.115.12,<0.116.0)
16
+ Requires-Dist: passlib (>=1.7.4,<2.0.0)
17
+ Requires-Dist: pyjwt (>=2.10.1,<3.0.0)
18
+ Requires-Dist: sqlalchemy (>=2.0.40,<3.0.0)
19
+ Description-Content-Type: text/markdown
20
+
21
+ # ABS Auth RBAC Core
22
+
23
+ A comprehensive authentication and Role-Based Access Control (RBAC) package for FastAPI applications. This package provides robust JWT-based authentication and flexible role-based permission management using Casbin.
24
+
25
+ ## Features
26
+
27
+ - JWT-based authentication with customizable token expiration
28
+ - Password hashing using bcrypt
29
+ - Role-Based Access Control (RBAC) with Casbin integration
30
+ - Flexible permission management
31
+ - User-role and role-permission associations
32
+ - Middleware for authentication and authorization
33
+
34
+ ## Installation
35
+
36
+ ```bash
37
+ pip install abs-auth-rbac-core
38
+ ```
39
+
40
+ ## Quick Start
41
+
42
+ ### 1. Authentication Setup
43
+
44
+ ```python
45
+ from abs_auth_rbac_core.auth.jwt_functions import JWTFunctions
46
+ import os
47
+
48
+ # Initialize JWT functions with environment variables
49
+ jwt_functions = JWTFunctions(
50
+ secret_key=os.getenv("JWT_SECRET_KEY"),
51
+ algorithm=os.getenv("JWT_ALGORITHM", "HS256"),
52
+ expire_minutes=int(os.getenv("JWT_EXPIRE_MINUTES", "60"))
53
+ )
54
+
55
+ # Create access token
56
+ token = jwt_functions.create_access_token(data={"sub": "user_id"})
57
+
58
+ # Verify password
59
+ is_valid = jwt_functions.verify_password(plain_password, hashed_password)
60
+
61
+ # Get password hash
62
+ hashed_password = jwt_functions.get_password_hash(plain_password)
63
+ ```
64
+
65
+ ### 2. RBAC Setup
66
+
67
+ ```python
68
+ from abs_auth_rbac_core.rbac.service import RBACService
69
+
70
+ # Initialize RBAC service
71
+ rbac_service = RBACService(
72
+ session=your_db_session
73
+ )
74
+
75
+ # Create a role with permissions
76
+ role = rbac_service.create_role(
77
+ name="admin",
78
+ description="Administrator role",
79
+ permission_ids=["permission_uuid1", "permission_uuid2"]
80
+ )
81
+
82
+ # Assign roles to user
83
+ rbac_service.bulk_assign_roles_to_user(
84
+ user_uuid="user_uuid",
85
+ role_uuids=["role_uuid1", "role_uuid2"]
86
+ )
87
+
88
+ # Check permission
89
+ has_permission = rbac_service.check_permission(
90
+ user_uuid="user_uuid",
91
+ resource="resource_name",
92
+ action="action_name",
93
+ module="module_name"
94
+ )
95
+ ```
96
+
97
+ ## Core Components
98
+
99
+ ### Authentication (`auth/`)
100
+ - `jwt_functions.py`: JWT token management and password hashing
101
+ - `middleware.py`: Authentication middleware for FastAPI
102
+ - `auth_functions.py`: Core authentication functions
103
+
104
+ ### RBAC (`rbac/`)
105
+ - `service.py`: Main RBAC service with role and permission management
106
+ - `decorator.py`: Decorators for permission checking
107
+
108
+ ### Models (`models/`)
109
+ - `user.py`: User model
110
+ - `roles.py`: Role model
111
+ - `permissions.py`: Permission model
112
+ - `user_role.py`: User-Role association model
113
+ - `role_permission.py`: Role-Permission association model
114
+ - `rbac_model.py`: Base RBAC model
115
+ - `base_model.py`: Base model with common fields
116
+
117
+ ## Usage Examples
118
+
119
+ ### 1. Setting Up Authentication Middleware
120
+
121
+ ```python
122
+ from fastapi import FastAPI, Depends
123
+ from dependency_injector import containers, providers
124
+ from abs_auth_rbac_core.auth.middleware import auth_middleware
125
+ from abs_auth_rbac_core.rbac import RBACService
126
+
127
+ # Create a container for dependency injection
128
+ class Container(containers.DeclarativeContainer):
129
+ # Database session provider
130
+ db_session = providers.Factory(your_db_session_factory)
131
+
132
+ # RBAC service provider
133
+ rbac_service = providers.Factory(
134
+ RBACService,
135
+ session=db_session
136
+ )
137
+
138
+ # Auth middleware provider
139
+ get_auth_middleware = providers.Factory(
140
+ auth_middleware,
141
+ db_session=db_session,
142
+ jwt_secret_key=os.getenv("JWT_SECRET_KEY"),
143
+ jwt_algorithm=os.getenv("JWT_ALGORITHM", "HS256")
144
+ )
145
+
146
+ # Initialize FastAPI app
147
+ app = FastAPI()
148
+ container = Container()
149
+ app.container = container
150
+ ```
151
+
152
+ ### 2. Applying Middleware to Routers
153
+
154
+ ```python
155
+ from fastapi import FastAPI, Depends
156
+ from src.core.container import Container
157
+
158
+ class CreateApp:
159
+ def __init__(self):
160
+ self.container = Container()
161
+ self.auth_middleware = self.container.get_auth_middleware()
162
+
163
+ self.app = FastAPI(
164
+ title="Your Service",
165
+ description="Service Description",
166
+ version="0.0.1"
167
+ )
168
+
169
+ # Apply middleware to specific routers
170
+ self.app.include_router(
171
+ users_router,
172
+ dependencies=[Depends(self.auth_middleware)],
173
+ tags=["Users"]
174
+ )
175
+
176
+ # Public routes (no middleware)
177
+ self.app.include_router(
178
+ public_router,
179
+ tags=["Public"]
180
+ )
181
+ ```
182
+
183
+ ### 3. Permission Management
184
+
185
+ ```python
186
+ from abs_auth_rbac_core.util.permission_constants import (
187
+ PermissionAction,
188
+ PermissionModule,
189
+ PermissionResource
190
+ )
191
+
192
+ # permissions
193
+ permission = PermissionData(
194
+ name="User Management",
195
+ description="Manage user accounts",
196
+ module=PermissionModule.USER_MANAGEMENT,
197
+ resource=PermissionResource.USER_MANAGEMENT,
198
+ action=PermissionAction.MANAGE
199
+ )
200
+
201
+ # Check permissions in route
202
+ @app.get("/users")
203
+ @rbac_require_permission(
204
+ f"{PermissionModule.USER_MANAGEMENT.value}:{PermissionResource.USER_MANAGEMENT.value}:{PermissionAction.VIEW.value}"
205
+ )
206
+ async def list_users():
207
+ return {"users": [...]}
208
+ ```
209
+
210
+ ## Error Handling
211
+
212
+ The package includes comprehensive error handling for common scenarios:
213
+ - `UnauthorizedError`: For invalid or expired tokens
214
+ - `ValidationError`: For invalid token formats
215
+ - `DuplicatedError`: For duplicate role names
216
+ - `NotFoundError`: For non-existent resources
217
+ - `PermissionDeniedError`: For insufficient permissions
218
+
219
+ ## Best Practices
220
+
221
+ 1. Always use environment variables for sensitive data (secret keys, etc.)
222
+ 2. Implement proper error handling for authentication and authorization failures
223
+ 3. Use the middleware for global authentication
224
+ 4. Implement proper logging for security-related events
225
+ 5. Regularly rotate secret keys and tokens
226
+ 6. Use strong password policies
227
+ 7. Implement rate limiting for authentication endpoints
228
+
229
+ ## License
230
+
231
+ This project is licensed under the MIT License - see the LICENSE file for details.
232
+
@@ -0,0 +1,25 @@
1
+ abs_auth_rbac_core/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
+ abs_auth_rbac_core/auth/__init__.py,sha256=Pvetd98VD9jsok1F9e82yS9kLZMFqTpbN51hkxZQHzw,67
3
+ abs_auth_rbac_core/auth/auth_functions.py,sha256=fhfKRhtpE_J9MHu2jSsIA-cy77A3wCAQKbGGZeh4pe4,947
4
+ abs_auth_rbac_core/auth/jwt_functions.py,sha256=9vhjWrxXdE8fVQ4FGrPj9y6PoSEsaeFohPhgI-3hToI,4111
5
+ abs_auth_rbac_core/auth/middleware.py,sha256=1J0yE2aEgfMzTgkSz1Tz_8xs0Nl095k5S1wbygShkl0,1806
6
+ abs_auth_rbac_core/models/__init__.py,sha256=HHnXZ1wIq6Us3MP9YywjHVeCViXD_zyMWl8mBglqFBc,231
7
+ abs_auth_rbac_core/models/base_model.py,sha256=AaWObslm8sTetv4H1Ia_gPpi_75uF5z1o7Et9WAvstU,612
8
+ abs_auth_rbac_core/models/gov_casbin_rule.py,sha256=uWkzOgtG0_8CXR4YLBz_JINA8apcB1D4aWiCgSkuTCs,789
9
+ abs_auth_rbac_core/models/permissions.py,sha256=RkOSWiHy3Bi6AOE7Oc9hLUZKGp41-jXkhKnCNsv9N0E,715
10
+ abs_auth_rbac_core/models/rbac_model.py,sha256=GbgMA-lJoU__xYP7wCw2FB9P1ftMzRAU2sWyGnmqnt4,323
11
+ abs_auth_rbac_core/models/role_permission.py,sha256=KQ7MGwFvHhXgWL73TGH_elfG0rTLj5Ct11EWi6ypNb8,414
12
+ abs_auth_rbac_core/models/roles.py,sha256=3g52YoCAnVdId4iaQa6Jz1NUTnaZTM_i_4oGF-FneA0,657
13
+ abs_auth_rbac_core/models/seeder/permission_seeder.py,sha256=j-aUy8uLHnUWpMmw1DqXq8yJcqWxQo-D5QjY1S-ifyA,3652
14
+ abs_auth_rbac_core/models/user.py,sha256=t_ardJOsfBiyedPg6Z4WywLiZRTPmAGYkMealZcEqJc,833
15
+ abs_auth_rbac_core/models/user_role.py,sha256=20pqmtJPzlUrI9ulHGouk8XlFgrGG7I6ikctb8sMUGs,706
16
+ abs_auth_rbac_core/rbac/__init__.py,sha256=oYjtpmfrkEbwWCBAWuRoU1fM4fCpBxkF_lwQrelK1As,79
17
+ abs_auth_rbac_core/rbac/database.py,sha256=4lxs3gDIMq4pnor8oHN51UImp9Ad-6_HiIdPiLI-LGc,1475
18
+ abs_auth_rbac_core/rbac/decorator.py,sha256=y1TJqVQLWJeMqj7PxYqlAQZ5sOj1hZv9AvChGspl8A0,1794
19
+ abs_auth_rbac_core/rbac/policy.conf,sha256=wghhhKxgZH0rPhh1QFrIpq9nevJT3s7OxxvXiU3zzuI,305
20
+ abs_auth_rbac_core/rbac/service.py,sha256=c7n7PIhs5EDPVvjjPc3gsgTFej8WFlmWuYBQiptJNY8,25645
21
+ abs_auth_rbac_core/util/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
22
+ abs_auth_rbac_core/util/permission_constants.py,sha256=JFavEKkAJ5mtltttCwRTTtpRlu_mJiVPB_MwsP-bIAg,65337
23
+ abs_auth_rbac_core-0.1.0.dist-info/METADATA,sha256=xnNkKtEVVrL39TehlfrKK4CLn_1AVia6BBAGITHrws8,6586
24
+ abs_auth_rbac_core-0.1.0.dist-info/WHEEL,sha256=fGIA9gx4Qxk2KDKeNJCbOEwSrmLtjWCwzBz351GyrPQ,88
25
+ abs_auth_rbac_core-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: poetry-core 2.1.2
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any