summaryrefslogtreecommitdiffstats
path: root/auth_api
diff options
context:
space:
mode:
Diffstat (limited to 'auth_api')
-rw-r--r--auth_api/__init__.py0
-rw-r--r--auth_api/admin.py4
-rw-r--r--auth_api/apps.py6
-rw-r--r--auth_api/migrations/__init__.py0
-rw-r--r--auth_api/models.py3
-rw-r--r--auth_api/serializers.py21
-rw-r--r--auth_api/tests.py3
-rw-r--r--auth_api/urls.py12
-rw-r--r--auth_api/views.py61
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 @@
1from django.contrib import admin
2from knox.models import AuthToken
3
4admin.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 @@
1from django.apps import AppConfig
2
3
4class 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 @@
1from 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 @@
1from rest_framework import serializers
2from django.contrib.auth.models import User
3
4class UserSerializer(serializers.ModelSerializer):
5 class Meta:
6 model = User
7 fields = ('id', 'username', 'is_staff')
8
9class 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 @@
1from 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 @@
1from django.urls import path
2from knox import views as knox_views
3from .views import LoginView, RegisterView, UserInfo
4
5
6urlpatterns = [
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 @@
1from django.contrib.auth import login
2from django.contrib.auth.models import User
3from django.http.response import Http404
4from django.shortcuts import get_object_or_404
5
6from rest_framework import permissions, serializers
7from rest_framework.authtoken.serializers import AuthTokenSerializer
8from rest_framework.response import Response
9from rest_framework.views import APIView
10
11from knox.views import LoginView as KnoxLoginView
12from knox.auth import TokenAuthentication
13
14from .serializers import UserSerializer, RegisterUserSerializer
15
16class 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
26class 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
35class 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