rb-commons 0.1.15__py3-none-any.whl → 0.1.17__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.
- rb_commons/configs/injections.py +5 -1
- rb_commons/permissions/__init__.py +2 -0
- rb_commons/permissions/http_exceptions.py +41 -0
- rb_commons/permissions/role_permissions.py +24 -0
- {rb_commons-0.1.15.dist-info → rb_commons-0.1.17.dist-info}/METADATA +1 -1
- {rb_commons-0.1.15.dist-info → rb_commons-0.1.17.dist-info}/RECORD +8 -5
- {rb_commons-0.1.15.dist-info → rb_commons-0.1.17.dist-info}/WHEEL +0 -0
- {rb_commons-0.1.15.dist-info → rb_commons-0.1.17.dist-info}/top_level.txt +0 -0
rb_commons/configs/injections.py
CHANGED
@@ -1,4 +1,6 @@
|
|
1
1
|
from typing import Annotated
|
2
|
+
|
3
|
+
from rb_commons.permissions import IsAdmin, IsCustomer
|
2
4
|
from rb_commons.schemes.jwt import Claims
|
3
5
|
from fastapi import Request, Depends
|
4
6
|
|
@@ -6,4 +8,6 @@ from fastapi import Request, Depends
|
|
6
8
|
async def get_claims(request: Request) -> Claims:
|
7
9
|
return Claims.from_headers(dict(request.headers))
|
8
10
|
|
9
|
-
ClaimsDep = Annotated[Claims, Depends(get_claims)]
|
11
|
+
ClaimsDep = Annotated[Claims, Depends(get_claims)]
|
12
|
+
IsAdminDep = Annotated[IsAdmin, Depends(IsAdmin())]
|
13
|
+
IsCustomerDep = Annotated[IsCustomer, Depends(IsCustomer())]
|
@@ -0,0 +1,41 @@
|
|
1
|
+
from fastapi import HTTPException, status
|
2
|
+
|
3
|
+
|
4
|
+
class CustomHTTPException(HTTPException):
|
5
|
+
def __init__(self, message: str, status: int, code: str = None, additional_info: dict = None):
|
6
|
+
super().__init__(status_code=status, detail=message)
|
7
|
+
|
8
|
+
self.code = code
|
9
|
+
self.message = message
|
10
|
+
self.status = status
|
11
|
+
self.additional_info = additional_info
|
12
|
+
|
13
|
+
|
14
|
+
class NotAuthorizedException(CustomHTTPException):
|
15
|
+
def __init__(self, message: str = "You are not authorized", status: int = 401, code: str = None,
|
16
|
+
additional_info: dict = None):
|
17
|
+
super().__init__(message=message, status=status, code=code, additional_info=additional_info)
|
18
|
+
|
19
|
+
|
20
|
+
class ForbiddenException(CustomHTTPException):
|
21
|
+
def __init__(self, message: str = "Permission denied", status: int = 403, code: str = None,
|
22
|
+
additional_info: dict = None):
|
23
|
+
super().__init__(message=message, status=status, code=code, additional_info=additional_info)
|
24
|
+
|
25
|
+
|
26
|
+
class BadRequestException(CustomHTTPException):
|
27
|
+
def __init__(self, message: str = "Bad request", status: int = 400, code: str = None,
|
28
|
+
additional_info: dict = None):
|
29
|
+
super().__init__(message=message, status=status, code=code, additional_info=additional_info)
|
30
|
+
|
31
|
+
|
32
|
+
class NotFoundException(CustomHTTPException):
|
33
|
+
def __init__(self, message: str = "Not found", status: int = 404, code: str = None,
|
34
|
+
additional_info: dict = None):
|
35
|
+
super().__init__(message=message, status=status, code=code, additional_info=additional_info)
|
36
|
+
|
37
|
+
|
38
|
+
class InternalException(CustomHTTPException):
|
39
|
+
def __init__(self, message: str = "Internal exception", status: int = 500, code: str = None,
|
40
|
+
additional_info: dict = None):
|
41
|
+
super().__init__(message=message, status=status, code=code, additional_info=additional_info)
|
@@ -0,0 +1,24 @@
|
|
1
|
+
from fastapi import Depends
|
2
|
+
|
3
|
+
from rb_commons.configs.injections import get_claims
|
4
|
+
from rb_commons.permissions.http_exceptions import ForbiddenException
|
5
|
+
from rb_commons.schemes.jwt import Claims, UserRole
|
6
|
+
|
7
|
+
|
8
|
+
class BasePermission:
|
9
|
+
def __call__(self, claims: Claims = Depends(get_claims)):
|
10
|
+
if not self.has_permission(claims):
|
11
|
+
raise ForbiddenException(message=f"Access denied", status=401, code="0000")
|
12
|
+
|
13
|
+
def has_permission(self, claims: Claims) -> bool:
|
14
|
+
return False
|
15
|
+
|
16
|
+
|
17
|
+
class IsAdmin(BasePermission):
|
18
|
+
def has_permission(self, claims: Claims) -> bool:
|
19
|
+
return claims.user_role == UserRole.ADMIN
|
20
|
+
|
21
|
+
|
22
|
+
class IsCustomer(BasePermission):
|
23
|
+
def has_permission(self, claims: Claims) -> bool:
|
24
|
+
return claims.user_role == UserRole.CUSTOMER
|
@@ -1,13 +1,16 @@
|
|
1
1
|
rb_commons/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
2
2
|
rb_commons/configs/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
3
3
|
rb_commons/configs/config.py,sha256=tpqC1V9PXx88m0N5L13WqkoalPUk6SthjxgL_lmf-lE,1439
|
4
|
-
rb_commons/configs/injections.py,sha256=
|
4
|
+
rb_commons/configs/injections.py,sha256=jOnabKVNj490nYqGuqw5jeSFFvq1lM8vfmx-R1NJobc,448
|
5
5
|
rb_commons/orm/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
6
6
|
rb_commons/orm/exceptions.py,sha256=1aMctiEwrPjyehoXVX1l6ML5ZOhmDkmBISzlTD5ey1Y,509
|
7
7
|
rb_commons/orm/managers.py,sha256=s0MeGfuDmQA287IV57s8kk467AJwskAoG4HvgQxKTYc,6711
|
8
|
+
rb_commons/permissions/__init__.py,sha256=Gd1yBLBTBph8NdHSA_-a-wN5ys4Uol647cKCwBFcsRY,61
|
9
|
+
rb_commons/permissions/http_exceptions.py,sha256=EGRMr1cRgiJ9Q2tkfANbf0c6-zzXf1CD6J3cmCaT_FA,1885
|
10
|
+
rb_commons/permissions/role_permissions.py,sha256=8oD2bwvugW-tV07J-T6EELjDeJqYNqnIYrgSKg7cgbk,802
|
8
11
|
rb_commons/schemes/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
9
12
|
rb_commons/schemes/jwt.py,sha256=F66JJDhholuOPPzlKeoC6f1TL4gXg4oRUrV5yheNpyo,1675
|
10
|
-
rb_commons-0.1.
|
11
|
-
rb_commons-0.1.
|
12
|
-
rb_commons-0.1.
|
13
|
-
rb_commons-0.1.
|
13
|
+
rb_commons-0.1.17.dist-info/METADATA,sha256=zAw74i9HyF57I2mWMpDHgxxfIu__w_OEu77qzv4T6kA,6533
|
14
|
+
rb_commons-0.1.17.dist-info/WHEEL,sha256=52BFRY2Up02UkjOa29eZOS2VxUrpPORXg1pkohGGUS8,91
|
15
|
+
rb_commons-0.1.17.dist-info/top_level.txt,sha256=HPx_WAYo3_fbg1WCeGHsz3wPGio1ucbnrlm2lmqlJog,11
|
16
|
+
rb_commons-0.1.17.dist-info/RECORD,,
|
File without changes
|
File without changes
|