diff options
author | jdlugosz963 <jdlugosz963@gmail.com> | 2021-11-07 18:45:40 +0100 |
---|---|---|
committer | jdlugosz963 <jdlugosz963@gmail.com> | 2021-11-07 18:45:40 +0100 |
commit | 1abb20d4871dcf8ab07f3f327fd3cbe65a22a9b1 (patch) | |
tree | daed477f3b4f2e56ab8723d58cc48c2b5de0dc36 /auth_api | |
download | lom_api-1abb20d4871dcf8ab07f3f327fd3cbe65a22a9b1.tar.gz lom_api-1abb20d4871dcf8ab07f3f327fd3cbe65a22a9b1.zip |
auth features
Diffstat (limited to 'auth_api')
-rw-r--r-- | auth_api/__init__.py | 0 | ||||
-rw-r--r-- | auth_api/admin.py | 4 | ||||
-rw-r--r-- | auth_api/apps.py | 6 | ||||
-rw-r--r-- | auth_api/migrations/__init__.py | 0 | ||||
-rw-r--r-- | auth_api/models.py | 3 | ||||
-rw-r--r-- | auth_api/serializers.py | 21 | ||||
-rw-r--r-- | auth_api/tests.py | 3 | ||||
-rw-r--r-- | auth_api/urls.py | 12 | ||||
-rw-r--r-- | auth_api/views.py | 61 |
9 files changed, 110 insertions, 0 deletions
diff --git a/auth_api/__init__.py b/auth_api/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/auth_api/__init__.py | |||
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 @@ | |||
1 | from django.contrib import admin | ||
2 | from knox.models import AuthToken | ||
3 | |||
4 | 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 @@ | |||
1 | from django.apps import AppConfig | ||
2 | |||
3 | |||
4 | class AuthApiConfig(AppConfig): | ||
5 | default_auto_field = 'django.db.models.BigAutoField' | ||
6 | name = 'auth_api' | ||
diff --git a/auth_api/migrations/__init__.py b/auth_api/migrations/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/auth_api/migrations/__init__.py | |||
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 @@ | |||
1 | from django.db import models | ||
2 | |||
3 | # 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 @@ | |||
1 | from rest_framework import serializers | ||
2 | from django.contrib.auth.models import User | ||
3 | |||
4 | class UserSerializer(serializers.ModelSerializer): | ||
5 | class Meta: | ||
6 | model = User | ||
7 | fields = ('id', 'username', 'is_staff') | ||
8 | |||
9 | class RegisterUserSerializer(serializers.ModelSerializer): | ||
10 | class Meta: | ||
11 | model = User | ||
12 | fields = ('username', 'password') | ||
13 | |||
14 | |||
15 | def register_user(self): | ||
16 | user = User.objects.create_user( | ||
17 | username = self.validated_data['username'], | ||
18 | password = self.validated_data['password'] | ||
19 | ) | ||
20 | |||
21 | 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 @@ | |||
1 | from django.test import TestCase | ||
2 | |||
3 | # 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 @@ | |||
1 | from django.urls import path | ||
2 | from knox import views as knox_views | ||
3 | from .views import LoginView, RegisterView, UserInfo | ||
4 | |||
5 | |||
6 | urlpatterns = [ | ||
7 | path('login/', LoginView.as_view(), name='login'), | ||
8 | path('register/', RegisterView.as_view(), name='register'), | ||
9 | path('info/', UserInfo.as_view(), name='info'), | ||
10 | path('logout/', knox_views.LogoutView.as_view(), name='logout'), | ||
11 | path('logoutall/', knox_views.LogoutAllView.as_view(), name='logoutall'), | ||
12 | ] | ||
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 @@ | |||
1 | from django.contrib.auth import login | ||
2 | from django.contrib.auth.models import User | ||
3 | from django.http.response import Http404 | ||
4 | from django.shortcuts import get_object_or_404 | ||
5 | |||
6 | from rest_framework import permissions, serializers | ||
7 | from rest_framework.authtoken.serializers import AuthTokenSerializer | ||
8 | from rest_framework.response import Response | ||
9 | from rest_framework.views import APIView | ||
10 | |||
11 | from knox.views import LoginView as KnoxLoginView | ||
12 | from knox.auth import TokenAuthentication | ||
13 | |||
14 | from .serializers import UserSerializer, RegisterUserSerializer | ||
15 | |||
16 | class LoginView(KnoxLoginView): | ||
17 | permission_classes = (permissions.AllowAny, ) | ||
18 | |||
19 | def post(self, request, format=None): | ||
20 | serializer = AuthTokenSerializer(data=request.data) | ||
21 | serializer.is_valid(raise_exception=True) | ||
22 | user = serializer.validated_data['user'] | ||
23 | login(request, user) | ||
24 | return super(LoginView, self).post(request, format=None) | ||
25 | |||
26 | class RegisterView(APIView): | ||
27 | |||
28 | def post(self, request): | ||
29 | serializer = RegisterUserSerializer(data=request.data) | ||
30 | serializer.is_valid(raise_exception=True) | ||
31 | user = serializer.register_user() | ||
32 | |||
33 | return Response(status=200) | ||
34 | |||
35 | class UserInfo(APIView): | ||
36 | authentication_classes = (TokenAuthentication, ) | ||
37 | permission_classes = (permissions.IsAuthenticated, ) | ||
38 | |||
39 | def get(self, request): | ||
40 | user = request.GET.get("pk", request.user) | ||
41 | username = request.GET.get("username", None) | ||
42 | |||
43 | if username: | ||
44 | users = User.objects.filter(username__startswith = username)[:5] | ||
45 | serializer = UserSerializer(users, many=True) | ||
46 | |||
47 | return Response({ | ||
48 | "users": serializer.data | ||
49 | }) | ||
50 | |||
51 | if not isinstance(user, User): | ||
52 | try: | ||
53 | user = get_object_or_404(User, pk=user) | ||
54 | except ValueError: | ||
55 | raise Http404 | ||
56 | |||
57 | serializer = UserSerializer(user) | ||
58 | |||
59 | return Response({ | ||
60 | "user": serializer.data | ||
61 | }) \ No newline at end of file | ||