summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjdlugosz963 <jdlugosz963@gmail.com>2021-11-07 18:45:40 +0100
committerjdlugosz963 <jdlugosz963@gmail.com>2021-11-07 18:45:40 +0100
commit1abb20d4871dcf8ab07f3f327fd3cbe65a22a9b1 (patch)
treedaed477f3b4f2e56ab8723d58cc48c2b5de0dc36
downloadlom_api-1abb20d4871dcf8ab07f3f327fd3cbe65a22a9b1.tar.gz
lom_api-1abb20d4871dcf8ab07f3f327fd3cbe65a22a9b1.zip
auth features
-rw-r--r--.gitignore3
-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
-rw-r--r--chat_api/__init__.py0
-rw-r--r--chat_api/admin.py3
-rw-r--r--chat_api/apps.py6
-rw-r--r--chat_api/migrations/__init__.py0
-rw-r--r--chat_api/models.py3
-rw-r--r--chat_api/tests.py3
-rw-r--r--chat_api/urls.py5
-rw-r--r--chat_api/views.py3
-rw-r--r--lom_api/__init__.py0
-rw-r--r--lom_api/asgi.py16
-rw-r--r--lom_api/settings.py137
-rw-r--r--lom_api/urls.py22
-rw-r--r--lom_api/wsgi.py16
-rwxr-xr-xmanage.py22
-rw-r--r--python.req46
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 @@
1db.sqlite3
2venv/
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 @@
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
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 @@
1from 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 @@
1from django.apps import AppConfig
2
3
4class 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 @@
1from 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 @@
1from 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 @@
1from django.urls import path
2
3urlpatterns = [
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 @@
1from 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"""
2ASGI config for lom_api project.
3
4It exposes the ASGI callable as a module-level variable named ``application``.
5
6For more information on this file, see
7https://docs.djangoproject.com/en/3.2/howto/deployment/asgi/
8"""
9
10import os
11
12from django.core.asgi import get_asgi_application
13
14os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'lom_api.settings')
15
16application = 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"""
2Django settings for lom_api project.
3
4Generated by 'django-admin startproject' using Django 3.2.9.
5
6For more information on this file, see
7https://docs.djangoproject.com/en/3.2/topics/settings/
8
9For the full list of settings and their values, see
10https://docs.djangoproject.com/en/3.2/ref/settings/
11"""
12
13from pathlib import Path
14
15# Build paths inside the project like this: BASE_DIR / 'subdir'.
16BASE_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!
23SECRET_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!
26DEBUG = True
27
28ALLOWED_HOSTS = ['*']
29
30
31# Application definition
32
33INSTALLED_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
46REST_FRAMEWORK = {
47 'DEFAULT_AUTHENTICATION_CLASSES': ('knox.auth.TokenAuthentication',),
48}
49
50REST_KNOX = {
51 'TOKEN_LIMIT_PER_USER': 1,
52}
53
54MIDDLEWARE = [
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
64ROOT_URLCONF = 'lom_api.urls'
65
66TEMPLATES = [
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
82WSGI_APPLICATION = 'lom_api.wsgi.application'
83
84
85# Database
86# https://docs.djangoproject.com/en/3.2/ref/settings/#databases
87
88DATABASES = {
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
99AUTH_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
118LANGUAGE_CODE = 'en-us'
119
120TIME_ZONE = 'UTC'
121
122USE_I18N = True
123
124USE_L10N = True
125
126USE_TZ = True
127
128
129# Static files (CSS, JavaScript, Images)
130# https://docs.djangoproject.com/en/3.2/howto/static-files/
131
132STATIC_URL = '/static/'
133
134# Default primary key field type
135# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
136
137DEFAULT_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
3The `urlpatterns` list routes URLs to views. For more information please see:
4 https://docs.djangoproject.com/en/3.2/topics/http/urls/
5Examples:
6Function views
7 1. Add an import: from my_app import views
8 2. Add a URL to urlpatterns: path('', views.home, name='home')
9Class-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')
12Including 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"""
16from django.contrib import admin
17from django.urls import path, include
18
19urlpatterns = [
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"""
2WSGI config for lom_api project.
3
4It exposes the WSGI callable as a module-level variable named ``application``.
5
6For more information on this file, see
7https://docs.djangoproject.com/en/3.2/howto/deployment/wsgi/
8"""
9
10import os
11
12from django.core.wsgi import get_wsgi_application
13
14os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'lom_api.settings')
15
16application = 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."""
3import os
4import sys
5
6
7def 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
21if __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 @@
1asgiref==3.4.1
2backcall==0.2.0
3backports.entry-points-selectable==1.1.0
4cffi==1.15.0
5cryptography==35.0.0
6debugpy==1.5.1
7decorator==5.1.0
8distlib==0.3.3
9Django==3.2.9
10django-rest-knox==4.1.0
11djangorestframework==3.12.4
12entrypoints==0.3
13filelock==3.3.2
14htmlentities==0.3.0
15ipykernel==6.5.0
16ipython==7.29.0
17jedi==0.18.0
18jupyter-client==7.0.6
19jupyter-core==4.9.1
20matplotlib-inline==0.1.3
21nest-asyncio==1.5.1
22parso==0.8.2
23pexpect==4.8.0
24pickleshare==0.7.5
25platformdirs==2.4.0
26prompt-toolkit==3.0.22
27ptyprocess==0.7.0
28pycairo==1.20.1
29pycparser==2.21
30Pygments==2.10.0
31PyGObject==3.40.1
32pyinotify==0.9.6
33python-dateutil==2.8.2
34python-Levenshtein==0.12.0
35pytz==2021.3
36pyxdg==0.27
37pyzmq==22.3.0
38ranger-fm==1.9.3
39six==1.16.0
40sqlparse==0.4.2
41tornado==6.1
42traitlets==5.1.1
43ulauncher==5.13.0
44virtualenv==20.10.0
45wcwidth==0.2.5
46websocket-client==1.2.1