From 0f06cc418c8f2bc8f68114afba46337191725288 Mon Sep 17 00:00:00 2001 From: Prithviraj Murthy <31995793+Prithviraj8@users.noreply.github.com> Date: Mon, 31 Oct 2022 18:45:29 +0530 Subject: [PATCH] Revert "Revert "[TESTNET-58] Add api to authenticate user"" --- auth/__init__.py | 0 auth/admin.py | 3 +++ auth/apps.py | 6 ++++++ auth/migrations/__init__.py | 0 auth/models.py | 3 +++ auth/tests.py | 3 +++ auth/urls.py | 7 +++++++ auth/views.py | 27 +++++++++++++++++++++++++++ settings_config/common.py | 24 ++++++++++++++++++++++++ settings_config/urls.py | 1 + 10 files changed, 74 insertions(+) create mode 100644 auth/__init__.py create mode 100644 auth/admin.py create mode 100644 auth/apps.py create mode 100644 auth/migrations/__init__.py create mode 100644 auth/models.py create mode 100644 auth/tests.py create mode 100644 auth/urls.py create mode 100644 auth/views.py diff --git a/auth/__init__.py b/auth/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/auth/admin.py b/auth/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/auth/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/auth/apps.py b/auth/apps.py new file mode 100644 index 0000000..6a09aaa --- /dev/null +++ b/auth/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class AuthConfig(AppConfig): + default_auto_field = "django.db.models.BigAutoField" + name = "auth" diff --git a/auth/migrations/__init__.py b/auth/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/auth/models.py b/auth/models.py new file mode 100644 index 0000000..71a8362 --- /dev/null +++ b/auth/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/auth/tests.py b/auth/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/auth/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/auth/urls.py b/auth/urls.py new file mode 100644 index 0000000..b5c8535 --- /dev/null +++ b/auth/urls.py @@ -0,0 +1,7 @@ +from django.urls import path + +from auth.views import login + +urlpatterns = [ + path("", login), +] diff --git a/auth/views.py b/auth/views.py new file mode 100644 index 0000000..5ae295a --- /dev/null +++ b/auth/views.py @@ -0,0 +1,27 @@ +from django.contrib.auth import authenticate +from django.views.decorators.csrf import csrf_exempt +from rest_framework.authtoken.models import Token +from rest_framework.decorators import api_view, permission_classes +from rest_framework.permissions import AllowAny +from rest_framework.status import HTTP_400_BAD_REQUEST, HTTP_404_NOT_FOUND, HTTP_200_OK +from rest_framework.response import Response + + +@csrf_exempt +@api_view(["POST"]) +@permission_classes((AllowAny,)) +def login(request): + username = request.data.get("username") + password = request.data.get("password") + print(username, password) + if username is None or password is None: + return Response( + {"error": "Please provide both username and password"}, + status=HTTP_400_BAD_REQUEST, + ) + user = authenticate(username=username, password=password) + print(user) + if not user: + return Response({"error": "Invalid Credentials"}, status=HTTP_404_NOT_FOUND) + token, _ = Token.objects.get_or_create(user=user) + return Response({"token": token.key}, status=HTTP_200_OK) diff --git a/settings_config/common.py b/settings_config/common.py index 6ae6b9e..50ce127 100644 --- a/settings_config/common.py +++ b/settings_config/common.py @@ -1,3 +1,10 @@ +import os +from pathlib import Path + +# Build paths inside the project like this: BASE_DIR / 'subdir'. +BASE_DIR = Path(__file__).resolve().parent.parent + + # Application definition INSTALLED_APPS = [ "django.contrib.admin", @@ -9,8 +16,17 @@ "django_celery_beat", "corsheaders", "rest_framework", + "rest_framework.authtoken", ] + +REST_FRAMEWORK = { + "DEFAULT_AUTHENTICATION_CLASSES": ( + "rest_framework.authentication.TokenAuthentication", + ), + "DEFAULT_PERMISSION_CLASSES": ("rest_framework.permissions.IsAuthenticated",), +} + MIDDLEWARE = [ "corsheaders.middleware.CorsMiddleware", "django.middleware.common.CommonMiddleware", @@ -39,6 +55,14 @@ }, ] +DATABASES = { + "default": { + "ENGINE": "django.db.backends.sqlite3", + "NAME": BASE_DIR / "db.sqlite3", + } +} + + LANGUAGE_CODE = "en-us" TIME_ZONE = "UTC" diff --git a/settings_config/urls.py b/settings_config/urls.py index 26dfcfc..ecf4a50 100644 --- a/settings_config/urls.py +++ b/settings_config/urls.py @@ -23,4 +23,5 @@ path("dydx_operations/", include("dydx_operations.urls")), path("cruize_operations/", include("cruize_operations.urls")), path("market_data/", include("market_data.urls")), + path("auth/", include("auth.urls")), ]