From 1abb20d4871dcf8ab07f3f327fd3cbe65a22a9b1 Mon Sep 17 00:00:00 2001 From: jdlugosz963 Date: Sun, 7 Nov 2021 18:45:40 +0100 Subject: auth features --- auth_api/__init__.py | 0 auth_api/admin.py | 4 +++ auth_api/apps.py | 6 ++++ auth_api/migrations/__init__.py | 0 auth_api/models.py | 3 ++ auth_api/serializers.py | 21 ++++++++++++++ auth_api/tests.py | 3 ++ auth_api/urls.py | 12 ++++++++ auth_api/views.py | 61 +++++++++++++++++++++++++++++++++++++++++ 9 files changed, 110 insertions(+) create mode 100644 auth_api/__init__.py create mode 100644 auth_api/admin.py create mode 100644 auth_api/apps.py create mode 100644 auth_api/migrations/__init__.py create mode 100644 auth_api/models.py create mode 100644 auth_api/serializers.py create mode 100644 auth_api/tests.py create mode 100644 auth_api/urls.py create mode 100644 auth_api/views.py (limited to 'auth_api') diff --git a/auth_api/__init__.py b/auth_api/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/auth_api/admin.py b/auth_api/admin.py new file mode 100644 index 0000000..979b710 --- /dev/null +++ b/auth_api/admin.py @@ -0,0 +1,4 @@ +from django.contrib import admin +from knox.models import AuthToken + +admin.register(AuthToken) diff --git a/auth_api/apps.py b/auth_api/apps.py new file mode 100644 index 0000000..875065a --- /dev/null +++ b/auth_api/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class AuthApiConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'auth_api' diff --git a/auth_api/migrations/__init__.py b/auth_api/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/auth_api/models.py b/auth_api/models.py new file mode 100644 index 0000000..71a8362 --- /dev/null +++ b/auth_api/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/auth_api/serializers.py b/auth_api/serializers.py new file mode 100644 index 0000000..d39c5f3 --- /dev/null +++ b/auth_api/serializers.py @@ -0,0 +1,21 @@ +from rest_framework import serializers +from django.contrib.auth.models import User + +class UserSerializer(serializers.ModelSerializer): + class Meta: + model = User + fields = ('id', 'username', 'is_staff') + +class RegisterUserSerializer(serializers.ModelSerializer): + class Meta: + model = User + fields = ('username', 'password') + + + def register_user(self): + user = User.objects.create_user( + username = self.validated_data['username'], + password = self.validated_data['password'] + ) + + return user \ No newline at end of file diff --git a/auth_api/tests.py b/auth_api/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/auth_api/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/auth_api/urls.py b/auth_api/urls.py new file mode 100644 index 0000000..e8a1d86 --- /dev/null +++ b/auth_api/urls.py @@ -0,0 +1,12 @@ +from django.urls import path +from knox import views as knox_views +from .views import LoginView, RegisterView, UserInfo + + +urlpatterns = [ + path('login/', LoginView.as_view(), name='login'), + path('register/', RegisterView.as_view(), name='register'), + path('info/', UserInfo.as_view(), name='info'), + path('logout/', knox_views.LogoutView.as_view(), name='logout'), + path('logoutall/', knox_views.LogoutAllView.as_view(), name='logoutall'), +] diff --git a/auth_api/views.py b/auth_api/views.py new file mode 100644 index 0000000..caf4782 --- /dev/null +++ b/auth_api/views.py @@ -0,0 +1,61 @@ +from django.contrib.auth import login +from django.contrib.auth.models import User +from django.http.response import Http404 +from django.shortcuts import get_object_or_404 + +from rest_framework import permissions, serializers +from rest_framework.authtoken.serializers import AuthTokenSerializer +from rest_framework.response import Response +from rest_framework.views import APIView + +from knox.views import LoginView as KnoxLoginView +from knox.auth import TokenAuthentication + +from .serializers import UserSerializer, RegisterUserSerializer + +class LoginView(KnoxLoginView): + permission_classes = (permissions.AllowAny, ) + + def post(self, request, format=None): + serializer = AuthTokenSerializer(data=request.data) + serializer.is_valid(raise_exception=True) + user = serializer.validated_data['user'] + login(request, user) + return super(LoginView, self).post(request, format=None) + +class RegisterView(APIView): + + def post(self, request): + serializer = RegisterUserSerializer(data=request.data) + serializer.is_valid(raise_exception=True) + user = serializer.register_user() + + return Response(status=200) + +class UserInfo(APIView): + authentication_classes = (TokenAuthentication, ) + permission_classes = (permissions.IsAuthenticated, ) + + def get(self, request): + user = request.GET.get("pk", request.user) + username = request.GET.get("username", None) + + if username: + users = User.objects.filter(username__startswith = username)[:5] + serializer = UserSerializer(users, many=True) + + return Response({ + "users": serializer.data + }) + + if not isinstance(user, User): + try: + user = get_object_or_404(User, pk=user) + except ValueError: + raise Http404 + + serializer = UserSerializer(user) + + return Response({ + "user": serializer.data + }) \ No newline at end of file -- cgit v1.2.3