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 | |
download | lom_api-1abb20d4871dcf8ab07f3f327fd3cbe65a22a9b1.tar.gz lom_api-1abb20d4871dcf8ab07f3f327fd3cbe65a22a9b1.zip |
auth features
-rw-r--r-- | .gitignore | 3 | ||||
-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 | ||||
-rw-r--r-- | chat_api/__init__.py | 0 | ||||
-rw-r--r-- | chat_api/admin.py | 3 | ||||
-rw-r--r-- | chat_api/apps.py | 6 | ||||
-rw-r--r-- | chat_api/migrations/__init__.py | 0 | ||||
-rw-r--r-- | chat_api/models.py | 3 | ||||
-rw-r--r-- | chat_api/tests.py | 3 | ||||
-rw-r--r-- | chat_api/urls.py | 5 | ||||
-rw-r--r-- | chat_api/views.py | 3 | ||||
-rw-r--r-- | lom_api/__init__.py | 0 | ||||
-rw-r--r-- | lom_api/asgi.py | 16 | ||||
-rw-r--r-- | lom_api/settings.py | 137 | ||||
-rw-r--r-- | lom_api/urls.py | 22 | ||||
-rw-r--r-- | lom_api/wsgi.py | 16 | ||||
-rwxr-xr-x | manage.py | 22 | ||||
-rw-r--r-- | python.req | 46 |
25 files changed, 395 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7de1644 --- /dev/null +++ b/.gitignore | |||
@@ -0,0 +1,3 @@ | |||
1 | db.sqlite3 | ||
2 | venv/ | ||
3 | __pycache__/ \ No newline at end of file | ||
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 | ||
diff --git a/chat_api/__init__.py b/chat_api/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/chat_api/__init__.py | |||
diff --git a/chat_api/admin.py b/chat_api/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/chat_api/admin.py | |||
@@ -0,0 +1,3 @@ | |||
1 | from django.contrib import admin | ||
2 | |||
3 | # Register your models here. | ||
diff --git a/chat_api/apps.py b/chat_api/apps.py new file mode 100644 index 0000000..89f7109 --- /dev/null +++ b/chat_api/apps.py | |||
@@ -0,0 +1,6 @@ | |||
1 | from django.apps import AppConfig | ||
2 | |||
3 | |||
4 | class ChatApiConfig(AppConfig): | ||
5 | default_auto_field = 'django.db.models.BigAutoField' | ||
6 | name = 'chat_api' | ||
diff --git a/chat_api/migrations/__init__.py b/chat_api/migrations/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/chat_api/migrations/__init__.py | |||
diff --git a/chat_api/models.py b/chat_api/models.py new file mode 100644 index 0000000..71a8362 --- /dev/null +++ b/chat_api/models.py | |||
@@ -0,0 +1,3 @@ | |||
1 | from django.db import models | ||
2 | |||
3 | # Create your models here. | ||
diff --git a/chat_api/tests.py b/chat_api/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/chat_api/tests.py | |||
@@ -0,0 +1,3 @@ | |||
1 | from django.test import TestCase | ||
2 | |||
3 | # Create your tests here. | ||
diff --git a/chat_api/urls.py b/chat_api/urls.py new file mode 100644 index 0000000..d2d839f --- /dev/null +++ b/chat_api/urls.py | |||
@@ -0,0 +1,5 @@ | |||
1 | from django.urls import path | ||
2 | |||
3 | urlpatterns = [ | ||
4 | |||
5 | ] | ||
diff --git a/chat_api/views.py b/chat_api/views.py new file mode 100644 index 0000000..91ea44a --- /dev/null +++ b/chat_api/views.py | |||
@@ -0,0 +1,3 @@ | |||
1 | from django.shortcuts import render | ||
2 | |||
3 | # Create your views here. | ||
diff --git a/lom_api/__init__.py b/lom_api/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/lom_api/__init__.py | |||
diff --git a/lom_api/asgi.py b/lom_api/asgi.py new file mode 100644 index 0000000..9f97758 --- /dev/null +++ b/lom_api/asgi.py | |||
@@ -0,0 +1,16 @@ | |||
1 | """ | ||
2 | ASGI config for lom_api project. | ||
3 | |||
4 | It exposes the ASGI callable as a module-level variable named ``application``. | ||
5 | |||
6 | For more information on this file, see | ||
7 | https://docs.djangoproject.com/en/3.2/howto/deployment/asgi/ | ||
8 | """ | ||
9 | |||
10 | import os | ||
11 | |||
12 | from django.core.asgi import get_asgi_application | ||
13 | |||
14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'lom_api.settings') | ||
15 | |||
16 | application = get_asgi_application() | ||
diff --git a/lom_api/settings.py b/lom_api/settings.py new file mode 100644 index 0000000..7c85027 --- /dev/null +++ b/lom_api/settings.py | |||
@@ -0,0 +1,137 @@ | |||
1 | """ | ||
2 | Django settings for lom_api project. | ||
3 | |||
4 | Generated by 'django-admin startproject' using Django 3.2.9. | ||
5 | |||
6 | For more information on this file, see | ||
7 | https://docs.djangoproject.com/en/3.2/topics/settings/ | ||
8 | |||
9 | For the full list of settings and their values, see | ||
10 | https://docs.djangoproject.com/en/3.2/ref/settings/ | ||
11 | """ | ||
12 | |||
13 | from pathlib import Path | ||
14 | |||
15 | # Build paths inside the project like this: BASE_DIR / 'subdir'. | ||
16 | BASE_DIR = Path(__file__).resolve().parent.parent | ||
17 | |||
18 | |||
19 | # Quick-start development settings - unsuitable for production | ||
20 | # See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/ | ||
21 | |||
22 | # SECURITY WARNING: keep the secret key used in production secret! | ||
23 | SECRET_KEY = 'django-insecure-w699xg1453&j057k$g465vldjn+4*skp8!13ua=aq0s_91b@c)' | ||
24 | |||
25 | # SECURITY WARNING: don't run with debug turned on in production! | ||
26 | DEBUG = True | ||
27 | |||
28 | ALLOWED_HOSTS = ['*'] | ||
29 | |||
30 | |||
31 | # Application definition | ||
32 | |||
33 | INSTALLED_APPS = [ | ||
34 | 'django.contrib.admin', | ||
35 | 'django.contrib.auth', | ||
36 | 'django.contrib.contenttypes', | ||
37 | 'django.contrib.sessions', | ||
38 | 'django.contrib.messages', | ||
39 | 'django.contrib.staticfiles', | ||
40 | 'rest_framework', | ||
41 | 'knox', | ||
42 | 'auth_api', | ||
43 | 'chat_api' | ||
44 | ] | ||
45 | |||
46 | REST_FRAMEWORK = { | ||
47 | 'DEFAULT_AUTHENTICATION_CLASSES': ('knox.auth.TokenAuthentication',), | ||
48 | } | ||
49 | |||
50 | REST_KNOX = { | ||
51 | 'TOKEN_LIMIT_PER_USER': 1, | ||
52 | } | ||
53 | |||
54 | MIDDLEWARE = [ | ||
55 | 'django.middleware.security.SecurityMiddleware', | ||
56 | 'django.contrib.sessions.middleware.SessionMiddleware', | ||
57 | 'django.middleware.common.CommonMiddleware', | ||
58 | 'django.middleware.csrf.CsrfViewMiddleware', | ||
59 | 'django.contrib.auth.middleware.AuthenticationMiddleware', | ||
60 | 'django.contrib.messages.middleware.MessageMiddleware', | ||
61 | 'django.middleware.clickjacking.XFrameOptionsMiddleware', | ||
62 | ] | ||
63 | |||
64 | ROOT_URLCONF = 'lom_api.urls' | ||
65 | |||
66 | TEMPLATES = [ | ||
67 | { | ||
68 | 'BACKEND': 'django.template.backends.django.DjangoTemplates', | ||
69 | 'DIRS': [], | ||
70 | 'APP_DIRS': True, | ||
71 | 'OPTIONS': { | ||
72 | 'context_processors': [ | ||
73 | 'django.template.context_processors.debug', | ||
74 | 'django.template.context_processors.request', | ||
75 | 'django.contrib.auth.context_processors.auth', | ||
76 | 'django.contrib.messages.context_processors.messages', | ||
77 | ], | ||
78 | }, | ||
79 | }, | ||
80 | ] | ||
81 | |||
82 | WSGI_APPLICATION = 'lom_api.wsgi.application' | ||
83 | |||
84 | |||
85 | # Database | ||
86 | # https://docs.djangoproject.com/en/3.2/ref/settings/#databases | ||
87 | |||
88 | DATABASES = { | ||
89 | 'default': { | ||
90 | 'ENGINE': 'django.db.backends.sqlite3', | ||
91 | 'NAME': BASE_DIR / 'db.sqlite3', | ||
92 | } | ||
93 | } | ||
94 | |||
95 | |||
96 | # Password validation | ||
97 | # https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators | ||
98 | |||
99 | AUTH_PASSWORD_VALIDATORS = [ | ||
100 | # { | ||
101 | # 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', | ||
102 | # }, | ||
103 | # { | ||
104 | # 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', | ||
105 | # }, | ||
106 | # { | ||
107 | # 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', | ||
108 | # }, | ||
109 | # { | ||
110 | # 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', | ||
111 | # }, | ||
112 | ] | ||
113 | |||
114 | |||
115 | # Internationalization | ||
116 | # https://docs.djangoproject.com/en/3.2/topics/i18n/ | ||
117 | |||
118 | LANGUAGE_CODE = 'en-us' | ||
119 | |||
120 | TIME_ZONE = 'UTC' | ||
121 | |||
122 | USE_I18N = True | ||
123 | |||
124 | USE_L10N = True | ||
125 | |||
126 | USE_TZ = True | ||
127 | |||
128 | |||
129 | # Static files (CSS, JavaScript, Images) | ||
130 | # https://docs.djangoproject.com/en/3.2/howto/static-files/ | ||
131 | |||
132 | STATIC_URL = '/static/' | ||
133 | |||
134 | # Default primary key field type | ||
135 | # https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field | ||
136 | |||
137 | DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' | ||
diff --git a/lom_api/urls.py b/lom_api/urls.py new file mode 100644 index 0000000..0bc8e32 --- /dev/null +++ b/lom_api/urls.py | |||
@@ -0,0 +1,22 @@ | |||
1 | """lom_api URL Configuration | ||
2 | |||
3 | The `urlpatterns` list routes URLs to views. For more information please see: | ||
4 | https://docs.djangoproject.com/en/3.2/topics/http/urls/ | ||
5 | Examples: | ||
6 | Function views | ||
7 | 1. Add an import: from my_app import views | ||
8 | 2. Add a URL to urlpatterns: path('', views.home, name='home') | ||
9 | Class-based views | ||
10 | 1. Add an import: from other_app.views import Home | ||
11 | 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') | ||
12 | Including another URLconf | ||
13 | 1. Import the include() function: from django.urls import include, path | ||
14 | 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) | ||
15 | """ | ||
16 | from django.contrib import admin | ||
17 | from django.urls import path, include | ||
18 | |||
19 | urlpatterns = [ | ||
20 | path('admin/', admin.site.urls), | ||
21 | path('api/auth/', include('auth_api.urls')) | ||
22 | ] | ||
diff --git a/lom_api/wsgi.py b/lom_api/wsgi.py new file mode 100644 index 0000000..3b98667 --- /dev/null +++ b/lom_api/wsgi.py | |||
@@ -0,0 +1,16 @@ | |||
1 | """ | ||
2 | WSGI config for lom_api project. | ||
3 | |||
4 | It exposes the WSGI callable as a module-level variable named ``application``. | ||
5 | |||
6 | For more information on this file, see | ||
7 | https://docs.djangoproject.com/en/3.2/howto/deployment/wsgi/ | ||
8 | """ | ||
9 | |||
10 | import os | ||
11 | |||
12 | from django.core.wsgi import get_wsgi_application | ||
13 | |||
14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'lom_api.settings') | ||
15 | |||
16 | application = get_wsgi_application() | ||
diff --git a/manage.py b/manage.py new file mode 100755 index 0000000..6af86cb --- /dev/null +++ b/manage.py | |||
@@ -0,0 +1,22 @@ | |||
1 | #!/usr/bin/env python | ||
2 | """Django's command-line utility for administrative tasks.""" | ||
3 | import os | ||
4 | import sys | ||
5 | |||
6 | |||
7 | def main(): | ||
8 | """Run administrative tasks.""" | ||
9 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'lom_api.settings') | ||
10 | try: | ||
11 | from django.core.management import execute_from_command_line | ||
12 | except ImportError as exc: | ||
13 | raise ImportError( | ||
14 | "Couldn't import Django. Are you sure it's installed and " | ||
15 | "available on your PYTHONPATH environment variable? Did you " | ||
16 | "forget to activate a virtual environment?" | ||
17 | ) from exc | ||
18 | execute_from_command_line(sys.argv) | ||
19 | |||
20 | |||
21 | if __name__ == '__main__': | ||
22 | main() | ||
diff --git a/python.req b/python.req new file mode 100644 index 0000000..abe973a --- /dev/null +++ b/python.req | |||
@@ -0,0 +1,46 @@ | |||
1 | asgiref==3.4.1 | ||
2 | backcall==0.2.0 | ||
3 | backports.entry-points-selectable==1.1.0 | ||
4 | cffi==1.15.0 | ||
5 | cryptography==35.0.0 | ||
6 | debugpy==1.5.1 | ||
7 | decorator==5.1.0 | ||
8 | distlib==0.3.3 | ||
9 | Django==3.2.9 | ||
10 | django-rest-knox==4.1.0 | ||
11 | djangorestframework==3.12.4 | ||
12 | entrypoints==0.3 | ||
13 | filelock==3.3.2 | ||
14 | htmlentities==0.3.0 | ||
15 | ipykernel==6.5.0 | ||
16 | ipython==7.29.0 | ||
17 | jedi==0.18.0 | ||
18 | jupyter-client==7.0.6 | ||
19 | jupyter-core==4.9.1 | ||
20 | matplotlib-inline==0.1.3 | ||
21 | nest-asyncio==1.5.1 | ||
22 | parso==0.8.2 | ||
23 | pexpect==4.8.0 | ||
24 | pickleshare==0.7.5 | ||
25 | platformdirs==2.4.0 | ||
26 | prompt-toolkit==3.0.22 | ||
27 | ptyprocess==0.7.0 | ||
28 | pycairo==1.20.1 | ||
29 | pycparser==2.21 | ||
30 | Pygments==2.10.0 | ||
31 | PyGObject==3.40.1 | ||
32 | pyinotify==0.9.6 | ||
33 | python-dateutil==2.8.2 | ||
34 | python-Levenshtein==0.12.0 | ||
35 | pytz==2021.3 | ||
36 | pyxdg==0.27 | ||
37 | pyzmq==22.3.0 | ||
38 | ranger-fm==1.9.3 | ||
39 | six==1.16.0 | ||
40 | sqlparse==0.4.2 | ||
41 | tornado==6.1 | ||
42 | traitlets==5.1.1 | ||
43 | ulauncher==5.13.0 | ||
44 | virtualenv==20.10.0 | ||
45 | wcwidth==0.2.5 | ||
46 | websocket-client==1.2.1 | ||