completed authentication serializers

This commit is contained in:
2024-03-06 00:38:41 +03:00
parent 7cf8cc4a6b
commit 23c3918697
13 changed files with 118 additions and 2 deletions
+1 -1
View File
@@ -7,8 +7,8 @@ name = "pypi"
django = "*" django = "*"
python-dotenv = "*" python-dotenv = "*"
djangorestframework = "*" djangorestframework = "*"
django-rest-swagger = "*"
drf-yasg = "*" drf-yasg = "*"
pyjwt = "*"
[dev-packages] [dev-packages]
black = "*" black = "*"
Generated
+10 -1
View File
@@ -1,7 +1,7 @@
{ {
"_meta": { "_meta": {
"hash": { "hash": {
"sha256": "e809082b9f93f07ff5ab032b2b74852d5953598169a20d6a35c8308b6c2f14ab" "sha256": "ecb5c24f0cd1dff8305c31b43f5cfdc663cf0435e9e672c44269060b890308fb"
}, },
"pipfile-spec": 6, "pipfile-spec": 6,
"requires": { "requires": {
@@ -288,6 +288,15 @@
"markers": "python_version >= '3.7'", "markers": "python_version >= '3.7'",
"version": "==23.2" "version": "==23.2"
}, },
"pyjwt": {
"hashes": [
"sha256:57e28d156e3d5c10088e0c68abb90bfac3df82b40a71bd0daa20c65ccd5c23de",
"sha256:59127c392cc44c2da5bb3192169a91f429924e17aff6534d70fdc02ab3e04320"
],
"index": "pypi",
"markers": "python_version >= '3.7'",
"version": "==2.8.0"
},
"python-dotenv": { "python-dotenv": {
"hashes": [ "hashes": [
"sha256:e324ee90a023d808f1959c46bcbc04446a10ced277783dc6ee09987c37ec10ca", "sha256:e324ee90a023d808f1959c46bcbc04446a10ced277783dc6ee09987c37ec10ca",
View File
+3
View File
@@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.
+6
View File
@@ -0,0 +1,6 @@
from django.apps import AppConfig
class AuthenticationConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "authentication"
+26
View File
@@ -0,0 +1,26 @@
import jwt
from django.conf import settings
from django.contrib.auth.models import User
from rest_framework import authentication, exceptions
class JWTAuthentication(authentication.BaseAuthentication):
def authenticate(self, request):
auth_data = authentication.get_authorization_header(request)
if not auth_data:
return None
prefix, token = auth_data.decode("utf-8").split(" ")
try:
payload = jwt.decode(token, settings.JWT_SECRET_KEY, algorithms="HS256")
user = User.objects.get(username=payload["username"])
return (user, token)
except jwt.DecodeError as err:
raise exceptions.AuthenticationFailed("Your token is invalid, please login")
except jwt.ExpiredSignatureError as err:
raise exceptions.AuthenticationFailed("Your token is expired, login")
return super().authenticate(request)
+3
View File
@@ -0,0 +1,3 @@
from django.db import models
# Create your models here.
+32
View File
@@ -0,0 +1,32 @@
from django.contrib.auth.models import User
from rest_framework import serializers
class UserSerializer(serializers.ModelSerializer):
password = serializers.CharField(max_length=65, min_length=8, write_only=True)
email = serializers.EmailField(max_length=255)
first_name = serializers.CharField(max_length=255)
last_name = serializers.CharField(max_length=255)
class Meta:
model = User
fields = ["username", "first_name", "last_name", "email", "password"]
def validate(self, attrs):
email = attrs.get("email", "")
if User.objects.filter(email=email).exists():
raise serializers.ValidationError({"error": "Email is already in use"})
return super().validate(attrs)
def create(self, validated_data):
return User.objects.create_user(**validated_data)
class LoginSerializer(serializers.ModelSerializer):
password = serializers.CharField(max_length=65)
username = serializers.CharField(max_length=255)
class Meta:
model = User
fields = ["username", "password"]
+3
View File
@@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.
+3
View File
@@ -0,0 +1,3 @@
from django.shortcuts import render
# Create your views here.
@@ -0,0 +1,27 @@
# Generated by Django 5.0.3 on 2024-03-05 21:08
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
("products", "0001_initial"),
]
operations = [
migrations.RemoveField(
model_name="product",
name="brand",
),
migrations.RemoveField(
model_name="product",
name="supplier",
),
migrations.DeleteModel(
name="Brand",
),
migrations.DeleteModel(
name="Supplier",
),
]
+4
View File
@@ -55,10 +55,14 @@ THIRD_PARTY_APPS = [
LOCAL_APPS = [ LOCAL_APPS = [
"products.apps.ProductsConfig", "products.apps.ProductsConfig",
"authentication.apps.AuthenticationConfig",
] ]
INSTALLED_APPS = DJANGO_APPS + THIRD_PARTY_APPS + LOCAL_APPS INSTALLED_APPS = DJANGO_APPS + THIRD_PARTY_APPS + LOCAL_APPS
# JWT settings
JWT_SECRET_KEY = os.environ.get("JWT_SECRET_KEY")
MIDDLEWARE = [ MIDDLEWARE = [
"django.middleware.security.SecurityMiddleware", "django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware", "django.contrib.sessions.middleware.SessionMiddleware",