-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from Farmacia-Solidaria/8-Authorization
- Loading branch information
Showing
63 changed files
with
435 additions
and
557 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# SERVER | ||
DJANGO_SECRET_KEY=secret_key | ||
|
||
# DATABASE | ||
DATABASE_USERNAME=root | ||
DATABASE_PASSWORD=root | ||
|
||
# TOKEN SIGNATURE | ||
PRIVATE_KEY=PRIVATE KEY | ||
PUBLIC_KEY=PUBLIC KEY | ||
|
||
# OTHERS | ||
DEBUG=TRUE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[submodule "secrets"] | ||
path = secrets | ||
url = https://github.com/Farmacia-Solidaria/secrets |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,14 @@ | ||
import os | ||
class ActionError(Exception): | ||
|
||
def __init__(self, information, status=400, where="undefined") -> None: | ||
self.information = information | ||
self.status = status | ||
self.where = where | ||
|
||
if where == "undefined" and 'NAME' in os.environ: | ||
self.where = os.environ['NAME'] | ||
|
||
super().__init__(self.information) | ||
|
||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import functools | ||
|
||
import jwt | ||
|
||
from common.utils.auth import get_token_permissions | ||
from common.utils.functions import treat_token | ||
from common.models.message import Message | ||
from common.error.error import ActionError | ||
|
||
def permissions_needed(permissions_needed: 'list[str]') -> 'function': | ||
|
||
def decorator(func): | ||
@functools.wraps(func) | ||
def wrapper(*args, **kwargs): | ||
|
||
permissions = [] | ||
|
||
if type(args[0]) is Message: | ||
message: Message = args[0] | ||
permissions = get_token_permissions(treat_token(message.token)) | ||
|
||
if len(set(permissions_needed).intersection(permissions)) > 0 or 'admin' in permissions: | ||
return func(*args, **kwargs) | ||
|
||
raise ActionError( | ||
information="Permission denied", | ||
status=401 | ||
) | ||
|
||
|
||
return wrapper | ||
|
||
return decorator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from django.http.request import HttpRequest | ||
from rest_framework import status | ||
from rest_framework.response import Response | ||
from rest_framework.views import APIView | ||
from common.kafka.send import send_and_wait_message | ||
|
||
class SimpleConnection(APIView): | ||
|
||
def __init__(self, name=None): | ||
className = self.__class__.__name__.lower().split("connection")[0] | ||
self.name = className if name is None else name | ||
|
||
|
||
def _default_send_routine(self, method: str, request: HttpRequest, action: str): | ||
|
||
token = request.headers.get("Authorization") or "" | ||
|
||
data = send_and_wait_message( | ||
service=self.name, | ||
method=method, | ||
action=action, | ||
data=request.data, | ||
filter=True, | ||
suppress_errors=True, | ||
token=token | ||
) | ||
|
||
if data: | ||
return Response(data, status=data["data"]["status"] if data["error"] else status.HTTP_200_OK) | ||
|
||
return Response(status=status.HTTP_408_REQUEST_TIMEOUT) | ||
|
||
|
||
def post(self, request, action): return self._default_send_routine('post', request, action) | ||
def get(self, request, action): return self._default_send_routine( 'get', request, action) | ||
def put(self, request, action): return self._default_send_routine('put', request, action) | ||
def patch(self, request, action): return self._default_send_routine('patch', request, action) | ||
def delete(self, request, action): return self._default_send_routine('delete', request, action) | ||
def options(self, request, action): return self._default_send_routine('options', request, action) | ||
def head(self, request, action): return self._default_send_routine('head', request, action) |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from common.error.error import ActionError | ||
import jwt | ||
|
||
from common.utils.information import get_public_key | ||
|
||
def get_token_permissions(token): | ||
try: | ||
data = jwt.decode(token, get_public_key(), 'RS256') | ||
|
||
return data['permissions'] | ||
except jwt.ExpiredSignatureError: | ||
raise ActionError( | ||
information="Token has expired", | ||
status=403 | ||
) | ||
except: | ||
return [] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
def treat_token(token): | ||
return token.split('Bearer ')[1] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import os | ||
|
||
def get_public_key(): | ||
return os.environ["PUBLIC_KEY"].replace("\\n", "\n") | ||
|
||
def get_private_key(): | ||
return os.environ["PRIVATE_KEY"].replace("\\n", "\n") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
|
||
def is_key_null(obj, key): | ||
if key in obj: | ||
if obj[key] != "": | ||
return False | ||
|
||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.