commit 94fc8668919178804b6f298246c5b24bf97ae25f Author: jar3b Date: Fri Jul 6 18:47:11 2018 +0300 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ae945fa --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.idea/ +venv/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..a6e59e6 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# drf-uuid-auth + +Плагин для DRF для аутентификации посредством tantald \ No newline at end of file diff --git a/drf_uuid_auth/__init__.py b/drf_uuid_auth/__init__.py new file mode 100644 index 0000000..84408a5 --- /dev/null +++ b/drf_uuid_auth/__init__.py @@ -0,0 +1,8 @@ +__title__ = 'drf-uuid-auth' +__version__ = '0.0.1' +__author__ = 'jar3b' +__license__ = 'MIT' +__copyright__ = 'Copyright 2017-2018 Jar3b' + +# Version synonym +VERSION = __version__ \ No newline at end of file diff --git a/drf_uuid_auth/authentication.py b/drf_uuid_auth/authentication.py new file mode 100644 index 0000000..97e367d --- /dev/null +++ b/drf_uuid_auth/authentication.py @@ -0,0 +1,33 @@ +from django.contrib.auth import get_user_model +from rest_framework import exceptions +from rest_framework.authentication import BaseAuthentication + +from drf_uuid_auth.settings import api_settings + + +class UuidAuthentication(BaseAuthentication): + def authenticate(self, request): + header = request.META.get(api_settings.AUTH_HEADER.upper(), None) + if header is None or header == "" or header == "-": + return None + + payload = header + user = self.authenticate_credentials(payload) + + return user, payload + + def authenticate_credentials(self, payload): + User = get_user_model() + + try: + user = User.object.get(uuid=payload) + except User.DoesNotExist: + raise exceptions.AuthenticationFailed("Invalid user id") + + if not user.is_active: + raise exceptions.AuthenticationFailed("User isn't active") + + return user + + def authenticate_header(self, request): + pass diff --git a/drf_uuid_auth/models.py b/drf_uuid_auth/models.py new file mode 100644 index 0000000..e69de29 diff --git a/drf_uuid_auth/settings.py b/drf_uuid_auth/settings.py new file mode 100644 index 0000000..acfad05 --- /dev/null +++ b/drf_uuid_auth/settings.py @@ -0,0 +1,13 @@ +import datetime + +from django.conf import settings +from rest_framework.settings import APISettings + + +USER_SETTINGS = getattr(settings, 'UUID_AUTH', None) + +DEFAULTS = { + 'AUTH_HEADER': 'HTTP_X_UUID_AUTH' +} + +api_settings = APISettings(USER_SETTINGS, DEFAULTS) \ No newline at end of file diff --git a/drf_uuid_auth/utils.py b/drf_uuid_auth/utils.py new file mode 100644 index 0000000..e69de29 diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..6ccbd8a --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +djangorestframework>=3.4.0 \ No newline at end of file