abs-auth-rbac-core 0.1.8__py3-none-any.whl → 0.3.18__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.
- abs_auth_rbac_core/auth/__init__.py +13 -1
- abs_auth_rbac_core/auth/auth_functions.py +4 -1
- abs_auth_rbac_core/auth/middleware.py +197 -3
- abs_auth_rbac_core/models/gov_casbin_rule.py +9 -0
- abs_auth_rbac_core/models/user.py +1 -0
- abs_auth_rbac_core/rbac/service.py +425 -139
- abs_auth_rbac_core/repository/__init__.py +4 -0
- abs_auth_rbac_core/repository/permission_repository.py +12 -0
- abs_auth_rbac_core/repository/role_repository.py +18 -0
- abs_auth_rbac_core/service/__init__.py +4 -0
- abs_auth_rbac_core/service/permission_service.py +15 -0
- abs_auth_rbac_core/service/role_service.py +18 -0
- abs_auth_rbac_core/util/permission_constants.py +850 -16
- abs_auth_rbac_core-0.3.18.dist-info/METADATA +727 -0
- {abs_auth_rbac_core-0.1.8.dist-info → abs_auth_rbac_core-0.3.18.dist-info}/RECORD +16 -10
- abs_auth_rbac_core-0.1.8.dist-info/METADATA +0 -233
- {abs_auth_rbac_core-0.1.8.dist-info → abs_auth_rbac_core-0.3.18.dist-info}/WHEEL +0 -0
|
@@ -1,27 +1,33 @@
|
|
|
1
1
|
abs_auth_rbac_core/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
-
abs_auth_rbac_core/auth/__init__.py,sha256=
|
|
3
|
-
abs_auth_rbac_core/auth/auth_functions.py,sha256=
|
|
2
|
+
abs_auth_rbac_core/auth/__init__.py,sha256=5PUd5wjIVgdfoD8Zce2uOOE8TtBUhznVHPVwWaiw_9A,312
|
|
3
|
+
abs_auth_rbac_core/auth/auth_functions.py,sha256=p9sOExXOUDXHaP4dccraJ6Xv3RZu2dH4AtRv22yPKXU,1054
|
|
4
4
|
abs_auth_rbac_core/auth/jwt_functions.py,sha256=9vhjWrxXdE8fVQ4FGrPj9y6PoSEsaeFohPhgI-3hToI,4111
|
|
5
|
-
abs_auth_rbac_core/auth/middleware.py,sha256=
|
|
5
|
+
abs_auth_rbac_core/auth/middleware.py,sha256=09gpOdvfgV8QsFEQO46QIRgtTy9-0kPRCVob6RJeE1U,8897
|
|
6
6
|
abs_auth_rbac_core/models/__init__.py,sha256=9ImboxQ04XxRjd5o1RDBn465BOj3F2pahuVXF15NuqE,292
|
|
7
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=
|
|
8
|
+
abs_auth_rbac_core/models/gov_casbin_rule.py,sha256=9PpCQWg6TWeBvgjRcC2VxSSFMkNW-B9a4e2LmmDmmiY,1000
|
|
9
9
|
abs_auth_rbac_core/models/permissions.py,sha256=op_6t3OKDGzQvroVhYgoM7g_umr7MD-ZABjk5HdTpB8,859
|
|
10
10
|
abs_auth_rbac_core/models/rbac_model.py,sha256=GbgMA-lJoU__xYP7wCw2FB9P1ftMzRAU2sWyGnmqnt4,323
|
|
11
11
|
abs_auth_rbac_core/models/role_permission.py,sha256=KQ7MGwFvHhXgWL73TGH_elfG0rTLj5Ct11EWi6ypNb8,414
|
|
12
12
|
abs_auth_rbac_core/models/roles.py,sha256=3g52YoCAnVdId4iaQa6Jz1NUTnaZTM_i_4oGF-FneA0,657
|
|
13
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=
|
|
14
|
+
abs_auth_rbac_core/models/user.py,sha256=rlNXyQ22GKwrg4nIKhpyH-arDIgLOovZr0ZTQNjkLco,1064
|
|
15
15
|
abs_auth_rbac_core/models/user_permission.py,sha256=t1_VUSFyyZAfJK71liDkFkg51yCshCbuRMG9rxFyOro,600
|
|
16
16
|
abs_auth_rbac_core/models/user_role.py,sha256=20pqmtJPzlUrI9ulHGouk8XlFgrGG7I6ikctb8sMUGs,706
|
|
17
17
|
abs_auth_rbac_core/rbac/__init__.py,sha256=oYjtpmfrkEbwWCBAWuRoU1fM4fCpBxkF_lwQrelK1As,79
|
|
18
18
|
abs_auth_rbac_core/rbac/decorator.py,sha256=pEFAW0Nn2iE4KBctPhNOmO_VLeJFDX2V9v2LsCu6kHY,1824
|
|
19
19
|
abs_auth_rbac_core/rbac/policy.conf,sha256=wghhhKxgZH0rPhh1QFrIpq9nevJT3s7OxxvXiU3zzuI,305
|
|
20
|
-
abs_auth_rbac_core/rbac/service.py,sha256=
|
|
20
|
+
abs_auth_rbac_core/rbac/service.py,sha256=rztdSP0wZ-uSv4ASzvxuoHoz3Yng2sYAXSHo-j_-18s,44315
|
|
21
|
+
abs_auth_rbac_core/repository/__init__.py,sha256=tuEdEV5HsePiaEg2Jrakf-QOR3evTeS-2Tq5VqbywyU,154
|
|
22
|
+
abs_auth_rbac_core/repository/permission_repository.py,sha256=SQJyyErrrMnTnLJjhwZythPbYVGt5z0N5GJ5fV6Gvuo,541
|
|
23
|
+
abs_auth_rbac_core/repository/role_repository.py,sha256=OEPpWIm_61rOljPEcejqXyOvowYDK8Uh5K_pvRLfb3Y,562
|
|
21
24
|
abs_auth_rbac_core/schema/__init__.py,sha256=v9xibJ8Wr9k0u6PEYNK0LCGUJD71SB5vxu9BZG0S7tM,46
|
|
22
25
|
abs_auth_rbac_core/schema/permission.py,sha256=XvxPU68FY0PFgkF4GR2bSrzNvFB8c8OgY_d0JOJvMc8,203
|
|
26
|
+
abs_auth_rbac_core/service/__init__.py,sha256=zzzxVCUYYb4heFksjbktWqbST3IcTcTfOMWWC-L5_A0,136
|
|
27
|
+
abs_auth_rbac_core/service/permission_service.py,sha256=tWasmKe0lr1QokmKzjD08O251_ppTnfN9amqVZX_CCU,661
|
|
28
|
+
abs_auth_rbac_core/service/role_service.py,sha256=Q68igKS-cArHaq-tqrjWPpptnrXYImRAEwKQep0ZOBQ,633
|
|
23
29
|
abs_auth_rbac_core/util/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
24
|
-
abs_auth_rbac_core/util/permission_constants.py,sha256=
|
|
25
|
-
abs_auth_rbac_core-0.
|
|
26
|
-
abs_auth_rbac_core-0.
|
|
27
|
-
abs_auth_rbac_core-0.
|
|
30
|
+
abs_auth_rbac_core/util/permission_constants.py,sha256=P7r6uW6f9YsuxSSsk_oRVN4YqD2CdYvfgnjEmID3czU,103776
|
|
31
|
+
abs_auth_rbac_core-0.3.18.dist-info/METADATA,sha256=5NYRBQI6vSXcu1c7KZl__Q2xgXNFQDyG5mJ7DbPFOzM,23694
|
|
32
|
+
abs_auth_rbac_core-0.3.18.dist-info/WHEEL,sha256=fGIA9gx4Qxk2KDKeNJCbOEwSrmLtjWCwzBz351GyrPQ,88
|
|
33
|
+
abs_auth_rbac_core-0.3.18.dist-info/RECORD,,
|
|
@@ -1,233 +0,0 @@
|
|
|
1
|
-
Metadata-Version: 2.3
|
|
2
|
-
Name: abs-auth-rbac-core
|
|
3
|
-
Version: 0.1.8
|
|
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: psycopg2-binary (>=2.9.10,<3.0.0)
|
|
18
|
-
Requires-Dist: pyjwt (>=2.10.1,<3.0.0)
|
|
19
|
-
Requires-Dist: sqlalchemy (>=2.0.40,<3.0.0)
|
|
20
|
-
Description-Content-Type: text/markdown
|
|
21
|
-
|
|
22
|
-
# ABS Auth RBAC Core
|
|
23
|
-
|
|
24
|
-
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.
|
|
25
|
-
|
|
26
|
-
## Features
|
|
27
|
-
|
|
28
|
-
- JWT-based authentication with customizable token expiration
|
|
29
|
-
- Password hashing using bcrypt
|
|
30
|
-
- Role-Based Access Control (RBAC) with Casbin integration
|
|
31
|
-
- Flexible permission management
|
|
32
|
-
- User-role and role-permission associations
|
|
33
|
-
- Middleware for authentication and authorization
|
|
34
|
-
|
|
35
|
-
## Installation
|
|
36
|
-
|
|
37
|
-
```bash
|
|
38
|
-
pip install abs-auth-rbac-core
|
|
39
|
-
```
|
|
40
|
-
|
|
41
|
-
## Quick Start
|
|
42
|
-
|
|
43
|
-
### 1. Authentication Setup
|
|
44
|
-
|
|
45
|
-
```python
|
|
46
|
-
from abs_auth_rbac_core.auth.jwt_functions import JWTFunctions
|
|
47
|
-
import os
|
|
48
|
-
|
|
49
|
-
# Initialize JWT functions with environment variables
|
|
50
|
-
jwt_functions = JWTFunctions(
|
|
51
|
-
secret_key=os.getenv("JWT_SECRET_KEY"),
|
|
52
|
-
algorithm=os.getenv("JWT_ALGORITHM", "HS256"),
|
|
53
|
-
expire_minutes=int(os.getenv("JWT_EXPIRE_MINUTES", "60"))
|
|
54
|
-
)
|
|
55
|
-
|
|
56
|
-
# Create access token
|
|
57
|
-
token = jwt_functions.create_access_token(data={"sub": "user_id"})
|
|
58
|
-
|
|
59
|
-
# Verify password
|
|
60
|
-
is_valid = jwt_functions.verify_password(plain_password, hashed_password)
|
|
61
|
-
|
|
62
|
-
# Get password hash
|
|
63
|
-
hashed_password = jwt_functions.get_password_hash(plain_password)
|
|
64
|
-
```
|
|
65
|
-
|
|
66
|
-
### 2. RBAC Setup
|
|
67
|
-
|
|
68
|
-
```python
|
|
69
|
-
from abs_auth_rbac_core.rbac.service import RBACService
|
|
70
|
-
|
|
71
|
-
# Initialize RBAC service
|
|
72
|
-
rbac_service = RBACService(
|
|
73
|
-
session=your_db_session
|
|
74
|
-
)
|
|
75
|
-
|
|
76
|
-
# Create a role with permissions
|
|
77
|
-
role = rbac_service.create_role(
|
|
78
|
-
name="admin",
|
|
79
|
-
description="Administrator role",
|
|
80
|
-
permission_ids=["permission_uuid1", "permission_uuid2"]
|
|
81
|
-
)
|
|
82
|
-
|
|
83
|
-
# Assign roles to user
|
|
84
|
-
rbac_service.bulk_assign_roles_to_user(
|
|
85
|
-
user_uuid="user_uuid",
|
|
86
|
-
role_uuids=["role_uuid1", "role_uuid2"]
|
|
87
|
-
)
|
|
88
|
-
|
|
89
|
-
# Check permission
|
|
90
|
-
has_permission = rbac_service.check_permission(
|
|
91
|
-
user_uuid="user_uuid",
|
|
92
|
-
resource="resource_name",
|
|
93
|
-
action="action_name",
|
|
94
|
-
module="module_name"
|
|
95
|
-
)
|
|
96
|
-
```
|
|
97
|
-
|
|
98
|
-
## Core Components
|
|
99
|
-
|
|
100
|
-
### Authentication (`auth/`)
|
|
101
|
-
- `jwt_functions.py`: JWT token management and password hashing
|
|
102
|
-
- `middleware.py`: Authentication middleware for FastAPI
|
|
103
|
-
- `auth_functions.py`: Core authentication functions
|
|
104
|
-
|
|
105
|
-
### RBAC (`rbac/`)
|
|
106
|
-
- `service.py`: Main RBAC service with role and permission management
|
|
107
|
-
- `decorator.py`: Decorators for permission checking
|
|
108
|
-
|
|
109
|
-
### Models (`models/`)
|
|
110
|
-
- `user.py`: User model
|
|
111
|
-
- `roles.py`: Role model
|
|
112
|
-
- `permissions.py`: Permission model
|
|
113
|
-
- `user_role.py`: User-Role association model
|
|
114
|
-
- `role_permission.py`: Role-Permission association model
|
|
115
|
-
- `rbac_model.py`: Base RBAC model
|
|
116
|
-
- `base_model.py`: Base model with common fields
|
|
117
|
-
|
|
118
|
-
## Usage Examples
|
|
119
|
-
|
|
120
|
-
### 1. Setting Up Authentication Middleware
|
|
121
|
-
|
|
122
|
-
```python
|
|
123
|
-
from fastapi import FastAPI, Depends
|
|
124
|
-
from dependency_injector import containers, providers
|
|
125
|
-
from abs_auth_rbac_core.auth.middleware import auth_middleware
|
|
126
|
-
from abs_auth_rbac_core.rbac import RBACService
|
|
127
|
-
|
|
128
|
-
# Create a container for dependency injection
|
|
129
|
-
class Container(containers.DeclarativeContainer):
|
|
130
|
-
# Database session provider
|
|
131
|
-
db_session = providers.Factory(your_db_session_factory)
|
|
132
|
-
|
|
133
|
-
# RBAC service provider
|
|
134
|
-
rbac_service = providers.Factory(
|
|
135
|
-
RBACService,
|
|
136
|
-
session=db_session
|
|
137
|
-
)
|
|
138
|
-
|
|
139
|
-
# Auth middleware provider
|
|
140
|
-
get_auth_middleware = providers.Factory(
|
|
141
|
-
auth_middleware,
|
|
142
|
-
db_session=db_session,
|
|
143
|
-
jwt_secret_key=os.getenv("JWT_SECRET_KEY"),
|
|
144
|
-
jwt_algorithm=os.getenv("JWT_ALGORITHM", "HS256")
|
|
145
|
-
)
|
|
146
|
-
|
|
147
|
-
# Initialize FastAPI app
|
|
148
|
-
app = FastAPI()
|
|
149
|
-
container = Container()
|
|
150
|
-
app.container = container
|
|
151
|
-
```
|
|
152
|
-
|
|
153
|
-
### 2. Applying Middleware to Routers
|
|
154
|
-
|
|
155
|
-
```python
|
|
156
|
-
from fastapi import FastAPI, Depends
|
|
157
|
-
from src.core.container import Container
|
|
158
|
-
|
|
159
|
-
class CreateApp:
|
|
160
|
-
def __init__(self):
|
|
161
|
-
self.container = Container()
|
|
162
|
-
self.auth_middleware = self.container.get_auth_middleware()
|
|
163
|
-
|
|
164
|
-
self.app = FastAPI(
|
|
165
|
-
title="Your Service",
|
|
166
|
-
description="Service Description",
|
|
167
|
-
version="0.0.1"
|
|
168
|
-
)
|
|
169
|
-
|
|
170
|
-
# Apply middleware to specific routers
|
|
171
|
-
self.app.include_router(
|
|
172
|
-
users_router,
|
|
173
|
-
dependencies=[Depends(self.auth_middleware)],
|
|
174
|
-
tags=["Users"]
|
|
175
|
-
)
|
|
176
|
-
|
|
177
|
-
# Public routes (no middleware)
|
|
178
|
-
self.app.include_router(
|
|
179
|
-
public_router,
|
|
180
|
-
tags=["Public"]
|
|
181
|
-
)
|
|
182
|
-
```
|
|
183
|
-
|
|
184
|
-
### 3. Permission Management
|
|
185
|
-
|
|
186
|
-
```python
|
|
187
|
-
from abs_auth_rbac_core.util.permission_constants import (
|
|
188
|
-
PermissionAction,
|
|
189
|
-
PermissionModule,
|
|
190
|
-
PermissionResource
|
|
191
|
-
)
|
|
192
|
-
|
|
193
|
-
# permissions
|
|
194
|
-
permission = PermissionData(
|
|
195
|
-
name="User Management",
|
|
196
|
-
description="Manage user accounts",
|
|
197
|
-
module=PermissionModule.USER_MANAGEMENT,
|
|
198
|
-
resource=PermissionResource.USER_MANAGEMENT,
|
|
199
|
-
action=PermissionAction.MANAGE
|
|
200
|
-
)
|
|
201
|
-
|
|
202
|
-
# Check permissions in route
|
|
203
|
-
@app.get("/users")
|
|
204
|
-
@rbac_require_permission(
|
|
205
|
-
f"{PermissionModule.USER_MANAGEMENT.value}:{PermissionResource.USER_MANAGEMENT.value}:{PermissionAction.VIEW.value}"
|
|
206
|
-
)
|
|
207
|
-
async def list_users():
|
|
208
|
-
return {"users": [...]}
|
|
209
|
-
```
|
|
210
|
-
|
|
211
|
-
## Error Handling
|
|
212
|
-
|
|
213
|
-
The package includes comprehensive error handling for common scenarios:
|
|
214
|
-
- `UnauthorizedError`: For invalid or expired tokens
|
|
215
|
-
- `ValidationError`: For invalid token formats
|
|
216
|
-
- `DuplicatedError`: For duplicate role names
|
|
217
|
-
- `NotFoundError`: For non-existent resources
|
|
218
|
-
- `PermissionDeniedError`: For insufficient permissions
|
|
219
|
-
|
|
220
|
-
## Best Practices
|
|
221
|
-
|
|
222
|
-
1. Always use environment variables for sensitive data (secret keys, etc.)
|
|
223
|
-
2. Implement proper error handling for authentication and authorization failures
|
|
224
|
-
3. Use the middleware for global authentication
|
|
225
|
-
4. Implement proper logging for security-related events
|
|
226
|
-
5. Regularly rotate secret keys and tokens
|
|
227
|
-
6. Use strong password policies
|
|
228
|
-
7. Implement rate limiting for authentication endpoints
|
|
229
|
-
|
|
230
|
-
## License
|
|
231
|
-
|
|
232
|
-
This project is licensed under the MIT License - see the LICENSE file for details.
|
|
233
|
-
|
|
File without changes
|